Skip to content

Commit eb6b518

Browse files
thedmailriggaroo
andauthored
Validate input (#347)
* Code snippet for Compose doc at https://developer.android.com/quick-guides/content/animate-text?hl=en (Animate text character-by-character). This commit slightly modifies (makes buildable in our repo) the existing code on the current DAC page. That code, in turn, was BNR's simplified version of Xoogler astamato's Medium article at https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5 * Code snippet for Compose doc at https://developer.android.com/quick-guides/content/animate-text?hl=en (Animate text character-by-character). This commit slightly modifies (makes buildable in our repo) the existing code on the current DAC page. That code, in turn, was BNR's simplified version of Xoogler astamato's Medium article at https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5 * Apply Spotless * Fix email input snippet * Apply Spotless --------- Co-authored-by: thedmail <[email protected]> Co-authored-by: Rebecca Franks <[email protected]> Co-authored-by: riggaroo <[email protected]>
1 parent 9736dd8 commit eb6b518

File tree

1 file changed

+58
-2
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/text

1 file changed

+58
-2
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/text/TextSnippets.kt

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package com.example.compose.snippets.text
2020

21+
import android.graphics.Typeface
2122
import androidx.compose.foundation.BorderStroke
2223
import androidx.compose.foundation.background
2324
import androidx.compose.foundation.basicMarquee
@@ -43,6 +44,7 @@ import androidx.compose.material3.OutlinedTextField
4344
import androidx.compose.material3.Text
4445
import androidx.compose.material3.TextField
4546
import androidx.compose.runtime.Composable
47+
import androidx.compose.runtime.derivedStateOf
4648
import androidx.compose.runtime.getValue
4749
import androidx.compose.runtime.mutableStateOf
4850
import androidx.compose.runtime.remember
@@ -86,6 +88,7 @@ import androidx.compose.ui.unit.dp
8688
import androidx.compose.ui.unit.em
8789
import androidx.compose.ui.unit.sp
8890
import androidx.lifecycle.ViewModel
91+
import androidx.lifecycle.viewmodel.compose.viewModel
8992

9093
/**
9194
* This file lets DevRel track changes to snippets present in
@@ -523,7 +526,7 @@ private object TextEffectiveStateManagement1 {
523526
private object TextEffectiveStateManagement2 {
524527
class UserRepository
525528

526-
val viewModel = SignUpViewModel(UserRepository())
529+
private val viewModel = SignUpViewModel(UserRepository())
527530

528531
// [START android_compose_text_state_management]
529532
// SignUpViewModel.kt
@@ -851,7 +854,7 @@ class NanpVisualTransformation() : VisualTransformation {
851854
}
852855
// [END android_compose_text_auto_format_phone_number_transformtext]
853856

854-
private val firaSansFamily = FontFamily()
857+
private val firaSansFamily = FontFamily(typeface = Typeface.DEFAULT)
855858

856859
val LightBlue = Color(0xFF0066FF)
857860
val Purple = Color(0xFF800080)
@@ -888,3 +891,56 @@ fun PasswordTextField() {
888891
)
889892
}
890893
// [END android_compose_text_showhidepassword]
894+
895+
// [START android_compose_text_auto_format_phone_number_validatetext]
896+
class EmailViewModel : ViewModel() {
897+
var email by mutableStateOf("")
898+
private set
899+
900+
val emailHasErrors by derivedStateOf {
901+
if (email.isNotEmpty()) {
902+
// Email is considered erroneous until it completely matches EMAIL_ADDRESS.
903+
!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
904+
} else {
905+
false
906+
}
907+
}
908+
909+
fun updateEmail(input: String) {
910+
email = input
911+
}
912+
}
913+
914+
@Composable
915+
fun ValidatingInputTextField(
916+
email: String,
917+
updateState: (String) -> Unit,
918+
validatorHasErrors: Boolean
919+
) {
920+
OutlinedTextField(
921+
modifier = Modifier
922+
.fillMaxWidth()
923+
.padding(10.dp),
924+
value = email,
925+
onValueChange = updateState,
926+
label = { Text("Email") },
927+
isError = validatorHasErrors,
928+
supportingText = {
929+
if (validatorHasErrors) {
930+
Text("Incorrect email format.")
931+
}
932+
}
933+
)
934+
}
935+
936+
@Preview
937+
@Composable
938+
fun ValidateInput() {
939+
val emailViewModel: EmailViewModel = viewModel<EmailViewModel>()
940+
ValidatingInputTextField(
941+
email = emailViewModel.email,
942+
updateState = { input -> emailViewModel.updateEmail(input) },
943+
validatorHasErrors = emailViewModel.emailHasErrors
944+
)
945+
}
946+
// [END android_compose_text_auto_format_phone_number_validatetext]

0 commit comments

Comments
 (0)