adding username/password autofill

- pulled in from  https://bryanherbst.com/2021/04/13/compose-autofill/
This commit is contained in:
Adam Brown 2022-04-25 13:07:34 +01:00
parent 05a41c7ddf
commit d93e65d215
1 changed files with 35 additions and 5 deletions

View File

@ -15,10 +15,14 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.autofill.AutofillNode
import androidx.compose.ui.autofill.AutofillType
import androidx.compose.ui.composed
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.*
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
@ -82,7 +86,9 @@ fun LoginScreen(loginViewModel: LoginViewModel, onLoggedIn: () -> Unit) {
val focusManager = LocalFocusManager.current
TextField(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.autofill(listOf(AutofillType.Username), onFill = { userName = it }),
value = userName,
onValueChange = { userName = it },
singleLine = true,
@ -102,7 +108,9 @@ fun LoginScreen(loginViewModel: LoginViewModel, onLoggedIn: () -> Unit) {
}
TextField(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.autofill(listOf(AutofillType.Password), onFill = { password = it }),
value = password,
onValueChange = { password = it },
label = { Text("Password") },
@ -165,6 +173,28 @@ fun LoginScreen(loginViewModel: LoginViewModel, onLoggedIn: () -> Unit) {
}
}
@OptIn(ExperimentalComposeUiApi::class)
private fun Modifier.autofill(
autofillTypes: List<AutofillType>,
onFill: ((String) -> Unit),
) = composed {
val autofill = LocalAutofill.current
val autofillNode = AutofillNode(onFill = onFill, autofillTypes = autofillTypes)
LocalAutofillTree.current += autofillNode
this
.onGloballyPositioned { autofillNode.boundingBox = it.boundsInWindow() }
.onFocusChanged { focusState ->
autofill?.run {
if (focusState.isFocused) {
requestAutofillForNode(autofillNode)
} else {
cancelAutofillForNode(autofillNode)
}
}
}
}
@Composable
private fun LoginViewModel.ObserveEvents(onLoggedIn: () -> Unit) {
val context = LocalContext.current