Fix dynamic alteration not working properly on client.
This commit is contained in:
		
							parent
							
								
									0aaa56a4aa
								
							
						
					
					
						commit
						2f4b30297c
					
				
					 2 changed files with 25 additions and 18 deletions
				
			
		| 
						 | 
				
			
			@ -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,15 +119,17 @@ class AlterationStore(
 | 
			
		|||
 | 
			
		||||
    private suspend fun handleMessage(message: SocketMessage) {
 | 
			
		||||
        when (message) {
 | 
			
		||||
            is ApiSynchronisation.AlterationUpdate -> updateAlterationFlow(
 | 
			
		||||
            is AlterationApiSynchronisation -> when (message) {
 | 
			
		||||
                is ApiSynchronisation.AlterationDelete -> updateAlterationFlow(
 | 
			
		||||
                    alterationId = message.alterationId,
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
            is ApiSynchronisation.AlterationDelete -> _alterationsFlow.update { alterations ->
 | 
			
		||||
                is ApiSynchronisation.AlterationUpdate -> _alterationsFlow.update { alterations ->
 | 
			
		||||
                    alterations.toMutableMap().also {
 | 
			
		||||
                        it.remove(message.alterationId)
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            else -> Unit
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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) }
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue