Rework the data update throughout the app.
This commit is contained in:
parent
cbe945d7f5
commit
b36cb9b601
19 changed files with 323 additions and 121 deletions
|
|
@ -0,0 +1,99 @@
|
|||
package com.pixelized.rplexicon
|
||||
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.pixelized.rplexicon.repository.data.ActionRepository
|
||||
import com.pixelized.rplexicon.repository.data.AlterationRepository
|
||||
import com.pixelized.rplexicon.repository.data.CharacterSheetRepository
|
||||
import com.pixelized.rplexicon.repository.data.LexiconRepository
|
||||
import com.pixelized.rplexicon.repository.data.LocationRepository
|
||||
import com.pixelized.rplexicon.repository.data.QuestRepository
|
||||
import com.pixelized.rplexicon.repository.data.SpellRepository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class LauncherViewModel @Inject constructor(
|
||||
lexiconRepository: LexiconRepository,
|
||||
locationRepository: LocationRepository,
|
||||
questRepository: QuestRepository,
|
||||
alterationRepository: AlterationRepository,
|
||||
characterSheetRepository: CharacterSheetRepository,
|
||||
actionRepository: ActionRepository,
|
||||
spellRepository: SpellRepository,
|
||||
) : ViewModel() {
|
||||
|
||||
private val _error = MutableSharedFlow<String>()
|
||||
val error: SharedFlow<String> get() = _error
|
||||
|
||||
var isLoading by mutableStateOf(true)
|
||||
private set
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
val lexicon = async {
|
||||
try {
|
||||
lexiconRepository.fetchLexicon()
|
||||
} catch (exception: Exception) {
|
||||
_error.tryEmit("Lexicon fail to update")
|
||||
}
|
||||
}
|
||||
val location = async {
|
||||
try {
|
||||
locationRepository.fetchLocation()
|
||||
} catch (exception: Exception) {
|
||||
_error.tryEmit("Location fail to update")
|
||||
}
|
||||
}
|
||||
val quest = async {
|
||||
try {
|
||||
questRepository.fetchQuests()
|
||||
} catch (exception: Exception) {
|
||||
_error.tryEmit("Quest fail to update")
|
||||
}
|
||||
}
|
||||
val alteration = async {
|
||||
try {
|
||||
alterationRepository.fetchAlterationSheet()
|
||||
alterationRepository.fetchStatusSheet()
|
||||
} catch (exception: Exception) {
|
||||
_error.tryEmit("Alteration fail to update")
|
||||
}
|
||||
}
|
||||
val characterSheet = async {
|
||||
try {
|
||||
characterSheetRepository.fetchCharacterSheet()
|
||||
} catch (exception: Exception) {
|
||||
_error.tryEmit("CharacterSheet fail to update")
|
||||
}
|
||||
}
|
||||
awaitAll(lexicon, location, quest, alteration, characterSheet)
|
||||
|
||||
val action = async {
|
||||
try {
|
||||
actionRepository.fetchActions()
|
||||
} catch (exception: Exception) {
|
||||
_error.tryEmit("Action fail to update")
|
||||
}
|
||||
}
|
||||
val spell = async {
|
||||
try {
|
||||
spellRepository.fetchSpells()
|
||||
} catch (exception: Exception) {
|
||||
_error.tryEmit("Spell fail to update")
|
||||
}
|
||||
}
|
||||
awaitAll(action, spell)
|
||||
|
||||
isLoading = false
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue