mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
feat: add Google Sheets data restoration
Implement data restoration functionality from Google Sheets, including repository methods to fetch custom foods and updated UI controls in the settings screen.
This commit is contained in:
1 parent
79e04f64d8
commit
3397fc1c60
12 files changed
+1076
-99
No files matched your search
@@ -15,6 +15,7 @@ import com.example.data.preferences.GoogleSheetsPreferencesManager
|
||||
import com.example.data.repository.MealRepository
|
||||
import com.example.domain.CsvExportUseCase
|
||||
import com.example.domain.GoogleSheetsSyncUseCase
|
||||
import com.example.domain.SheetsRestoreResult
|
||||
import com.example.domain.SheetsSyncResult
|
||||
import com.example.notification.MealNotificationManager
|
||||
import com.example.ui.common.UiEvent
|
||||
@@ -305,7 +306,8 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
is SheetsSyncResult.Success -> {
|
||||
_spreadsheetUrl.value = result.url
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Success(result.url, result.count, result.timestamp)
|
||||
emitEvent(UiEvent.ShowSnackbar("Synchronisé avec succès vers Google Sheets (${result.count} repas) 📊"))
|
||||
val foodsInfo = if (result.foodsCount > 0) " et ${result.foodsCount} aliments" else ""
|
||||
emitEvent(UiEvent.ShowSnackbar("Synchronisé vers Google Sheets (${result.count} repas$foodsInfo) 📊"))
|
||||
}
|
||||
is SheetsSyncResult.RecoverableAuth -> {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Error(result.message, result.intent)
|
||||
@@ -325,6 +327,42 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun restoreFromGoogleSheets(
|
||||
onAuthConsentNeeded: ((Intent) -> Unit)? = null
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Syncing
|
||||
when (val result = sheetsSyncUseCase.restoreFromSheets()) {
|
||||
is SheetsRestoreResult.Success -> {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Idle
|
||||
val msg = buildString {
|
||||
append("Restauration réussie ✨ : ")
|
||||
append("${result.mealsCount} repas")
|
||||
if (result.foodsCount > 0) {
|
||||
append(" et ${result.foodsCount} aliments")
|
||||
}
|
||||
append(" restaurés")
|
||||
}
|
||||
emitEvent(UiEvent.ShowSnackbar(msg))
|
||||
}
|
||||
is SheetsRestoreResult.RecoverableAuth -> {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Error(result.message, result.intent)
|
||||
if (result.intent != null && onAuthConsentNeeded != null) {
|
||||
onAuthConsentNeeded(result.intent)
|
||||
} else if (result.intent != null) {
|
||||
emitEvent(UiEvent.StartActivity(result.intent))
|
||||
} else {
|
||||
emitEvent(UiEvent.ShowSnackbar(result.message))
|
||||
}
|
||||
}
|
||||
is SheetsRestoreResult.Failure -> {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Error(result.message)
|
||||
emitEvent(UiEvent.ShowSnackbar(result.message))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun exportCsvToUri(
|
||||
targetUri: Uri,
|
||||
meals: List<MealEntry>
|
||||
@@ -373,6 +411,8 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
result.fold(
|
||||
onSuccess = { count ->
|
||||
emitEvent(UiEvent.ShowSnackbar("Compte lié ($count repas Cloud synchronisés) ☁️"))
|
||||
// Trigger uploading any local meals that were created offline
|
||||
syncLocalToCloud()
|
||||
},
|
||||
onFailure = { err ->
|
||||
val msg = err.localizedMessage ?: "Erreur lors de la synchronisation"
|
||||
@@ -382,6 +422,12 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun syncLocalToCloud() {
|
||||
viewModelScope.launch {
|
||||
repository.syncLocalToCloud()
|
||||
}
|
||||
}
|
||||
|
||||
fun shareCsv(meals: List<MealEntry>) {
|
||||
viewModelScope.launch {
|
||||
val result = csvExportUseCase.createShareIntent(meals)
|
||||
|
||||
@@ -43,6 +43,7 @@ fun SettingsScreen(
|
||||
onSyncSheetsClick: () -> Unit,
|
||||
onAutoSyncSheetsChange: (Boolean) -> Unit,
|
||||
onOpenSheetsClick: (String) -> Unit,
|
||||
onRestoreSheetsClick: () -> Unit = {},
|
||||
onSignInClick: () -> Unit,
|
||||
onSignOut: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
@@ -94,7 +95,8 @@ fun SettingsScreen(
|
||||
spreadsheetUrl = spreadsheetUrl,
|
||||
onSyncSheetsClick = onSyncSheetsClick,
|
||||
onAutoSyncSheetsChange = onAutoSyncSheetsChange,
|
||||
onOpenSheetsClick = onOpenSheetsClick
|
||||
onOpenSheetsClick = onOpenSheetsClick,
|
||||
onRestoreSheetsClick = onRestoreSheetsClick
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+65
@@ -16,9 +16,11 @@ import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.OpenInNew
|
||||
import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.CloudDownload
|
||||
import androidx.compose.material.icons.filled.ErrorOutline
|
||||
import androidx.compose.material.icons.filled.Sync
|
||||
import androidx.compose.material.icons.filled.TableChart
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
@@ -31,7 +33,12 @@ import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.SwitchDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@@ -53,8 +60,10 @@ fun GoogleSheetsIntegrationSection(
|
||||
onSyncSheetsClick: () -> Unit,
|
||||
onAutoSyncSheetsChange: (Boolean) -> Unit,
|
||||
onOpenSheetsClick: (String) -> Unit,
|
||||
onRestoreSheetsClick: () -> Unit = {},
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
var showRestoreConfirmDialog by remember { mutableStateOf(false) }
|
||||
Card(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
@@ -320,6 +329,25 @@ fun GoogleSheetsIntegrationSection(
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(10.dp))
|
||||
|
||||
OutlinedButton(
|
||||
onClick = { showRestoreConfirmDialog = true },
|
||||
enabled = spreadsheetUrl != null && sheetsUiState !is GoogleSheetsUiState.Syncing,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("restore_sheets_button"),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CloudDownload,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(6.dp))
|
||||
Text("Restaurer mes données depuis Google Drive & Sheets")
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Text(
|
||||
@@ -329,4 +357,41 @@ fun GoogleSheetsIntegrationSection(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (showRestoreConfirmDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showRestoreConfirmDialog = false },
|
||||
title = {
|
||||
Text(
|
||||
text = "Restaurer depuis Google Drive & Sheets",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
},
|
||||
text = {
|
||||
Text(
|
||||
text = "Voulez-vous restaurer vos repas et plats personnalisés depuis votre classeur Google Sheets ?\n\nVos ajouts locaux récents seront conservés et fusionnés sans doublon.",
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
},
|
||||
confirmButton = {
|
||||
Button(
|
||||
onClick = {
|
||||
showRestoreConfirmDialog = false
|
||||
onRestoreSheetsClick()
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = Color(0xFF0F9D58)
|
||||
)
|
||||
) {
|
||||
Text("Restaurer")
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showRestoreConfirmDialog = false }) {
|
||||
Text("Annuler")
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user