Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@Composable
fun InputValidationScreen(..) {
val focusManager = LocalFocusManager.current
val keyboardController = LocalSoftwareKeyboardController.current
val creditCardNumberFocusRequester = remember { FocusRequester() }
val nameFocusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
events.collect { event ->
when (event) {
is ScreenEvent.UpdateKeyboard -> {
if (event.show) keyboardController?.show() else keyboardController?.hide()
}
is ScreenEvent.ClearFocus -> focusManager.clearFocus()
is ScreenEvent.RequestFocus -> {
when (event.textFieldKey) {
FocusedTextFieldKey.NAME -> nameFocusRequester.requestFocus()
FocusedTextFieldKey.CREDIT_CARD_NUMBER -> creditCardNumberFocusRequester.requestFocus()
}
}
is ScreenEvent.MoveFocus -> focusManager.moveFocus(event.direction)
}
}
}
Column {
CustomTextField(
modifier = Modifier
.focusRequester(nameFocusRequester)
.onFocusChanged { focusState ->
viewModel.onTextFieldFocusChanged(
key = FocusedTextFieldKey.NAME,
isFocused = focusState.isFocused
)
},
onImeKeyAction = viewModel::onNameImeActionClick
)
CustomTextField(
modifier = Modifier
.focusRequester(creditCardNumberFocusRequester)
.onFocusChanged { focusState ->
viewModel.onTextFieldFocusChanged(
key = FocusedTextFieldKey.CREDIT_CARD_NUMBER,
isFocused = focusState.isFocused
)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment