diff --git a/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt b/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt index c669479..cee7226 100644 --- a/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt +++ b/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt @@ -6,7 +6,7 @@ import androidx.credentials.CredentialManager import androidx.credentials.GetCredentialRequest import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption +import com.google.android.libraries.identity.googleid.GetGoogleIdOption import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential import com.google.firebase.auth.GoogleAuthProvider import com.google.firebase.auth.ktx.auth @@ -16,14 +16,13 @@ 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.flow.collect import kotlinx.coroutines.launch import kotlinx.coroutines.tasks.await import kotlinx.coroutines.withContext import javax.inject.Inject + /** * https://developer.android.com/identity/sign-in/credential-manager-siwg */ @@ -38,26 +37,31 @@ class AuthenticationViewModel @Inject constructor( ) fun signIn(activity: Activity) { - val credentialManager = CredentialManager.create(context = activity) - - val signInWithGoogleOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption - .Builder(serverClientId = activity.getString(R.string.google_sign_in_id)) - .build() - - val request: GetCredentialRequest = GetCredentialRequest.Builder() - .addCredentialOption(credentialOption = signInWithGoogleOption) - .build() - viewModelScope.launch(Dispatchers.IO) { try { - withContext(Dispatchers.Main) { - authenticationState.value = AuthenticationStateUio.Progress - } + // create the credential manager + val credentialManager = CredentialManager.create( + context = activity, + ) + // build the google authentication bottom sheet. + val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder() + .setFilterByAuthorizedAccounts(false) + .setAutoSelectEnabled(true) + .setServerClientId(activity.getString(R.string.google_sign_in_id)) + .build() + // build the request from the google Id. + val request: GetCredentialRequest = GetCredentialRequest.Builder() + .addCredentialOption(credentialOption = googleIdOption) + .build() // login to google. val result = credentialManager.getCredential( request = request, context = activity, ) + // the google UI Took care up to now of the scrim. + withContext(Dispatchers.Main) { + authenticationState.value = AuthenticationStateUio.Progress + } // extract the token Id from the credential val tokenIdCredential = GoogleIdTokenCredential.createFrom( data = result.credential.data, @@ -77,13 +81,13 @@ class AuthenticationViewModel @Inject constructor( } // Launch a preload collect to warm the repository flow. // Can't use Eager for that because of auth permission. - val firebaseRequest = async(Dispatchers.Default) { - firebaseRepository.getAlterationStatus().firstOrNull() + launch(Dispatchers.Default) { + firebaseRepository.getAlterationStatus().collect() } - val sheetRequest = async(Dispatchers.Default) { + // prefetch the character sheet + withContext(Dispatchers.Default) { sheetRepository.fetchCharacterSheet() } - awaitAll(firebaseRequest, sheetRequest) // propagate the auth success. withContext(Dispatchers.Main) { authenticationState.value = AuthenticationStateUio.Success