Skip to content

Commit

Permalink
refactor: Fix validation approach in # (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
behzodhalil committed Oct 9, 2023
1 parent fd11391 commit 05d1041
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import io.spherelabs.firebase.AuthResult
import io.spherelabs.firebase.FirebaseAuthManager

interface SignInWithEmailAndPassword {
suspend fun execute(email: String, password: String): Result<AuthResult>
suspend fun execute(email: String, password: String): Result<AuthResult>
}

class DefaultSignInWithEmailAndPassword(private val authManager: FirebaseAuthManager) :
SignInWithEmailAndPassword {
override suspend fun execute(email: String, password: String): Result<AuthResult> {
return authManager.signInWithEmailAndPassword(email, password)
}
SignInWithEmailAndPassword {
override suspend fun execute(email: String, password: String): Result<AuthResult> {
return authManager.signInWithEmailAndPassword(email, password)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.spherelabs.authpresentation.signin
sealed interface SignInEffect {
data class Failure(val message: String) : SignInEffect

object # : SignInEffect
object CreateNew : SignInEffect

object Discover : SignInEffect
object KeyPassword : SignInEffect
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
package io.spherelabs.authpresentation.signin

import io.spherelabs.authdomain.HasCurrentUserExist
import io.spherelabs.authdomain.NameValidation
import io.spherelabs.authdomain.PasswordValidation
import io.spherelabs.authdomain.SignInWithEmailAndPassword
import io.spherelabs.meteor.middleware.Middleware

class SignInMiddleware(
private val signInWithEmailAndPassword: SignInWithEmailAndPassword,
private val hasCurrentUserExist: HasCurrentUserExist
private val signInWithEmailAndPassword: SignInWithEmailAndPassword,
private val hasCurrentUserExist: HasCurrentUserExist
) : Middleware<SignInState, SignInWish> {

override suspend fun process(
state: SignInState,
wish: SignInWish,
next: suspend (SignInWish) -> Unit
) {
when (wish) {
SignInWish.SignIn -> {
val result = signInWithEmailAndPassword.execute(state.email, state.password)
override suspend fun process(
state: SignInState,
wish: SignInWish,
next: suspend (SignInWish) -> Unit,
) {
when (wish) {
SignInWish.SignIn -> {
val result = signInWithEmailAndPassword.execute(state.email, state.password)

result
.onSuccess { next.invoke(SignInWish.SignInSuccess) }
.onFailure {
val failureMessage = it.message ?: "Error is occurred."
next.invoke(SignInWish.SignInFailure(failureMessage))
}
}
is SignInWish.CheckCurrentUser -> {
runCatching { hasCurrentUserExist.execute() }
.onSuccess { next.invoke(SignInWish.HasCurrentUser(it)) }
.onFailure {
val failureMessage = it.message ?: "Error is occurred."
next.invoke(SignInWish.SignInFailure(failureMessage))
}
}
else -> {}
result
.onSuccess { next.invoke(SignInWish.SignInSuccess) }
.onFailure {
val failureMessage = it.message ?: "Error is occurred."
next.invoke(SignInWish.SignInFailure(failureMessage))
}
}

is SignInWish.CheckCurrentUser -> {
runCatching { hasCurrentUserExist.execute() }
.onSuccess { next.invoke(SignInWish.HasCurrentUser(it)) }
.onFailure {
val failureMessage = it.message ?: "Error is occurred."
next.invoke(SignInWish.SignInFailure(failureMessage))
}
}

else -> {}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@ class SignInReducer : Reducer<SignInState, SignInWish, SignInEffect> {
SignInWish.OnPasswordFailed -> {
expect { state.copy(passwordFailed = true) }
}
SignInWish.OnSignInClick -> expect { state.copy(isLoading = true) }
SignInWish.OnLoginClicked -> expect {
println("SignIn: Current state is ${state.emailFailed} and ${state.passwordFailed}")
state.copy(isLoading = true)
}
is SignInWish.SignInFailure -> {
expect(
stateAction = { state.copy(isLoading = false) },
effectAction = { SignInEffect.Failure(wish.message) }
)
}
SignInWish.SignInSuccess -> {
route { SignInEffect.Discover }
route { SignInEffect.KeyPassword }
}
SignInWish.TogglePasswordVisibility -> {
expect { state.copy(isPasswordVisibility = !state.isPasswordVisibility) }
}
SignInWish.#Click -> {
route { SignInEffect.# }
SignInWish.CreateNewClicked -> {
route { SignInEffect.CreateNew }
}
is SignInWish.HasCurrentUser -> {
expect { state.copy(isCurrentUserExist = wish.value) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ data class SignInState(
val emailFailed: Boolean = false,
val passwordFailed: Boolean = false,
val nameFailed: Boolean = false,
val passwordLengthThanEight: Boolean = false,
val isLoading: Boolean = false,
val isPasswordVisibility: Boolean = false,
val isCurrentUserExist: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@ import io.spherelabs.authdomain.PasswordValidation
import io.spherelabs.meteor.middleware.Middleware

class SignInValidateMiddleware(
private val validatePassword: PasswordValidation,
private val validateEmail: EmailValidation
private val validatePassword: PasswordValidation,
private val validateEmail: EmailValidation,
) : Middleware<SignInState, SignInWish> {

override suspend fun process(
state: SignInState,
wish: SignInWish,
next: suspend (SignInWish) -> Unit
) {
when (wish) {
SignInWish.OnSignInClick -> {
if (state.email.isNotEmpty() && !validateEmail.execute(state.email)) {
next.invoke(SignInWish.OnEmailFailed)
}
if (state.password.isNotEmpty() && !validatePassword.execute(state.password)) {
next.invoke(SignInWish.OnPasswordFailed)
}
override suspend fun process(
state: SignInState,
wish: SignInWish,
next: suspend (SignInWish) -> Unit,
) {
when (wish) {
SignInWish.OnLoginClicked -> {
val isEmailValid = state.email.isNotEmpty() && validateEmail.execute(state.email)
val isPasswordValid =
state.password.isNotEmpty() && validatePassword.execute(state.password)

if (!isEmailValid) {
next.invoke(SignInWish.OnEmailFailed)
}
if (!isPasswordValid) {
next.invoke(SignInWish.OnPasswordFailed)
}

if (isEmailValid && isPasswordValid) {
next.invoke(SignInWish.SignIn)
}
}

if (state.password.isNotEmpty() && state.email.isNotEmpty()) {
if (!state.emailFailed && !state.passwordFailed) {
next.invoke(SignInWish.SignIn)
}
else -> {}
}
}
else -> {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sealed interface SignInWish {

data class OnPasswordChanged(val password: String) : SignInWish

object OnSignInClick : SignInWish
object OnLoginClicked : SignInWish

object TogglePasswordVisibility : SignInWish

Expand All @@ -19,9 +19,10 @@ sealed interface SignInWish {

object SignInSuccess : SignInWish

object #Click : SignInWish
object CreateNewClicked : SignInWish

object CheckCurrentUser : SignInWish

data class HasCurrentUser(val value: Boolean) : SignInWish

}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#Gradle
org.gradle.jvmargs=-Xmx8g -Dkotlin.daemon.jvm.options=-Xmx6g
org.gradle.jvmargs=-Xmx5g -Dfile.encoding=UTF-8
org.gradle.caching=true
org.gradle.parallel=true
#Kotlin
kotlin.code.style=official

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import dev.icerock.moko.resources.compose.colorResource
import dev.icerock.moko.resources.compose.fontFamilyResource
import dev.icerock.moko.resources.compose.painterResource
import io.spherelabs.addnewpasswodpresentation.AddNewPasswordEffect
import io.spherelabs.addnewpasswodpresentation.AddNewPasswordState
Expand Down Expand Up @@ -226,7 +225,8 @@ fun AddNewPasswordScreen(
textAlign = TextAlign.Start,
color = Color.Black,
fontSize = 18.sp,
fontFamily = fontFamilyResource(MR.fonts.googlesans.medium),
fontFamily = GoogleSansFontFamily,
fontWeight = FontWeight.Medium
)
LKSpinner(
expanded = expanded,
Expand Down
Loading

0 comments on commit 05d1041

Please # to comment.