Move alterations activation status to firebase.

This commit is contained in:
Thomas Andres Gomez 2023-12-21 15:10:15 +01:00
parent 1e3332b447
commit 1c6fd505a5
4 changed files with 45 additions and 11 deletions

View file

@ -22,6 +22,10 @@ data class CharacterSheetFire(
@get:PropertyName(SPELLS) @get:PropertyName(SPELLS)
@set:PropertyName(SPELLS) @set:PropertyName(SPELLS)
var spells: Map<String, Int> = emptyMap(), var spells: Map<String, Int> = emptyMap(),
@get:PropertyName(ALTERATIONS)
@set:PropertyName(ALTERATIONS)
var alterations : Map<String, Boolean> = emptyMap()
) { ) {
@Keep @Keep
@IgnoreExtraProperties @IgnoreExtraProperties
@ -52,6 +56,7 @@ data class CharacterSheetFire(
const val DEATH = "death" const val DEATH = "death"
const val SKILLS = "skills" const val SKILLS = "skills"
const val SPELLS = "spells" const val SPELLS = "spells"
const val ALTERATIONS = "alterations"
const val SPELL_PREFIX = "lvl_" const val SPELL_PREFIX = "lvl_"
} }
} }

View file

@ -10,12 +10,20 @@ import com.google.firebase.ktx.Firebase
import com.pixelized.rplexicon.R import com.pixelized.rplexicon.R
import com.pixelized.rplexicon.data.model.CharacterSheetFire import com.pixelized.rplexicon.data.model.CharacterSheetFire
import com.pixelized.rplexicon.data.model.CharacterSheetFireMap import com.pixelized.rplexicon.data.model.CharacterSheetFireMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -23,6 +31,7 @@ import javax.inject.Singleton
class FirebaseRepository @Inject constructor( class FirebaseRepository @Inject constructor(
application: Application, application: Application,
) { ) {
private val externalScope: CoroutineScope = CoroutineScope(Dispatchers.Default + Job())
private val database = Firebase.database( private val database = Firebase.database(
url = application.getString(R.string.firebase_realtime_database), url = application.getString(R.string.firebase_realtime_database),
) )
@ -30,8 +39,23 @@ class FirebaseRepository @Inject constructor(
private val _error = MutableSharedFlow<Exception>() private val _error = MutableSharedFlow<Exception>()
val error: SharedFlow<Exception> get() = _error val error: SharedFlow<Exception> get() = _error
val status: StateFlow<Map<String, Boolean>>
init { init {
Firebase.database.setPersistenceEnabled(true) Firebase.database.setPersistenceEnabled(true)
status = MutableStateFlow(emptyMap())
externalScope.launch {
getCharacter().map {
it.characters.flatMap { character ->
character.value.alterations.map { alteration ->
(character.key + alteration.key) to alteration.value
}
}.toMap()
}.collectLatest {
status.value = it
}
}
} }
fun getCharacter(): Flow<CharacterSheetFireMap> { fun getCharacter(): Flow<CharacterSheetFireMap> {
@ -132,6 +156,13 @@ class FirebaseRepository @Inject constructor(
reference.setValue(value) reference.setValue(value)
} }
fun setStatus(character: String, status: String, value: Boolean) {
val reference = database.getReference(
"$PATH_CHARACTERS/$character/${CharacterSheetFire.ALTERATIONS}/$status"
)
reference.setValue(value)
}
companion object { companion object {
private const val TAG = "FirebaseRepository" private const val TAG = "FirebaseRepository"
private const val PATH_CHARACTERS = "Characters" private const val PATH_CHARACTERS = "Characters"

View file

@ -6,11 +6,13 @@ import com.pixelized.rplexicon.data.model.Property
import com.pixelized.rplexicon.data.parser.alteration.AlterationParser import com.pixelized.rplexicon.data.parser.alteration.AlterationParser
import com.pixelized.rplexicon.data.repository.CharacterBinder import com.pixelized.rplexicon.data.repository.CharacterBinder
import com.pixelized.rplexicon.data.repository.GoogleSheetServiceRepository import com.pixelized.rplexicon.data.repository.GoogleSheetServiceRepository
import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
import com.pixelized.rplexicon.utilitary.Update import com.pixelized.rplexicon.utilitary.Update
import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure import com.pixelized.rplexicon.utilitary.exceptions.IncompatibleSheetStructure
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -18,11 +20,11 @@ import javax.inject.Singleton
class AlterationRepository @Inject constructor( class AlterationRepository @Inject constructor(
private val googleRepository: GoogleSheetServiceRepository, private val googleRepository: GoogleSheetServiceRepository,
private val alterationParser: AlterationParser, private val alterationParser: AlterationParser,
private val fireRepository: FirebaseRepository,
) { ) {
private val _status = MutableStateFlow<Map<String, Boolean>>(emptyMap())
private val _assignedAlterations = MutableStateFlow<Map<String, List<Alteration>>>(emptyMap()) private val _assignedAlterations = MutableStateFlow<Map<String, List<Alteration>>>(emptyMap())
val assignedAlterations: Flow<Map<String, List<Alteration>>> = val assignedAlterations: Flow<Map<String, List<Alteration>>> =
combine(_assignedAlterations, _status) { alt, _ -> alt } combine(_assignedAlterations, fireRepository.status) { alt, _ -> alt }
var lastSuccessFullUpdate: Update = Update.INITIAL var lastSuccessFullUpdate: Update = Update.INITIAL
private set private set
@ -64,7 +66,7 @@ class AlterationRepository @Inject constructor(
* @return true if the alteration is activated otherwise false * @return true if the alteration is activated otherwise false
*/ */
fun getStatus(character: String, alteration: String): Boolean { fun getStatus(character: String, alteration: String): Boolean {
return _status.value[character + alteration] return fireRepository.status.value[character + alteration]
?: _assignedAlterations.value[character]?.firstOrNull { it.name == alteration }?.active ?: _assignedAlterations.value[character]?.firstOrNull { it.name == alteration }?.active
?: false ?: false
} }
@ -77,7 +79,7 @@ class AlterationRepository @Inject constructor(
fun getActiveAlterationsStatus(character: String): Map<Property, List<Alteration.Status>> { fun getActiveAlterationsStatus(character: String): Map<Property, List<Alteration.Status>> {
val status = hashMapOf<Property, MutableList<Alteration.Status>>() val status = hashMapOf<Property, MutableList<Alteration.Status>>()
_assignedAlterations.value[character]?.forEach { alteration -> _assignedAlterations.value[character]?.forEach { alteration ->
if (_status.value[character + alteration.name] ?: alteration.active) { if (fireRepository.status.value[character + alteration.name] ?: alteration.active) {
alteration.status.forEach { alteration.status.forEach {
status.getOrPut(it.key) { mutableListOf() }.add(it.value) status.getOrPut(it.key) { mutableListOf() }.add(it.value)
} }
@ -86,12 +88,8 @@ class AlterationRepository @Inject constructor(
return status return status
} }
suspend fun setStatus(character: String, alteration: String, value: Boolean?) { fun setStatus(character: String, alteration: String, value: Boolean?) {
_status.emit(if (value != null) { fireRepository.setStatus(character = character, status = alteration, value = value ?: false)
_status.value.toMutableMap().also { it[character + alteration] = value }
} else {
_status.value.toMutableMap().also { it.remove(character + alteration) }
})
} }
@Throws(IncompatibleSheetStructure::class, Exception::class) @Throws(IncompatibleSheetStructure::class, Exception::class)

View file

@ -70,7 +70,7 @@ class AlterationViewModel @Inject constructor(
} }
} }
suspend fun toggleAlteration(alteration: String) { fun toggleAlteration(alteration: String) {
val value = alterationRepository.getStatus(character = character, alteration = alteration) val value = alterationRepository.getStatus(character = character, alteration = alteration)
alterationRepository.setStatus(character = character, alteration = alteration, value.not()) alterationRepository.setStatus(character = character, alteration = alteration, value.not())
} }