feat: implement drill-down modal and Google Sign-In for overdue activity management in analytics
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package com.example.ui.screens
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.pager.HorizontalPager
|
||||
import androidx.compose.foundation.pager.rememberPagerState
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
@@ -25,6 +31,7 @@ import androidx.compose.ui.graphics.Path
|
||||
import androidx.compose.ui.graphics.PathEffect
|
||||
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||
import androidx.compose.ui.graphics.nativeCanvas
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -39,11 +46,21 @@ import com.example.ui.theme.BrandBlack
|
||||
import com.example.ui.theme.BrandGreen
|
||||
import com.example.ui.theme.LightGrey
|
||||
import com.example.ui.viewmodel.MaintenanceViewModel
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
enum class OverdueGroupType { TAG, SETOR }
|
||||
|
||||
data class SelectedOverdueGroup(
|
||||
val type: OverdueGroupType,
|
||||
val label: String
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun AnalyticsScreen(
|
||||
@@ -53,6 +70,39 @@ fun AnalyticsScreen(
|
||||
) {
|
||||
val activities by viewModel.allActivitiesState.collectAsStateWithLifecycle()
|
||||
val analyticsState by viewModel.analyticsState.collectAsStateWithLifecycle()
|
||||
val currentUser by viewModel.currentUser.collectAsStateWithLifecycle()
|
||||
val userAccessLevel by viewModel.userAccessLevel.collectAsStateWithLifecycle()
|
||||
val allHistory by viewModel.allHistoryState.collectAsStateWithLifecycle()
|
||||
|
||||
var selectedOverdueGroup by remember { mutableStateOf<SelectedOverdueGroup?>(null) }
|
||||
var selectedActivityToEdit by remember { mutableStateOf<MaintenanceActivity?>(null) }
|
||||
|
||||
val context = LocalContext.current
|
||||
|
||||
val gso = remember {
|
||||
GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
||||
.requestEmail()
|
||||
.requestProfile()
|
||||
.build()
|
||||
}
|
||||
val googleSignInClient = remember { GoogleSignIn.getClient(context, gso) }
|
||||
|
||||
val googleSignInLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
val task = GoogleSignIn.getSignedInAccountFromIntent(result.data)
|
||||
try {
|
||||
val account = task.getResult(ApiException::class.java)
|
||||
if (account != null) {
|
||||
val email = account.email ?: ""
|
||||
val name = account.displayName ?: ""
|
||||
viewModel.loginUser(email, name)
|
||||
Toast.makeText(context, "Conectado como $name", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
} catch (e: ApiException) {
|
||||
Toast.makeText(context, "Falha no Google Sign-In (Erro ${e.statusCode})", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
@@ -123,9 +173,57 @@ fun AnalyticsScreen(
|
||||
// Card 2: Progresso do Projeto: Planejado vs. Realizado
|
||||
SCurveCard(analyticsState)
|
||||
|
||||
// Card 3: OSs Atrasadas (TAG / Setor alternáveis por swipe)
|
||||
OverdueAnalyticsCard(analyticsState)
|
||||
// Card 3: OSs Atrasadas (TAG / Setor alternáveis por swipe e clicáveis)
|
||||
OverdueAnalyticsCard(
|
||||
analyticsState = analyticsState,
|
||||
onBarClick = { type, label ->
|
||||
selectedOverdueGroup = SelectedOverdueGroup(type, label)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Modal 1: Listagem das OSs Atrasadas por Grupo (TAG ou Setor)
|
||||
selectedOverdueGroup?.let { group ->
|
||||
val topSectors = analyticsState.overdueBySector.take(3).map { it.first }
|
||||
OverdueActivitiesListModal(
|
||||
group = group,
|
||||
allActivities = activities,
|
||||
projectEnd = analyticsState.projectEnd,
|
||||
topSectors = topSectors,
|
||||
onDismiss = { selectedOverdueGroup = null },
|
||||
onActivityClick = { activity ->
|
||||
selectedOverdueGroup = null
|
||||
selectedActivityToEdit = activity
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
// Modal 2: Edição da OS Selecionada
|
||||
selectedActivityToEdit?.let { activity ->
|
||||
val history = remember(allHistory, activity) {
|
||||
allHistory.filter { it.numeroOs == activity.numeroOs }
|
||||
}
|
||||
EditActivityBottomSheet(
|
||||
activity = activity,
|
||||
history = history,
|
||||
currentUser = currentUser,
|
||||
userAccessLevel = userAccessLevel,
|
||||
onDismiss = { selectedActivityToEdit = null },
|
||||
onUpdateProgress = { newPercentual, observation, photoPath ->
|
||||
viewModel.updateProgress(
|
||||
numeroOs = activity.numeroOs,
|
||||
novoPercentual = newPercentual,
|
||||
observacao = observation,
|
||||
fotoPath = photoPath,
|
||||
recurso = currentUser?.displayName ?: "Encarregado Campo",
|
||||
emailEditor = currentUser?.email
|
||||
)
|
||||
},
|
||||
onGoogleLoginClick = {
|
||||
googleSignInLauncher.launch(googleSignInClient.signInIntent)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -366,10 +464,13 @@ fun SCurveCard(analyticsState: AnalyticsState) {
|
||||
|
||||
/**
|
||||
* Gráfico 3: OSs Atrasadas por TAG e por Setor
|
||||
* Permite alternar horizontalmente (swipe) entre as duas visualizações.
|
||||
* Permite alternar horizontalmente (swipe) entre as duas visualizações e clicar nas barras para ver detalhes.
|
||||
*/
|
||||
@Composable
|
||||
fun OverdueAnalyticsCard(analyticsState: AnalyticsState) {
|
||||
fun OverdueAnalyticsCard(
|
||||
analyticsState: AnalyticsState,
|
||||
onBarClick: (OverdueGroupType, String) -> Unit = { _, _ -> }
|
||||
) {
|
||||
val pagerState = rememberPagerState(pageCount = { 2 })
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
|
||||
@@ -435,7 +536,11 @@ fun OverdueAnalyticsCard(analyticsState: AnalyticsState) {
|
||||
|
||||
OverdueBarChartContent(
|
||||
displayList = items,
|
||||
emptyMessage = emptyMsg
|
||||
emptyMessage = emptyMsg,
|
||||
onItemClick = { label ->
|
||||
val groupType = if (page == 0) OverdueGroupType.TAG else OverdueGroupType.SETOR
|
||||
onBarClick(groupType, label)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -445,7 +550,8 @@ fun OverdueAnalyticsCard(analyticsState: AnalyticsState) {
|
||||
@Composable
|
||||
private fun OverdueBarChartContent(
|
||||
displayList: List<Pair<String, Int>>,
|
||||
emptyMessage: String
|
||||
emptyMessage: String,
|
||||
onItemClick: (String) -> Unit = {}
|
||||
) {
|
||||
if (displayList.isEmpty()) {
|
||||
Box(
|
||||
@@ -480,7 +586,10 @@ private fun OverdueBarChartContent(
|
||||
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { onItemClick(label) }
|
||||
.padding(vertical = 4.dp)
|
||||
) {
|
||||
// Label (TAG Name or Sector Name)
|
||||
Text(
|
||||
@@ -523,3 +632,172 @@ private fun OverdueBarChartContent(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modal dialog displaying the list of overdue OSs for a selected TAG or Sector.
|
||||
*/
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun OverdueActivitiesListModal(
|
||||
group: SelectedOverdueGroup,
|
||||
allActivities: List<MaintenanceActivity>,
|
||||
projectEnd: Date,
|
||||
topSectors: List<String>,
|
||||
onDismiss: () -> Unit,
|
||||
onActivityClick: (MaintenanceActivity) -> Unit
|
||||
) {
|
||||
val referenceDate = remember(projectEnd) {
|
||||
if (Date().before(projectEnd)) projectEnd else Date()
|
||||
}
|
||||
|
||||
val filteredActivities = remember(group, allActivities, referenceDate, topSectors) {
|
||||
allActivities.filter { act ->
|
||||
val isNotCompleted = act.percentualExecucao < 100
|
||||
val endDate = parseDateOnly(act.dataFim)
|
||||
val isOverdue = isNotCompleted && endDate != null && endDate.before(referenceDate)
|
||||
|
||||
if (!isOverdue) return@filter false
|
||||
|
||||
when (group.type) {
|
||||
OverdueGroupType.TAG -> {
|
||||
if (group.label == "Sem TAG") act.tag.isBlank() else act.tag == group.label
|
||||
}
|
||||
OverdueGroupType.SETOR -> {
|
||||
val actSetor = if (act.setor.isBlank()) "Sem Setor" else act.setor
|
||||
if (group.label == "Outros") {
|
||||
!topSectors.contains(actSetor)
|
||||
} else if (group.label == "Sem Setor") {
|
||||
act.setor.isBlank()
|
||||
} else {
|
||||
act.setor == group.label
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
confirmButton = {
|
||||
TextButton(onClick = onDismiss) {
|
||||
Text("Fechar", color = BrandGreen, fontWeight = FontWeight.Bold)
|
||||
}
|
||||
},
|
||||
title = {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = "OSs Atrasadas",
|
||||
fontSize = 12.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = Color.Gray
|
||||
)
|
||||
Text(
|
||||
text = "${if (group.type == OverdueGroupType.TAG) "TAG" else "Setor"}: ${group.label}",
|
||||
fontSize = 16.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = BrandBlack
|
||||
)
|
||||
}
|
||||
Badge(
|
||||
containerColor = Color(0xFFC5221F),
|
||||
contentColor = Color.White
|
||||
) {
|
||||
Text(
|
||||
text = "${filteredActivities.size} OS",
|
||||
fontSize = 11.sp,
|
||||
modifier = Modifier.padding(horizontal = 6.dp, vertical = 2.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
text = {
|
||||
if (filteredActivities.isEmpty()) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(100.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(
|
||||
text = "Nenhuma ordem de serviço pendente encontrada nesta categoria.",
|
||||
fontSize = 13.sp,
|
||||
color = Color.Gray,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(max = 350.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
items(filteredActivities) { activity ->
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { onActivityClick(activity) },
|
||||
colors = CardDefaults.cardColors(containerColor = Color(0xFFF9F9F9)),
|
||||
border = BorderStroke(1.dp, Color(0xFFE0E0E0)),
|
||||
shape = RoundedCornerShape(8.dp)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(12.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = activity.numeroOs,
|
||||
fontSize = 14.sp,
|
||||
fontWeight = FontWeight.Black,
|
||||
color = BrandBlack
|
||||
)
|
||||
Surface(
|
||||
color = Color(0xFFFFF3E0),
|
||||
shape = RoundedCornerShape(4.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "${activity.percentualExecucao}%",
|
||||
fontSize = 11.sp,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = Color(0xFFE65100),
|
||||
modifier = Modifier.padding(horizontal = 6.dp, vertical = 2.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = activity.descricao,
|
||||
fontSize = 12.sp,
|
||||
fontWeight = FontWeight.Medium,
|
||||
color = Color.DarkGray,
|
||||
maxLines = 2
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(6.dp))
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
if (activity.setor.isNotBlank()) {
|
||||
Text("Setor: ${activity.setor}", fontSize = 10.sp, color = BrandGreen, fontWeight = FontWeight.Bold)
|
||||
}
|
||||
if (activity.tag.isNotBlank()) {
|
||||
Text("TAG: ${activity.tag}", fontSize = 10.sp, color = Color(0xFF1976D2), fontWeight = FontWeight.Bold)
|
||||
}
|
||||
Text("Fim: ${activity.dataFim}", fontSize = 10.sp, color = Color.Gray)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
containerColor = Color.White
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user