feat: add overdue analytics by sector with swipeable UI in AnalyticsScreen

This commit is contained in:
Jhony Cesar
2026-07-29 10:43:06 -04:00
parent e696ceb399
commit 694daa1cf3
4 changed files with 170 additions and 82 deletions

View File

@@ -9,5 +9,6 @@ data class AnalyticsState(
val timelineDates: List<Date> = emptyList(), val timelineDates: List<Date> = emptyList(),
val plannedProgressPoints: List<Float> = emptyList(), val plannedProgressPoints: List<Float> = emptyList(),
val realizedProgressPoints: List<Float> = emptyList(), val realizedProgressPoints: List<Float> = emptyList(),
val overdueByTag: List<Pair<String, Int>> = emptyList() val overdueByTag: List<Pair<String, Int>> = emptyList(),
val overdueBySector: List<Pair<String, Int>> = emptyList()
) )

View File

@@ -4,9 +4,13 @@ import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import kotlinx.coroutines.launch
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.* import androidx.compose.material3.*
@@ -119,8 +123,8 @@ fun AnalyticsScreen(
// Card 2: Progresso do Projeto: Planejado vs. Realizado // Card 2: Progresso do Projeto: Planejado vs. Realizado
SCurveCard(analyticsState) SCurveCard(analyticsState)
// Card 3: Atrasados por TAG // Card 3: OSs Atrasadas (TAG / Setor alternáveis por swipe)
OverdueByTagCard(analyticsState.overdueByTag) OverdueAnalyticsCard(analyticsState)
} }
} }
} }
@@ -361,11 +365,14 @@ fun SCurveCard(analyticsState: AnalyticsState) {
} }
/** /**
* Gráfico 3: OSs Atrasadas por TAG (Ativo) * Gráfico 3: OSs Atrasadas por TAG e por Setor
* Lista as TAGs com maior volume de OSs pendentes e com prazo vencido * Permite alternar horizontalmente (swipe) entre as duas visualizações.
*/ */
@Composable @Composable
fun OverdueByTagCard(overdueByTag: List<Pair<String, Int>>) { fun OverdueAnalyticsCard(analyticsState: AnalyticsState) {
val pagerState = rememberPagerState(pageCount = { 2 })
val coroutineScope = rememberCoroutineScope()
Card( Card(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(containerColor = Color.White), colors = CardDefaults.cardColors(containerColor = Color.White),
@@ -377,18 +384,70 @@ fun OverdueByTagCard(overdueByTag: List<Pair<String, Int>>) {
.padding(16.dp) .padding(16.dp)
.fillMaxWidth() .fillMaxWidth()
) { ) {
Text( // Header with Dynamic Title & Page Indicator Dots
text = "Atrasados por TAG", Row(
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
color = BrandBlack,
textAlign = TextAlign.Center,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(bottom = 16.dp) .padding(bottom = 16.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = if (pagerState.currentPage == 0) "Atrasados por TAG" else "Atrasados por Setor",
fontSize = 16.sp,
fontWeight = FontWeight.Bold,
color = BrandBlack
) )
if (overdueByTag.isEmpty()) { // Page Indicator Dots
Row(
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalAlignment = Alignment.CenterVertically
) {
repeat(2) { index ->
val isSelected = pagerState.currentPage == index
Box(
modifier = Modifier
.size(if (isSelected) 10.dp else 8.dp)
.background(
color = if (isSelected) BrandGreen else Color.LightGray,
shape = CircleShape
)
.clickable {
coroutineScope.launch {
pagerState.animateScrollToPage(index)
}
}
)
}
}
}
HorizontalPager(
state = pagerState,
modifier = Modifier.fillMaxWidth()
) { page ->
val (items, emptyMsg) = if (page == 0) {
analyticsState.overdueByTag to "Parabéns! Nenhuma OS atrasada por TAG no momento."
} else {
analyticsState.overdueBySector to "Parabéns! Nenhuma OS atrasada por Setor no momento."
}
OverdueBarChartContent(
displayList = items,
emptyMessage = emptyMsg
)
}
}
}
}
@Composable
private fun OverdueBarChartContent(
displayList: List<Pair<String, Int>>,
emptyMessage: String
) {
if (displayList.isEmpty()) {
Box( Box(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@@ -396,21 +455,21 @@ fun OverdueByTagCard(overdueByTag: List<Pair<String, Int>>) {
contentAlignment = Alignment.Center contentAlignment = Alignment.Center
) { ) {
Text( Text(
text = "Parabéns! Nenhuma OS atrasada no momento.", text = emptyMessage,
color = Color(0xFF2E7D32), color = Color(0xFF2E7D32),
fontWeight = FontWeight.Medium, fontWeight = FontWeight.Medium,
fontSize = 13.sp fontSize = 13.sp,
textAlign = TextAlign.Center
) )
} }
} else { } else {
val displayList = overdueByTag
val maxCount = displayList.maxOfOrNull { it.second } ?: 1 val maxCount = displayList.maxOfOrNull { it.second } ?: 1
Column( Column(
verticalArrangement = Arrangement.spacedBy(12.dp), verticalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) { ) {
displayList.forEachIndexed { index, (tag, count) -> displayList.forEachIndexed { index, (label, count) ->
val barColor = when (index) { val barColor = when (index) {
0 -> Color(0xFF4CAF50) // Green 0 -> Color(0xFF4CAF50) // Green
1 -> Color(0xFF2196F3) // Blue 1 -> Color(0xFF2196F3) // Blue
@@ -423,9 +482,9 @@ fun OverdueByTagCard(overdueByTag: List<Pair<String, Int>>) {
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) { ) {
// TAG Name // Label (TAG Name or Sector Name)
Text( Text(
text = tag, text = label,
fontSize = 12.sp, fontSize = 12.sp,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
color = BrandBlack, color = BrandBlack,
@@ -464,5 +523,3 @@ fun OverdueByTagCard(overdueByTag: List<Pair<String, Int>>) {
} }
} }
} }
}
}

View File

@@ -557,7 +557,7 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
} }
} }
// 3. Overdue tags // 3. Overdue tags and sectors
val referenceDate = if (Date().before(projectEnd)) projectEnd else Date() val referenceDate = if (Date().before(projectEnd)) projectEnd else Date()
val overdueActivities = activities.filter { act -> val overdueActivities = activities.filter { act ->
@@ -581,6 +581,31 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
top + listOf("Outros" to othersCount) top + listOf("Outros" to othersCount)
} }
val overdueBySectorMap = overdueActivities
.groupBy { if (it.setor.isBlank()) "Sem Setor" else it.setor }
.mapValues { it.value.size }
val finalOverdueBySector = if (overdueActivities.isEmpty()) {
emptyList()
} else {
val plantSectors = activities
.map { if (it.setor.isBlank()) "Sem Setor" else it.setor }
.distinct()
val fullOverdueBySector = plantSectors
.map { sector -> sector to (overdueBySectorMap[sector] ?: 0) }
.sortedWith(compareByDescending<Pair<String, Int>> { it.second }.thenBy { it.first })
val displayLimit = 4
if (fullOverdueBySector.size <= displayLimit) {
fullOverdueBySector
} else {
val top = fullOverdueBySector.take(displayLimit - 1)
val othersCount = fullOverdueBySector.drop(displayLimit - 1).sumOf { it.second }
top + listOf("Outros" to othersCount)
}
}
return AnalyticsState( return AnalyticsState(
teamMetrics = teamMetrics, teamMetrics = teamMetrics,
projectStart = projectStart, projectStart = projectStart,
@@ -588,7 +613,8 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
timelineDates = timelineDates, timelineDates = timelineDates,
plannedProgressPoints = plannedProgressPoints, plannedProgressPoints = plannedProgressPoints,
realizedProgressPoints = realizedProgressPoints, realizedProgressPoints = realizedProgressPoints,
overdueByTag = finalOverdueByTag overdueByTag = finalOverdueByTag,
overdueBySector = finalOverdueBySector
) )
} }
} }

View File

@@ -5,6 +5,7 @@ import androidx.test.core.app.ApplicationProvider
import com.example.data.local.AppDatabase import com.example.data.local.AppDatabase
import com.example.data.remote.GoogleSheetsSyncService import com.example.data.remote.GoogleSheetsSyncService
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner import org.robolectric.RobolectricTestRunner
@@ -252,6 +253,9 @@ class ExampleRobolectricTest {
// 4. Verify realized progress points has correct count // 4. Verify realized progress points has correct count
assertEquals(5, state.realizedProgressPoints.size) assertEquals(5, state.realizedProgressPoints.size)
// 5. Verify overdueBySector is present
assertNotNull(state.overdueBySector)
collectionJob.cancel() collectionJob.cancel()
} }
db.close() db.close()