added sign up viewmodel
This commit is contained in:
		
							parent
							
								
									2f5e7e31aa
								
							
						
					
					
						commit
						d3d5bb7dbe
					
				
					 1 changed files with 67 additions and 0 deletions
				
			
		|  | @ -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) | ||||
|     } | ||||
| } | ||||
		Reference in a new issue
	
	 lbarraga
						lbarraga