Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,49 @@ import com.example.compose.snippets.touchinput.Button

@Composable
fun AddAutofill() {
// [START android_compose_autofill_1]
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}

// [START android_compose_autofill_1_value]
TextField(
value = textFieldValue.value,
onValueChange = {textFieldValue.value = it},
modifier = Modifier.semantics { contentType = ContentType.Username }
)
// [END android_compose_autofill_1_value]

// [START android_compose_autofill_1_state]
TextField(
state = rememberTextFieldState(),
modifier = Modifier.semantics { contentType = ContentType.Username }
)
// [END android_compose_autofill_1]
// [END android_compose_autofill_1_state]
}

@Composable
fun AddMultipleTypesOfAutofill() {
// [START android_compose_autofill_2]
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_2_value]
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
modifier = Modifier.semantics {
contentType = ContentType.Username + ContentType.EmailAddress
}
)
// [END android_compose_autofill_2_value]

// [START android_compose_autofill_2_state]
TextField(
state = rememberTextFieldState(),
modifier = Modifier.semantics {
contentType = ContentType.Username + ContentType.EmailAddress
}
)
// [END android_compose_autofill_2]
// [END android_compose_autofill_2_state]
}

@Composable
Expand All @@ -68,11 +93,35 @@ fun AutofillManager() {
}

@Composable
fun SaveDataWithAutofill() {
fun SaveDateWithAutofillValue() {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_4]
// [START android_compose_autofill_4_value]
val autofillManager = LocalAutofillManager.current

Column {
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
modifier = Modifier.semantics { contentType = ContentType.NewUsername }
)

Spacer(modifier = Modifier.height(16.dp))

TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
modifier = Modifier.semantics { contentType = ContentType.NewPassword }
)
}
// [END android_compose_autofill_4_value]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This snippet has a few issues that should be addressed:

  1. There's a typo in the function name: SaveDate... should be SaveData....
  2. The state holder textFieldValue should be declared with val, not var, as the MutableState instance itself shouldn't be reassigned.
  3. Most critically, both the username and password TextFields are sharing the same state (textFieldValue). This is a bug; typing in one field will update the other. They need separate state holders.

Here is a corrected version of the snippet:

@Composable
fun SaveDataWithAutofillValue() {
    val usernameValue = remember {
        mutableStateOf(TextFieldValue(""))
    }
    val passwordValue = remember {
        mutableStateOf(TextFieldValue(""))
    }
    // [START android_compose_autofill_4_value]
    val autofillManager = LocalAutofillManager.current

    Column {
        TextField(
            value = usernameValue.value,
            onValueChange = { usernameValue.value = it },
            modifier = Modifier.semantics { contentType = ContentType.NewUsername }
        )

        Spacer(modifier = Modifier.height(16.dp))

        TextField(
            value = passwordValue.value,
            onValueChange = { passwordValue.value = it },
            modifier = Modifier.semantics { contentType = ContentType.NewPassword }
        )
    }
    // [END android_compose_autofill_4_value]
}


@Composable
fun SaveDataWithAutofillState() {

// [START android_compose_autofill_4_state]
val autofillManager = LocalAutofillManager.current

Column {
Expand All @@ -88,12 +137,42 @@ fun SaveDataWithAutofill() {
modifier = Modifier.semantics { contentType = ContentType.NewPassword }
)
}
// [END android_compose_autofill_4]
// [END android_compose_autofill_4_state]
}


@Composable
fun SaveDataWithAutofillOnClick() {
// [START android_compose_autofill_5]
fun SaveDataWithAutofillOnClickValue() {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}
// [START android_compose_autofill_5_value]
val autofillManager = LocalAutofillManager.current

Column {
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
modifier = Modifier.semantics { contentType = ContentType.NewUsername },
)

Spacer(modifier = Modifier.height(16.dp))

TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
modifier = Modifier.semantics { contentType = ContentType.NewPassword },
)

// Submit button
Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
}
// [END android_compose_autofill_5_value]
}
Comment on lines 145 to 174
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This snippet has a couple of issues:

  1. The state holder textFieldValue should be declared with val, not var.
  2. Both the username and password TextFields are sharing the same state (textFieldValue), which is incorrect. Typing in one field will update the other. They should each have their own state.

Here is a corrected version:

@Composable
fun SaveDataWithAutofillOnClickValue() {
    val usernameValue = remember {
        mutableStateOf(TextFieldValue(""))
    }
    val passwordValue = remember {
        mutableStateOf(TextFieldValue(""))
    }
    // [START android_compose_autofill_5_value]
    val autofillManager = LocalAutofillManager.current

    Column {
        TextField(
            value = usernameValue.value,
            onValueChange = { usernameValue.value = it },
            modifier = Modifier.semantics { contentType = ContentType.NewUsername },
        )

        Spacer(modifier = Modifier.height(16.dp))

        TextField(
            value = passwordValue.value,
            onValueChange = { passwordValue.value = it },
            modifier = Modifier.semantics { contentType = ContentType.NewPassword },
        )

        // Submit button
        Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
    }
    // [END android_compose_autofill_5_value]
}


@Composable
fun SaveDataWithAutofillOnClickState() {
// [START android_compose_autofill_5_state]
val autofillManager = LocalAutofillManager.current

Column {
Expand All @@ -112,12 +191,31 @@ fun SaveDataWithAutofillOnClick() {
// Submit button
Button(onClick = { autofillManager?.commit() }) { Text("Reset credentials") }
}
// [END android_compose_autofill_5]
// [END android_compose_autofill_5_state]
}

@Composable
fun CustomAutofillHighlightValue() {
var textFieldValue = remember {
mutableStateOf(TextFieldValue(""))
}

// [START android_compose_autofill_6_value]
val customHighlightColor = Color.Red

CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
TextField(
value = textFieldValue.value,
onValueChange = { textFieldValue.value = it },
modifier = Modifier.semantics { contentType = ContentType.Username }
)
}
// [END android_compose_autofill_6_value]
}

@Composable
fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) {
// [START android_compose_autofill_6]
fun CustomAutofillHighlightState(customHighlightColor: Color = Color.Red) {
// [START android_compose_autofill_6_state]
val customHighlightColor = Color.Red

CompositionLocalProvider(LocalAutofillHighlightColor provides customHighlightColor) {
Expand All @@ -126,5 +224,5 @@ fun CustomAutofillHighlight(customHighlightColor: Color = Color.Red) {
modifier = Modifier.semantics { contentType = ContentType.Username }
)
}
// [END android_compose_autofill_6]
// [END android_compose_autofill_6_state]
}
Loading