feat: implement sync conflict resolution using LWW strategy and add UI for conflict history monitoring
This commit is contained in:
@@ -6,10 +6,11 @@ import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import com.example.data.model.MaintenanceActivity
|
||||
import com.example.data.model.OSUpdateHistory
|
||||
import com.example.data.model.SyncConflictLog
|
||||
|
||||
@Database(
|
||||
entities = [MaintenanceActivity::class, OSUpdateHistory::class],
|
||||
version = 2,
|
||||
entities = [MaintenanceActivity::class, OSUpdateHistory::class, SyncConflictLog::class],
|
||||
version = 3,
|
||||
exportSchema = false
|
||||
)
|
||||
abstract class AppDatabase : RoomDatabase() {
|
||||
|
||||
@@ -7,6 +7,7 @@ import androidx.room.Query
|
||||
import androidx.room.Update
|
||||
import com.example.data.model.MaintenanceActivity
|
||||
import com.example.data.model.OSUpdateHistory
|
||||
import com.example.data.model.SyncConflictLog
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
@@ -49,4 +50,13 @@ interface MaintenanceDao {
|
||||
|
||||
@Query("DELETE FROM os_update_history")
|
||||
suspend fun clearAllHistory()
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertConflictLog(log: SyncConflictLog)
|
||||
|
||||
@Query("SELECT * FROM sync_conflict_logs ORDER BY timestampResolucao DESC")
|
||||
fun getAllConflictLogs(): Flow<List<SyncConflictLog>>
|
||||
|
||||
@Query("DELETE FROM sync_conflict_logs")
|
||||
suspend fun clearAllConflictLogs()
|
||||
}
|
||||
|
||||
19
app/src/main/java/com/example/data/model/SyncConflictLog.kt
Normal file
19
app/src/main/java/com/example/data/model/SyncConflictLog.kt
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.example.data.model
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "sync_conflict_logs")
|
||||
data class SyncConflictLog(
|
||||
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
||||
val numeroOs: String,
|
||||
val descricaoOs: String,
|
||||
val timestampResolucao: Long = System.currentTimeMillis(),
|
||||
val localPercentual: Int,
|
||||
val localEditor: String?,
|
||||
val localTimestamp: Long,
|
||||
val remotePercentual: Int,
|
||||
val remoteEditor: String?,
|
||||
val remoteTimestamp: Long,
|
||||
val resolvidoPorLocal: Boolean
|
||||
)
|
||||
@@ -6,6 +6,7 @@ import com.example.data.local.MaintenanceDao
|
||||
import com.example.data.model.MaintenanceActivity
|
||||
import com.example.data.model.OSUpdateHistory
|
||||
import com.example.data.model.GoogleUser
|
||||
import com.example.data.model.SyncConflictLog
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@@ -234,7 +235,55 @@ class MaintenanceRepository(
|
||||
android.util.Log.d("MaintenanceRepository", "Reconciled and saved ${reconciledActivities.size} OS activities from Google Sheet.")
|
||||
}
|
||||
|
||||
// 8. Upload any local history logs that are not on Google Sheets yet
|
||||
// 8. Identify and resolve sync conflicts (LWW)
|
||||
val unuploadedLocalLogs = localHistory.filter { "${it.numeroOs}_${it.timestamp}" !in remoteHistoryKeys }
|
||||
val ossWithUnuploadedLogs = unuploadedLocalLogs.map { it.numeroOs }.toSet()
|
||||
|
||||
val newRemoteLogs = remoteHistory.filter { "${it.numeroOs}_${it.timestamp}" !in localHistoryKeys }
|
||||
val ossWithNewRemoteLogs = newRemoteLogs.map { it.numeroOs }.toSet()
|
||||
|
||||
val conflictingOSs = ossWithUnuploadedLogs.intersect(ossWithNewRemoteLogs)
|
||||
val conflictLogsToInsert = mutableListOf<SyncConflictLog>()
|
||||
|
||||
for (numeroOs in conflictingOSs) {
|
||||
val latestLocal = unuploadedLocalLogs
|
||||
.filter { it.numeroOs == numeroOs }
|
||||
.maxByOrNull { it.timestamp }
|
||||
|
||||
val latestRemote = newRemoteLogs
|
||||
.filter { it.numeroOs == numeroOs }
|
||||
.maxByOrNull { it.timestamp }
|
||||
|
||||
if (latestLocal != null && latestRemote != null) {
|
||||
val resolvedByLocal = latestLocal.timestamp >= latestRemote.timestamp
|
||||
val desc = remoteActivities.find { it.numeroOs == numeroOs }?.descricao
|
||||
?: reconciledActivities.find { it.numeroOs == numeroOs }?.descricao
|
||||
?: "OS Sem Descrição"
|
||||
|
||||
conflictLogsToInsert.add(
|
||||
SyncConflictLog(
|
||||
numeroOs = numeroOs,
|
||||
descricaoOs = desc,
|
||||
localPercentual = latestLocal.percentualNovo,
|
||||
localEditor = latestLocal.emailEditor ?: "Local (Sem Email)",
|
||||
localTimestamp = latestLocal.timestamp,
|
||||
remotePercentual = latestRemote.percentualNovo,
|
||||
remoteEditor = latestRemote.emailEditor ?: "Remoto (Sem Email)",
|
||||
remoteTimestamp = latestRemote.timestamp,
|
||||
resolvidoPorLocal = resolvedByLocal
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (conflictLogsToInsert.isNotEmpty()) {
|
||||
for (conflict in conflictLogsToInsert) {
|
||||
dao.insertConflictLog(conflict)
|
||||
}
|
||||
android.util.Log.d("MaintenanceRepository", "Inserted ${conflictLogsToInsert.size} resolved conflict logs.")
|
||||
}
|
||||
|
||||
// 9. Upload any local history logs that are not on Google Sheets yet
|
||||
val historyToUpload = localHistory.filter { "${it.numeroOs}_${it.timestamp}" !in remoteHistoryKeys }
|
||||
if (historyToUpload.isNotEmpty()) {
|
||||
sheetsSyncService.pushHistoryToSheet(historyToUpload)
|
||||
@@ -243,7 +292,7 @@ class MaintenanceRepository(
|
||||
|
||||
SyncResult.Success(
|
||||
syncedItems = reconciledActivities.size,
|
||||
conflictsResolved = 0
|
||||
conflictsResolved = conflictingOSs.size
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.e("MaintenanceRepository", "Synchronization failed", e)
|
||||
@@ -317,6 +366,12 @@ class MaintenanceRepository(
|
||||
Unit
|
||||
}
|
||||
}
|
||||
|
||||
val allConflictLogs: Flow<List<SyncConflictLog>> = dao.getAllConflictLogs()
|
||||
|
||||
suspend fun clearConflictLogs() = withContext(Dispatchers.IO) {
|
||||
dao.clearAllConflictLogs()
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface SyncResult {
|
||||
|
||||
@@ -13,9 +13,16 @@ import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.*
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.draw.shadow
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -46,9 +53,11 @@ fun SettingsScreen(
|
||||
val apiStatusLoading by viewModel.apiStatusLoading.collectAsStateWithLifecycle()
|
||||
val connectionTested by viewModel.connectionTested.collectAsStateWithLifecycle()
|
||||
val isSyncing by viewModel.isSyncing.collectAsStateWithLifecycle()
|
||||
val conflictLogs by viewModel.conflictLogs.collectAsStateWithLifecycle()
|
||||
|
||||
var tempSpreadsheetId by remember(activeSpreadsheetId) { mutableStateOf(activeSpreadsheetId) }
|
||||
var showClearCacheDialog by remember { mutableStateOf(false) }
|
||||
var showConflictsDialog by remember { mutableStateOf(false) }
|
||||
|
||||
// Start monitoring GCP API status when screen is open, stop when closed
|
||||
DisposableEffect(Unit) {
|
||||
@@ -435,6 +444,15 @@ fun SettingsScreen(
|
||||
backupLauncher.launch("cargill_maintenance_backup.db")
|
||||
}
|
||||
)
|
||||
|
||||
// Botão 4: Histórico de Conflitos
|
||||
AdminButton(
|
||||
text = "Histórico de Conflitos",
|
||||
icon = Icons.Default.Warning,
|
||||
onClick = {
|
||||
showConflictsDialog = true
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -479,6 +497,288 @@ fun SettingsScreen(
|
||||
containerColor = Color.White
|
||||
)
|
||||
}
|
||||
|
||||
if (showConflictsDialog) {
|
||||
ConflictsDialog(
|
||||
conflictLogs = conflictLogs,
|
||||
onClear = { viewModel.clearConflictHistory() },
|
||||
onDismiss = { showConflictsDialog = false }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ConflictsDialog(
|
||||
conflictLogs: List<com.example.data.model.SyncConflictLog>,
|
||||
onClear: () -> Unit,
|
||||
onDismiss: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
var showClearConfirm by remember { mutableStateOf(false) }
|
||||
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Warning,
|
||||
contentDescription = "Conflitos",
|
||||
tint = Color(0xFFF57C00)
|
||||
)
|
||||
Text(
|
||||
text = "Histórico de Conflitos",
|
||||
fontWeight = FontWeight.Bold,
|
||||
fontSize = 18.sp
|
||||
)
|
||||
}
|
||||
},
|
||||
text = {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(max = 450.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Visualização detalhada das resoluções de concorrência baseadas no timestamp mais recente (Last-Write-Wins).",
|
||||
fontSize = 12.sp,
|
||||
color = Color.Gray,
|
||||
modifier = Modifier.padding(bottom = 12.dp)
|
||||
)
|
||||
|
||||
if (conflictLogs.isEmpty()) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(200.dp)
|
||||
.padding(vertical = 32.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CheckCircle,
|
||||
contentDescription = "Sem conflitos",
|
||||
tint = CargillGreen,
|
||||
modifier = Modifier.size(48.dp)
|
||||
)
|
||||
Text(
|
||||
text = "Nenhum conflito registrado.",
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = CargillBlack,
|
||||
fontSize = 14.sp
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier.weight(1f),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
items(conflictLogs) { log ->
|
||||
ConflictItemCard(log)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
if (conflictLogs.isNotEmpty()) {
|
||||
TextButton(
|
||||
onClick = { showClearConfirm = true }
|
||||
) {
|
||||
Text(
|
||||
text = "Limpar Histórico",
|
||||
color = Color.Red,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = onDismiss) {
|
||||
Text("Fechar")
|
||||
}
|
||||
},
|
||||
containerColor = Color.White
|
||||
)
|
||||
|
||||
if (showClearConfirm) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showClearConfirm = false },
|
||||
title = { Text("Limpar Histórico", fontWeight = FontWeight.Bold) },
|
||||
text = { Text("Deseja realmente apagar todo o histórico de conflitos salvos localmente?") },
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
onClear()
|
||||
Toast.makeText(context, "Histórico de conflitos limpo!", Toast.LENGTH_SHORT).show()
|
||||
showClearConfirm = false
|
||||
}
|
||||
) {
|
||||
Text("Limpar", color = Color.Red, fontWeight = FontWeight.Bold)
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showClearConfirm = false }) {
|
||||
Text("Cancelar")
|
||||
}
|
||||
},
|
||||
containerColor = Color.White
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ConflictItemCard(log: com.example.data.model.SyncConflictLog) {
|
||||
val sdf = remember { SimpleDateFormat("dd/MM/yy HH:mm:ss", Locale.getDefault()) }
|
||||
val dataResolucao = remember(log.timestampResolucao) { sdf.format(Date(log.timestampResolucao)) }
|
||||
val dataLocal = remember(log.localTimestamp) { sdf.format(Date(log.localTimestamp)) }
|
||||
val dataRemote = remember(log.remoteTimestamp) { sdf.format(Date(log.remoteTimestamp)) }
|
||||
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = Color(0xFFF9F9F9)),
|
||||
shape = RoundedCornerShape(8.dp),
|
||||
border = BorderStroke(1.dp, Color(0xFFE5E5E5)),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(12.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = "OS #${log.numeroOs}",
|
||||
fontWeight = FontWeight.Bold,
|
||||
fontSize = 15.sp,
|
||||
color = Color(0xFF0F2644)
|
||||
)
|
||||
|
||||
// Winner Badge
|
||||
val winnerLabel = if (log.resolvidoPorLocal) "Venceu: Local" else "Venceu: Servidor"
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(4.dp))
|
||||
.background(Color(0xFFE8F5E8))
|
||||
.padding(horizontal = 6.dp, vertical = 2.dp)
|
||||
) {
|
||||
Text(
|
||||
text = winnerLabel,
|
||||
color = CargillGreen,
|
||||
fontSize = 11.sp,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Text(
|
||||
text = log.descricaoOs,
|
||||
fontSize = 13.sp,
|
||||
color = CargillBlack,
|
||||
fontWeight = FontWeight.Normal
|
||||
)
|
||||
|
||||
Text(
|
||||
text = "Resolução LWW em: $dataResolucao",
|
||||
fontSize = 11.sp,
|
||||
color = Color.Gray
|
||||
)
|
||||
|
||||
HorizontalDivider(color = Color(0xFFE5E5E5), thickness = 0.5.dp)
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
// Local Column
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.clip(RoundedCornerShape(6.dp))
|
||||
.background(if (log.resolvidoPorLocal) Color(0xFFF0FDF4) else Color.Transparent)
|
||||
.border(
|
||||
BorderStroke(
|
||||
1.dp,
|
||||
if (log.resolvidoPorLocal) Color(0xFFDCFCE7) else Color.Transparent
|
||||
),
|
||||
shape = RoundedCornerShape(6.dp)
|
||||
)
|
||||
.padding(8.dp)
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp)
|
||||
) {
|
||||
Text(text = "Local", fontWeight = FontWeight.Bold, fontSize = 12.sp, color = CargillBlack)
|
||||
if (log.resolvidoPorLocal) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Check,
|
||||
contentDescription = "Vencedor",
|
||||
tint = CargillGreen,
|
||||
modifier = Modifier.size(12.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
Text(
|
||||
text = "Progresso: ${log.localPercentual}%",
|
||||
fontSize = 12.sp,
|
||||
fontWeight = if (log.resolvidoPorLocal) FontWeight.Bold else FontWeight.Normal,
|
||||
color = CargillBlack
|
||||
)
|
||||
Text(text = "Editor: ${log.localEditor?.substringBefore("@") ?: "Local"}", fontSize = 10.sp, color = Color.Gray)
|
||||
Text(text = dataLocal, fontSize = 10.sp, color = Color.Gray)
|
||||
}
|
||||
|
||||
// Remote Column
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.clip(RoundedCornerShape(6.dp))
|
||||
.background(if (!log.resolvidoPorLocal) Color(0xFFF0FDF4) else Color.Transparent)
|
||||
.border(
|
||||
BorderStroke(
|
||||
1.dp,
|
||||
if (!log.resolvidoPorLocal) Color(0xFFDCFCE7) else Color.Transparent
|
||||
),
|
||||
shape = RoundedCornerShape(6.dp)
|
||||
)
|
||||
.padding(8.dp)
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp)
|
||||
) {
|
||||
Text(text = "Servidor", fontWeight = FontWeight.Bold, fontSize = 12.sp, color = CargillBlack)
|
||||
if (!log.resolvidoPorLocal) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Check,
|
||||
contentDescription = "Vencedor",
|
||||
tint = CargillGreen,
|
||||
modifier = Modifier.size(12.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
Text(
|
||||
text = "Progresso: ${log.remotePercentual}%",
|
||||
fontSize = 12.sp,
|
||||
fontWeight = if (!log.resolvidoPorLocal) FontWeight.Bold else FontWeight.Normal,
|
||||
color = CargillBlack
|
||||
)
|
||||
Text(text = "Editor: ${log.remoteEditor?.substringBefore("@") ?: "Servidor"}", fontSize = 10.sp, color = Color.Gray)
|
||||
Text(text = dataRemote, fontSize = 10.sp, color = Color.Gray)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
@@ -8,6 +8,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.example.data.model.ActivityStatus
|
||||
import com.example.data.model.MaintenanceActivity
|
||||
import com.example.data.model.OSUpdateHistory
|
||||
import com.example.data.model.SyncConflictLog
|
||||
import com.example.data.model.extractDateOnly
|
||||
import com.example.data.model.formatToShortDate
|
||||
import com.example.data.model.parseFullDateTime
|
||||
@@ -370,6 +371,15 @@ class MaintenanceViewModel(private val repository: MaintenanceRepository) : View
|
||||
onResult(result.isSuccess)
|
||||
}
|
||||
}
|
||||
|
||||
val conflictLogs: StateFlow<List<SyncConflictLog>> = repository.allConflictLogs
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), emptyList())
|
||||
|
||||
fun clearConflictHistory() {
|
||||
viewModelScope.launch {
|
||||
repository.clearConflictLogs()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MaintenanceViewModelFactory(private val repository: MaintenanceRepository) : ViewModelProvider.Factory {
|
||||
|
||||
@@ -82,4 +82,54 @@ class ExampleUnitTest {
|
||||
assertEquals("Todas", formatToShortDate("Todas"))
|
||||
assertEquals("N/A", formatToShortDate("N/A"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testConflictResolutionComputation() {
|
||||
val localHistory = listOf(
|
||||
com.example.data.model.OSUpdateHistory(id = 1, numeroOs = "OS1", timestamp = 1000L, percentualAnterior = 0, percentualNovo = 20, recurso = "Mecânico", emailEditor = "local@cargill.com"),
|
||||
com.example.data.model.OSUpdateHistory(id = 2, numeroOs = "OS2", timestamp = 1500L, percentualAnterior = 0, percentualNovo = 50, recurso = "Elétrico", emailEditor = "local@cargill.com"),
|
||||
com.example.data.model.OSUpdateHistory(id = 4, numeroOs = "OS4", timestamp = 3000L, percentualAnterior = 0, percentualNovo = 90, recurso = "Mecânico", emailEditor = "local@cargill.com"),
|
||||
com.example.data.model.OSUpdateHistory(id = 5, numeroOs = "OS5", timestamp = 4000L, percentualAnterior = 0, percentualNovo = 10, recurso = "Elétrico", emailEditor = "local@cargill.com")
|
||||
)
|
||||
|
||||
val remoteHistory = listOf(
|
||||
com.example.data.model.OSUpdateHistory(id = 10, numeroOs = "OS1", timestamp = 1200L, percentualAnterior = 0, percentualNovo = 30, recurso = "Mecânico", emailEditor = "remote@cargill.com"),
|
||||
com.example.data.model.OSUpdateHistory(id = 11, numeroOs = "OS3", timestamp = 2000L, percentualAnterior = 0, percentualNovo = 70, recurso = "Elétrico", emailEditor = "remote@cargill.com"),
|
||||
com.example.data.model.OSUpdateHistory(id = 12, numeroOs = "OS4", timestamp = 2500L, percentualAnterior = 0, percentualNovo = 80, recurso = "Mecânico", emailEditor = "remote@cargill.com"),
|
||||
com.example.data.model.OSUpdateHistory(id = 13, numeroOs = "OS5", timestamp = 4500L, percentualAnterior = 0, percentualNovo = 40, recurso = "Elétrico", emailEditor = "remote@cargill.com")
|
||||
)
|
||||
|
||||
val localHistoryKeys = localHistory.map { "${it.numeroOs}_${it.timestamp}" }.toSet()
|
||||
val remoteHistoryKeys = remoteHistory.map { "${it.numeroOs}_${it.timestamp}" }.toSet()
|
||||
|
||||
val unuploadedLocalLogs = localHistory.filter { "${it.numeroOs}_${it.timestamp}" !in remoteHistoryKeys }
|
||||
val ossWithUnuploadedLogs = unuploadedLocalLogs.map { it.numeroOs }.toSet()
|
||||
|
||||
val newRemoteLogs = remoteHistory.filter { "${it.numeroOs}_${it.timestamp}" !in localHistoryKeys }
|
||||
val ossWithNewRemoteLogs = newRemoteLogs.map { it.numeroOs }.toSet()
|
||||
|
||||
val conflictingOSs = ossWithUnuploadedLogs.intersect(ossWithNewRemoteLogs)
|
||||
|
||||
assertEquals(3, conflictingOSs.size)
|
||||
assertTrue(conflictingOSs.contains("OS1"))
|
||||
assertTrue(conflictingOSs.contains("OS4"))
|
||||
assertTrue(conflictingOSs.contains("OS5"))
|
||||
assertFalse(conflictingOSs.contains("OS2"))
|
||||
assertFalse(conflictingOSs.contains("OS3"))
|
||||
|
||||
// OS1: Remote (1200) wins over Local (1000)
|
||||
val os1Local = unuploadedLocalLogs.filter { it.numeroOs == "OS1" }.maxByOrNull { it.timestamp }!!
|
||||
val os1Remote = newRemoteLogs.filter { it.numeroOs == "OS1" }.maxByOrNull { it.timestamp }!!
|
||||
assertFalse(os1Local.timestamp >= os1Remote.timestamp)
|
||||
|
||||
// OS4: Local (3000) wins over Remote (2500)
|
||||
val os4Local = unuploadedLocalLogs.filter { it.numeroOs == "OS4" }.maxByOrNull { it.timestamp }!!
|
||||
val os4Remote = newRemoteLogs.filter { it.numeroOs == "OS4" }.maxByOrNull { it.timestamp }!!
|
||||
assertTrue(os4Local.timestamp >= os4Remote.timestamp)
|
||||
|
||||
// OS5: Remote (4500) wins over Local (4000)
|
||||
val os5Local = unuploadedLocalLogs.filter { it.numeroOs == "OS5" }.maxByOrNull { it.timestamp }!!
|
||||
val os5Remote = newRemoteLogs.filter { it.numeroOs == "OS5" }.maxByOrNull { it.timestamp }!!
|
||||
assertFalse(os5Local.timestamp >= os5Remote.timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ Abaixo está o levantamento detalhado de cada um desses elementos, com suas resp
|
||||
|
||||
---
|
||||
|
||||
## 4. Contador de Conflitos na Sincronização
|
||||
## 4. IMPLEMENTADO!!! Contador de Conflitos na Sincronização
|
||||
|
||||
* **Status:** **Truncado (Simplificado)**
|
||||
* **Arquivos Relacionados:**
|
||||
@@ -80,7 +80,7 @@ Abaixo está o levantamento detalhado de cada um desses elementos, com suas resp
|
||||
|
||||
---
|
||||
|
||||
## 5. Carga de Dados Inicial em Caso de Falha de Conexão (Fallback)
|
||||
## 5. REMOVIDO!!! Carga de Dados Inicial em Caso de Falha de Conexão (Fallback)
|
||||
|
||||
* **Status:** **Removido**
|
||||
* **Arquivos Relacionados:**
|
||||
|
||||
Reference in New Issue
Block a user