Add loading and error dialogs for authentication.
This commit is contained in:
parent
b07bfd45d3
commit
59d84963a9
11 changed files with 296 additions and 127 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package com.pixelized.biblib.ui.viewmodel.authentication
|
||||
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
|
|
@ -14,12 +13,12 @@ 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.data.network.query.AuthLoginQuery
|
||||
import com.pixelized.biblib.utils.injection.inject
|
||||
import com.pixelized.biblib.network.client.IBibLibClient
|
||||
import com.pixelized.biblib.repository.credential.ICredentialRepository
|
||||
import com.pixelized.biblib.repository.googlesignin.IGoogleSingInRepository
|
||||
import com.pixelized.biblib.ui.viewmodel.authentication.IAuthentication.State
|
||||
import com.pixelized.biblib.utils.exception.MissingTokenException
|
||||
import com.pixelized.biblib.utils.injection.inject
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
|
@ -61,10 +60,13 @@ class AuthenticationViewModel : ViewModel(), IAuthentication {
|
|||
}
|
||||
|
||||
override fun updateRememberCredential(rememberCredential: Boolean) {
|
||||
_rememberCredential.postValue(rememberCredential)
|
||||
viewModelScope.launch {
|
||||
_rememberCredential.postValue(rememberCredential)
|
||||
credentialRepository.rememberCredential = rememberCredential
|
||||
if (rememberCredential.not()) {
|
||||
if (rememberCredential) {
|
||||
credentialRepository.login = login.value
|
||||
credentialRepository.password = password.value
|
||||
} else {
|
||||
credentialRepository.login = null
|
||||
credentialRepository.password = null
|
||||
}
|
||||
|
|
@ -75,24 +77,37 @@ class AuthenticationViewModel : ViewModel(), IAuthentication {
|
|||
viewModelScope.launch {
|
||||
_state.postValue(State.Loading)
|
||||
delay(3000)
|
||||
_state.postValue(State.Initial)
|
||||
_state.postValue(State.Error(MissingTokenException()))
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearState() {
|
||||
_state.postValue(State.Initial)
|
||||
}
|
||||
|
||||
override fun login() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
// TODO : validation !
|
||||
if (rememberCredential.value == true) {
|
||||
credentialRepository.login = login.value
|
||||
credentialRepository.password = password.value
|
||||
} else {
|
||||
credentialRepository.login = null
|
||||
credentialRepository.password = null
|
||||
}
|
||||
// TODO : validation !
|
||||
val query = AuthLoginQuery(
|
||||
username = login.value,
|
||||
password = password.value
|
||||
)
|
||||
// TODO : Repository (token management & co)
|
||||
val response = client.service.login(query)
|
||||
Log.e("pouet", response.toString())
|
||||
_state.postValue(State.Loading)
|
||||
try {
|
||||
val response = client.service.login(query)
|
||||
val idToken = response.token ?: throw MissingTokenException()
|
||||
client.updateBearerToken(idToken)
|
||||
_state.postValue(State.Connect)
|
||||
} catch (exception: Exception) {
|
||||
_state.postValue(State.Error(exception))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -105,7 +120,8 @@ class AuthenticationViewModel : ViewModel(), IAuthentication {
|
|||
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
|
||||
val account: GoogleSignInAccount? = task.getResult(ApiException::class.java)
|
||||
val idToken = account?.idToken ?: throw MissingTokenException()
|
||||
_state.postValue(State.Connect(idToken))
|
||||
client.updateBearerToken(idToken)
|
||||
_state.postValue(State.Connect)
|
||||
} catch (exception: Exception) {
|
||||
_state.postValue(State.Error(exception))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ interface IAuthentication {
|
|||
fun updateLoginField(login: String)
|
||||
fun updatePasswordField(password: String)
|
||||
fun updateRememberCredential(rememberCredential: Boolean)
|
||||
fun clearState()
|
||||
|
||||
fun register()
|
||||
fun login()
|
||||
|
|
@ -24,13 +25,12 @@ interface IAuthentication {
|
|||
sealed class State {
|
||||
object Initial : State()
|
||||
object Loading : State()
|
||||
data class Connect(val token: String) : State()
|
||||
object Connect : State()
|
||||
data class Error(val exception: Exception) : State()
|
||||
}
|
||||
|
||||
class Mock(waiting: Boolean = false) : IAuthentication {
|
||||
override val state: LiveData<State> =
|
||||
MutableLiveData(if (waiting) State.Loading else State.Initial)
|
||||
class Mock(state: State = State.Initial) : IAuthentication {
|
||||
override val state: LiveData<State> = MutableLiveData(state)
|
||||
override val login: LiveData<String?> = MutableLiveData("")
|
||||
override val password: LiveData<String?> = MutableLiveData("")
|
||||
override val rememberCredential: LiveData<Boolean> = MutableLiveData(true)
|
||||
|
|
@ -38,6 +38,8 @@ interface IAuthentication {
|
|||
override fun updateLoginField(login: String) = Unit
|
||||
override fun updatePasswordField(password: String) = Unit
|
||||
override fun updateRememberCredential(rememberCredential: Boolean) = Unit
|
||||
override fun clearState() = Unit
|
||||
|
||||
override fun register() = Unit
|
||||
override fun login() = Unit
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue