Skip to content

Commit

Permalink
Show proper errors for invalid email and password formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mitrejcevski committed Aug 14, 2024
1 parent 4563c40 commit efcfff3
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nl.jovmit.androiddevs.core.view.composables

import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -15,6 +16,7 @@ import nl.jovmit.androiddevs.core.view.theme.AppTheme
fun EmailInput(
modifier: Modifier = Modifier,
email: String,
isInvalidEmailFormat: Boolean = false,
keyboardOptions: KeyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Email,
imeAction = ImeAction.Done
Expand All @@ -29,7 +31,15 @@ fun EmailInput(
hint = stringResource(id = R.string.email_hint),
onTextChanged = onEmailChanged,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions
keyboardActions = keyboardActions,
error = {
if (isInvalidEmailFormat) {
Text(
text = stringResource(id = R.string.error_bad_email_format),
color = AppTheme.colorScheme.error
)
}
}
)
}

Expand All @@ -42,4 +52,16 @@ private fun EmailInputPreview() {
onEmailChanged = {}
)
}
}

@Composable
@PreviewLightDark
private fun EmailInputPreviewWithError() {
AppTheme {
EmailInput(
email = "",
onEmailChanged = {},
isInvalidEmailFormat = true
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.material.icons.Icons.Default
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -24,6 +25,7 @@ import nl.jovmit.androiddevs.core.view.theme.AppTheme
fun PasswordInput(
modifier: Modifier = Modifier,
password: String,
isInvalidPasswordFormat: Boolean = false,
keyboardActions: KeyboardActions = KeyboardActions.Default,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
onPasswordChanged: (newValue: String) -> Unit
Expand Down Expand Up @@ -58,6 +60,14 @@ fun PasswordInput(
contentDescription = null,
tint = AppTheme.colorScheme.onBackground
)
},
error = {
if (isInvalidPasswordFormat) {
Text(
text = stringResource(id = R.string.error_bad_password_format),
color = AppTheme.colorScheme.error
)
}
}
)
}
Expand All @@ -82,4 +92,16 @@ private fun PreviewPasswordInputWithHint() {
onPasswordChanged = {}
)
}
}

@Composable
@PreviewLightDark
private fun PreviewPasswordInputWithError() {
AppTheme {
PasswordInput(
password = "",
onPasswordChanged = {},
isInvalidPasswordFormat = true
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal fun TextInput(
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
trailingIcon: (@Composable () -> Unit)? = null,
error: (@Composable () -> Unit)? = null,
onTextChanged: (newValue: String) -> Unit
) {
var actualBorderColor by remember { mutableStateOf(borderColor) }
Expand Down Expand Up @@ -122,6 +123,9 @@ internal fun TextInput(
}
}
}
if (error != null) {
error()
}
}
}

Expand Down Expand Up @@ -213,3 +217,29 @@ private fun TextInputPreviewWithTrailingIcon() {
}
}
}

@PreviewLightDark
@Composable
private fun TextInputPreviewWithError() {
AppTheme {
Box(
modifier = Modifier
.background(AppTheme.colorScheme.background)
.padding(AppTheme.size.normal)
) {
TextInput(
modifier = Modifier.fillMaxWidth(),
text = "something",
label = "Enter Pass",
onTextChanged = {},
visualTransformation = PasswordVisualTransformation(),
error = {
Text(
text = "Error of some kind",
color = AppTheme.colorScheme.onBackground
)
}
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ data class AppColorScheme(
val onPrimary: Color,
val secondary: Color,
val onSecondary: Color,
val separator: Color
val separator: Color,
val error: Color
) {

val primaryHorizontalGradient: Brush = Brush.horizontalGradient(listOf(primary, secondary))
Expand Down Expand Up @@ -57,7 +58,8 @@ val LocalAppColorScheme = staticCompositionLocalOf {
onPrimary = Color.Unspecified,
secondary = Color.Unspecified,
onSecondary = Color.Unspecified,
separator = Color.Unspecified
separator = Color.Unspecified,
error = Color.Unspecified
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ private val darkColorScheme = AppColorScheme(
onPrimary = PurpleGrey80,
secondary = Pink40,
onSecondary = Pink80,
separator = LightGray
separator = LightGray,
error = DarkRed
)

private val lightColorScheme = AppColorScheme(
Expand All @@ -30,7 +31,8 @@ private val lightColorScheme = AppColorScheme(
onPrimary = PurpleGrey40,
secondary = Pink80,
onSecondary = Pink40,
separator = DarkGray
separator = DarkGray,
error = LightRed
)

private val typography = AppTypography(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ val Pink80 = Color(0xFFEFB8C8)

val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)
val Pink40 = Color(0xFF7D5260)

val LightRed = Color(0xFFFF1744)
val DarkRed = Color(0xFFFF3D00)
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ internal fun LoginScreenContent(
.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(AppTheme.size.normal)
) {
//todo: Show bad email
val passwordFocusRequester = FocusRequester()
EmailInput(
modifier = Modifier
.fillMaxWidth()
.testTag("email"),
email = screenState.email,
onEmailChanged = onEmailUpdate,
isInvalidEmailFormat = screenState.isWrongEmailFormat,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Email,
imeAction = ImeAction.Next
Expand All @@ -146,6 +146,7 @@ internal fun LoginScreenContent(
.focusRequester(passwordFocusRequester)
.testTag("password"),
password = screenState.password,
isInvalidPasswordFormat = screenState.isBadPasswordFormat,
onPasswordChanged = onPasswordUpdate,
keyboardActions = KeyboardActions(
onDone = { onLoginClicked() }
Expand Down
4 changes: 0 additions & 4 deletions testutils/src/main/AndroidManifest.xml

This file was deleted.

0 comments on commit efcfff3

Please sign in to comment.