refactor: implement animated sheet dismissal and granular keyboard handling for BackHandler in MaintenanceDashboardScreen
This commit is contained in:
@@ -1082,20 +1082,40 @@ fun EditActivityBottomSheet(
|
||||
val density = LocalDensity.current
|
||||
val keyboardController = LocalSoftwareKeyboardController.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) {
|
||||
if (isKeyboardOpen) {
|
||||
if (isKeyboardOpenState.value) {
|
||||
focusManager.clearFocus()
|
||||
keyboardController?.hide()
|
||||
} else {
|
||||
onDismiss()
|
||||
coroutineScope.launch {
|
||||
sheetState.hide()
|
||||
}.invokeOnCompletion {
|
||||
if (!sheetState.isVisible) {
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ModalBottomSheet(
|
||||
onDismissRequest = onDismiss,
|
||||
onDismissRequest = {
|
||||
coroutineScope.launch {
|
||||
sheetState.hide()
|
||||
}.invokeOnCompletion {
|
||||
if (!sheetState.isVisible) {
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
},
|
||||
sheetState = sheetState,
|
||||
containerColor = Color.White,
|
||||
properties = ModalBottomSheetDefaults.properties(
|
||||
|
||||
Reference in New Issue
Block a user