refactor: lift sheet state management to parent and implement custom back press handling for EditActivityBottomSheet

This commit is contained in:
Jhony Cesar
2026-07-28 09:20:00 -04:00
parent af1c782ba5
commit d4b50787d4

View File

@@ -12,6 +12,7 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable import androidx.compose.foundation.focusable
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import com.google.android.gms.auth.api.signin.GoogleSignIn import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInOptions import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException import com.google.android.gms.common.api.ApiException
@@ -575,11 +576,14 @@ fun MaintenanceDashboardScreen(
// Details & Edit bottom sheet // Details & Edit bottom sheet
if (showEditSheet && selectedActivity != null) { if (showEditSheet && selectedActivity != null) {
val editSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
EditActivityBottomSheet( EditActivityBottomSheet(
activity = selectedActivity!!, activity = selectedActivity!!,
history = selectedActivityHistory, history = selectedActivityHistory,
currentUser = currentUser, currentUser = currentUser,
userAccessLevel = userAccessLevel, userAccessLevel = userAccessLevel,
sheetState = editSheetState,
onDismiss = { onDismiss = {
showEditSheet = false showEditSheet = false
viewModel.selectActivity(null) viewModel.selectActivity(null)
@@ -1070,6 +1074,7 @@ fun EditActivityBottomSheet(
history: List<OSUpdateHistory>, history: List<OSUpdateHistory>,
currentUser: com.example.data.model.GoogleUser?, currentUser: com.example.data.model.GoogleUser?,
userAccessLevel: String?, userAccessLevel: String?,
sheetState: SheetState,
onDismiss: () -> Unit, onDismiss: () -> Unit,
onUpdateProgress: (Int, String, String?) -> Unit, onUpdateProgress: (Int, String, String?) -> Unit,
onGoogleLoginClick: () -> Unit onGoogleLoginClick: () -> Unit
@@ -1077,28 +1082,25 @@ 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
// Track keyboard visibility reactively
val isKeyboardOpen = WindowInsets.ime.getBottom(density) > 0 val isKeyboardOpen = WindowInsets.ime.getBottom(density) > 0
val sheetState = rememberModalBottomSheetState( // Handle system Back button: if keyboard is open, close keyboard; otherwise close sheet
skipPartiallyExpanded = true, BackHandler(enabled = true) {
confirmValueChange = { targetValue -> if (isKeyboardOpen) {
if (targetValue == SheetValue.Hidden && isKeyboardOpen) { focusManager.clearFocus()
// Keyboard is open: reject sheet dismissal and hide keyboard instead keyboardController?.hide()
keyboardController?.hide() } else {
focusManager.clearFocus() onDismiss()
false
} else {
true
}
} }
) }
ModalBottomSheet( ModalBottomSheet(
onDismissRequest = onDismiss, onDismissRequest = onDismiss,
sheetState = sheetState, sheetState = sheetState,
containerColor = Color.White containerColor = Color.White,
properties = ModalBottomSheetDefaults.properties(
shouldDismissOnBackPress = false
)
) { ) {
var progressValue by remember(activity) { mutableStateOf(activity.percentualExecucao.toFloat()) } var progressValue by remember(activity) { mutableStateOf(activity.percentualExecucao.toFloat()) }