#7 added login viewmodel
This commit is contained in:
parent
fe66aa386a
commit
5bc0e51f8d
1 changed files with 69 additions and 0 deletions
|
@ -0,0 +1,69 @@
|
||||||
|
package be.ugent.sel.studeez.screens.sign_in
|
||||||
|
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import be.ugent.sel.studeez.common.ext.isValidEmail
|
||||||
|
import be.ugent.sel.studeez.common.snackbar.SnackbarManager
|
||||||
|
import be.ugent.sel.studeez.domain.AccountDAO
|
||||||
|
import be.ugent.sel.studeez.domain.LogService
|
||||||
|
import be.ugent.sel.studeez.navigation.StudeezDestinations
|
||||||
|
import be.ugent.sel.studeez.navigation.StudeezDestinations.LOGIN_SCREEN
|
||||||
|
import be.ugent.sel.studeez.navigation.StudeezDestinations.SIGN_UP_SCREEN
|
||||||
|
import be.ugent.sel.studeez.screens.StudeezViewModel
|
||||||
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import javax.inject.Inject
|
||||||
|
import be.ugent.sel.studeez.R.string as AppText
|
||||||
|
|
||||||
|
@HiltViewModel
|
||||||
|
class LoginViewModel @Inject constructor(
|
||||||
|
private val accountDAO: AccountDAO,
|
||||||
|
logService: LogService
|
||||||
|
) : StudeezViewModel(logService) {
|
||||||
|
var uiState = mutableStateOf(LoginUiState())
|
||||||
|
private set
|
||||||
|
|
||||||
|
private val email
|
||||||
|
get() = uiState.value.email
|
||||||
|
private val password
|
||||||
|
get() = uiState.value.password
|
||||||
|
|
||||||
|
fun onEmailChange(newValue: String) {
|
||||||
|
uiState.value = uiState.value.copy(email = newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onPasswordChange(newValue: String) {
|
||||||
|
uiState.value = uiState.value.copy(password = newValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onSignInClick(openAndPopUp: (String, String) -> Unit) {
|
||||||
|
if (!email.isValidEmail()) {
|
||||||
|
SnackbarManager.showMessage(AppText.email_error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (password.isBlank()) {
|
||||||
|
SnackbarManager.showMessage(AppText.empty_password_error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
launchCatching {
|
||||||
|
accountDAO.signInWithEmailAndPassword(email, password)
|
||||||
|
openAndPopUp(SIGN_UP_SCREEN, LOGIN_SCREEN) // Is not reached when error occurs.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onForgotPasswordClick() {
|
||||||
|
if (!email.isValidEmail()) {
|
||||||
|
SnackbarManager.showMessage(AppText.email_error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
launchCatching {
|
||||||
|
accountDAO.sendRecoveryEmail(email)
|
||||||
|
SnackbarManager.showMessage(AppText.recovery_email_sent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onNotAlreadyUser(openAndPopUp: (String, String) -> Unit) {
|
||||||
|
openAndPopUp(SIGN_UP_SCREEN, LOGIN_SCREEN)
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue