refactor: implement animated sheet dismissal and granular keyboard handling for BackHandler in MaintenanceDashboardScreen

This commit is contained in:
Jhony Cesar
2026-07-28 09:36:56 -04:00
parent d4b50787d4
commit 977c87c125

View File

@@ -1082,20 +1082,40 @@ fun EditActivityBottomSheet(
val density = LocalDensity.current val density = LocalDensity.current
val keyboardController = LocalSoftwareKeyboardController.current val keyboardController = LocalSoftwareKeyboardController.current
val focusManager = LocalFocusManager.current val focusManager = LocalFocusManager.current
val isKeyboardOpen = WindowInsets.ime.getBottom(density) > 0 val coroutineScope = rememberCoroutineScope()
// Handle system Back button: if keyboard is open, close keyboard; otherwise close sheet // Read WindowInsets.ime in Composable scope and wrap in rememberUpdatedState
val isKeyboardOpen = WindowInsets.ime.getBottom(density) > 0
val isKeyboardOpenState = rememberUpdatedState(isKeyboardOpen)
// Handle system Back button:
// 1. If keyboard is open -> clear focus and hide keyboard (sheet stays open)
// 2. If keyboard is closed -> animate sheet hiding and call onDismiss()
BackHandler(enabled = true) { BackHandler(enabled = true) {
if (isKeyboardOpen) { if (isKeyboardOpenState.value) {
focusManager.clearFocus() focusManager.clearFocus()
keyboardController?.hide() keyboardController?.hide()
} else { } else {
onDismiss() coroutineScope.launch {
sheetState.hide()
}.invokeOnCompletion {
if (!sheetState.isVisible) {
onDismiss()
}
}
} }
} }
ModalBottomSheet( ModalBottomSheet(
onDismissRequest = onDismiss, onDismissRequest = {
coroutineScope.launch {
sheetState.hide()
}.invokeOnCompletion {
if (!sheetState.isVisible) {
onDismiss()
}
}
},
sheetState = sheetState, sheetState = sheetState,
containerColor = Color.White, containerColor = Color.White,
properties = ModalBottomSheetDefaults.properties( properties = ModalBottomSheetDefaults.properties(