Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active October 6, 2021 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skyyo/df178fa376ab8ba14bcc186a41228f23 to your computer and use it in GitHub Desktop.
Save Skyyo/df178fa376ab8ba14bcc186a41228f23 to your computer and use it in GitHub Desktop.
@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