Fix potential crash with getValue
This commit is contained in:
parent
e98b2864e2
commit
003390e844
3 changed files with 68 additions and 33 deletions
|
|
@ -6,9 +6,11 @@ import androidx.compose.runtime.mutableStateOf
|
|||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.pixelized.rplexicon.data.model.Alteration
|
||||
import com.pixelized.rplexicon.data.model.Attack
|
||||
import com.pixelized.rplexicon.data.model.CharacterSheet
|
||||
import com.pixelized.rplexicon.data.model.DiceThrow
|
||||
import com.pixelized.rplexicon.data.model.Property
|
||||
import com.pixelized.rplexicon.data.repository.character.ActionRepository
|
||||
import com.pixelized.rplexicon.data.repository.character.AlterationRepository
|
||||
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
|
||||
|
|
@ -32,27 +34,42 @@ class AttacksViewModel @Inject constructor(
|
|||
attackFactory: AttackUioFactory,
|
||||
) : AndroidViewModel(application) {
|
||||
private val character = savedStateHandle.characterSheetArgument.name
|
||||
private val model: CharacterSheet get() = characterRepository.data.value.getValue(character)
|
||||
|
||||
private val _attacks = mutableStateOf<List<AttackUio>>(emptyList())
|
||||
val attacks: State<List<AttackUio>> get() = _attacks
|
||||
|
||||
init {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
actionRepository.data
|
||||
.combine(alterationRepository.assignedAlterations) { actions, _ -> actions }
|
||||
.collect { sheets ->
|
||||
val alterations = alterationRepository.getActiveAlterationsStatus(
|
||||
character = character,
|
||||
characterRepository.data
|
||||
.combine(actionRepository.data) { characters, actions ->
|
||||
ActionData(
|
||||
character = characters[character],
|
||||
actions = actions[character] ?: emptyList()
|
||||
)
|
||||
val attacks = sheets[character]?.map { action ->
|
||||
attackFactory.convert(
|
||||
characterSheet = model,
|
||||
alterations = alterations,
|
||||
attack = action,
|
||||
)
|
||||
} ?: emptyList()
|
||||
withContext(Dispatchers.Main) { _attacks.value = attacks }
|
||||
}
|
||||
.combine(alterationRepository.assignedAlterations) { data, _ ->
|
||||
data.also {
|
||||
it.alterations = alterationRepository.getActiveAlterationsStatus(character)
|
||||
}
|
||||
}
|
||||
.collect { data ->
|
||||
val characterSheet = data.character
|
||||
if (characterSheet != null) {
|
||||
val attacks = data.actions.map { action ->
|
||||
attackFactory.convert(
|
||||
characterSheet = characterSheet,
|
||||
alterations = data.alterations,
|
||||
attack = action,
|
||||
)
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
_attacks.value = attacks
|
||||
}
|
||||
} else {
|
||||
launch {
|
||||
characterRepository.fetchCharacterSheet()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -90,4 +107,11 @@ class AttacksViewModel @Inject constructor(
|
|||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private data class ActionData(
|
||||
var character: CharacterSheet?,
|
||||
var actions: List<Attack>,
|
||||
) {
|
||||
lateinit var alterations: Map<Property, List<Alteration.Status>>
|
||||
}
|
||||
}
|
||||
|
|
@ -53,18 +53,22 @@ class HeaderViewModel @Inject constructor(
|
|||
characterRepository.data
|
||||
.combine(alterationRepository.assignedAlterations) { sheets, _ -> sheets }
|
||||
.collect { sheets ->
|
||||
val character = sheets.getValue(character)
|
||||
val alterations = alterationRepository.getActiveAlterationsStatus(
|
||||
character = character.name,
|
||||
)
|
||||
val data = SheetHeaderData(
|
||||
hpMax = character.hitPoint + alterations[Property.HIT_POINT].sum,
|
||||
speed = character.speed,
|
||||
ca = character.armorClass + alterations[Property.ARMOR_CLASS].sum,
|
||||
dc = character.dC,
|
||||
)
|
||||
withContext(Dispatchers.Main) {
|
||||
sheetData.value = data
|
||||
val character = sheets[character]
|
||||
if (character != null) {
|
||||
val alterations = alterationRepository.getActiveAlterationsStatus(
|
||||
character = character.name,
|
||||
)
|
||||
val data = SheetHeaderData(
|
||||
hpMax = character.hitPoint + alterations[Property.HIT_POINT].sum,
|
||||
speed = character.speed,
|
||||
ca = character.armorClass + alterations[Property.ARMOR_CLASS].sum,
|
||||
dc = character.dC,
|
||||
)
|
||||
withContext(Dispatchers.Main) {
|
||||
sheetData.value = data
|
||||
}
|
||||
} else {
|
||||
launch { characterRepository.fetchCharacterSheet() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,13 +38,20 @@ class ProficiencyViewModel @Inject constructor(
|
|||
characterRepository.data
|
||||
.combine(alterationRepository.assignedAlterations) { sheets, _ -> sheets }
|
||||
.collect {
|
||||
val alterations = alterationRepository.getActiveAlterationsStatus(character)
|
||||
val sheet = characterSheetFactory.convert(
|
||||
sheet = it.getValue(key = character),
|
||||
alterations = alterations,
|
||||
)
|
||||
withContext(Dispatchers.Main) {
|
||||
_sheet.value = sheet
|
||||
val characterSheet = it[character]
|
||||
if (characterSheet != null) {
|
||||
val alterations = alterationRepository.getActiveAlterationsStatus(character)
|
||||
val sheet = characterSheetFactory.convert(
|
||||
sheet = characterSheet,
|
||||
alterations = alterations,
|
||||
)
|
||||
withContext(Dispatchers.Main) {
|
||||
_sheet.value = sheet
|
||||
}
|
||||
} else {
|
||||
launch {
|
||||
characterRepository.fetchCharacterSheet()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue