feat: add quick filter navigation and refine sorting by predicted completion date in MaintenanceDashboard
This commit is contained in:
@@ -78,6 +78,7 @@ fun MaintenanceDashboardScreen(
|
|||||||
val selectedDataInicio by viewModel.selectedDataInicio.collectAsStateWithLifecycle()
|
val selectedDataInicio by viewModel.selectedDataInicio.collectAsStateWithLifecycle()
|
||||||
val selectedRecurso by viewModel.selectedRecurso.collectAsStateWithLifecycle()
|
val selectedRecurso by viewModel.selectedRecurso.collectAsStateWithLifecycle()
|
||||||
val searchQuery by viewModel.searchQuery.collectAsStateWithLifecycle()
|
val searchQuery by viewModel.searchQuery.collectAsStateWithLifecycle()
|
||||||
|
val selectedQuickFilter by viewModel.selectedQuickFilter.collectAsStateWithLifecycle()
|
||||||
|
|
||||||
val isSyncing by viewModel.isSyncing.collectAsStateWithLifecycle()
|
val isSyncing by viewModel.isSyncing.collectAsStateWithLifecycle()
|
||||||
val syncMessage by viewModel.syncStatusMessage.collectAsStateWithLifecycle()
|
val syncMessage by viewModel.syncStatusMessage.collectAsStateWithLifecycle()
|
||||||
@@ -91,6 +92,24 @@ fun MaintenanceDashboardScreen(
|
|||||||
var showImporterSheet by remember { mutableStateOf(false) }
|
var showImporterSheet by remember { mutableStateOf(false) }
|
||||||
var isListExpanded by remember { mutableStateOf(false) }
|
var isListExpanded by remember { mutableStateOf(false) }
|
||||||
var showProfileDropdown by remember { mutableStateOf(false) }
|
var showProfileDropdown by remember { mutableStateOf(false) }
|
||||||
|
var showAdvancedFilters by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
val pendingCount = remember(allActivities) {
|
||||||
|
allActivities.count { it.status != ActivityStatus.CONCLUIDO }
|
||||||
|
}
|
||||||
|
|
||||||
|
val quickFilters = remember(pendingCount) {
|
||||||
|
listOf(
|
||||||
|
"Todos" to "Todos",
|
||||||
|
"Pendentes $pendingCount" to "Pendentes",
|
||||||
|
"Hoje" to "Hoje",
|
||||||
|
"Em andamento" to "Em andamento",
|
||||||
|
"Não iniciado" to "Não iniciado",
|
||||||
|
"Mecânica" to "Mecânica",
|
||||||
|
"Elétrica" to "Elétrica",
|
||||||
|
"Confiabilidade" to "Confiabilidade"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
val gso = remember {
|
val gso = remember {
|
||||||
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
||||||
@@ -365,12 +384,7 @@ fun MaintenanceDashboardScreen(
|
|||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
.padding(innerPadding)
|
.padding(innerPadding)
|
||||||
) {
|
) {
|
||||||
// Summary Dashboard Metrics Block
|
// Search Bar & Filter Toggle Button
|
||||||
AnimatedVisibility(visible = !isListExpanded) {
|
|
||||||
DashboardMetrics(activities = allActivities)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search Bar & Filter Button
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
@@ -404,24 +418,57 @@ fun MaintenanceDashboardScreen(
|
|||||||
),
|
),
|
||||||
singleLine = true
|
singleLine = true
|
||||||
)
|
)
|
||||||
|
|
||||||
|
IconButton(
|
||||||
|
onClick = { showAdvancedFilters = !showAdvancedFilters },
|
||||||
|
modifier = Modifier
|
||||||
|
.size(52.dp)
|
||||||
|
.clip(RoundedCornerShape(12.dp))
|
||||||
|
.background(if (showAdvancedFilters) CargillGreen else Color.White)
|
||||||
|
.testTag("toggle_filters_button")
|
||||||
|
) {
|
||||||
|
Icon(
|
||||||
|
imageVector = Icons.Default.Tune,
|
||||||
|
contentDescription = "Filtros avançados",
|
||||||
|
tint = if (showAdvancedFilters) Color.White else CargillBlack
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Horizontally Scrollable Filters
|
// Horizontally Scrollable Quick Filters
|
||||||
FilterPillsRow(
|
LazyRow(
|
||||||
selectedSetor = selectedSetor,
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
||||||
selectedEquipe = selectedEquipe,
|
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 4.dp),
|
||||||
selectedStatus = selectedStatus,
|
modifier = Modifier.fillMaxWidth().testTag("quick_filters_row")
|
||||||
selectedDataInicio = selectedDataInicio,
|
) {
|
||||||
selectedRecurso = selectedRecurso,
|
items(quickFilters) { (label, value) ->
|
||||||
uniqueSectors = uniqueSectors,
|
val isSelected = selectedQuickFilter == value
|
||||||
uniqueDates = uniqueDates,
|
FilterChipWidget(
|
||||||
uniqueRecursos = uniqueRecursos,
|
text = label,
|
||||||
onSetorSelected = { viewModel.setFilterSetor(it) },
|
isSelected = isSelected,
|
||||||
onEquipeSelected = { viewModel.setFilterEquipe(it) },
|
onClick = { viewModel.setSelectedQuickFilter(value) }
|
||||||
onStatusSelected = { viewModel.setFilterStatus(it) },
|
)
|
||||||
onDataInicioSelected = { viewModel.setFilterDataInicio(it) },
|
}
|
||||||
onRecursoSelected = { viewModel.setFilterRecurso(it) }
|
}
|
||||||
)
|
|
||||||
|
// Horizontally Scrollable Advanced Filters (shown conditionally)
|
||||||
|
AnimatedVisibility(visible = showAdvancedFilters) {
|
||||||
|
FilterPillsRow(
|
||||||
|
selectedSetor = selectedSetor,
|
||||||
|
selectedEquipe = selectedEquipe,
|
||||||
|
selectedStatus = selectedStatus,
|
||||||
|
selectedDataInicio = selectedDataInicio,
|
||||||
|
selectedRecurso = selectedRecurso,
|
||||||
|
uniqueSectors = uniqueSectors,
|
||||||
|
uniqueDates = uniqueDates,
|
||||||
|
uniqueRecursos = uniqueRecursos,
|
||||||
|
onSetorSelected = { viewModel.setFilterSetor(it) },
|
||||||
|
onEquipeSelected = { viewModel.setFilterEquipe(it) },
|
||||||
|
onStatusSelected = { viewModel.setFilterStatus(it) },
|
||||||
|
onDataInicioSelected = { viewModel.setFilterDataInicio(it) },
|
||||||
|
onRecursoSelected = { viewModel.setFilterRecurso(it) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// List Title and Item Count
|
// List Title and Item Count
|
||||||
Row(
|
Row(
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
|||||||
private val _searchQuery = MutableStateFlow("")
|
private val _searchQuery = MutableStateFlow("")
|
||||||
val searchQuery = _searchQuery.asStateFlow()
|
val searchQuery = _searchQuery.asStateFlow()
|
||||||
|
|
||||||
|
private val _selectedQuickFilter = MutableStateFlow("Todos")
|
||||||
|
val selectedQuickFilter = _selectedQuickFilter.asStateFlow()
|
||||||
|
|
||||||
// Google User authentication state
|
// Google User authentication state
|
||||||
private val _currentUser = MutableStateFlow<com.example.data.model.GoogleUser?>(null)
|
private val _currentUser = MutableStateFlow<com.example.data.model.GoogleUser?>(null)
|
||||||
val currentUser = _currentUser.asStateFlow()
|
val currentUser = _currentUser.asStateFlow()
|
||||||
@@ -92,7 +95,8 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
|||||||
_selectedStatus,
|
_selectedStatus,
|
||||||
_selectedDataInicio,
|
_selectedDataInicio,
|
||||||
_selectedRecurso,
|
_selectedRecurso,
|
||||||
_searchQuery
|
_searchQuery,
|
||||||
|
_selectedQuickFilter
|
||||||
) { flowsArray ->
|
) { flowsArray ->
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
val activities = flowsArray[0] as List<MaintenanceActivity>
|
val activities = flowsArray[0] as List<MaintenanceActivity>
|
||||||
@@ -102,6 +106,7 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
|||||||
val dataInicio = flowsArray[4] as String
|
val dataInicio = flowsArray[4] as String
|
||||||
val recurso = flowsArray[5] as String
|
val recurso = flowsArray[5] as String
|
||||||
val query = flowsArray[6] as String
|
val query = flowsArray[6] as String
|
||||||
|
val quickFilter = flowsArray[7] as String
|
||||||
|
|
||||||
val filtered = activities.filter { activity ->
|
val filtered = activities.filter { activity ->
|
||||||
val matchSetor = setor == "Todos" || activity.setor == setor
|
val matchSetor = setor == "Todos" || activity.setor == setor
|
||||||
@@ -119,22 +124,44 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
|||||||
activity.descricao.contains(query, ignoreCase = true) ||
|
activity.descricao.contains(query, ignoreCase = true) ||
|
||||||
activity.recurso.contains(query, ignoreCase = true)
|
activity.recurso.contains(query, ignoreCase = true)
|
||||||
|
|
||||||
matchSetor && matchEquipe && matchStatus && matchDataInicio && matchRecurso && matchQuery
|
val matchQuickFilter = when (quickFilter) {
|
||||||
|
"Todos" -> true
|
||||||
|
"Pendentes" -> activity.status != ActivityStatus.CONCLUIDO
|
||||||
|
"Hoje" -> {
|
||||||
|
val completionDate = parseFullDateTime(activity.dataFim)
|
||||||
|
if (completionDate == null) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
val calActivity = java.util.Calendar.getInstance()
|
||||||
|
calActivity.time = completionDate
|
||||||
|
|
||||||
|
val calToday = java.util.Calendar.getInstance()
|
||||||
|
calActivity.get(java.util.Calendar.YEAR) == calToday.get(java.util.Calendar.YEAR) &&
|
||||||
|
calActivity.get(java.util.Calendar.DAY_OF_YEAR) == calToday.get(java.util.Calendar.DAY_OF_YEAR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"Em andamento" -> activity.status == ActivityStatus.EM_ANDAMENTO
|
||||||
|
"Não iniciado" -> activity.status == ActivityStatus.NAO_INICIADO
|
||||||
|
"Mecânica" -> activity.equipe == "Mecânica"
|
||||||
|
"Elétrica" -> activity.equipe == "Elétrica"
|
||||||
|
"Confiabilidade" -> activity.equipe == "Confiabilidade"
|
||||||
|
else -> true
|
||||||
|
}
|
||||||
|
|
||||||
|
val result = matchSetor && matchEquipe && matchStatus && matchDataInicio && matchRecurso && matchQuery && matchQuickFilter
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataInicio != "Todas") {
|
// Always sort by dataFim (predicted completion date) ascending, oldest first
|
||||||
filtered.sortedWith { a1, a2 ->
|
filtered.sortedWith { a1, a2 ->
|
||||||
val p1 = parseFullDateTime(a1.dataInicio)
|
val p1 = parseFullDateTime(a1.dataFim)
|
||||||
val p2 = parseFullDateTime(a2.dataInicio)
|
val p2 = parseFullDateTime(a2.dataFim)
|
||||||
when {
|
when {
|
||||||
p1 == null && p2 == null -> a1.numeroOs.compareTo(a2.numeroOs)
|
p1 == null && p2 == null -> a1.numeroOs.compareTo(a2.numeroOs)
|
||||||
p1 == null -> 1
|
p1 == null -> 1 // Keep null/invalid date at the end
|
||||||
p2 == null -> -1
|
p2 == null -> -1
|
||||||
else -> p1.compareTo(p2)
|
else -> p1.compareTo(p2)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
filtered
|
|
||||||
}
|
}
|
||||||
}.stateIn(
|
}.stateIn(
|
||||||
scope = viewModelScope,
|
scope = viewModelScope,
|
||||||
@@ -208,6 +235,10 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
|||||||
_searchQuery.value = query
|
_searchQuery.value = query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setSelectedQuickFilter(filter: String) {
|
||||||
|
_selectedQuickFilter.value = filter
|
||||||
|
}
|
||||||
|
|
||||||
fun toggleInternet(available: Boolean) {
|
fun toggleInternet(available: Boolean) {
|
||||||
// App is always online.
|
// App is always online.
|
||||||
_internetAvailable.value = true
|
_internetAvailable.value = true
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ import org.robolectric.RobolectricTestRunner
|
|||||||
import org.robolectric.annotation.Config
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.test.setMain
|
||||||
|
import kotlinx.coroutines.test.resetMain
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner::class)
|
@RunWith(RobolectricTestRunner::class)
|
||||||
@Config(sdk = [34])
|
@Config(sdk = [34])
|
||||||
@@ -66,4 +70,115 @@ class ExampleRobolectricTest {
|
|||||||
println("--- END SYNCHRONIZE DIAGNOSTIC ---")
|
println("--- END SYNCHRONIZE DIAGNOSTIC ---")
|
||||||
db.close()
|
db.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
|
||||||
|
@Test
|
||||||
|
fun `test quick filters and sorting by predicted end date`() {
|
||||||
|
val testDispatcher = kotlinx.coroutines.test.UnconfinedTestDispatcher()
|
||||||
|
kotlinx.coroutines.Dispatchers.setMain(testDispatcher)
|
||||||
|
try {
|
||||||
|
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||||
|
val db = androidx.room.Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
|
||||||
|
.allowMainThreadQueries()
|
||||||
|
.setQueryExecutor { it.run() }
|
||||||
|
.setTransactionExecutor { it.run() }
|
||||||
|
.build()
|
||||||
|
val repository = com.example.data.repository.MaintenanceRepository(db.maintenanceDao(), context)
|
||||||
|
|
||||||
|
val todayStr = java.text.SimpleDateFormat("dd/MM/yyyy HH:mm", java.util.Locale.US).format(java.util.Date())
|
||||||
|
|
||||||
|
val act1 = com.example.data.model.MaintenanceActivity(
|
||||||
|
numeroOs = "OS-FUTURE",
|
||||||
|
descricao = "Future OS",
|
||||||
|
setor = "Geral",
|
||||||
|
equipe = "Mecânica",
|
||||||
|
recurso = "Sem Recurso",
|
||||||
|
dataInicio = "27/08/2027 10:00",
|
||||||
|
dataFim = "27/08/2027 17:00",
|
||||||
|
percentualExecucao = 50,
|
||||||
|
synced = true
|
||||||
|
)
|
||||||
|
val act2 = com.example.data.model.MaintenanceActivity(
|
||||||
|
numeroOs = "OS-OLDEST",
|
||||||
|
descricao = "Oldest OS",
|
||||||
|
setor = "Geral",
|
||||||
|
equipe = "Elétrica",
|
||||||
|
recurso = "Sem Recurso",
|
||||||
|
dataInicio = "25/07/2026 10:00",
|
||||||
|
dataFim = "25/07/2026 17:00",
|
||||||
|
percentualExecucao = 100,
|
||||||
|
synced = true
|
||||||
|
)
|
||||||
|
val act3 = com.example.data.model.MaintenanceActivity(
|
||||||
|
numeroOs = "OS-TODAY",
|
||||||
|
descricao = "Today OS",
|
||||||
|
setor = "Geral",
|
||||||
|
equipe = "Confiabilidade",
|
||||||
|
recurso = "Sem Recurso",
|
||||||
|
dataInicio = todayStr,
|
||||||
|
dataFim = todayStr,
|
||||||
|
percentualExecucao = 0,
|
||||||
|
synced = true
|
||||||
|
)
|
||||||
|
|
||||||
|
kotlinx.coroutines.runBlocking {
|
||||||
|
db.maintenanceDao().insertActivities(listOf(act1, act2, act3))
|
||||||
|
|
||||||
|
val allLocal = db.maintenanceDao().getAllActivitiesList()
|
||||||
|
println("ALL LOCAL ACTIVITIES IN DB BEFORE VM: ${allLocal.size}")
|
||||||
|
for (a in allLocal) {
|
||||||
|
println(" - OS: ${a.numeroOs}, dataFim: ${a.dataFim}")
|
||||||
|
}
|
||||||
|
|
||||||
|
val viewModel = com.example.ui.viewmodel.MaintenanceViewModel(repository)
|
||||||
|
|
||||||
|
// Start collection to activate stateIn subscription
|
||||||
|
val collectionJob = viewModel.viewModelScope.launch {
|
||||||
|
viewModel.filteredActivities.collect {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let background threads run (like Room flow query)
|
||||||
|
Thread.sleep(200)
|
||||||
|
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||||
|
|
||||||
|
// 1. Test default sorting by dataFim (oldest first): OS-OLDEST -> OS-TODAY (current day) -> OS-FUTURE (next year)
|
||||||
|
val defaultList = viewModel.filteredActivities.value
|
||||||
|
assertEquals(3, defaultList.size)
|
||||||
|
assertEquals("OS-OLDEST", defaultList[0].numeroOs)
|
||||||
|
assertEquals("OS-TODAY", defaultList[1].numeroOs)
|
||||||
|
assertEquals("OS-FUTURE", defaultList[2].numeroOs)
|
||||||
|
|
||||||
|
// 2. Test "Pendentes" filter (should return act1 and act3, but not act2)
|
||||||
|
viewModel.setSelectedQuickFilter("Pendentes")
|
||||||
|
Thread.sleep(200)
|
||||||
|
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||||
|
val pendingList = viewModel.filteredActivities.value
|
||||||
|
assertEquals(2, pendingList.size)
|
||||||
|
assert(pendingList.any { it.numeroOs == "OS-FUTURE" })
|
||||||
|
assert(pendingList.any { it.numeroOs == "OS-TODAY" })
|
||||||
|
|
||||||
|
// 3. Test "Hoje" filter (should return act3 only)
|
||||||
|
viewModel.setSelectedQuickFilter("Hoje")
|
||||||
|
Thread.sleep(200)
|
||||||
|
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||||
|
val todayList = viewModel.filteredActivities.value
|
||||||
|
assertEquals(1, todayList.size)
|
||||||
|
assertEquals("OS-TODAY", todayList[0].numeroOs)
|
||||||
|
|
||||||
|
// 4. Test "Mecânica" filter (should return act1 only)
|
||||||
|
viewModel.setSelectedQuickFilter("Mecânica")
|
||||||
|
Thread.sleep(200)
|
||||||
|
org.robolectric.shadows.ShadowLooper.idleMainLooper()
|
||||||
|
val mechanicalList = viewModel.filteredActivities.value
|
||||||
|
assertEquals(1, mechanicalList.size)
|
||||||
|
assertEquals("OS-FUTURE", mechanicalList[0].numeroOs)
|
||||||
|
|
||||||
|
collectionJob.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
} finally {
|
||||||
|
kotlinx.coroutines.Dispatchers.resetMain()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user