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.AndroidViewModel
|
||||||
import androidx.lifecycle.SavedStateHandle
|
import androidx.lifecycle.SavedStateHandle
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.pixelized.rplexicon.data.model.Alteration
|
||||||
import com.pixelized.rplexicon.data.model.Attack
|
import com.pixelized.rplexicon.data.model.Attack
|
||||||
import com.pixelized.rplexicon.data.model.CharacterSheet
|
import com.pixelized.rplexicon.data.model.CharacterSheet
|
||||||
import com.pixelized.rplexicon.data.model.DiceThrow
|
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.ActionRepository
|
||||||
import com.pixelized.rplexicon.data.repository.character.AlterationRepository
|
import com.pixelized.rplexicon.data.repository.character.AlterationRepository
|
||||||
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
|
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
|
||||||
|
|
@ -32,27 +34,42 @@ class AttacksViewModel @Inject constructor(
|
||||||
attackFactory: AttackUioFactory,
|
attackFactory: AttackUioFactory,
|
||||||
) : AndroidViewModel(application) {
|
) : AndroidViewModel(application) {
|
||||||
private val character = savedStateHandle.characterSheetArgument.name
|
private val character = savedStateHandle.characterSheetArgument.name
|
||||||
private val model: CharacterSheet get() = characterRepository.data.value.getValue(character)
|
|
||||||
|
|
||||||
private val _attacks = mutableStateOf<List<AttackUio>>(emptyList())
|
private val _attacks = mutableStateOf<List<AttackUio>>(emptyList())
|
||||||
val attacks: State<List<AttackUio>> get() = _attacks
|
val attacks: State<List<AttackUio>> get() = _attacks
|
||||||
|
|
||||||
init {
|
init {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
actionRepository.data
|
characterRepository.data
|
||||||
.combine(alterationRepository.assignedAlterations) { actions, _ -> actions }
|
.combine(actionRepository.data) { characters, actions ->
|
||||||
.collect { sheets ->
|
ActionData(
|
||||||
val alterations = alterationRepository.getActiveAlterationsStatus(
|
character = characters[character],
|
||||||
character = character,
|
actions = actions[character] ?: emptyList()
|
||||||
)
|
)
|
||||||
val attacks = sheets[character]?.map { action ->
|
}
|
||||||
attackFactory.convert(
|
.combine(alterationRepository.assignedAlterations) { data, _ ->
|
||||||
characterSheet = model,
|
data.also {
|
||||||
alterations = alterations,
|
it.alterations = alterationRepository.getActiveAlterationsStatus(character)
|
||||||
attack = action,
|
}
|
||||||
)
|
}
|
||||||
} ?: emptyList()
|
.collect { data ->
|
||||||
withContext(Dispatchers.Main) { _attacks.value = attacks }
|
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
|
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
|
characterRepository.data
|
||||||
.combine(alterationRepository.assignedAlterations) { sheets, _ -> sheets }
|
.combine(alterationRepository.assignedAlterations) { sheets, _ -> sheets }
|
||||||
.collect { sheets ->
|
.collect { sheets ->
|
||||||
val character = sheets.getValue(character)
|
val character = sheets[character]
|
||||||
val alterations = alterationRepository.getActiveAlterationsStatus(
|
if (character != null) {
|
||||||
character = character.name,
|
val alterations = alterationRepository.getActiveAlterationsStatus(
|
||||||
)
|
character = character.name,
|
||||||
val data = SheetHeaderData(
|
)
|
||||||
hpMax = character.hitPoint + alterations[Property.HIT_POINT].sum,
|
val data = SheetHeaderData(
|
||||||
speed = character.speed,
|
hpMax = character.hitPoint + alterations[Property.HIT_POINT].sum,
|
||||||
ca = character.armorClass + alterations[Property.ARMOR_CLASS].sum,
|
speed = character.speed,
|
||||||
dc = character.dC,
|
ca = character.armorClass + alterations[Property.ARMOR_CLASS].sum,
|
||||||
)
|
dc = character.dC,
|
||||||
withContext(Dispatchers.Main) {
|
)
|
||||||
sheetData.value = data
|
withContext(Dispatchers.Main) {
|
||||||
|
sheetData.value = data
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
launch { characterRepository.fetchCharacterSheet() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,20 @@ class ProficiencyViewModel @Inject constructor(
|
||||||
characterRepository.data
|
characterRepository.data
|
||||||
.combine(alterationRepository.assignedAlterations) { sheets, _ -> sheets }
|
.combine(alterationRepository.assignedAlterations) { sheets, _ -> sheets }
|
||||||
.collect {
|
.collect {
|
||||||
val alterations = alterationRepository.getActiveAlterationsStatus(character)
|
val characterSheet = it[character]
|
||||||
val sheet = characterSheetFactory.convert(
|
if (characterSheet != null) {
|
||||||
sheet = it.getValue(key = character),
|
val alterations = alterationRepository.getActiveAlterationsStatus(character)
|
||||||
alterations = alterations,
|
val sheet = characterSheetFactory.convert(
|
||||||
)
|
sheet = characterSheet,
|
||||||
withContext(Dispatchers.Main) {
|
alterations = alterations,
|
||||||
_sheet.value = sheet
|
)
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
_sheet.value = sheet
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
launch {
|
||||||
|
characterRepository.fetchCharacterSheet()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue