ViewModel refactor so Preview work.

This commit is contained in:
Thomas Andres Gomez 2021-05-08 15:33:24 +02:00
parent 45d2fe1336
commit 903d1973c2
16 changed files with 404 additions and 302 deletions

View file

@ -4,7 +4,7 @@ import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.pixelized.biblib.ui.viewmodel.AuthenticationViewModel
import com.pixelized.biblib.ui.viewmodel.authentication.AuthenticationViewModel
class CredentialRepository(application: Application) : ICredentialRepository {
private val preferences =

View file

@ -1,56 +1,21 @@
package com.pixelized.biblib.repository.googlesignin
import android.app.Application
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.pixelized.biblib.R
import com.pixelized.biblib.utils.exception.MissingTokenException
class GoogleSingInRepository(application: Application) : IGoogleSingInRepository {
override val googleSignInOption: GoogleSignInOptions by lazy {
override val option: GoogleSignInOptions by lazy {
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(application.getString(R.string.biblib_server_id))
.requestEmail()
.build()
}
override val googleSignIn: GoogleSignInClient by lazy {
GoogleSignIn.getClient(application, googleSignInOption)
}
@Composable
override fun prepareLoginWithGoogle(): IGoogleSingInRepository.Request {
val result = remember {
mutableStateOf<IGoogleSingInRepository.AuthenticationState>(
IGoogleSingInRepository.AuthenticationState.Initial
)
}
val 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()
result.value = IGoogleSingInRepository.AuthenticationState.Connect(idToken)
} catch (exception: Exception) {
result.value = IGoogleSingInRepository.AuthenticationState.Error(exception)
}
}
return IGoogleSingInRepository.Request(result, launcher)
}
override fun loginWithGoogle(request: IGoogleSingInRepository.Request) {
request.result.value = IGoogleSingInRepository.AuthenticationState.Loading
request.launcher.launch(googleSignIn.signInIntent)
override val client: GoogleSignInClient by lazy {
GoogleSignIn.getClient(application, option)
}
}

View file

@ -1,30 +1,9 @@
package com.pixelized.biblib.repository.googlesignin
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
interface IGoogleSingInRepository {
val googleSignInOption: GoogleSignInOptions
val googleSignIn: GoogleSignInClient
@Composable
fun prepareLoginWithGoogle(): Request
fun loginWithGoogle(request: Request)
data class Request(
val result: MutableState<AuthenticationState>,
val launcher: ActivityResultLauncher<Intent>,
)
sealed class AuthenticationState {
object Initial : AuthenticationState()
object Loading : AuthenticationState()
data class Connect(val token: String) : AuthenticationState()
data class Error(val exception: Exception) : AuthenticationState()
}
val option: GoogleSignInOptions
val client: GoogleSignInClient
}