@@ -24,6 +24,7 @@ import androidx.compose.material3.Button
2424import androidx.compose.material3.CircularProgressIndicator
2525import androidx.compose.material3.Text
2626import androidx.compose.runtime.Composable
27+ import androidx.compose.runtime.LaunchedEffect
2728import androidx.compose.runtime.getValue
2829import androidx.compose.runtime.mutableStateOf
2930import androidx.compose.runtime.remember
@@ -93,60 +94,17 @@ fun StyleWithBrush() {
9394 // [END android_compose_state_text_5]
9495}
9596
96- // [START android_compose_state_text_6]
97- // TODO fix this snippet
98- // val usernameState = rememberTextFieldState()
99- // TextField(
100- // state = usernameState,
101- // lineLimits = TextFieldLineLimits.SingleLine,
102- // placeholder = { Text("Enter Username") }
103- // )
104- val LoginRepository = " repository"
105-
106- class LoginViewModel (val loginRepository : Repository ): ViewModel() {
107- val username = TextFieldState ()
108- val password = TextFieldState ()
109-
110- val isLoginButtonEnabled: Boolean
111- get() = ! isLoginButtonLoading && username.text.length > 6 && password.text.length > 6
112-
113- var isLoginButtonLoading by mutableStateOf(false )
114- private set
115-
116- fun loginButtonClick () {
117- viewModelScope.launch {
118- isLoginButtonLoading = true
119- val result = loginRepository.login(
120- username.text.toString(),
121- password.text.toString()
122- )
123- // process result
124- isLoginButtonLoading = false
125- }
126- }
127- }
128-
12997@Composable
130- fun LoginForm (
131- viewModel : LoginViewModel ,
132- modifier : Modifier
133- ) {
134- Column (modifier) {
135- TextField (viewModel.username)
136- SecureTextField (viewModel.password)
137- Button (
138- onClick = viewModel::loginButtonClick,
139- enabled = viewModel.isLoginButtonEnabled
140- ) {
141- if (viewModel.isLoginButtonLoading) {
142- CircularProgressIndicator ()
143- } else {
144- Text (" Login" )
145- }
146- }
147- }
98+ fun StateHoisting () {
99+ // [START android_compose_state_text_6]
100+ val usernameState = rememberTextFieldState()
101+ TextField (
102+ state = usernameState,
103+ lineLimits = TextFieldLineLimits .SingleLine ,
104+ placeholder = { Text (" Enter Username" ) }
105+ )
106+ // [END android_compose_state_text_6]
148107}
149- // [END android_compose_state_text_6]
150108
151109class Repository {
152110 fun login (username : String , password : String ) {
@@ -159,7 +117,6 @@ fun TextFieldPlaceholder() {
159117
160118}
161119
162- @Preview
163120@Composable
164121fun TextFieldInitialState () {
165122 // [START android_compose_state_text_7]
@@ -173,9 +130,28 @@ fun TextFieldInitialState() {
173130@Composable
174131fun TextFieldBuffer () {
175132 // [START android_compose_state_text_8]
133+ val phoneNumberState = rememberTextFieldState()
134+
135+ LaunchedEffect (phoneNumberState) {
136+ phoneNumberState.edit { // TextFieldBuffer scope
137+ append(" 123456789" )
138+ }
139+ }
140+
141+ TextField (
142+ state = phoneNumberState,
143+ inputTransformation = InputTransformation { // TextFieldBuffer scope
144+ if (TextUtils .isDigitsOnly(asCharSequence())) {
145+ revertAllChanges()
146+ }
147+ },
148+ outputTransformation = OutputTransformation {
149+ if (length > 0 ) insert(0 , " (" )
150+ if (length > 4 ) insert(4 , " )" )
151+ if (length > 8 ) insert(8 , " -" )
152+ }
153+ )
176154 // [END android_compose_state_text_8]
177- // [START android_compose_state_text_9]
178- // [END android_compose_state_text_9]
179155}
180156
181157@Preview
@@ -213,16 +189,18 @@ fun EditTextFieldState() {
213189}
214190
215191class TextFieldViewModel : ViewModel () {
192+ val usernameState = TextFieldState ()
216193 fun validateUsername () {
217194
218195 }
219196}
220197val textFieldViewModel = TextFieldViewModel ()
198+
221199@Composable
222200fun TextFieldKeyboardOptions () {
223201 // [START android_compose_state_text_13]
224- BasicTextField (
225- state = rememberTextFieldState() ,
202+ TextField (
203+ state = textFieldViewModel.usernameState ,
226204 keyboardOptions = KeyboardOptions (imeAction = ImeAction .Next ),
227205 onKeyboardAction = { performDefaultAction ->
228206 textFieldViewModel.validateUsername()
0 commit comments