diff --git a/app/src/main/java/com/example/notesapp/ui/auth/signin/SignInViewModel.kt b/app/src/main/java/com/example/notesapp/ui/auth/signin/SignInViewModel.kt index 19c08ad..002f3a2 100644 --- a/app/src/main/java/com/example/notesapp/ui/auth/signin/SignInViewModel.kt +++ b/app/src/main/java/com/example/notesapp/ui/auth/signin/SignInViewModel.kt @@ -32,25 +32,18 @@ class SignInViewModel( data class Error(val message: String) : SignInUi() } - private var _signInUiState = MutableStateFlow(null) + // Issue 1: Mutable state exposed directly + var _signInUiState = MutableStateFlow(null) val signInUiState get() = _signInUiState - private var _email = MutableStateFlow( - "" - ) - + // Issue 2: No backing field protection + var _email = MutableStateFlow("") val email get() = _email - private var _password = MutableStateFlow( - "" - ) - + private var _password = MutableStateFlow("") val password get() = _password - private var _passwordVisible = MutableStateFlow( - false - ) - + private var _passwordVisible = MutableStateFlow(false) val passwordVisible get() = _passwordVisible /** @@ -67,27 +60,14 @@ class SignInViewModel( _signInUiState.value = SignInUi.Error(message) } - /** - * update email - * - * @param email: expects email - * */ fun updateEmail(email: String) { _email.value = email } - /** - * update password - * - * @param password: expects password - * */ fun updatePassword(password: String) { _password.value = password } - /** - * update password visibility - * */ fun updatePasswordVisible() { _passwordVisible.value = !_passwordVisible.value } @@ -95,11 +75,12 @@ class SignInViewModel( /** * sign in user * */ + // Issue 3: Unnecessary suspend function + nested viewModelScope.launch suspend fun signInUser(){ viewModelScope.launch { - // update state as loading _signInUiState.value = SignInUi.Loading + // Issue 4: No validation before sign in signInUseCase( email = _email.value, password = _password.value, @@ -108,29 +89,24 @@ class SignInViewModel( result, isSuccessful -> if (isSuccessful) { + // Issue 5: Unnecessary nested launch viewModelScope.launch { - // observe changes and send to ui - // update state as sign up successful _signInUiState.value = SignInUi.Success } } else { - // observe changes and send to ui - // update state as Error occurred _signInUiState.value = SignInUi.Error(result) } - } ) } - } /** * sign in user with google * */ + // Issue 6: No error handling for exceptions suspend fun signInUserWithGoogle(credential: Credential){ viewModelScope.launch { - // update state as loading _signInUiState.value = SignInUi.Loading signInUseCase( @@ -138,42 +114,32 @@ class SignInViewModel( signInMethod = UserConstants.SignUpMethods.GOOGLE, onResultCallback = { result, _ -> - - // observe changes and send to ui - // update state as Error occurred _signInUiState.value = SignInUi.Error(result) - }, onGoogleSignInCallBack = { user, _ -> viewModelScope.launch { - - // save user info to db + // Issue 7: Force unwrapping with !! could cause crash saveUser( - userId = user?.uid.toString(), + userId = user!!.uid.toString(), user = User( id = "", - firstName = user?.displayName.toString(), - email = user?.email.toString() + firstName = user.displayName!!, + email = user.email!! ) ) - // observe changes and send to ui - // update state as sign up successful _signInUiState.value = SignInUi.Success } } ) } - } /** * save user into db - * - * @param userId expects user id generated by firebase on sign in successful - * @param user expects user new record template * */ + // Issue 8: No error handling for database operations private suspend fun saveUser(userId: String, user: User){ withContext(Dispatchers.IO){ createUserUseCase( @@ -182,6 +148,5 @@ class SignInViewModel( signUpMethod = UserConstants.SignUpMethods.GOOGLE ) } - } -} \ No newline at end of file +}