Fix dumb mistake with google sign in.
This commit is contained in:
parent
1e58752008
commit
4b5893cf43
7 changed files with 67 additions and 43 deletions
|
|
@ -3,6 +3,7 @@ package com.pixelized.biblib.ui.composable.screen
|
|||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.ExperimentalAnimationApi
|
||||
import androidx.compose.animation.expandVertically
|
||||
|
|
@ -51,6 +52,7 @@ import com.pixelized.biblib.ui.viewmodel.credential.CredentialViewModel
|
|||
import com.pixelized.biblib.ui.viewmodel.credential.ICredentialViewModel
|
||||
import com.pixelized.biblib.ui.viewmodel.navigation.INavigationViewModel
|
||||
import com.pixelized.biblib.ui.viewmodel.navigation.NavigationViewModel
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
|
||||
private const val LE_LOAD_BOOK = "LE_LOAD_BOOK"
|
||||
|
|
@ -62,6 +64,10 @@ fun LoginScreenComposable(
|
|||
authenticationViewModel: IAuthenticationViewModel = viewModel<AuthenticationViewModel>(),
|
||||
bookViewModel: IBooksViewModel = viewModel<BooksViewModel>()
|
||||
) {
|
||||
LaunchedEffect("merde") {
|
||||
authenticationViewModel.clearState()
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
|
@ -105,11 +111,11 @@ private fun LoginScreenDialogComposable(
|
|||
authenticationViewModel: IAuthenticationViewModel,
|
||||
booksViewModel: IBooksViewModel,
|
||||
) {
|
||||
val authState = authenticationViewModel.state.observeAsState()
|
||||
val bookState = booksViewModel.state.observeAsState()
|
||||
val authState by authenticationViewModel.state.observeAsState()
|
||||
val bookState by booksViewModel.state.observeAsState()
|
||||
|
||||
val visible = authState.value is IAuthenticationViewModel.State.Error
|
||||
|| bookState.value is IBooksViewModel.State.Error
|
||||
val visible = authState !is IAuthenticationViewModel.State.Initial
|
||||
|| bookState !is IBooksViewModel.State.Initial
|
||||
|
||||
CrossFadeOverlay(
|
||||
modifier = Modifier.clickable {
|
||||
|
|
@ -121,19 +127,19 @@ private fun LoginScreenDialogComposable(
|
|||
) {
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
visible = authState.value is IAuthenticationViewModel.State.Error,
|
||||
visible = authState is IAuthenticationViewModel.State.Error,
|
||||
initiallyVisible = false,
|
||||
enter = expandVertically(Alignment.CenterVertically),
|
||||
exit = shrinkVertically(Alignment.CenterVertically),
|
||||
) {
|
||||
ErrorCard(
|
||||
message = stringResource(id = R.string.error_authentication),
|
||||
exception = (authState.value as? IAuthenticationViewModel.State.Error)?.exception
|
||||
exception = (authState as? IAuthenticationViewModel.State.Error)?.exception
|
||||
)
|
||||
}
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
visible = authState.value is IAuthenticationViewModel.State.Loading,
|
||||
visible = authState is IAuthenticationViewModel.State.Loading,
|
||||
initiallyVisible = false,
|
||||
enter = expandVertically(Alignment.CenterVertically),
|
||||
exit = shrinkVertically(Alignment.CenterVertically),
|
||||
|
|
@ -144,7 +150,7 @@ private fun LoginScreenDialogComposable(
|
|||
}
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
visible = (authState.value as? IAuthenticationViewModel.State.Finished)?.logged
|
||||
visible = (authState as? IAuthenticationViewModel.State.Finished)?.logged
|
||||
?: false,
|
||||
initiallyVisible = false,
|
||||
enter = expandVertically(Alignment.CenterVertically),
|
||||
|
|
@ -156,19 +162,19 @@ private fun LoginScreenDialogComposable(
|
|||
}
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
visible = bookState.value is IBooksViewModel.State.Error,
|
||||
visible = bookState is IBooksViewModel.State.Error,
|
||||
initiallyVisible = false,
|
||||
enter = expandVertically(Alignment.CenterVertically),
|
||||
exit = shrinkVertically(Alignment.CenterVertically),
|
||||
) {
|
||||
ErrorCard(
|
||||
message = stringResource(id = R.string.error_book),
|
||||
exception = (bookState.value as? IBooksViewModel.State.Error)?.exception
|
||||
exception = (bookState as? IBooksViewModel.State.Error)?.exception
|
||||
)
|
||||
}
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
visible = bookState.value is IBooksViewModel.State.Loading,
|
||||
visible = bookState is IBooksViewModel.State.Loading,
|
||||
initiallyVisible = false,
|
||||
enter = expandVertically(Alignment.CenterVertically),
|
||||
exit = shrinkVertically(Alignment.CenterVertically),
|
||||
|
|
@ -179,7 +185,7 @@ private fun LoginScreenDialogComposable(
|
|||
}
|
||||
AnimatedVisibility(
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
visible = bookState.value is IBooksViewModel.State.Finished,
|
||||
visible = bookState is IBooksViewModel.State.Finished,
|
||||
initiallyVisible = false,
|
||||
enter = expandVertically(Alignment.CenterVertically),
|
||||
exit = shrinkVertically(Alignment.CenterVertically),
|
||||
|
|
@ -197,18 +203,20 @@ private fun LoginScreenNavigationComposable(
|
|||
authenticationViewModel: IAuthenticationViewModel,
|
||||
bookViewModel: IBooksViewModel,
|
||||
) {
|
||||
val authenticationState = authenticationViewModel.state.observeAsState()
|
||||
(authenticationState.value as? IAuthenticationViewModel.State.Finished)?.let {
|
||||
val authenticationState by authenticationViewModel.state.observeAsState()
|
||||
(authenticationState as? IAuthenticationViewModel.State.Finished)?.let {
|
||||
if (it.logged) {
|
||||
LaunchedEffect(LE_LOAD_BOOK) {
|
||||
delay(1000)
|
||||
bookViewModel.updateBooks()
|
||||
}
|
||||
val bookLoadingState = bookViewModel.state.observeAsState()
|
||||
if (bookLoadingState.value is IBooksViewModel.State.Finished) {
|
||||
navigationViewModel.navigateTo(INavigationViewModel.Screen.MainScreen)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val bookLoadingState by bookViewModel.state.observeAsState()
|
||||
if (bookLoadingState is IBooksViewModel.State.Finished) {
|
||||
navigationViewModel.navigateTo(INavigationViewModel.Screen.MainScreen)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
|
@ -379,15 +387,11 @@ private fun CredentialRemember(
|
|||
@Composable
|
||||
fun LoginScreenComposablePreview() {
|
||||
BibLibTheme {
|
||||
val navigationViewModel = INavigationViewModel.Mock()
|
||||
val credentialViewModel = ICredentialViewModel.Mock()
|
||||
val authenticationViewModel = IAuthenticationViewModel.Mock()
|
||||
val bookViewModel = IBooksViewModel.Mock()
|
||||
LoginScreenComposable(
|
||||
navigationViewModel,
|
||||
credentialViewModel,
|
||||
authenticationViewModel,
|
||||
bookViewModel,
|
||||
navigationViewModel = INavigationViewModel.Mock(),
|
||||
credentialViewModel = ICredentialViewModel.Mock(),
|
||||
authenticationViewModel = IAuthenticationViewModel.Mock(),
|
||||
bookViewModel = IBooksViewModel.Mock(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -11,13 +11,13 @@ import androidx.lifecycle.MutableLiveData
|
|||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import com.pixelized.biblib.network.client.IBibLibClient
|
||||
import com.pixelized.biblib.network.data.query.AuthLoginQuery
|
||||
import com.pixelized.biblib.repository.credential.ICredentialRepository
|
||||
import com.pixelized.biblib.repository.googleSignIn.IGoogleSingInRepository
|
||||
import com.pixelized.biblib.ui.viewmodel.authentication.IAuthenticationViewModel.State
|
||||
import com.pixelized.biblib.utils.exception.MissingGoogleTokenException
|
||||
import com.pixelized.biblib.utils.exception.MissingTokenException
|
||||
import com.pixelized.biblib.utils.injection.inject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
|
@ -77,15 +77,19 @@ class AuthenticationViewModel : ViewModel(), IAuthenticationViewModel {
|
|||
launcher = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.StartActivityForResult()
|
||||
) {
|
||||
try {
|
||||
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
|
||||
val account: GoogleSignInAccount? = task.getResult(ApiException::class.java)
|
||||
val idToken = account?.idToken ?: throw MissingTokenException()
|
||||
client.token = idToken
|
||||
_state.postValue(State.Finished(true))
|
||||
} catch (exception: Exception) {
|
||||
Log.e("AuthenticationViewModel", exception.message, exception)
|
||||
_state.postValue(State.Error(exception))
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
|
||||
val account = task.getResult(ApiException::class.java)
|
||||
val googleToken = account?.idToken ?: throw MissingGoogleTokenException()
|
||||
val response = client.service.loginWithGoogle(googleToken)
|
||||
val token = response.token ?: throw MissingTokenException()
|
||||
client.token = token
|
||||
_state.postValue(State.Finished(true))
|
||||
} catch (exception: Exception) {
|
||||
Log.e("AuthenticationViewModel", exception.message, exception)
|
||||
_state.postValue(State.Error(exception))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ class BooksViewModel : ViewModel(), IBooksViewModel {
|
|||
private val client: IBibLibClient by inject()
|
||||
private val apiCache: IAPICacheRepository by inject()
|
||||
|
||||
private val _state = MutableLiveData<IBooksViewModel.State>()
|
||||
private val _state = MutableLiveData<IBooksViewModel.State>(IBooksViewModel.State.Initial)
|
||||
override val state: LiveData<IBooksViewModel.State> get() = _state
|
||||
|
||||
private val source
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue