K Merle
04/12/2024, 10:14 AMonDismissRequest
. Callback is called, however, keyboard doesn't clear. It does clear on back press. 🧵K Merle
04/12/2024, 10:14 AM@Composable
fun OtpDialog(state: OtpDialogState) {
val focusRequester = remember { FocusRequester() }
var isFieldFocused by remember { mutableStateOf(false) }
val content = state.content
BackHandler { if (isFieldFocused) focusRequester.freeFocus() }
if (state.visible && content != null) {
OtpAutoFill(lifecycleOwner = LocalLifecycleOwner.current, otpDialogState = state)
Dialog(
onDismissRequest = {
focusRequester.freeFocus()
},
properties = state.properties
) {
Card(colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceContainerHigh)) {
Column(
modifier = Modifier.padding(LargePadding),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(DefaultPadding)
) {
content.title?.let {
Text(
text = it,
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier.fillMaxWidth()
)
}
OtpTextField(
modifier = Modifier
.padding(top = DefaultPadding)
.focusRequester(focusRequester)
.onFocusChanged { isFieldFocused = it.isFocused },
otpText = state.password,
onOtpTextChange = { password -> state.updateOneTimePassword(password) }
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(top = MediumPadding),
horizontalArrangement = Arrangement.End
) {
content.negativeButtonText?.let {
TextButton(onClick = content.onNegativeButton ?: state::hide) { Text(text = it) }
}
state.content?.positiveButtonText?.let {
TextButton(onClick = content.onPositiveButton) { Text(text = it) }
}
}
}
}
LaunchedEffect(Unit) { focusRequester.requestFocus() }
}
}
}