Last active
October 6, 2021 14:03
-
-
Save Skyyo/df178fa376ab8ba14bcc186a41228f23 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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