feat: add date simulation feature to manipulate system time in dashboard and settings
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.example.data.model
|
||||
|
||||
enum class DateSimulationMode {
|
||||
DISABLED,
|
||||
VISUALIZATION_ONLY,
|
||||
EDIT_PROGRESS
|
||||
}
|
||||
|
||||
data class DateSimulationConfig(
|
||||
val simulatedDateMillis: Long? = null,
|
||||
val mode: DateSimulationMode = DateSimulationMode.DISABLED,
|
||||
val expirationTimestampMillis: Long? = null
|
||||
) {
|
||||
val isActive: Boolean get() = mode != DateSimulationMode.DISABLED && simulatedDateMillis != null
|
||||
}
|
||||
@@ -61,7 +61,8 @@ class MaintenanceRepository(
|
||||
observacao: String?,
|
||||
fotoPath: String?,
|
||||
recurso: String,
|
||||
emailEditor: String?
|
||||
emailEditor: String?,
|
||||
customTimestampMillis: Long? = null
|
||||
) = withContext(Dispatchers.IO) {
|
||||
// Validate system date only when online
|
||||
val isOnline = isCurrentlyConnected()
|
||||
@@ -71,13 +72,14 @@ class MaintenanceRepository(
|
||||
}
|
||||
|
||||
val currentActivity = dao.getActivityByOsOneShot(numeroOs) ?: return@withContext
|
||||
val effectiveTimestamp = customTimestampMillis ?: System.currentTimeMillis()
|
||||
|
||||
val updatedActivity = currentActivity.copy(
|
||||
percentualExecucao = novoPercentual.coerceIn(0, 100),
|
||||
observacoes = observacao ?: currentActivity.observacoes,
|
||||
fotoPath = fotoPath ?: currentActivity.fotoPath,
|
||||
synced = false, // Must sync later
|
||||
lastUpdated = System.currentTimeMillis()
|
||||
lastUpdated = effectiveTimestamp
|
||||
)
|
||||
|
||||
dao.updateActivity(updatedActivity)
|
||||
@@ -85,6 +87,7 @@ class MaintenanceRepository(
|
||||
// Log to history
|
||||
val historyEntry = OSUpdateHistory(
|
||||
numeroOs = numeroOs,
|
||||
timestamp = effectiveTimestamp,
|
||||
percentualAnterior = currentActivity.percentualExecucao,
|
||||
percentualNovo = novoPercentual,
|
||||
observacao = observacao,
|
||||
|
||||
@@ -23,6 +23,7 @@ import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
|
||||
@@ -98,6 +98,7 @@ fun MaintenanceDashboardScreen(
|
||||
val selectedActivityHistory by viewModel.selectedActivityHistory.collectAsStateWithLifecycle()
|
||||
val currentUser by viewModel.currentUser.collectAsStateWithLifecycle()
|
||||
val userAccessLevel by viewModel.userAccessLevel.collectAsStateWithLifecycle()
|
||||
val simulationConfig by viewModel.simulationConfig.collectAsStateWithLifecycle()
|
||||
|
||||
var showEditSheet by remember { mutableStateOf(false) }
|
||||
var showImporterSheet by remember { mutableStateOf(false) }
|
||||
@@ -403,6 +404,11 @@ fun MaintenanceDashboardScreen(
|
||||
.fillMaxSize()
|
||||
.padding(innerPadding)
|
||||
) {
|
||||
// Banner de Alerta de Simulação de Data
|
||||
SimulationAlertBanner(
|
||||
simulationConfig = simulationConfig,
|
||||
onDismiss = { viewModel.clearDateSimulation() }
|
||||
)
|
||||
// Search Bar & Filter Toggle Button
|
||||
Row(
|
||||
modifier = Modifier
|
||||
@@ -1988,13 +1994,76 @@ fun ImporterConsoleSheet(
|
||||
@Composable
|
||||
fun rememberScrollState() = androidx.compose.foundation.rememberScrollState()
|
||||
|
||||
|
||||
|
||||
@Composable
|
||||
fun GoogleIcon(modifier: Modifier = Modifier.size(16.dp)) {
|
||||
androidx.compose.foundation.Image(
|
||||
painter = androidx.compose.ui.res.painterResource(id = com.example.R.drawable.ic_google),
|
||||
contentDescription = "Google Logo",
|
||||
modifier = modifier
|
||||
fun GoogleIcon(modifier: Modifier = Modifier) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.AccountCircle,
|
||||
contentDescription = "Google",
|
||||
modifier = modifier,
|
||||
tint = Color.White
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SimulationAlertBanner(
|
||||
simulationConfig: com.example.data.model.DateSimulationConfig,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
if (!simulationConfig.isActive || simulationConfig.simulatedDateMillis == null) return
|
||||
|
||||
val sdf = remember { SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()) }
|
||||
val formattedDate = remember(simulationConfig.simulatedDateMillis) {
|
||||
sdf.format(Date(simulationConfig.simulatedDateMillis))
|
||||
}
|
||||
|
||||
val modeLabel = if (simulationConfig.mode == com.example.data.model.DateSimulationMode.EDIT_PROGRESS) {
|
||||
"Edição de OSs"
|
||||
} else {
|
||||
"Visualização"
|
||||
}
|
||||
|
||||
Surface(
|
||||
color = Color(0xFFFFF3E0),
|
||||
contentColor = Color(0xFFE65100),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Warning,
|
||||
contentDescription = "Simulação Ativa",
|
||||
tint = Color(0xFFE65100),
|
||||
modifier = Modifier.size(20.dp)
|
||||
)
|
||||
Text(
|
||||
text = "Modo Simulação: $formattedDate ($modeLabel)",
|
||||
fontSize = 13.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = Color(0xFFE65100)
|
||||
)
|
||||
}
|
||||
|
||||
TextButton(
|
||||
onClick = onDismiss,
|
||||
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 2.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Desativar Simulação",
|
||||
fontSize = 12.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = Color(0xFFD84315)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,12 @@ fun SettingsScreen(
|
||||
val isSyncing by viewModel.isSyncing.collectAsStateWithLifecycle()
|
||||
val conflictLogs by viewModel.conflictLogs.collectAsStateWithLifecycle()
|
||||
val appLogs by viewModel.appLogsState.collectAsStateWithLifecycle()
|
||||
val userAccessLevel by viewModel.userAccessLevel.collectAsStateWithLifecycle()
|
||||
val simulationConfig by viewModel.simulationConfig.collectAsStateWithLifecycle()
|
||||
|
||||
val hasTotalAccess = remember(userAccessLevel) {
|
||||
userAccessLevel?.trim()?.equals("total", ignoreCase = true) == true
|
||||
}
|
||||
|
||||
var showClearCacheDialog by remember { mutableStateOf(false) }
|
||||
var showConflictsDialog by remember { mutableStateOf(false) }
|
||||
@@ -317,6 +323,18 @@ fun SettingsScreen(
|
||||
}
|
||||
}
|
||||
|
||||
// Seção: Simulação de Data
|
||||
DateSimulationSection(
|
||||
hasTotalAccess = hasTotalAccess,
|
||||
simulationConfig = simulationConfig,
|
||||
onActivateSimulation = { dateMillis, mode ->
|
||||
viewModel.setDateSimulation(dateMillis, mode)
|
||||
},
|
||||
onClearSimulation = {
|
||||
viewModel.clearDateSimulation()
|
||||
}
|
||||
)
|
||||
|
||||
// Seção 3: Painel do Administrador
|
||||
Column {
|
||||
Text(
|
||||
@@ -438,6 +456,242 @@ fun SettingsScreen(
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun DateSimulationSection(
|
||||
hasTotalAccess: Boolean,
|
||||
simulationConfig: com.example.data.model.DateSimulationConfig,
|
||||
onActivateSimulation: (dateMillis: Long, mode: com.example.data.model.DateSimulationMode) -> Unit,
|
||||
onClearSimulation: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
var showDatePicker by remember { mutableStateOf(false) }
|
||||
var selectedDateMillis by remember(simulationConfig.simulatedDateMillis) {
|
||||
mutableLongStateOf(simulationConfig.simulatedDateMillis ?: System.currentTimeMillis())
|
||||
}
|
||||
var selectedMode by remember(simulationConfig.mode) {
|
||||
mutableStateOf(
|
||||
if (simulationConfig.mode == com.example.data.model.DateSimulationMode.DISABLED)
|
||||
com.example.data.model.DateSimulationMode.VISUALIZATION_ONLY
|
||||
else simulationConfig.mode
|
||||
)
|
||||
}
|
||||
|
||||
val dateFormatter = remember { SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()) }
|
||||
val formattedDate = remember(selectedDateMillis) { dateFormatter.format(Date(selectedDateMillis)) }
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "Simulação de Data do Sistema",
|
||||
fontSize = 20.sp,
|
||||
fontWeight = FontWeight.Black,
|
||||
color = Color(0xFF0F2644),
|
||||
modifier = Modifier.padding(bottom = 8.dp)
|
||||
)
|
||||
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = Color.White),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
elevation = CardDefaults.cardElevation(defaultElevation = 1.dp),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
if (!hasTotalAccess) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp),
|
||||
modifier = Modifier.padding(vertical = 8.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Lock,
|
||||
contentDescription = "Acesso Bloqueado",
|
||||
tint = Color.Gray,
|
||||
modifier = Modifier.size(28.dp)
|
||||
)
|
||||
Column {
|
||||
Text(
|
||||
text = "Simulação de Data Restrita",
|
||||
fontSize = 16.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = Color.Gray
|
||||
)
|
||||
Text(
|
||||
text = "Este recurso requer permissão de Nível Total no aplicativo.",
|
||||
fontSize = 13.sp,
|
||||
color = Color.Gray
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.DateRange,
|
||||
contentDescription = "Simulação de Data",
|
||||
tint = BrandGreen,
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
Text(
|
||||
text = if (simulationConfig.isActive) "Simulação Ativa" else "Configurar Simulação",
|
||||
fontSize = 16.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = BrandBlack
|
||||
)
|
||||
}
|
||||
|
||||
HorizontalDivider(color = Color(0xFFE5E5E5))
|
||||
|
||||
// Seleção de Data
|
||||
Text(
|
||||
text = "Data para Simulação:",
|
||||
fontSize = 14.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = BrandBlack
|
||||
)
|
||||
|
||||
OutlinedButton(
|
||||
onClick = { showDatePicker = true },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
border = BorderStroke(1.dp, BrandGreen)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CalendarToday,
|
||||
contentDescription = "Calendário",
|
||||
tint = BrandGreen,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = "Data Selecionada: $formattedDate",
|
||||
color = BrandBlack,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
|
||||
// Seleção de Modo
|
||||
Text(
|
||||
text = "Modo de Simulação:",
|
||||
fontSize = 14.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = BrandBlack
|
||||
)
|
||||
|
||||
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { selectedMode = com.example.data.model.DateSimulationMode.VISUALIZATION_ONLY }
|
||||
) {
|
||||
RadioButton(
|
||||
selected = selectedMode == com.example.data.model.DateSimulationMode.VISUALIZATION_ONLY,
|
||||
onClick = { selectedMode = com.example.data.model.DateSimulationMode.VISUALIZATION_ONLY },
|
||||
colors = RadioButtonDefaults.colors(selectedColor = BrandGreen)
|
||||
)
|
||||
Text(
|
||||
text = "Somente Simulação para Visualização",
|
||||
fontSize = 14.sp,
|
||||
color = BrandBlack
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { selectedMode = com.example.data.model.DateSimulationMode.EDIT_PROGRESS }
|
||||
) {
|
||||
RadioButton(
|
||||
selected = selectedMode == com.example.data.model.DateSimulationMode.EDIT_PROGRESS,
|
||||
onClick = { selectedMode = com.example.data.model.DateSimulationMode.EDIT_PROGRESS },
|
||||
colors = RadioButtonDefaults.colors(selectedColor = BrandGreen)
|
||||
)
|
||||
Text(
|
||||
text = "Simulação para Edição do Progresso das Ordens",
|
||||
fontSize = 14.sp,
|
||||
color = BrandBlack
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Text(
|
||||
text = "Nota: A simulação se encerra automaticamente após 15 minutos de uso contínuo.",
|
||||
fontSize = 11.sp,
|
||||
color = Color.Gray
|
||||
)
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Button(
|
||||
onClick = {
|
||||
onActivateSimulation(selectedDateMillis, selectedMode)
|
||||
Toast.makeText(context, "Simulação de data ativada!", Toast.LENGTH_SHORT).show()
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(containerColor = BrandGreen),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
Text("Ativar Simulação", fontWeight = FontWeight.Bold, color = Color.White)
|
||||
}
|
||||
|
||||
if (simulationConfig.isActive) {
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
onClearSimulation()
|
||||
Toast.makeText(context, "Simulação encerrada.", Toast.LENGTH_SHORT).show()
|
||||
},
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
border = BorderStroke(1.dp, Color.Red),
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
Text("Restaurar Data Real", fontWeight = FontWeight.Bold, color = Color.Red)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showDatePicker) {
|
||||
val datePickerState = rememberDatePickerState(
|
||||
initialSelectedDateMillis = selectedDateMillis
|
||||
)
|
||||
DatePickerDialog(
|
||||
onDismissRequest = { showDatePicker = false },
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
datePickerState.selectedDateMillis?.let {
|
||||
selectedDateMillis = it
|
||||
}
|
||||
showDatePicker = false
|
||||
}
|
||||
) {
|
||||
Text("OK", fontWeight = FontWeight.Bold, color = BrandGreen)
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showDatePicker = false }) {
|
||||
Text("Cancelar")
|
||||
}
|
||||
}
|
||||
) {
|
||||
DatePicker(state = datePickerState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ConflictsDialog(
|
||||
|
||||
@@ -12,10 +12,12 @@ import com.example.data.model.SyncConflictLog
|
||||
import com.example.data.model.extractDateOnly
|
||||
import com.example.data.model.formatToShortDate
|
||||
import com.example.data.model.parseFullDateTime
|
||||
import com.example.data.model.AnalyticsState
|
||||
import com.example.data.model.parseDateOnly
|
||||
import com.example.data.repository.MaintenanceRepository
|
||||
import com.example.data.repository.SyncResult
|
||||
import com.example.data.model.AnalyticsState
|
||||
import com.example.data.model.parseDateOnly
|
||||
import com.example.data.model.DateSimulationConfig
|
||||
import com.example.data.model.DateSimulationMode
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
import java.util.Calendar
|
||||
@@ -81,6 +83,36 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
||||
private val _selectedActivity = MutableStateFlow<MaintenanceActivity?>(null)
|
||||
val selectedActivity = _selectedActivity.asStateFlow()
|
||||
|
||||
// Simulation state
|
||||
private val _simulationConfig = MutableStateFlow(DateSimulationConfig())
|
||||
val simulationConfig: StateFlow<DateSimulationConfig> = _simulationConfig.asStateFlow()
|
||||
|
||||
private var simulationTimerJob: kotlinx.coroutines.Job? = null
|
||||
|
||||
fun setDateSimulation(simulatedDateMillis: Long, mode: DateSimulationMode) {
|
||||
simulationTimerJob?.cancel()
|
||||
val expirationMs = System.currentTimeMillis() + 15 * 60 * 1000L // 15 min
|
||||
_simulationConfig.value = DateSimulationConfig(
|
||||
simulatedDateMillis = simulatedDateMillis,
|
||||
mode = mode,
|
||||
expirationTimestampMillis = expirationMs
|
||||
)
|
||||
|
||||
simulationTimerJob = viewModelScope.launch {
|
||||
kotlinx.coroutines.delay(15 * 60 * 1000L)
|
||||
clearDateSimulation(byTimeout = true)
|
||||
}
|
||||
}
|
||||
|
||||
fun clearDateSimulation(byTimeout: Boolean = false) {
|
||||
simulationTimerJob?.cancel()
|
||||
simulationTimerJob = null
|
||||
_simulationConfig.value = DateSimulationConfig()
|
||||
if (byTimeout) {
|
||||
_syncStatusMessage.value = "Tempo máximo de 15 minutos no modo simulação atingido. A simulação foi encerrada."
|
||||
}
|
||||
}
|
||||
|
||||
// All activities from repository
|
||||
val allActivitiesState: StateFlow<List<MaintenanceActivity>> = repository.allActivities
|
||||
.stateIn(
|
||||
@@ -173,9 +205,11 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
||||
val analyticsState: StateFlow<AnalyticsState> = combine(
|
||||
repository.allActivities,
|
||||
repository.allHistory,
|
||||
_selectedSetorSCurve
|
||||
) { activities, history, setorSCurve ->
|
||||
calculateAnalyticsState(activities, history, selectedSetorSCurve = setorSCurve)
|
||||
_selectedSetorSCurve,
|
||||
_simulationConfig
|
||||
) { activities, history, setorSCurve, simConfig ->
|
||||
val refDate = if (simConfig.isActive && simConfig.simulatedDateMillis != null) Date(simConfig.simulatedDateMillis) else null
|
||||
calculateAnalyticsState(activities, history, referenceDateOverride = refDate, selectedSetorSCurve = setorSCurve)
|
||||
}
|
||||
.flowOn(Dispatchers.Default)
|
||||
.stateIn(
|
||||
@@ -451,7 +485,13 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
||||
return@launch
|
||||
}
|
||||
try {
|
||||
repository.updateProgress(numeroOs, novoPercentual, observacao, fotoPath, recurso, emailEditor)
|
||||
val currentSim = _simulationConfig.value
|
||||
val customTimestamp = if (currentSim.mode == DateSimulationMode.EDIT_PROGRESS) {
|
||||
currentSim.simulatedDateMillis
|
||||
} else {
|
||||
null
|
||||
}
|
||||
repository.updateProgress(numeroOs, novoPercentual, observacao, fotoPath, recurso, emailEditor, customTimestamp)
|
||||
} catch (e: Exception) {
|
||||
_syncStatusMessage.value = e.message ?: "Erro ao atualizar a OS."
|
||||
return@launch
|
||||
|
||||
Reference in New Issue
Block a user