Skip to content

Commit

Permalink
auth validation forms
Browse files Browse the repository at this point in the history
  • Loading branch information
RikoAppDev committed Apr 22, 2024
1 parent 930cac4 commit 4a6dfd2
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<string name="register_screen__welcome">Všetko je pripravené!</string>
<string name="register_screen__collecting_adventures">Začať je ľahké – vyberte si, čo a kde by ste chceli zbierať. Tešíme sa na naše spoločné dobrodružstvá pri zbieraní!</string>
<string name="register_screen__start_using">Začnite používať aplikáciu!</string>
<string name="register_screen__empty_name">Zadajte meno</string>
<string name="event_detail_screen__title">Detaily zberu</string>
<string name="event_detail_screen__featured_image">Hlavný obrázok udalosti</string>
<string name="event_detail_screen__sign_in_for_harvest">Prihlásiť sa na zber</string>
Expand Down Expand Up @@ -111,11 +112,13 @@
<!-- Form errors -->
<string name="event_create_update_screen__title_error">Názov je požadovaný</string>
<string name="event_create_update_screen__capacity_error">Kapacita je požadovaná</string>
<string name="event_create_update_screen__capacity_invalid_error">Kapacita nie je validná</string>
<string name="event_create_update_screen__date_error">Dátum je požadovaný</string>
<string name="event_create_update_screen__time_error">Čas je požadovaný</string>
<string name="event_create_update_screen__date_time_past">Dátum a čas musia byť v budúcnosti</string>
<string name="event_create_update_screen__location_error">Miesto je požadované</string>
<string name="event_create_update_screen__salary_amount_error">Suma platu je požadovaná</string>
<string name="event_create_update_screen__salary_invalid_amount_error">Suma platu nie je validná</string>

<string name="event_create_update_screen__create_harvest">Vytvoriť zber</string>
<string name="event_create_update_screen__update_harvest">Aktualizovať zber</string>
Expand Down
3 changes: 3 additions & 0 deletions composeApp/src/commonMain/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<string name="register_screen__welcome">Everything is prepared!</string>
<string name="register_screen__collecting_adventures">Getting started is easy – choose what and where you'd like to harvest. We're excited for our shared gathering adventures!</string>
<string name="register_screen__start_using">Start using application!</string>
<string name="register_screen__empty_name">Name cannot be empty!</string>

<string name="event_detail_screen__title">Harvest Detail</string>
<string name="event_detail_screen__featured_image">Event featured image</string>
Expand Down Expand Up @@ -130,11 +131,13 @@
<!-- Form errors -->
<string name="event_create_update_screen__title_error">Title is required</string>
<string name="event_create_update_screen__capacity_error">Capacity is required</string>
<string name="event_create_update_screen__capacity_invalid_error">Capacity is invalid</string>
<string name="event_create_update_screen__date_error">Date is required</string>
<string name="event_create_update_screen__time_error">Time is required</string>
<string name="event_create_update_screen__date_time_past">Date and time must be in future</string>
<string name="event_create_update_screen__location_error">Location is required</string>
<string name="event_create_update_screen__salary_amount_error">Salary amount is required</string>
<string name="event_create_update_screen__salary_invalid_amount_error">Salary amount is invalid</string>

<string name="event_create_update_screen__create_harvest">Create harvest</string>
<string name="event_create_update_screen__update_harvest">Update harvest</string>
Expand Down
4 changes: 4 additions & 0 deletions composeApp/src/commonMain/kotlin/auth/domain/AuthError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ sealed interface AuthError : Error {
enum class TokenError : AuthError {
EXPIRED
}

enum class ProfileError : AuthError {
EMPTY_NAME
}
}
15 changes: 13 additions & 2 deletions composeApp/src/commonMain/kotlin/auth/domain/AuthValidation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class AuthValidation {
if (password.length < 8) {
emit(ResultHandler.Error(AuthError.PasswordError.TOO_SHORT))
} else {
val hasDigit = password.all { it.isDigit() }
println(password)
val hasDigit = password.any { it.isDigit() }
if (!hasDigit) {
emit(ResultHandler.Error(AuthError.PasswordError.NO_DIGIT))
} else {
Expand All @@ -36,8 +37,18 @@ class AuthValidation {
): Flow<ResultHandler<Boolean, AuthError.PasswordError>> = flow {
if (password != passwordRepeated) {
emit(ResultHandler.Error(AuthError.PasswordError.NO_MATCH))
} else {
emit(ResultHandler.Success(true))
}
}

emit(ResultHandler.Success(true))
suspend fun validateName(
name: String,
): Flow<ResultHandler<Boolean, AuthError.ProfileError>> = flow {
if (name.isEmpty()) {
emit(ResultHandler.Error(AuthError.ProfileError.EMPTY_NAME))
} else {
emit(ResultHandler.Success(true))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ class LoginScreenComponent(
}

is LoginScreenEvent.ClickLoginButton -> {
println(_stateLogin.value)
// validateEmail()
// validatePassword()

loginUser(
login = Login(
email = _stateLogin.value.email,
password = _stateLogin.value.password
validateForm()
if (isFormValid()) {
loginUser(
login = Login(
email = _stateLogin.value.email,
password = _stateLogin.value.password
)
)
)
}
}

is LoginScreenEvent.UpdateEmail -> {
Expand All @@ -71,24 +70,27 @@ class LoginScreenComponent(
}

private fun isFormValid(): Boolean {
return _stateLogin.value.email.isNotEmpty() && _stateLogin.value.password.isNotEmpty() && _stateLogin.value.emailError != null && _stateLogin.value.passwordError != null
return _stateLogin.value.email.isNotEmpty() && _stateLogin.value.password.isNotEmpty() && _stateLogin.value.emailError == null && _stateLogin.value.passwordError == null
}

private fun validateForm() {
validateEmail()
if (_stateLogin.value.emailError == null) {
validatePassword()
}
}

private fun validateEmail() {
coroutineScope().launch {
authValidation.validateEmail(_stateLogin.value.email).collect { result ->
if (result is ResultHandler.Error) {
println("Email result: $result")
_stateLogin.value = _stateLogin.value.copy(
emailError = result.error.asUiText().asNonCompString()
)
println(result)
println(_stateLogin.value)
} else if (result is ResultHandler.Success) {
_stateLogin.value = _stateLogin.value.copy(
emailError = null
)
println(_stateLogin.value)
}
}
}
Expand All @@ -98,21 +100,16 @@ class LoginScreenComponent(
coroutineScope().launch {
authValidation.validatePassword(_stateLogin.value.password).collect { result ->
if (result is ResultHandler.Error) {
println("Password result: $result")
_stateLogin.value = _stateLogin.value.copy(
passwordError = result.error.asUiText().asNonCompString()
)
println(result)
println(_stateLogin.value)
} else if (result is ResultHandler.Success) {
_stateLogin.value = _stateLogin.value.copy(
passwordError = null
)
println(_stateLogin.value)
}
}
}
println(_stateLogin.value)
}

private fun loginUser(login: Login) {
Expand Down
Loading

0 comments on commit 4a6dfd2

Please sign in to comment.