diff --git a/app/src/main/java/be/ugent/sel/studeez/screens/sign_up/SignUpViewModel.kt b/app/src/main/java/be/ugent/sel/studeez/screens/sign_up/SignUpViewModel.kt new file mode 100644 index 0000000..a22fe68 --- /dev/null +++ b/app/src/main/java/be/ugent/sel/studeez/screens/sign_up/SignUpViewModel.kt @@ -0,0 +1,67 @@ +package be.ugent.sel.studeez.screens.sign_up + +import androidx.compose.runtime.mutableStateOf +import be.ugent.sel.studeez.common.ext.isValidEmail +import be.ugent.sel.studeez.common.ext.isValidPassword +import be.ugent.sel.studeez.common.ext.passwordMatches +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.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 be.ugent.sel.studeez.R.string as AppText +import javax.inject.Inject + +@HiltViewModel +class SignUpViewModel @Inject constructor( + private val accountService: AccountDAO, + logService: LogService + ) : StudeezViewModel(logService) { + var uiState = mutableStateOf(SignUpUiState()) + 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 onRepeatPasswordChange(newValue: String) { + uiState.value = uiState.value.copy(repeatPassword = newValue) + } + + fun onSignUpClick(openAndPopUp: (String, String) -> Unit) { + if (!email.isValidEmail()) { + SnackbarManager.showMessage(AppText.email_error) + return + } + + if (!password.isValidPassword()) { + SnackbarManager.showMessage(AppText.password_error) + return + } + + if (!password.passwordMatches(uiState.value.repeatPassword)) { + SnackbarManager.showMessage(AppText.password_match_error) + return + } + + launchCatching { + accountService.signUpWithEmailAndPassword(email, password) + openAndPopUp(LOGIN_SCREEN, SIGN_UP_SCREEN) + } + } + + fun onLoginScreenClick(openAndPopUp: (String, String) -> Unit) { + openAndPopUp(LOGIN_SCREEN, SIGN_UP_SCREEN) + } +} \ No newline at end of file