Fix a potential issue with character prefetch
This commit is contained in:
parent
0ae8c7962c
commit
d16db3d48a
1 changed files with 11 additions and 2 deletions
|
|
@ -13,8 +13,12 @@ import com.google.firebase.auth.ktx.auth
|
||||||
import com.google.firebase.ktx.Firebase
|
import com.google.firebase.ktx.Firebase
|
||||||
import com.pixelized.rplexicon.R
|
import com.pixelized.rplexicon.R
|
||||||
import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
|
import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
|
||||||
|
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.awaitAll
|
||||||
|
import kotlinx.coroutines.flow.firstOrNull
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.tasks.await
|
import kotlinx.coroutines.tasks.await
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
@ -26,6 +30,7 @@ import javax.inject.Inject
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class AuthenticationViewModel @Inject constructor(
|
class AuthenticationViewModel @Inject constructor(
|
||||||
private val firebaseRepository: FirebaseRepository,
|
private val firebaseRepository: FirebaseRepository,
|
||||||
|
private val sheetRepository: CharacterSheetRepository,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
val authenticationState = mutableStateOf<AuthenticationStateUio>(
|
val authenticationState = mutableStateOf<AuthenticationStateUio>(
|
||||||
|
|
@ -72,9 +77,13 @@ class AuthenticationViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
// Launch a preload collect to warm the repository flow.
|
// Launch a preload collect to warm the repository flow.
|
||||||
// Can't use Eager for that because of auth permission.
|
// Can't use Eager for that because of auth permission.
|
||||||
launch(Dispatchers.Default) {
|
val firebaseRequest = async(Dispatchers.Default) {
|
||||||
firebaseRepository.getAlterationStatus().collect { }
|
firebaseRepository.getAlterationStatus().firstOrNull()
|
||||||
}
|
}
|
||||||
|
val sheetRequest = async(Dispatchers.Default) {
|
||||||
|
sheetRepository.fetchCharacterSheet()
|
||||||
|
}
|
||||||
|
awaitAll(firebaseRequest, sheetRequest)
|
||||||
// propagate the auth success.
|
// propagate the auth success.
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
authenticationState.value = AuthenticationStateUio.Success
|
authenticationState.value = AuthenticationStateUio.Success
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue