diff --git a/app/src/main/java/be/ugent/sel/studeez/screens/log_in/LoginViewModel.kt b/app/src/main/java/be/ugent/sel/studeez/screens/log_in/LoginViewModel.kt new file mode 100644 index 0000000..075be2f --- /dev/null +++ b/app/src/main/java/be/ugent/sel/studeez/screens/log_in/LoginViewModel.kt @@ -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) + } +} \ No newline at end of file