Fix a potential issue with character prefetch

This commit is contained in:
Andres Gomez, Thomas (ITDV RL) 2024-06-06 18:51:50 +02:00
parent 0ae8c7962c
commit d16db3d48a

View file

@ -13,8 +13,12 @@ import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.pixelized.rplexicon.R
import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
@ -26,6 +30,7 @@ import javax.inject.Inject
@HiltViewModel
class AuthenticationViewModel @Inject constructor(
private val firebaseRepository: FirebaseRepository,
private val sheetRepository: CharacterSheetRepository,
) : ViewModel() {
val authenticationState = mutableStateOf<AuthenticationStateUio>(
@ -72,9 +77,13 @@ class AuthenticationViewModel @Inject constructor(
}
// Launch a preload collect to warm the repository flow.
// Can't use Eager for that because of auth permission.
launch(Dispatchers.Default) {
firebaseRepository.getAlterationStatus().collect { }
val firebaseRequest = async(Dispatchers.Default) {
firebaseRepository.getAlterationStatus().firstOrNull()
}
val sheetRequest = async(Dispatchers.Default) {
sheetRepository.fetchCharacterSheet()
}
awaitAll(firebaseRequest, sheetRequest)
// propagate the auth success.
withContext(Dispatchers.Main) {
authenticationState.value = AuthenticationStateUio.Success