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 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 {
|
||||||
|
coroutineScope.launch {
|
||||||
|
sheetState.hide()
|
||||||
|
}.invokeOnCompletion {
|
||||||
|
if (!sheetState.isVisible) {
|
||||||
onDismiss()
|
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user