Created
March 12, 2025 18:11
-
-
Save sajjadyousefnia/3c985774dcd9b1b0ab8676600e655362 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.divadventure.divadventure.ViewModel | |
import com.divadventure.divadventure.BaseViewModel | |
import com.divadventure.divadventure.data.AuthService | |
import dagger.hilt.android.lifecycle.HiltViewModel | |
import timber.log.Timber | |
import javax.inject.Inject | |
import javax.inject.Singleton | |
@HiltViewModel | |
class AuthViewModel @Inject constructor( | |
private val authService: AuthService | |
) : BaseViewModel<AuthIntent, AuthState>(AuthState()) { | |
override suspend fun handleIntent(intent: AuthIntent) { | |
when (intent) { | |
is AuthIntent.SignUp -> { | |
updateState( | |
state.value.copy( | |
isLoadingSignUp = false, signupError = "", executeSigneUp = true | |
) | |
) | |
/** | |
updateState(state.value.copy(isLoadingSignUp = true)) | |
val signupRequest = SignupRequest( | |
email = intent.email, | |
password = intent.password, | |
password_confirmation = intent.password | |
) | |
authService.signup(request = signupRequest) | |
.enqueue(object : Callback<SignUpResponse> { | |
override fun onResponse( | |
call: Call<SignUpResponse>, | |
response: Response<SignUpResponse> | |
) { | |
updateState( | |
state.value.copy( | |
isLoadingSignUp = false, | |
signupError = "", | |
isSigneUp = true | |
) | |
) | |
} | |
override fun onFailure(call: Call<SignUpResponse>, t: Throwable) { | |
state.value.copy( | |
isLoadingSignUp = false, | |
isSigneUp = false, | |
signupError = t.message ?: "", | |
) | |
} | |
}) | |
*/ | |
} | |
AuthIntent.CheckIfLoggedIn -> {} | |
AuthIntent.ClearError -> {} | |
is AuthIntent.ForgotPassword -> {} | |
AuthIntent.GoToForgotPassword -> {} | |
AuthIntent.GoToLanding -> {} | |
AuthIntent.GoToSignIn -> {} | |
is AuthIntent.SignIn -> {} | |
AuthIntent.SignUpWithGoogle -> { | |
updateState( | |
state.value.copy( | |
executeSigneUp = true | |
) | |
) | |
Timber.d("Signing up with Google...") | |
} | |
is AuthIntent.OnEmailChanged -> { | |
updateState( | |
state.value.copy( | |
email = intent.email | |
) | |
) | |
checkSignupCardColor() | |
} | |
is AuthIntent.OnPasswordChanged -> { | |
updateState( | |
state.value.copy( | |
password = intent.password | |
) | |
) | |
checkSignupCardColor() | |
} | |
is AuthIntent.OnPasswordConfirmationChanged -> { | |
updateState( | |
state.value.copy( | |
passwordConfirmation = intent.passwordConfirmation | |
) | |
) | |
checkSignupCardColor() | |
} | |
is AuthIntent.OnFirstNameChanged -> { | |
updateState( | |
state.value.copy( | |
firstName = intent.firstName | |
) | |
) | |
checkOnbaordingValues() | |
} | |
is AuthIntent.OnLastNameChanged -> { | |
updateState( | |
state.value.copy( | |
lastName = intent.lastName | |
) | |
) | |
checkOnbaordingValues() | |
} | |
is AuthIntent.OnUserNameChanged -> { | |
updateState( | |
state.value.copy( | |
userName = intent.userName | |
) | |
) | |
checkOnbaordingValues() | |
} | |
is AuthIntent.Onboard -> { | |
if (state.value.onboardingDataAccepted) { | |
updateState( | |
state.value.copy( | |
executeOnboard = true | |
) | |
) | |
} | |
} | |
is AuthIntent.OnEmailRewrite -> { | |
if (isEmailValid(intent.email)) { | |
updateState( | |
state.value.copy(permitEmailRevision = true) | |
) | |
} else { | |
updateState( | |
state.value.copy( | |
permitEmailRevision = false | |
) | |
) | |
} | |
} | |
is AuthIntent.OnChangeOtp -> { | |
updateState( | |
state.value.copy( | |
otpCode = intent.otp | |
) | |
) | |
if (intent.otp.length == 5) { | |
updateState( | |
state.value.copy( | |
isOtpCorrect = true | |
) | |
) | |
} else { | |
updateState( | |
state.value.copy(isOtpCorrect = true) | |
) | |
} | |
} | |
is AuthIntent.Login -> { | |
updateState( | |
state.value.copy( | |
executeSigneUp = true | |
) | |
) | |
} | |
AuthIntent.GoToSignUp -> { | |
} | |
is AuthIntent.OnNavigation -> { | |
} | |
} | |
} | |
private fun resetValuesInLogin() { | |
updateState( | |
state.value.copy( | |
email = "", | |
password = "", | |
loginDataAccepted = false, | |
executeLogin = false | |
) | |
) | |
} | |
private fun resetValuesSignup() { | |
updateState( | |
state.value.copy( | |
email = "", | |
password = "", | |
passwordConfirmation = "", | |
signupDataAccepted = false | |
) | |
) | |
} | |
private fun checkOnbaordingValues() { | |
val firstName = state.value.firstName | |
val lastName = state.value.lastName | |
val userName = state.value.userName | |
if (firstName.isNotEmpty() && lastName.isNotEmpty() && userName.isNotEmpty()) { | |
updateState( | |
state.value.copy( | |
onboardingDataAccepted = true | |
) | |
) | |
} else { | |
updateState( | |
state.value.copy( | |
onboardingDataAccepted = false | |
) | |
) | |
} | |
} | |
private fun checkSignupCardColor() { | |
val email = state.value.email | |
val password = state.value.password | |
val passwordConfirmation = state.value.passwordConfirmation | |
val isEmailNotEmpty = email.isNotEmpty() | |
val isPasswordNotEmpty = password.isNotEmpty() | |
val isEmailValid = isEmailValid(email) | |
val isPasswordAtLeast8Chars = isAtleast8Characters(password) | |
val doPasswordsMatch = | |
passwordsMatch(password = password, passwordConfirmation = passwordConfirmation) | |
val isValid = | |
isEmailNotEmpty && isPasswordNotEmpty && isEmailValid && isPasswordAtLeast8Chars && doPasswordsMatch | |
val issue = when { | |
!isEmailNotEmpty -> "Email can't be empty" | |
!isEmailValid -> "Email is not valid" | |
!isPasswordNotEmpty -> "Password can't be empty" | |
!isPasswordAtLeast8Chars -> "Password must have at least 8 characters" | |
!doPasswordsMatch -> "Passwords does not match" | |
else -> null | |
} | |
updateState( | |
state.value.copy( | |
signupDataAccepted = isValid, | |
formError = issue ?: "" | |
) | |
) | |
} | |
private fun passwordsMatch(password: String, passwordConfirmation: String): Boolean { | |
return password == passwordConfirmation | |
} | |
private fun isAtleast8Characters(password: String): Boolean { | |
return password.length >= 8 | |
} | |
private fun isEmailValid(email: String): Boolean { | |
val emailRegex = """^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$""".toRegex() | |
return emailRegex.matches(email) | |
} | |
} | |
sealed class AuthIntent { | |
data class SignIn(val email: String, val password: String) : AuthIntent() | |
data class Login( | |
val emailOrUsername: String, val password: String | |
) : AuthIntent() | |
//* data class SignUp(val user: User, val password: String) : AuthIntent() | |
data class ForgotPassword(val email: String) : AuthIntent() | |
data class SignUp(val email: String, val password: String, val passwordConfirmation: String) : | |
AuthIntent() | |
data class OnEmailChanged(var email: String) : AuthIntent() | |
data class OnPasswordChanged(var password: String) : AuthIntent() | |
data class OnPasswordConfirmationChanged(var passwordConfirmation: String) : AuthIntent() | |
data class OnUserNameChanged(var userName: String) : AuthIntent() | |
data class OnFirstNameChanged(var firstName: String) : AuthIntent() | |
data class OnLastNameChanged(var lastName: String) : AuthIntent() | |
data class OnChangeOtp(val otp: String) : AuthIntent() | |
data class Onboard(var firstName: String, var lastName: String, var userName: String) : | |
AuthIntent() | |
data class OnEmailRewrite(var email: String) : AuthIntent() | |
object CheckIfLoggedIn : AuthIntent() | |
object GoToSignUp : AuthIntent() | |
object GoToSignIn : AuthIntent() | |
object GoToForgotPassword : AuthIntent() | |
object GoToLanding : AuthIntent() | |
object ClearError : AuthIntent() | |
object SignUpWithGoogle : AuthIntent() | |
data class OnNavigation( | |
val source: String, | |
val destination: String, | |
) : AuthIntent() | |
} | |
@Singleton | |
data class AuthState( | |
var email: String = (""), | |
var password: String = "", | |
var passwordConfirmation: String = "", | |
var firstName: String = "", | |
var lastName: String = "", | |
var userName: String = "", | |
var formError: String = "", | |
var signupDataAccepted: Boolean = false, | |
var loginDataAccepted: Boolean = false, | |
var onboardingDataAccepted: Boolean = false, | |
var changingEmailEnabled: Boolean = false, | |
var permitEmailRevision: Boolean = false, | |
var otpCode: String = "", | |
var isOtpCorrect: Boolean = false, | |
val signupError: String = "", | |
var isLoadingSignUp: Boolean = false, | |
var executeSigneUp: Boolean = false, | |
var executeLogin: Boolean = false, | |
var executeOnboard: Boolean = false, | |
val isEmailVerified: Boolean = false | |
) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment