Add login management.

This commit is contained in:
Thomas Andres Gomez 2021-05-08 14:01:57 +02:00
parent 8fbe3c0b7b
commit 45d2fe1336
14 changed files with 366 additions and 158 deletions

View file

@ -0,0 +1,42 @@
package com.pixelized.biblib.repository.credential
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.pixelized.biblib.ui.viewmodel.AuthenticationViewModel
class CredentialRepository(application: Application) : ICredentialRepository {
private val preferences =
application.getSharedPreferences(AuthenticationViewModel.SHARED_PREF, Context.MODE_PRIVATE)
override var login
get() = preferences.login
set(value) {
preferences.login = value
}
override var password: String?
get() = preferences.password
set(value) {
preferences.password = value
}
override var rememberCredential: Boolean
get() = preferences.rememberCredential
set(value) {
preferences.rememberCredential = value
}
private var SharedPreferences.login: String?
get() = getString(AuthenticationViewModel.REMEMBER_USER, null)
set(value) = edit { putString(AuthenticationViewModel.REMEMBER_USER, value) }
private var SharedPreferences.password: String?
get() = getString(AuthenticationViewModel.REMEMBER_PASSWORD, null)
set(value) = edit { putString(AuthenticationViewModel.REMEMBER_PASSWORD, value) }
private var SharedPreferences.rememberCredential: Boolean
get() = getBoolean(AuthenticationViewModel.REMEMBER_CREDENTIAL, false)
set(value) = edit { putBoolean(AuthenticationViewModel.REMEMBER_CREDENTIAL, value) }
}

View file

@ -0,0 +1,7 @@
package com.pixelized.biblib.repository.credential
interface ICredentialRepository {
var login: String?
var password: String?
var rememberCredential: Boolean
}

View file

@ -0,0 +1,56 @@
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 {
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)
}
}

View file

@ -0,0 +1,30 @@
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()
}
}