18
18
19
19
package com.example.compose.snippets.text
20
20
21
+ import android.graphics.Typeface
21
22
import androidx.compose.foundation.BorderStroke
22
23
import androidx.compose.foundation.background
23
24
import androidx.compose.foundation.basicMarquee
@@ -43,6 +44,7 @@ import androidx.compose.material3.OutlinedTextField
43
44
import androidx.compose.material3.Text
44
45
import androidx.compose.material3.TextField
45
46
import androidx.compose.runtime.Composable
47
+ import androidx.compose.runtime.derivedStateOf
46
48
import androidx.compose.runtime.getValue
47
49
import androidx.compose.runtime.mutableStateOf
48
50
import androidx.compose.runtime.remember
@@ -86,6 +88,7 @@ import androidx.compose.ui.unit.dp
86
88
import androidx.compose.ui.unit.em
87
89
import androidx.compose.ui.unit.sp
88
90
import androidx.lifecycle.ViewModel
91
+ import androidx.lifecycle.viewmodel.compose.viewModel
89
92
90
93
/* *
91
94
* This file lets DevRel track changes to snippets present in
@@ -523,7 +526,7 @@ private object TextEffectiveStateManagement1 {
523
526
private object TextEffectiveStateManagement2 {
524
527
class UserRepository
525
528
526
- val viewModel = SignUpViewModel (UserRepository ())
529
+ private val viewModel = SignUpViewModel (UserRepository ())
527
530
528
531
// [START android_compose_text_state_management]
529
532
// SignUpViewModel.kt
@@ -851,7 +854,7 @@ class NanpVisualTransformation() : VisualTransformation {
851
854
}
852
855
// [END android_compose_text_auto_format_phone_number_transformtext]
853
856
854
- private val firaSansFamily = FontFamily ()
857
+ private val firaSansFamily = FontFamily (typeface = Typeface . DEFAULT )
855
858
856
859
val LightBlue = Color (0xFF0066FF )
857
860
val Purple = Color (0xFF800080 )
@@ -888,3 +891,56 @@ fun PasswordTextField() {
888
891
)
889
892
}
890
893
// [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