|
18 | 18 |
|
19 | 19 | package com.example.compose.snippets.text |
20 | 20 |
|
| 21 | +import android.graphics.Typeface |
| 22 | +import android.provider.ContactsContract.CommonDataKinds.Email |
21 | 23 | import androidx.compose.foundation.BorderStroke |
22 | 24 | import androidx.compose.foundation.background |
23 | 25 | import androidx.compose.foundation.basicMarquee |
@@ -82,6 +84,7 @@ import androidx.compose.ui.unit.dp |
82 | 84 | import androidx.compose.ui.unit.em |
83 | 85 | import androidx.compose.ui.unit.sp |
84 | 86 | import androidx.lifecycle.ViewModel |
| 87 | +import androidx.lifecycle.viewmodel.compose.viewModel |
85 | 88 |
|
86 | 89 | /** |
87 | 90 | * This file lets DevRel track changes to snippets present in |
@@ -519,7 +522,7 @@ private object TextEffectiveStateManagement1 { |
519 | 522 | private object TextEffectiveStateManagement2 { |
520 | 523 | class UserRepository |
521 | 524 |
|
522 | | - val viewModel = SignUpViewModel(UserRepository()) |
| 525 | + private val viewModel = SignUpViewModel(UserRepository()) |
523 | 526 |
|
524 | 527 | // [START android_compose_text_state_management] |
525 | 528 | // SignUpViewModel.kt |
@@ -836,59 +839,60 @@ class NanpVisualTransformation() : VisualTransformation { |
836 | 839 | } |
837 | 840 | // [END android_compose_text_auto_format_phone_number_transformtext] |
838 | 841 |
|
839 | | -private val firaSansFamily = FontFamily() |
| 842 | +private val firaSansFamily = FontFamily(typeface = Typeface.DEFAULT) |
840 | 843 |
|
841 | 844 | val LightBlue = Color(0xFF0066FF) |
842 | 845 | val Purple = Color(0xFF800080) |
843 | 846 |
|
844 | 847 | // [START android_compose_text_auto_format_phone_number_validatetext] |
845 | | -@Composable |
846 | | -fun ValidateInput() { |
847 | | - class EmailViewModel : ViewModel() { |
848 | | - var email by mutableStateOf("") |
849 | | - private set |
850 | | - |
851 | | - val emailHasErrors by derivedStateOf { |
852 | | - if (email.isNotEmpty()) { |
853 | | - // Email is considered erroneous until it completely matches EMAIL_ADDRESS. |
854 | | - !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() |
855 | | - } else { |
856 | | - false |
857 | | - } |
| 848 | +class EmailViewModel : ViewModel() { |
| 849 | + var email by mutableStateOf("") |
| 850 | + private set |
| 851 | + |
| 852 | + val emailHasErrors by derivedStateOf { |
| 853 | + if (email.isNotEmpty()) { |
| 854 | + // Email is considered erroneous until it completely matches EMAIL_ADDRESS. |
| 855 | + !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() |
| 856 | + } else { |
| 857 | + false |
858 | 858 | } |
| 859 | + } |
859 | 860 |
|
860 | | - fun updateEmail(input: String) { |
861 | | - email = input |
862 | | - } |
| 861 | + fun updateEmail(input: String) { |
| 862 | + email = input |
863 | 863 | } |
| 864 | +} |
864 | 865 |
|
865 | | - @Composable |
866 | | - fun ValidatingInputTextField( |
867 | | - email: String, |
868 | | - updateState: (String) -> Unit, |
869 | | - validatorHasErrors: Boolean |
870 | | - ) { |
871 | | - val emailViewModel = EmailViewModel() |
872 | | - OutlinedTextField( |
873 | | - modifier = Modifier |
874 | | - .fillMaxWidth() |
875 | | - .padding(10.dp), |
876 | | - value = email, |
877 | | - onValueChange = updateState, |
878 | | - label = { Text("Email") }, |
879 | | - isError = validatorHasErrors, |
880 | | - supportingText = { |
881 | | - if (validatorHasErrors) { |
882 | | - Text("Incorrect email format.") |
883 | | - } |
| 866 | +@Composable |
| 867 | +fun ValidatingInputTextField( |
| 868 | + email: String, |
| 869 | + updateState: (String) -> Unit, |
| 870 | + validatorHasErrors: Boolean |
| 871 | +) { |
| 872 | + OutlinedTextField( |
| 873 | + modifier = Modifier |
| 874 | + .fillMaxWidth() |
| 875 | + .padding(10.dp), |
| 876 | + value = email, |
| 877 | + onValueChange = updateState, |
| 878 | + label = { Text("Email") }, |
| 879 | + isError = validatorHasErrors, |
| 880 | + supportingText = { |
| 881 | + if (validatorHasErrors) { |
| 882 | + Text("Incorrect email format.") |
884 | 883 | } |
885 | | - ) |
| 884 | + } |
| 885 | + ) |
| 886 | +} |
886 | 887 |
|
887 | | - ValidatingInputTextField( |
888 | | - email = emailViewModel.email, |
889 | | - updateState = { input -> emailViewModel.updateEmail(input) }, |
890 | | - validatorHasErrors = emailViewModel.emailHasErrors |
891 | | - ) |
892 | | - } |
| 888 | +@Preview |
| 889 | +@Composable |
| 890 | +fun ValidateInput() { |
| 891 | + val emailViewModel : EmailViewModel = viewModel<EmailViewModel>() |
| 892 | + ValidatingInputTextField( |
| 893 | + email = emailViewModel.email, |
| 894 | + updateState = { input -> emailViewModel.updateEmail(input) }, |
| 895 | + validatorHasErrors = emailViewModel.emailHasErrors |
| 896 | + ) |
893 | 897 | } |
894 | 898 | // [END android_compose_text_auto_format_phone_number_validatetext] |
0 commit comments