Move alterations activation status to firebase.
This commit is contained in:
parent
1e3332b447
commit
1c6fd505a5
4 changed files with 45 additions and 11 deletions
|
|
@ -22,6 +22,10 @@ data class CharacterSheetFire(
|
|||
@get:PropertyName(SPELLS)
|
||||
@set:PropertyName(SPELLS)
|
||||
var spells: Map<String, Int> = emptyMap(),
|
||||
|
||||
@get:PropertyName(ALTERATIONS)
|
||||
@set:PropertyName(ALTERATIONS)
|
||||
var alterations : Map<String, Boolean> = emptyMap()
|
||||
) {
|
||||
@Keep
|
||||
@IgnoreExtraProperties
|
||||
|
|
@ -52,6 +56,7 @@ data class CharacterSheetFire(
|
|||
const val DEATH = "death"
|
||||
const val SKILLS = "skills"
|
||||
const val SPELLS = "spells"
|
||||
const val ALTERATIONS = "alterations"
|
||||
const val SPELL_PREFIX = "lvl_"
|
||||
}
|
||||
}
|
||||
|
|
@ -10,12 +10,20 @@ import com.google.firebase.ktx.Firebase
|
|||
import com.pixelized.rplexicon.R
|
||||
import com.pixelized.rplexicon.data.model.CharacterSheetFire
|
||||
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.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
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.Singleton
|
||||
|
||||
|
|
@ -23,6 +31,7 @@ import javax.inject.Singleton
|
|||
class FirebaseRepository @Inject constructor(
|
||||
application: Application,
|
||||
) {
|
||||
private val externalScope: CoroutineScope = CoroutineScope(Dispatchers.Default + Job())
|
||||
private val database = Firebase.database(
|
||||
url = application.getString(R.string.firebase_realtime_database),
|
||||
)
|
||||
|
|
@ -30,8 +39,23 @@ class FirebaseRepository @Inject constructor(
|
|||
private val _error = MutableSharedFlow<Exception>()
|
||||
val error: SharedFlow<Exception> get() = _error
|
||||
|
||||
val status: StateFlow<Map<String, Boolean>>
|
||||
|
||||
init {
|
||||
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> {
|
||||
|
|
@ -132,6 +156,13 @@ class FirebaseRepository @Inject constructor(
|
|||
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 {
|
||||
private const val TAG = "FirebaseRepository"
|
||||
private const val PATH_CHARACTERS = "Characters"
|
||||
|
|
|
|||
|
|
@ -6,11 +6,13 @@ import com.pixelized.rplexicon.data.model.Property
|
|||
import com.pixelized.rplexicon.data.parser.alteration.AlterationParser
|
||||
import com.pixelized.rplexicon.data.repository.CharacterBinder
|
||||
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.exceptions.IncompatibleSheetStructure
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.map
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
|
|
@ -18,11 +20,11 @@ import javax.inject.Singleton
|
|||
class AlterationRepository @Inject constructor(
|
||||
private val googleRepository: GoogleSheetServiceRepository,
|
||||
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())
|
||||
val assignedAlterations: Flow<Map<String, List<Alteration>>> =
|
||||
combine(_assignedAlterations, _status) { alt, _ -> alt }
|
||||
combine(_assignedAlterations, fireRepository.status) { alt, _ -> alt }
|
||||
|
||||
var lastSuccessFullUpdate: Update = Update.INITIAL
|
||||
private set
|
||||
|
|
@ -64,7 +66,7 @@ class AlterationRepository @Inject constructor(
|
|||
* @return true if the alteration is activated otherwise false
|
||||
*/
|
||||
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
|
||||
?: false
|
||||
}
|
||||
|
|
@ -77,7 +79,7 @@ class AlterationRepository @Inject constructor(
|
|||
fun getActiveAlterationsStatus(character: String): Map<Property, List<Alteration.Status>> {
|
||||
val status = hashMapOf<Property, MutableList<Alteration.Status>>()
|
||||
_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 {
|
||||
status.getOrPut(it.key) { mutableListOf() }.add(it.value)
|
||||
}
|
||||
|
|
@ -86,12 +88,8 @@ class AlterationRepository @Inject constructor(
|
|||
return status
|
||||
}
|
||||
|
||||
suspend fun setStatus(character: String, alteration: String, value: Boolean?) {
|
||||
_status.emit(if (value != null) {
|
||||
_status.value.toMutableMap().also { it[character + alteration] = value }
|
||||
} else {
|
||||
_status.value.toMutableMap().also { it.remove(character + alteration) }
|
||||
})
|
||||
fun setStatus(character: String, alteration: String, value: Boolean?) {
|
||||
fireRepository.setStatus(character = character, status = alteration, value = value ?: false)
|
||||
}
|
||||
|
||||
@Throws(IncompatibleSheetStructure::class, Exception::class)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
alterationRepository.setStatus(character = character, alteration = alteration, value.not())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue