Skip to content

Commit abb4277

Browse files
committed
Fix email input snippet
1 parent 638b01a commit abb4277

File tree

1 file changed

+48
-44
lines changed
  • compose/snippets/src/main/java/com/example/compose/snippets/text

1 file changed

+48
-44
lines changed

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

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

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

21+
import android.graphics.Typeface
22+
import android.provider.ContactsContract.CommonDataKinds.Email
2123
import androidx.compose.foundation.BorderStroke
2224
import androidx.compose.foundation.background
2325
import androidx.compose.foundation.basicMarquee
@@ -82,6 +84,7 @@ import androidx.compose.ui.unit.dp
8284
import androidx.compose.ui.unit.em
8385
import androidx.compose.ui.unit.sp
8486
import androidx.lifecycle.ViewModel
87+
import androidx.lifecycle.viewmodel.compose.viewModel
8588

8689
/**
8790
* This file lets DevRel track changes to snippets present in
@@ -519,7 +522,7 @@ private object TextEffectiveStateManagement1 {
519522
private object TextEffectiveStateManagement2 {
520523
class UserRepository
521524

522-
val viewModel = SignUpViewModel(UserRepository())
525+
private val viewModel = SignUpViewModel(UserRepository())
523526

524527
// [START android_compose_text_state_management]
525528
// SignUpViewModel.kt
@@ -836,59 +839,60 @@ class NanpVisualTransformation() : VisualTransformation {
836839
}
837840
// [END android_compose_text_auto_format_phone_number_transformtext]
838841

839-
private val firaSansFamily = FontFamily()
842+
private val firaSansFamily = FontFamily(typeface = Typeface.DEFAULT)
840843

841844
val LightBlue = Color(0xFF0066FF)
842845
val Purple = Color(0xFF800080)
843846

844847
// [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
858858
}
859+
}
859860

860-
fun updateEmail(input: String) {
861-
email = input
862-
}
861+
fun updateEmail(input: String) {
862+
email = input
863863
}
864+
}
864865

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.")
884883
}
885-
)
884+
}
885+
)
886+
}
886887

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+
)
893897
}
894898
// [END android_compose_text_auto_format_phone_number_validatetext]

0 commit comments

Comments
 (0)