mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
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:
1 parent
87bbb448c2
commit
79e04f64d8
28 files changed
+1606
-1230
No files matched your search
@@ -210,9 +210,7 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
note: String,
|
||||
foodItems: List<MealFoodItem>,
|
||||
photoUris: List<String> = emptyList(),
|
||||
photosBase64: List<String> = emptyList(),
|
||||
onSuccess: () -> Unit = {},
|
||||
onError: (String) -> Unit = {}
|
||||
photosBase64: List<String> = emptyList()
|
||||
) {
|
||||
enrichCatalogWithFoodItems(foodItems)
|
||||
viewModelScope.launch {
|
||||
@@ -228,12 +226,10 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
)
|
||||
val result = repository.addMeal(entry)
|
||||
if (result.isSuccess) {
|
||||
onSuccess()
|
||||
emitEvent(UiEvent.ShowSnackbar("Repas enregistré avec succès ✨"))
|
||||
triggerAutoSheetsSyncIfNeeded()
|
||||
} else {
|
||||
val err = result.exceptionOrNull()?.localizedMessage ?: "Erreur d'enregistrement"
|
||||
onError(err)
|
||||
emitEvent(UiEvent.ShowSnackbar(err))
|
||||
}
|
||||
}
|
||||
@@ -257,9 +253,7 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
fun updateMeal(
|
||||
meal: MealEntry,
|
||||
foodItems: List<MealFoodItem>,
|
||||
onSuccess: () -> Unit = {},
|
||||
onError: (String) -> Unit = {}
|
||||
foodItems: List<MealFoodItem>
|
||||
) {
|
||||
enrichCatalogWithFoodItems(foodItems)
|
||||
viewModelScope.launch {
|
||||
@@ -270,31 +264,23 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
)
|
||||
val result = repository.updateMeal(sanitizedMeal)
|
||||
if (result.isSuccess) {
|
||||
onSuccess()
|
||||
emitEvent(UiEvent.ShowSnackbar("Repas mis à jour ✨"))
|
||||
triggerAutoSheetsSyncIfNeeded()
|
||||
} else {
|
||||
val err = result.exceptionOrNull()?.localizedMessage ?: "Erreur de mise à jour"
|
||||
onError(err)
|
||||
emitEvent(UiEvent.ShowSnackbar(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteMeal(
|
||||
mealId: String,
|
||||
onSuccess: () -> Unit = {},
|
||||
onError: (String) -> Unit = {}
|
||||
) {
|
||||
fun deleteMeal(mealId: String) {
|
||||
viewModelScope.launch {
|
||||
val result = repository.deleteMeal(mealId)
|
||||
if (result.isSuccess) {
|
||||
onSuccess()
|
||||
emitEvent(UiEvent.ShowSnackbar("Repas supprimé"))
|
||||
triggerAutoSheetsSyncIfNeeded()
|
||||
} else {
|
||||
val err = result.exceptionOrNull()?.localizedMessage ?: "Erreur de suppression"
|
||||
onError(err)
|
||||
emitEvent(UiEvent.ShowSnackbar(err))
|
||||
}
|
||||
}
|
||||
@@ -311,8 +297,7 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
}
|
||||
|
||||
fun syncToGoogleSheets(
|
||||
onSuccess: (url: String, count: Int) -> Unit = { _, _ -> },
|
||||
onError: (message: String, recoverableIntent: Intent?) -> Unit = { _, _ -> }
|
||||
onAuthConsentNeeded: ((Intent) -> Unit)? = null
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Syncing
|
||||
@@ -320,16 +305,20 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
is SheetsSyncResult.Success -> {
|
||||
_spreadsheetUrl.value = result.url
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Success(result.url, result.count, result.timestamp)
|
||||
onSuccess(result.url, result.count)
|
||||
emitEvent(UiEvent.ShowSnackbar("Synchronisé avec succès vers Google Sheets (${result.count} repas) 📊"))
|
||||
}
|
||||
is SheetsSyncResult.RecoverableAuth -> {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Error(result.message, result.intent)
|
||||
onError(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 SheetsSyncResult.Failure -> {
|
||||
_sheetsUiState.value = GoogleSheetsUiState.Error(result.message)
|
||||
onError(result.message, null)
|
||||
emitEvent(UiEvent.ShowSnackbar(result.message))
|
||||
}
|
||||
}
|
||||
@@ -338,36 +327,27 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
fun exportCsvToUri(
|
||||
targetUri: Uri,
|
||||
meals: List<MealEntry>,
|
||||
onSuccess: (Int) -> Unit,
|
||||
onError: (String) -> Unit
|
||||
meals: List<MealEntry>
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
val result = csvExportUseCase.writeToUri(targetUri, meals)
|
||||
result.fold(
|
||||
onSuccess = { count ->
|
||||
onSuccess(count)
|
||||
emitEvent(UiEvent.ShowSnackbar("Fichier CSV enregistré ($count repas) ✨"))
|
||||
},
|
||||
onFailure = { err ->
|
||||
val msg = err.localizedMessage ?: "Erreur lors de l'export CSV"
|
||||
onError(msg)
|
||||
emitEvent(UiEvent.ShowSnackbar(msg))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun importCsvFromUri(
|
||||
sourceUri: Uri,
|
||||
onSuccess: (CsvImportResult) -> Unit,
|
||||
onError: (String) -> Unit
|
||||
) {
|
||||
fun importCsvFromUri(sourceUri: Uri) {
|
||||
viewModelScope.launch {
|
||||
val result = repository.importMealsFromCsv(context, sourceUri)
|
||||
result.fold(
|
||||
onSuccess = { importResult ->
|
||||
onSuccess(importResult)
|
||||
val count = importResult.importedMeals.size
|
||||
val skipped = importResult.skippedDuplicatesCount
|
||||
val newFoods = importResult.newFoodItems.size
|
||||
@@ -381,48 +361,36 @@ class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
},
|
||||
onFailure = { err ->
|
||||
val msg = err.localizedMessage ?: "Erreur lors de l'import du fichier CSV"
|
||||
onError(msg)
|
||||
emitEvent(UiEvent.ShowSnackbar(msg))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun syncCloudToLocal(
|
||||
onSuccess: (Int) -> Unit,
|
||||
onError: (String) -> Unit
|
||||
) {
|
||||
fun syncCloudToLocal() {
|
||||
viewModelScope.launch {
|
||||
val result = repository.syncCloudToLocal()
|
||||
result.fold(
|
||||
onSuccess = { count ->
|
||||
onSuccess(count)
|
||||
emitEvent(UiEvent.ShowSnackbar("Compte lié ($count repas Cloud synchronisés) ☁️"))
|
||||
},
|
||||
onFailure = { err ->
|
||||
val msg = err.localizedMessage ?: "Erreur lors de la synchronisation"
|
||||
onError(msg)
|
||||
emitEvent(UiEvent.ShowSnackbar(msg))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun shareCsv(
|
||||
activityContext: Context,
|
||||
meals: List<MealEntry>,
|
||||
onIntentReady: (Intent) -> Unit,
|
||||
onError: (String) -> Unit
|
||||
) {
|
||||
fun shareCsv(meals: List<MealEntry>) {
|
||||
viewModelScope.launch {
|
||||
val result = csvExportUseCase.createShareIntent(activityContext, meals)
|
||||
val result = csvExportUseCase.createShareIntent(meals)
|
||||
result.fold(
|
||||
onSuccess = { chooserIntent ->
|
||||
onIntentReady(chooserIntent)
|
||||
emitEvent(UiEvent.StartActivity(chooserIntent))
|
||||
},
|
||||
onFailure = { err ->
|
||||
val msg = err.localizedMessage ?: "Impossible de préparer le partage CSV"
|
||||
onError(msg)
|
||||
emitEvent(UiEvent.ShowSnackbar(msg))
|
||||
}
|
||||
)
|
||||
|
||||
@@ -36,8 +36,10 @@ import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.saveable.listSaver
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -59,6 +61,24 @@ import com.example.util.ImageUtils
|
||||
import kotlinx.coroutines.launch
|
||||
import java.util.Locale
|
||||
|
||||
private val FoodItemListSaver = listSaver<SnapshotStateList<MealFoodItem>, String>(
|
||||
save = { stateList -> stateList.map { it.serialize() } },
|
||||
restore = { savedStrings ->
|
||||
mutableStateListOf<MealFoodItem>().apply {
|
||||
addAll(savedStrings.mapNotNull { MealFoodItem.deserialize(it) })
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
private val StringListSaver = listSaver<SnapshotStateList<String>, String>(
|
||||
save = { stateList -> stateList.toList() },
|
||||
restore = { savedStrings ->
|
||||
mutableStateListOf<String>().apply {
|
||||
addAll(savedStrings)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun MealEntrySheet(
|
||||
@@ -89,14 +109,15 @@ fun MealEntrySheet(
|
||||
mutableStateOf(initialMeal?.note ?: "")
|
||||
}
|
||||
|
||||
val foodItemsList = remember {
|
||||
// Preserved across rotation via custom ListSaver
|
||||
val foodItemsList = rememberSaveable(saver = FoodItemListSaver) {
|
||||
mutableStateListOf<MealFoodItem>().apply {
|
||||
if (initialMeal != null) {
|
||||
addAll(initialMeal.parsedFoodItems())
|
||||
}
|
||||
}
|
||||
}
|
||||
val photoUrisList = remember {
|
||||
val photoUrisList = rememberSaveable(saver = StringListSaver) {
|
||||
mutableStateListOf<String>().apply {
|
||||
if (initialMeal != null) {
|
||||
addAll(initialMeal.displayPhotoUris)
|
||||
@@ -104,6 +125,14 @@ fun MealEntrySheet(
|
||||
}
|
||||
}
|
||||
|
||||
// Deferred photo management
|
||||
val photosMarkedForDeletion = rememberSaveable(saver = StringListSaver) {
|
||||
mutableStateListOf<String>()
|
||||
}
|
||||
val newlyCapturedPhotos = rememberSaveable(saver = StringListSaver) {
|
||||
mutableStateListOf<String>()
|
||||
}
|
||||
|
||||
// Current food builder input state
|
||||
var foodNameInput by rememberSaveable { mutableStateOf("") }
|
||||
var foodWeightInput by rememberSaveable { mutableStateOf("") }
|
||||
@@ -177,14 +206,23 @@ fun MealEntrySheet(
|
||||
val savedUri = ImageUtils.saveImageToPrivateStorage(context, uri)
|
||||
if (savedUri != null && photoUrisList.size < 4) {
|
||||
photoUrisList.add(savedUri)
|
||||
newlyCapturedPhotos.add(savedUri)
|
||||
}
|
||||
isProcessingPhoto = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val handleDismiss = {
|
||||
// Clean up only uncommitted new photos; do NOT delete existing saved photos
|
||||
newlyCapturedPhotos.forEach { photoUri ->
|
||||
ImageUtils.deletePhotoFile(context, photoUri)
|
||||
}
|
||||
onDismiss()
|
||||
}
|
||||
|
||||
ModalBottomSheet(
|
||||
onDismissRequest = onDismiss,
|
||||
onDismissRequest = handleDismiss,
|
||||
sheetState = sheetState,
|
||||
containerColor = MaterialTheme.colorScheme.surface,
|
||||
dragHandle = null
|
||||
@@ -215,7 +253,7 @@ fun MealEntrySheet(
|
||||
)
|
||||
}
|
||||
IconButton(
|
||||
onClick = onDismiss,
|
||||
onClick = handleDismiss,
|
||||
modifier = Modifier.testTag("dismiss_sheet_button")
|
||||
) {
|
||||
Icon(
|
||||
@@ -254,8 +292,8 @@ fun MealEntrySheet(
|
||||
)
|
||||
},
|
||||
onDeletePhotoClick = { photoUri ->
|
||||
ImageUtils.deletePhotoFile(photoUri)
|
||||
photoUrisList.remove(photoUri)
|
||||
photosMarkedForDeletion.add(photoUri)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -333,7 +371,7 @@ fun MealEntrySheet(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
TextButton(
|
||||
onClick = onDismiss,
|
||||
onClick = handleDismiss,
|
||||
modifier = Modifier.testTag("cancel_meal_button")
|
||||
) {
|
||||
Text("Annuler")
|
||||
@@ -344,6 +382,12 @@ fun MealEntrySheet(
|
||||
if (foodNameInput.trim().isNotEmpty()) {
|
||||
addCurrentFoodItem()
|
||||
}
|
||||
// Physically delete only photos marked for deletion that were not re-added
|
||||
photosMarkedForDeletion.forEach { photoUri ->
|
||||
if (!photoUrisList.contains(photoUri)) {
|
||||
ImageUtils.deletePhotoFile(context, photoUri)
|
||||
}
|
||||
}
|
||||
onSaveMeal(
|
||||
selectedType,
|
||||
mealTimestamp,
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,195 @@
|
||||
package com.example.ui.settings.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ExitToApp
|
||||
import androidx.compose.material.icons.automirrored.filled.Login
|
||||
import androidx.compose.material.icons.filled.AccountCircle
|
||||
import androidx.compose.material.icons.filled.CloudDone
|
||||
import androidx.compose.material.icons.filled.CloudOff
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.google.firebase.auth.FirebaseUser
|
||||
|
||||
@Composable
|
||||
fun AccountAndSecuritySection(
|
||||
currentUser: FirebaseUser?,
|
||||
onSignInClick: () -> Unit,
|
||||
onSignOut: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
Card(
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(20.dp)) {
|
||||
if (currentUser != null) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Surface(
|
||||
modifier = Modifier.size(40.dp),
|
||||
shape = CircleShape,
|
||||
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.15f)
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.AccountCircle,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = currentUser.displayName ?: "Compte Google",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = currentUser.email ?: "Synchronisation Cloud Firestore active",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.4f),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CloudDone,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(20.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = "Base de données cloud sécurisée • Synchronisation continue",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Button(
|
||||
onClick = onSignOut,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("signout_button"),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.error.copy(alpha = 0.15f),
|
||||
contentColor = MaterialTheme.colorScheme.error
|
||||
)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.ExitToApp,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text("Passer en mode local uniquement (Déconnexion)")
|
||||
}
|
||||
} else {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Surface(
|
||||
modifier = Modifier.size(40.dp),
|
||||
shape = CircleShape,
|
||||
color = MaterialTheme.colorScheme.secondaryContainer
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CloudOff,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onSecondaryContainer,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "Mode hors-ligne local actif",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Données stockées sur cet appareil sans aucun compte",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
Text(
|
||||
text = "Vous pouvez utiliser Satiéa librement en local. La connexion Google est optionnelle pour sauvegarder et synchroniser vos repas entre appareils.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Button(
|
||||
onClick = onSignInClick,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("signin_google_optional_button"),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.Login,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text("Se connecter avec Google (Optionnel)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.example.ui.settings.components
|
||||
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Brightness4
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.FilterChip
|
||||
import androidx.compose.material3.FilterChipDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.ui.theme.AppThemeMode
|
||||
|
||||
@Composable
|
||||
fun AppearanceThemeSection(
|
||||
currentThemeMode: AppThemeMode,
|
||||
onThemeModeChange: (AppThemeMode) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
Card(
|
||||
modifier = modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(20.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Surface(
|
||||
modifier = Modifier.size(40.dp),
|
||||
shape = CircleShape,
|
||||
color = MaterialTheme.colorScheme.secondaryContainer
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Brightness4,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onSecondaryContainer,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "Apparence visuelle",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Mode sombre élégant par défaut pour le repos des yeux",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
AppThemeMode.entries.forEach { mode ->
|
||||
val isSelected = currentThemeMode == mode
|
||||
FilterChip(
|
||||
selected = isSelected,
|
||||
onClick = { onThemeModeChange(mode) },
|
||||
label = {
|
||||
Text(
|
||||
text = mode.displayName,
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
)
|
||||
},
|
||||
colors = FilterChipDefaults.filterChipColors(
|
||||
selectedContainerColor = MaterialTheme.colorScheme.primaryContainer,
|
||||
selectedLabelColor = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
),
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 4.dp)
|
||||
.testTag("theme_chip_${mode.name.lowercase()}")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package com.example.ui.settings.components
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.FileDownload
|
||||
import androidx.compose.material.icons.filled.FileUpload
|
||||
import androidx.compose.material.icons.filled.Share
|
||||
import androidx.compose.material.icons.filled.Storage
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
@Composable
|
||||
fun DataExportImportSection(
|
||||
totalMealsCount: Int,
|
||||
onImportCsvClick: () -> Unit,
|
||||
onSaveCsvClick: () -> Unit,
|
||||
onShareCsvClick: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
Card(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("settings_csv_export_card"),
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(20.dp)) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Surface(
|
||||
modifier = Modifier.size(40.dp),
|
||||
shape = CircleShape,
|
||||
color = MaterialTheme.colorScheme.primaryContainer
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Storage,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "Base locale & Fichiers CSV",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Base SQLite locale active • $totalMealsCount repas sur l'appareil",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Button(
|
||||
onClick = onImportCsvClick,
|
||||
modifier = Modifier
|
||||
.weight(1.1f)
|
||||
.testTag("settings_import_csv_button"),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.secondaryContainer,
|
||||
contentColor = MaterialTheme.colorScheme.onSecondaryContainer
|
||||
)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.FileUpload,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(4.dp))
|
||||
Text("Importer", maxLines = 1)
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = onSaveCsvClick,
|
||||
enabled = totalMealsCount > 0,
|
||||
modifier = Modifier
|
||||
.weight(1.1f)
|
||||
.testTag("settings_save_csv_button"),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.primary,
|
||||
contentColor = MaterialTheme.colorScheme.onPrimary
|
||||
)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.FileDownload,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(4.dp))
|
||||
Text("Sauver", maxLines = 1)
|
||||
}
|
||||
|
||||
OutlinedButton(
|
||||
onClick = onShareCsvClick,
|
||||
enabled = totalMealsCount > 0,
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.testTag("settings_share_csv_button"),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Share,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(4.dp))
|
||||
Text("Partager", maxLines = 1)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Text(
|
||||
text = "L'importation lit vos fichiers CSV (Excel/Sheets), fusionne les repas sans doublon et enrichit automatiquement votre catalogue d'aliments.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.8f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+332
@@ -0,0 +1,332 @@
|
||||
package com.example.ui.settings.components
|
||||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
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.ErrorOutline
|
||||
import androidx.compose.material.icons.filled.Sync
|
||||
import androidx.compose.material.icons.filled.TableChart
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.SwitchDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.example.ui.journal.GoogleSheetsUiState
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Locale
|
||||
|
||||
@Composable
|
||||
fun GoogleSheetsIntegrationSection(
|
||||
sheetsUiState: GoogleSheetsUiState,
|
||||
isAutoSyncSheets: Boolean,
|
||||
spreadsheetUrl: String?,
|
||||
onSyncSheetsClick: () -> Unit,
|
||||
onAutoSyncSheetsChange: (Boolean) -> Unit,
|
||||
onOpenSheetsClick: (String) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
Card(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("google_sheets_card"),
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(20.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier.size(42.dp),
|
||||
shape = CircleShape,
|
||||
color = Color(0xFF0F9D58).copy(alpha = 0.15f)
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.TableChart,
|
||||
contentDescription = null,
|
||||
tint = Color(0xFF0F9D58),
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "Google Sheets & Drive",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Synchronisation automatique dans votre cloud",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
// State status display
|
||||
when (sheetsUiState) {
|
||||
is GoogleSheetsUiState.Syncing -> {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(18.dp),
|
||||
strokeWidth = 2.dp,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Spacer(modifier = Modifier.width(10.dp))
|
||||
Text(
|
||||
text = "Synchronisation Google Sheets en cours...",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
is GoogleSheetsUiState.Success -> {
|
||||
val dateFormatted = try {
|
||||
val zdt = Instant.ofEpochMilli(sheetsUiState.timestamp).atZone(ZoneId.systemDefault())
|
||||
DateTimeFormatter.ofPattern("dd/MM à HH:mm", Locale.FRENCH).format(zdt)
|
||||
} catch (_: Exception) {
|
||||
""
|
||||
}
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = Color(0xFF0F9D58).copy(alpha = 0.12f),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CheckCircle,
|
||||
contentDescription = null,
|
||||
tint = Color(0xFF0F9D58),
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = "${sheetsUiState.count} repas synchronisés ($dateFormatted)",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Medium
|
||||
)
|
||||
}
|
||||
}
|
||||
is GoogleSheetsUiState.Error -> {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.errorContainer.copy(alpha = 0.5f),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.ErrorOutline,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = sheetsUiState.message,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
}
|
||||
}
|
||||
GoogleSheetsUiState.Idle -> {
|
||||
if (spreadsheetUrl != null) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CheckCircle,
|
||||
contentDescription = null,
|
||||
tint = Color(0xFF0F9D58),
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = "Classeur associé et prêt",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.4f),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = "Classeur non encore créé ou synchronisé",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
// Auto sync toggle
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.35f),
|
||||
shape = RoundedCornerShape(14.dp)
|
||||
)
|
||||
.padding(12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = "Envoi automatique à chaque repas",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
Text(
|
||||
text = "Met à jour votre Google Sheet à l'ajout ou la modification d'un repas",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
lineHeight = 16.sp
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
|
||||
Switch(
|
||||
checked = isAutoSyncSheets,
|
||||
onCheckedChange = onAutoSyncSheetsChange,
|
||||
modifier = Modifier.testTag("switch_auto_sync_sheets"),
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedThumbColor = Color(0xFF0F9D58),
|
||||
checkedTrackColor = Color(0xFF0F9D58).copy(alpha = 0.4f)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
// Buttons row
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Button(
|
||||
onClick = onSyncSheetsClick,
|
||||
enabled = sheetsUiState !is GoogleSheetsUiState.Syncing,
|
||||
modifier = Modifier
|
||||
.weight(1.2f)
|
||||
.testTag("sync_sheets_button"),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = Color(0xFF0F9D58),
|
||||
contentColor = Color.White
|
||||
)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Sync,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(6.dp))
|
||||
Text("Synchroniser", maxLines = 1)
|
||||
}
|
||||
|
||||
OutlinedButton(
|
||||
onClick = { spreadsheetUrl?.let { onOpenSheetsClick(it) } },
|
||||
enabled = spreadsheetUrl != null,
|
||||
modifier = Modifier
|
||||
.weight(1.1f)
|
||||
.testTag("open_sheets_button"),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.OpenInNew,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(6.dp))
|
||||
Text("Ouvrir ↗", maxLines = 1)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
Text(
|
||||
text = "Un classeur dédié « Satiéa — Journal des Repas » est géré automatiquement dans votre Google Drive personnel avec en-têtes et données ordonnées.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.8f)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package com.example.ui.settings.components
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.provider.Settings
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Notifications
|
||||
import androidx.compose.material.icons.filled.NotificationsActive
|
||||
import androidx.compose.material.icons.filled.NotificationsOff
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.SwitchDefaults
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
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.platform.LocalContext
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.compose.LifecycleEventEffect
|
||||
import com.example.data.model.MealType
|
||||
import com.example.data.preferences.ReminderPreferencesManager
|
||||
|
||||
@Composable
|
||||
fun NotificationRemindersSection(
|
||||
onScheduleReminder: (MealType, Int, Int, Boolean) -> Unit,
|
||||
onTestNotification: (MealType) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val prefs = remember { ReminderPreferencesManager.getInstance(context) }
|
||||
|
||||
var notificationsEnabledBySystem by remember {
|
||||
mutableStateOf(NotificationManagerCompat.from(context).areNotificationsEnabled())
|
||||
}
|
||||
|
||||
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
|
||||
notificationsEnabledBySystem = NotificationManagerCompat.from(context).areNotificationsEnabled()
|
||||
}
|
||||
|
||||
val permissionLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.RequestPermission()
|
||||
) { isGranted ->
|
||||
notificationsEnabledBySystem = isGranted || NotificationManagerCompat.from(context).areNotificationsEnabled()
|
||||
}
|
||||
|
||||
val reminderStates = remember {
|
||||
mutableStateMapOf<MealType, Boolean>().apply {
|
||||
MealType.entries.forEach { mealType ->
|
||||
put(mealType, prefs.isReminderEnabled(mealType))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("settings_reminders_card"),
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(20.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier.size(40.dp),
|
||||
shape = CircleShape,
|
||||
color = MaterialTheme.colorScheme.primaryContainer
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.NotificationsActive,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "Rappels push personnalisés",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Une notification discrète au moment de manger",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (!notificationsEnabledBySystem) {
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.errorContainer.copy(alpha = 0.45f)
|
||||
),
|
||||
shape = RoundedCornerShape(14.dp),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(14.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.NotificationsOff,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.error,
|
||||
modifier = Modifier.size(24.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(10.dp))
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = "Notifications bloquées",
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Autorisez les notifications pour recevoir les alertes de repas.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onErrorContainer
|
||||
)
|
||||
}
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Button(
|
||||
onClick = {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
} else {
|
||||
try {
|
||||
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
|
||||
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
|
||||
}
|
||||
context.startActivity(intent)
|
||||
} catch (_: Exception) {}
|
||||
}
|
||||
},
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.error
|
||||
),
|
||||
shape = RoundedCornerShape(10.dp)
|
||||
) {
|
||||
Text("Activer")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(18.dp))
|
||||
|
||||
MealType.entries.forEach { mealType ->
|
||||
val isEnabled = reminderStates[mealType] ?: false
|
||||
val (hour, minute) = prefs.getReminderTime(mealType)
|
||||
val formattedTime = String.format("%02dh%02d", hour, minute)
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 8.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column {
|
||||
Text(
|
||||
text = mealType.displayName,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.SemiBold
|
||||
)
|
||||
Text(
|
||||
text = "Heure de rappel: $formattedTime",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Switch(
|
||||
checked = isEnabled,
|
||||
onCheckedChange = { checked ->
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && checked && !notificationsEnabledBySystem) {
|
||||
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
reminderStates[mealType] = checked
|
||||
prefs.setReminderEnabled(mealType, checked)
|
||||
onScheduleReminder(mealType, hour, minute, checked)
|
||||
},
|
||||
colors = SwitchDefaults.colors(
|
||||
checkedThumbColor = MaterialTheme.colorScheme.primary,
|
||||
checkedTrackColor = MaterialTheme.colorScheme.primaryContainer
|
||||
),
|
||||
modifier = Modifier.testTag("switch_${mealType.name.lowercase()}")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && !notificationsEnabledBySystem) {
|
||||
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
} else {
|
||||
onTestNotification(MealType.LUNCH)
|
||||
}
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("test_notification_button"),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Notifications,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text("Tester une notification maintenant")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user