diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt index 708f579..7af774f 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/alteration/AlterationStore.kt @@ -5,6 +5,7 @@ import com.pixelized.desktop.lwa.repository.network.NetworkRepository import com.pixelized.shared.lwa.model.alteration.Alteration import com.pixelized.shared.lwa.model.alteration.AlterationJsonFactory import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation +import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation.AlterationApiSynchronisation import com.pixelized.shared.lwa.protocol.websocket.SocketMessage import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -118,13 +119,15 @@ class AlterationStore( private suspend fun handleMessage(message: SocketMessage) { when (message) { - is ApiSynchronisation.AlterationUpdate -> updateAlterationFlow( - alterationId = message.alterationId, - ) + is AlterationApiSynchronisation -> when (message) { + is ApiSynchronisation.AlterationDelete -> updateAlterationFlow( + alterationId = message.alterationId, + ) - is ApiSynchronisation.AlterationDelete -> _alterationsFlow.update { alterations -> - alterations.toMutableMap().also { - it.remove(message.alterationId) + is ApiSynchronisation.AlterationUpdate -> _alterationsFlow.update { alterations -> + alterations.toMutableMap().also { + it.remove(message.alterationId) + } } } diff --git a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt index 2877ffb..1bcedce 100644 --- a/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt +++ b/composeApp/src/commonMain/kotlin/com/pixelized/desktop/lwa/repository/characterSheet/CharacterSheetStore.kt @@ -195,27 +195,31 @@ class CharacterSheetStore( _detailFlow.update(character) } - private suspend fun updateAlterations( + private fun updateAlterations( characterSheetId: String, alterationId: String, active: Boolean, ) { - if (alterationStore.alteration(alterationId = alterationId) == null) return - val sheet = getCharacterSheet(characterSheetId = characterSheetId) - + // check if the alteration exist + alterationStore.alteration(alterationId = alterationId) ?: return + // check if the character exit (detail may have not been downloaded) + val sheet = detailFlow.value[characterSheetId] ?: return + // check if the alteration is already active or not on this character. val containAlteration = sheet.alterations.contains(alterationId) if (containAlteration.not() && active) { - val alterations = sheet.alterations.toMutableList().also { - it.add(alterationId) - } - _detailFlow.update(sheet.copy(alterations = alterations)) + _detailFlow.update( + sheet = sheet.copy( + alterations = sheet.alterations.toMutableList().also { it.add(alterationId) } + ) + ) } if (containAlteration && active.not()) { - val alterations = sheet.alterations.toMutableList().also { - it.remove(alterationId) - } - _detailFlow.update(sheet.copy(alterations = alterations)) + _detailFlow.update( + sheet = sheet.copy( + alterations = sheet.alterations.toMutableList().also { it.remove(alterationId) } + ) + ) } }