refactor: optimize Firestore rules and meal cleanup

- Tighten Firestore validation rules (data size and limits)
- Pass context to `deletePhotoFile` for safer file management
- Cleanup unused dependencies in build configuration
- Update Robolectric SDK target for tests
This commit is contained in:
bleackstorm committed 2026-11-08 20:06:30 +01:00
1 parent 87bbb448c2
commit 79e04f64d8
28 files changed
+1606 -1230

No files matched your search

@@ -2,14 +2,16 @@ package com.example.domain
import android.content.Context
import android.content.Intent
import com.example.data.model.MealEntry
import com.example.data.preferences.GoogleSheetsPreferencesManager
import com.example.data.repository.MealRepository
import com.example.util.AuthExpiredException
import com.example.util.GoogleSheetsAuthHelper
import com.example.util.GoogleSheetsSyncResult
import com.example.util.GoogleSheetsSyncService
import com.google.android.gms.auth.UserRecoverableAuthException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
sealed interface SheetsSyncResult {
@@ -24,11 +26,19 @@ class GoogleSheetsSyncUseCase(
private val sheetsPrefs: GoogleSheetsPreferencesManager
) {
suspend fun execute(): SheetsSyncResult = withContext(Dispatchers.IO) {
private val syncMutex = Mutex()
suspend fun execute(): SheetsSyncResult = syncMutex.withLock {
withContext(Dispatchers.IO) {
executeInternal(retryOnAuthFailure = true)
}
}
private suspend fun executeInternal(retryOnAuthFailure: Boolean): SheetsSyncResult {
val tokenResult = GoogleSheetsAuthHelper.getAccessToken(context)
if (tokenResult.isFailure) {
val exception = tokenResult.exceptionOrNull()
return@withContext if (exception is UserRecoverableAuthException) {
return if (exception is UserRecoverableAuthException) {
SheetsSyncResult.RecoverableAuth(
message = "Autorisation requise pour Google Sheets & Drive",
intent = exception.intent
@@ -38,20 +48,42 @@ class GoogleSheetsSyncUseCase(
}
}
val token = tokenResult.getOrThrow()
val spreadsheetResult = GoogleSheetsSyncService.getOrCreateSpreadsheet(token, sheetsPrefs.spreadsheetId)
var token = tokenResult.getOrThrow()
// 1. Get or create spreadsheet
var spreadsheetResult = GoogleSheetsSyncService.getOrCreateSpreadsheet(token, sheetsPrefs.spreadsheetId)
if (spreadsheetResult.isFailure && spreadsheetResult.exceptionOrNull() is AuthExpiredException && retryOnAuthFailure) {
// Token expired, invalidate and retry once
GoogleSheetsAuthHelper.invalidateToken(context, token)
val refreshedTokenResult = GoogleSheetsAuthHelper.getAccessToken(context)
if (refreshedTokenResult.isSuccess) {
token = refreshedTokenResult.getOrThrow()
spreadsheetResult = GoogleSheetsSyncService.getOrCreateSpreadsheet(token, sheetsPrefs.spreadsheetId)
}
}
if (spreadsheetResult.isFailure) {
val err = spreadsheetResult.exceptionOrNull()?.localizedMessage ?: "Erreur de création du classeur"
return@withContext SheetsSyncResult.Failure(err)
return SheetsSyncResult.Failure(err)
}
val (id, url) = spreadsheetResult.getOrThrow()
sheetsPrefs.spreadsheetId = id
sheetsPrefs.spreadsheetUrl = url
// 2. Sync meals
val currentMeals = repository.getAllMealsSnapshot()
val syncResult = GoogleSheetsSyncService.syncMeals(token, id, currentMeals)
if (syncResult.isSuccess) {
var syncResult = GoogleSheetsSyncService.syncMeals(token, id, currentMeals)
if (syncResult.isFailure && syncResult.exceptionOrNull() is AuthExpiredException && retryOnAuthFailure) {
GoogleSheetsAuthHelper.invalidateToken(context, token)
val refreshedTokenResult = GoogleSheetsAuthHelper.getAccessToken(context)
if (refreshedTokenResult.isSuccess) {
token = refreshedTokenResult.getOrThrow()
syncResult = GoogleSheetsSyncService.syncMeals(token, id, currentMeals)
}
}
return if (syncResult.isSuccess) {
val res = syncResult.getOrThrow()
sheetsPrefs.lastSyncTimestamp = res.timestamp
sheetsPrefs.lastSyncCount = res.mealsCount
@@ -63,23 +95,30 @@ class GoogleSheetsSyncUseCase(
}
}
suspend fun executeAutoSyncQuietly(): GoogleSheetsSyncResult? = withContext(Dispatchers.IO) {
if (!sheetsPrefs.isAutoSyncEnabled) return@withContext null
try {
val token = GoogleSheetsAuthHelper.getAccessToken(context).getOrNull() ?: return@withContext null
val (id, url) = GoogleSheetsSyncService.getOrCreateSpreadsheet(token, sheetsPrefs.spreadsheetId).getOrNull() ?: return@withContext null
sheetsPrefs.spreadsheetId = id
sheetsPrefs.spreadsheetUrl = url
suspend fun executeAutoSyncQuietly(): GoogleSheetsSyncResult? {
if (!sheetsPrefs.isAutoSyncEnabled) return null
if (syncMutex.isLocked) return null // Drop redundant concurrent background trigger
val currentMeals = repository.getAllMealsSnapshot()
val syncResult = GoogleSheetsSyncService.syncMeals(token, id, currentMeals)
if (syncResult.isSuccess) {
val res = syncResult.getOrThrow()
sheetsPrefs.lastSyncTimestamp = res.timestamp
sheetsPrefs.lastSyncCount = res.mealsCount
sheetsPrefs.lastSyncStatus = "Synchronisé automatiquement"
res
} else null
return try {
syncMutex.withLock {
withContext(Dispatchers.IO) {
val token = GoogleSheetsAuthHelper.getAccessToken(context).getOrNull() ?: return@withContext null
val (id, url) = GoogleSheetsSyncService.getOrCreateSpreadsheet(token, sheetsPrefs.spreadsheetId).getOrNull()
?: return@withContext null
sheetsPrefs.spreadsheetId = id
sheetsPrefs.spreadsheetUrl = url
val currentMeals = repository.getAllMealsSnapshot()
val syncResult = GoogleSheetsSyncService.syncMeals(token, id, currentMeals)
if (syncResult.isSuccess) {
val res = syncResult.getOrThrow()
sheetsPrefs.lastSyncTimestamp = res.timestamp
sheetsPrefs.lastSyncCount = res.mealsCount
sheetsPrefs.lastSyncStatus = "Synchronisé automatiquement"
res
} else null
}
}
} catch (_: Exception) {
null
}