refactor: replace ModalBottomSheet with custom Dialog component for activity editing to improve state management and back navigation
This commit is contained in:
@@ -11,6 +11,8 @@ import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.focusable
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
||||
@@ -576,14 +578,11 @@ fun MaintenanceDashboardScreen(
|
||||
|
||||
// Details & Edit bottom sheet
|
||||
if (showEditSheet && selectedActivity != null) {
|
||||
val editSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
|
||||
|
||||
EditActivityBottomSheet(
|
||||
activity = selectedActivity!!,
|
||||
history = selectedActivityHistory,
|
||||
currentUser = currentUser,
|
||||
userAccessLevel = userAccessLevel,
|
||||
sheetState = editSheetState,
|
||||
onDismiss = {
|
||||
showEditSheet = false
|
||||
viewModel.selectActivity(null)
|
||||
@@ -1067,89 +1066,82 @@ fun OSListItemCard(
|
||||
/**
|
||||
* Modal Bottom Sheet for OS Details, Editing and Timeline History
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
fun EditActivityBottomSheet(
|
||||
activity: MaintenanceActivity,
|
||||
history: List<OSUpdateHistory>,
|
||||
currentUser: com.example.data.model.GoogleUser?,
|
||||
userAccessLevel: String?,
|
||||
sheetState: SheetState,
|
||||
onDismiss: () -> Unit,
|
||||
onUpdateProgress: (Int, String, String?) -> Unit,
|
||||
onGoogleLoginClick: () -> Unit
|
||||
) {
|
||||
val density = LocalDensity.current
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
val focusManager = LocalFocusManager.current
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val keyboardController = LocalSoftwareKeyboardController.current
|
||||
val isImeVisible = WindowInsets.isImeVisible
|
||||
|
||||
val isKeyboardOpen = WindowInsets.ime.getBottom(density) > 0
|
||||
|
||||
// State flag to track if keyboard was already dismissed by a back press.
|
||||
// This solves Samsung OneUI inset update delay issues after keyboardController.hide().
|
||||
var keyboardDismissedByBack by remember { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(isKeyboardOpen) {
|
||||
if (isKeyboardOpen) {
|
||||
keyboardDismissedByBack = false
|
||||
}
|
||||
}
|
||||
|
||||
// Handle system Back button:
|
||||
// 1st back press (keyboard open): clear focus and hide keyboard.
|
||||
// 2nd back press (or keyboard closed): animate sheet hiding and call onDismiss().
|
||||
BackHandler(enabled = true) {
|
||||
if (isKeyboardOpen && !keyboardDismissedByBack) {
|
||||
keyboardDismissedByBack = true
|
||||
focusManager.clearFocus(force = true)
|
||||
keyboardController?.hide()
|
||||
} else {
|
||||
keyboardDismissedByBack = false
|
||||
coroutineScope.launch {
|
||||
sheetState.hide()
|
||||
}.invokeOnCompletion {
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ModalBottomSheet(
|
||||
onDismissRequest = {
|
||||
coroutineScope.launch {
|
||||
sheetState.hide()
|
||||
}.invokeOnCompletion {
|
||||
onDismiss()
|
||||
}
|
||||
},
|
||||
sheetState = sheetState,
|
||||
containerColor = Color.White,
|
||||
properties = ModalBottomSheetDefaults.properties(
|
||||
shouldDismissOnBackPress = false
|
||||
)
|
||||
Dialog(
|
||||
onDismissRequest = onDismiss,
|
||||
properties = DialogProperties(usePlatformDefaultWidth = false)
|
||||
) {
|
||||
BackHandler(enabled = true) {
|
||||
if (isImeVisible) {
|
||||
focusManager.clearFocus(force = true)
|
||||
keyboardController?.hide()
|
||||
} else {
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
|
||||
var progressValue by remember(activity) { mutableStateOf(activity.percentualExecucao.toFloat()) }
|
||||
var observationText by remember { mutableStateOf("") }
|
||||
var selectedMockPhotoPath by remember { mutableStateOf<String?>(null) }
|
||||
var isSubmitting by remember { mutableStateOf(false) }
|
||||
|
||||
// Mock photos available in the field for maintenance logs
|
||||
val mockPhotos = listOf(
|
||||
"Turbina" to "ic_turbine_mock",
|
||||
"Fiação Elétrica" to "ic_electrical_mock",
|
||||
"Mancal Engraxado" to "ic_gear_mock",
|
||||
"Caldeira Lavada" to "ic_boiler_mock"
|
||||
)
|
||||
|
||||
Column(
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 8.dp)
|
||||
.verticalScroll(rememberScrollState()) // Allow full content scrolling on small screens
|
||||
.navigationBarsPadding(), // Prevent covering by standard bottom navigation buttons
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
.fillMaxSize()
|
||||
.background(Color.Black.copy(alpha = 0.5f))
|
||||
.clickable(onClick = onDismiss),
|
||||
contentAlignment = Alignment.BottomCenter
|
||||
) {
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable(enabled = false) {}, // Prevent card content click from dismissing
|
||||
shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
|
||||
colors = CardDefaults.cardColors(containerColor = Color.White)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 20.dp, vertical = 12.dp)
|
||||
.verticalScroll(rememberScrollState())
|
||||
.navigationBarsPadding(),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
// Top Drag Handle Indicator
|
||||
Box(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.width(32.dp)
|
||||
.height(4.dp)
|
||||
.clip(RoundedCornerShape(2.dp))
|
||||
.background(Color(0xFFE0E0E0))
|
||||
)
|
||||
}
|
||||
|
||||
var progressValue by remember(activity) { mutableStateOf(activity.percentualExecucao.toFloat()) }
|
||||
var observationText by remember { mutableStateOf("") }
|
||||
var selectedMockPhotoPath by remember { mutableStateOf<String?>(null) }
|
||||
var isSubmitting by remember { mutableStateOf(false) }
|
||||
|
||||
// Mock photos available in the field for maintenance logs
|
||||
val mockPhotos = listOf(
|
||||
"Turbina" to "ic_turbine_mock",
|
||||
"Fiação Elétrica" to "ic_electrical_mock",
|
||||
"Mancal Engraxado" to "ic_gear_mock",
|
||||
"Caldeira Lavada" to "ic_boiler_mock"
|
||||
)
|
||||
// Header: OS + Close
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
@@ -1521,6 +1513,8 @@ fun EditActivityBottomSheet(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
package com.example.ui.screens
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import kotlinx.coroutines.delay
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
@@ -58,6 +68,7 @@ fun SettingsScreen(
|
||||
var tempSpreadsheetId by remember(activeSpreadsheetId) { mutableStateOf(activeSpreadsheetId) }
|
||||
var showClearCacheDialog by remember { mutableStateOf(false) }
|
||||
var showConflictsDialog by remember { mutableStateOf(false) }
|
||||
var activeTestModal by remember { mutableStateOf<Int?>(null) }
|
||||
|
||||
// Start monitoring GCP API status when screen is open, stop when closed
|
||||
DisposableEffect(Unit) {
|
||||
@@ -828,3 +839,5 @@ fun AdminButton(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user