mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
build: initialize project structure
Set up Android project configuration, Gradle build scripts, typography, and default resource assets.
This commit is contained in:
1 parent
0a595f619a
commit
f13d46f778
65 files changed
+6056
-11
No files matched your search
@@ -0,0 +1,326 @@
|
||||
package com.example.ui.auth
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
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.fillMaxSize
|
||||
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.CloudDone
|
||||
import androidx.compose.material.icons.filled.Eco
|
||||
import androidx.compose.material.icons.filled.Restaurant
|
||||
import androidx.compose.material.icons.filled.Timeline
|
||||
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.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.credentials.CredentialManager
|
||||
import androidx.credentials.CustomCredential
|
||||
import androidx.credentials.GetCredentialRequest
|
||||
import androidx.credentials.exceptions.GetCredentialCancellationException
|
||||
import com.example.R
|
||||
import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential.Companion.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL
|
||||
import com.google.firebase.Firebase
|
||||
import com.google.firebase.auth.GoogleAuthProvider
|
||||
import com.google.firebase.auth.auth
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.tasks.await
|
||||
|
||||
@Composable
|
||||
fun AuthScreen(
|
||||
onAuthSuccess: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
val credentialManager = remember { CredentialManager.create(context) }
|
||||
var isLoading by remember { mutableStateOf(false) }
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(horizontal = 28.dp, vertical = 32.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Spacer(modifier = Modifier.height(24.dp))
|
||||
|
||||
// Logo & Hero Section
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier
|
||||
.size(88.dp)
|
||||
.clip(CircleShape),
|
||||
color = MaterialTheme.colorScheme.primaryContainer
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Restaurant,
|
||||
contentDescription = "Logo Satiéa",
|
||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
modifier = Modifier.size(44.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
|
||||
Text(
|
||||
text = "Satiéa",
|
||||
style = MaterialTheme.typography.displayMedium,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Text(
|
||||
text = "Journal de repas & écoute de soi",
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.8f),
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
|
||||
// Benefits cards
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
verticalArrangement = Arrangement.spacedBy(14.dp)
|
||||
) {
|
||||
FeaturePill(
|
||||
icon = Icons.Default.Eco,
|
||||
title = "Sensations de faim & satiété",
|
||||
subtitle = "Évaluez votre ressenti avant et après chaque repas en 10 secondes."
|
||||
)
|
||||
FeaturePill(
|
||||
icon = Icons.Default.Timeline,
|
||||
title = "Statistiques 7 jours",
|
||||
subtitle = "Suivez votre équilibre digestif et la régularité de vos habitudes."
|
||||
)
|
||||
FeaturePill(
|
||||
icon = Icons.Default.CloudDone,
|
||||
title = "Synchronisation Cloud sécurisée",
|
||||
subtitle = "Vos repas et photos d'assiettes accessibles sur tous vos appareils."
|
||||
)
|
||||
}
|
||||
|
||||
// Sign In Button
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Button(
|
||||
onClick = {
|
||||
isLoading = true
|
||||
onGoogleSignInClicked(
|
||||
context = context,
|
||||
credentialManager = credentialManager,
|
||||
onAuthSuccess = {
|
||||
isLoading = false
|
||||
onAuthSuccess()
|
||||
},
|
||||
onAuthError = { errorMsg ->
|
||||
isLoading = false
|
||||
coroutineScope.launch {
|
||||
snackbarHostState.showSnackbar(errorMsg)
|
||||
}
|
||||
},
|
||||
scope = coroutineScope,
|
||||
onAuthCancelled = {
|
||||
isLoading = false
|
||||
}
|
||||
)
|
||||
},
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(54.dp)
|
||||
.testTag("google_signin_button"),
|
||||
shape = RoundedCornerShape(16.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.primary,
|
||||
contentColor = MaterialTheme.colorScheme.onPrimary
|
||||
),
|
||||
enabled = !isLoading
|
||||
) {
|
||||
if (isLoading) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(24.dp),
|
||||
color = MaterialTheme.colorScheme.onPrimary,
|
||||
strokeWidth = 2.dp
|
||||
)
|
||||
} else {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CloudDone,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(20.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(10.dp))
|
||||
Text(
|
||||
text = "Continuer avec Google",
|
||||
style = MaterialTheme.typography.titleMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
Text(
|
||||
text = "Connexion sécurisée via Google • Sans publicité",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.5f),
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
SnackbarHost(
|
||||
hostState = snackbarHostState,
|
||||
modifier = Modifier.align(Alignment.BottomCenter)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FeaturePill(
|
||||
icon: ImageVector,
|
||||
title: String,
|
||||
subtitle: String
|
||||
) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(18.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.6f)
|
||||
)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier.size(40.dp),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surface
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(14.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = title,
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
Text(
|
||||
text = subtitle,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
lineHeight = 16.sp
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onGoogleSignInClicked(
|
||||
context: Context,
|
||||
credentialManager: CredentialManager,
|
||||
onAuthSuccess: () -> Unit,
|
||||
onAuthError: (String) -> Unit,
|
||||
scope: CoroutineScope,
|
||||
onAuthCancelled: () -> Unit = {}
|
||||
) {
|
||||
val clientId = try {
|
||||
context.getString(R.string.default_web_client_id)
|
||||
} catch (e: Exception) {
|
||||
onAuthError("Configuration Google manquante (default_web_client_id)")
|
||||
return
|
||||
}
|
||||
|
||||
val signInOption = GetSignInWithGoogleOption.Builder(serverClientId = clientId).build()
|
||||
val request = GetCredentialRequest.Builder().addCredentialOption(signInOption).build()
|
||||
|
||||
scope.launch {
|
||||
try {
|
||||
val activity = context as? Activity
|
||||
if (activity == null) {
|
||||
onAuthError("Contexte invalide pour la connexion")
|
||||
return@launch
|
||||
}
|
||||
val result = credentialManager.getCredential(activity, request)
|
||||
val credential = result.credential
|
||||
if (credential is CustomCredential && credential.type == TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
|
||||
val googleIdToken = GoogleIdTokenCredential.createFrom(credential.data).idToken
|
||||
val authCredential = GoogleAuthProvider.getCredential(googleIdToken, null)
|
||||
Firebase.auth.signInWithCredential(authCredential).await()
|
||||
onAuthSuccess()
|
||||
} else {
|
||||
onAuthError("Type d'identifiant non reconnu")
|
||||
}
|
||||
} catch (e: GetCredentialCancellationException) {
|
||||
Log.w("Auth", "Connexion Google annulée: ${e.message}", e)
|
||||
onAuthCancelled()
|
||||
} catch (e: Exception) {
|
||||
Log.e("Auth", "Échec de la connexion Google", e)
|
||||
onAuthError(e.localizedMessage ?: "Échec de connexion")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,633 @@
|
||||
package com.example.ui.journal
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||
import androidx.compose.foundation.layout.FlowRow
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
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.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.CalendarMonth
|
||||
import androidx.compose.material.icons.filled.Delete
|
||||
import androidx.compose.material.icons.filled.Edit
|
||||
import androidx.compose.material.icons.filled.FileDownload
|
||||
import androidx.compose.material.icons.filled.MoreVert
|
||||
import androidx.compose.material.icons.filled.Restaurant
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
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.draw.clip
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.data.model.HungerSatietyScale
|
||||
import com.example.data.model.MealEntry
|
||||
import com.example.data.model.MealType
|
||||
import com.example.util.ImageUtils
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
@Composable
|
||||
fun JournalScreen(
|
||||
mealsUiState: MealsUiState,
|
||||
onAddMealClick: () -> Unit,
|
||||
onEditMeal: (MealEntry) -> Unit,
|
||||
onDeleteMeal: (String) -> Unit,
|
||||
onExportCsvClick: (List<MealEntry>) -> Unit
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
) {
|
||||
when (mealsUiState) {
|
||||
is MealsUiState.Loading -> {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
CircularProgressIndicator(
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.testTag("journal_loading_indicator")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is MealsUiState.Error -> {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(24.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(
|
||||
text = mealsUiState.message,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is MealsUiState.Success -> {
|
||||
val meals = mealsUiState.meals
|
||||
val todayCalendar = Calendar.getInstance().apply {
|
||||
set(Calendar.HOUR_OF_DAY, 0)
|
||||
set(Calendar.MINUTE, 0)
|
||||
set(Calendar.SECOND, 0)
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
}
|
||||
val todayStartMillis = todayCalendar.timeInMillis
|
||||
val todayEndMillis = todayStartMillis + (24L * 60 * 60 * 1000) - 1
|
||||
val todayMeals = meals.filter { it.mealTimestamp in todayStartMillis..todayEndMillis }
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.testTag("journal_meals_list"),
|
||||
contentPadding = PaddingValues(
|
||||
start = 20.dp,
|
||||
end = 20.dp,
|
||||
top = 16.dp,
|
||||
bottom = 96.dp
|
||||
),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
item {
|
||||
DailyOverviewCard(todayMeals = todayMeals)
|
||||
}
|
||||
|
||||
if (meals.isEmpty()) {
|
||||
item {
|
||||
EmptyMealsView(onAddMealClick = onAddMealClick)
|
||||
}
|
||||
} else {
|
||||
item {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(top = 6.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = "Historique des repas (${meals.size})",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
TextButton(
|
||||
onClick = { onExportCsvClick(meals) },
|
||||
modifier = Modifier.testTag("journal_export_csv_button")
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.FileDownload,
|
||||
contentDescription = "Exporter en CSV",
|
||||
modifier = Modifier.size(18.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Spacer(modifier = Modifier.width(6.dp))
|
||||
Text(
|
||||
text = "Export CSV",
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
items(meals, key = { it.id.ifEmpty { it.mealTimestamp.toString() } }) { meal ->
|
||||
MealCardItem(
|
||||
meal = meal,
|
||||
onEdit = { onEditMeal(meal) },
|
||||
onDelete = { onDeleteMeal(meal.id) }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FloatingActionButton(
|
||||
onClick = onAddMealClick,
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.padding(24.dp)
|
||||
.testTag("add_meal_fab"),
|
||||
containerColor = MaterialTheme.colorScheme.primary,
|
||||
contentColor = MaterialTheme.colorScheme.onPrimary,
|
||||
shape = CircleShape
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Add,
|
||||
contentDescription = "Ajouter un repas",
|
||||
modifier = Modifier.size(28.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun DailyOverviewCard(todayMeals: List<MealEntry>) {
|
||||
val dateFormat = remember { SimpleDateFormat("EEEE d MMMM", Locale.FRENCH) }
|
||||
val todayFormatted = remember {
|
||||
dateFormat.format(Date()).replaceFirstChar {
|
||||
if (it.isLowerCase()) it.titlecase(Locale.FRENCH) else it.toString()
|
||||
}
|
||||
}
|
||||
|
||||
val avgSatiety = if (todayMeals.isNotEmpty()) {
|
||||
todayMeals.map { it.satietyAfter }.average().toFloat()
|
||||
} else 0f
|
||||
|
||||
val comfortStatus = when {
|
||||
todayMeals.isEmpty() -> "En attente du premier repas"
|
||||
avgSatiety in 5.5f..8.2f -> "Zone de confort optimale (6–8/10) ✨"
|
||||
avgSatiety > 8.2f -> "Tendance aux repas très copieux (>8/10)"
|
||||
else -> "Sensations de faim encore présentes (<6/10)"
|
||||
}
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("daily_overview_card"),
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.7f)
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(20.dp)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = todayFormatted,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
Surface(
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f)
|
||||
) {
|
||||
Text(
|
||||
text = "${todayMeals.size} repas aujourd'hui",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.padding(horizontal = 10.dp, vertical = 5.dp),
|
||||
fontWeight = FontWeight.SemiBold
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.Bottom
|
||||
) {
|
||||
Column {
|
||||
Text(
|
||||
text = "Satiété moyenne aujourd'hui",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = if (todayMeals.isNotEmpty()) String.format(Locale.FRENCH, "%.1f / 10", avgSatiety) else "— / 10",
|
||||
style = MaterialTheme.typography.displayMedium,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
text = comfortStatus,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.secondary,
|
||||
fontWeight = FontWeight.Medium,
|
||||
textAlign = TextAlign.End,
|
||||
modifier = Modifier.width(160.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalLayoutApi::class)
|
||||
@Composable
|
||||
private fun MealCardItem(
|
||||
meal: MealEntry,
|
||||
onEdit: () -> Unit,
|
||||
onDelete: () -> Unit
|
||||
) {
|
||||
var showMenu by remember { mutableStateOf(false) }
|
||||
var showDeleteDialog by remember { mutableStateOf(false) }
|
||||
val dateTimeFormat = remember { SimpleDateFormat("EEE d MMM yyyy • HH:mm", Locale.FRENCH) }
|
||||
val mealType = MealType.fromString(meal.mealType)
|
||||
val parsedFoods = remember(meal.foodItemsSerialized) { meal.parsedFoodItems() }
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.clickable { onEdit() }
|
||||
.testTag("meal_card_${meal.id}"),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(18.dp)
|
||||
) {
|
||||
// Top row: Type, Date & Time, Options
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.weight(1f)
|
||||
) {
|
||||
Surface(
|
||||
shape = RoundedCornerShape(10.dp),
|
||||
color = MaterialTheme.colorScheme.primaryContainer
|
||||
) {
|
||||
Text(
|
||||
text = mealType.displayName,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp),
|
||||
fontWeight = FontWeight.SemiBold
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(10.dp))
|
||||
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(8.dp))
|
||||
.clickable { onEdit() }
|
||||
.padding(horizontal = 6.dp, vertical = 4.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.CalendarMonth,
|
||||
contentDescription = "Modifier la date",
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(14.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(4.dp))
|
||||
Text(
|
||||
text = dateTimeFormat.format(Date(meal.mealTimestamp)).replaceFirstChar {
|
||||
if (it.isLowerCase()) it.titlecase(Locale.FRENCH) else it.toString()
|
||||
},
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Box {
|
||||
IconButton(
|
||||
onClick = { showMenu = true },
|
||||
modifier = Modifier.size(32.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.MoreVert,
|
||||
contentDescription = "Options du repas",
|
||||
tint = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
DropdownMenu(
|
||||
expanded = showMenu,
|
||||
onDismissRequest = { showMenu = false }
|
||||
) {
|
||||
DropdownMenuItem(
|
||||
text = { Text("Corriger date / aliments / note") },
|
||||
leadingIcon = { Icon(Icons.Default.Edit, contentDescription = null) },
|
||||
onClick = {
|
||||
showMenu = false
|
||||
onEdit()
|
||||
}
|
||||
)
|
||||
DropdownMenuItem(
|
||||
text = { Text("Supprimer", color = MaterialTheme.colorScheme.error) },
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
Icons.Default.Delete,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.error
|
||||
)
|
||||
},
|
||||
onClick = {
|
||||
showMenu = false
|
||||
showDeleteDialog = true
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
// Dual Gauge: Hunger /10 -> Satiety /10
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(
|
||||
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.4f),
|
||||
shape = RoundedCornerShape(14.dp)
|
||||
)
|
||||
.padding(horizontal = 14.dp, vertical = 10.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = "Faim avant",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Text(
|
||||
text = "${meal.hungerBefore} / 10",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.secondary,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = HungerSatietyScale.getHungerLabel(meal.hungerBefore),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
text = "→",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier.padding(horizontal = 8.dp)
|
||||
)
|
||||
|
||||
Column(
|
||||
modifier = Modifier.weight(1f),
|
||||
horizontalAlignment = Alignment.End
|
||||
) {
|
||||
Text(
|
||||
text = "Satiété après",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Text(
|
||||
text = "${meal.satietyAfter} / 10",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = HungerSatietyScale.getSatietyLabel(meal.satietyAfter),
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
textAlign = TextAlign.End
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Food Items Badges (with optional weights)
|
||||
if (parsedFoods.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Text(
|
||||
text = "Composition de l'assiette",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Spacer(modifier = Modifier.height(6.dp))
|
||||
FlowRow(
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(6.dp),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
parsedFoods.forEach { food ->
|
||||
Surface(
|
||||
shape = RoundedCornerShape(10.dp),
|
||||
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.14f)
|
||||
) {
|
||||
Text(
|
||||
text = food.displayBadge(),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
fontWeight = FontWeight.Medium,
|
||||
modifier = Modifier.padding(horizontal = 10.dp, vertical = 5.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Photos Carousel
|
||||
if (meal.photosBase64.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
LazyRow(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
items(meal.photosBase64) { b64 ->
|
||||
val bitmap: Bitmap? = remember(b64) { ImageUtils.base64ToBitmap(b64) }
|
||||
if (bitmap != null) {
|
||||
Image(
|
||||
bitmap = bitmap.asImageBitmap(),
|
||||
contentDescription = "Photo assiette",
|
||||
modifier = Modifier
|
||||
.size(76.dp)
|
||||
.clip(RoundedCornerShape(12.dp)),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Global Note
|
||||
if (meal.note.isNotBlank()) {
|
||||
Spacer(modifier = Modifier.height(12.dp))
|
||||
Text(
|
||||
text = "Note globale : « ${meal.note} »",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.9f),
|
||||
fontStyle = androidx.compose.ui.text.font.FontStyle.Italic
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showDeleteDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showDeleteDialog = false },
|
||||
title = { Text("Supprimer ce repas ?") },
|
||||
text = { Text("Cette action est irréversible et supprimera le repas de votre historique synchronisé.") },
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
showDeleteDialog = false
|
||||
onDelete()
|
||||
}
|
||||
) {
|
||||
Text("Supprimer", color = MaterialTheme.colorScheme.error)
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showDeleteDialog = false }) {
|
||||
Text("Annuler")
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EmptyMealsView(onAddMealClick: () -> Unit) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 40.dp, horizontal = 20.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier.size(80.dp),
|
||||
shape = CircleShape,
|
||||
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Restaurant,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(38.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
|
||||
Text(
|
||||
text = "Aucun repas pour le moment",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Text(
|
||||
text = "Prenez 15 secondes pour noter vos aliments (avec ou sans poids), votre faim et votre satiété sur 10.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.7f),
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
|
||||
TextButton(
|
||||
onClick = onAddMealClick,
|
||||
modifier = Modifier.testTag("empty_state_add_button")
|
||||
) {
|
||||
Text(
|
||||
text = "+ Noter mon premier repas",
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,308 @@
|
||||
package com.example.ui.journal
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.data.model.CustomFoodEntry
|
||||
import com.example.data.model.DefaultFoodCatalog
|
||||
import com.example.data.model.MealEntry
|
||||
import com.example.data.model.MealFoodItem
|
||||
import com.example.data.model.MealType
|
||||
import com.example.data.repository.MealRepository
|
||||
import com.example.notification.MealNotificationManager
|
||||
import com.example.ui.theme.AppThemeMode
|
||||
import com.example.util.CsvExporter
|
||||
import com.google.firebase.firestore.FirebaseFirestoreException
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Locale
|
||||
|
||||
sealed interface MealsUiState {
|
||||
object Loading : MealsUiState
|
||||
data class Success(val meals: List<MealEntry>) : MealsUiState
|
||||
data class Error(val message: String) : MealsUiState
|
||||
}
|
||||
|
||||
data class DayStat(
|
||||
val dayLabel: String,
|
||||
val dayShortName: String,
|
||||
val timestampMillis: Long,
|
||||
val avgSatiety: Float,
|
||||
val avgHunger: Float,
|
||||
val mealCount: Int
|
||||
)
|
||||
|
||||
data class WeeklyStats(
|
||||
val dailyStats: List<DayStat>,
|
||||
val averageSatiety: Float,
|
||||
val averageHunger: Float,
|
||||
val optimalZonePercentage: Int,
|
||||
val totalMealsLast7Days: Int,
|
||||
val mealTypeDistribution: Map<MealType, Int>
|
||||
)
|
||||
|
||||
class MealViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
private val repository = MealRepository(application)
|
||||
private val context = application.applicationContext
|
||||
|
||||
val mealsState: StateFlow<MealsUiState> = repository.observeMeals()
|
||||
.map<List<MealEntry>, MealsUiState> { meals ->
|
||||
MealsUiState.Success(meals)
|
||||
}
|
||||
.catch { error ->
|
||||
val firestoreError = generateSequence(error) { it.cause }
|
||||
.filterIsInstance<FirebaseFirestoreException>()
|
||||
.firstOrNull()
|
||||
if (firestoreError == null && error is CancellationException) throw error
|
||||
val rootError = firestoreError ?: error
|
||||
emit(MealsUiState.Error(rootError.message ?: "Impossible de charger les repas"))
|
||||
}
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000L),
|
||||
initialValue = MealsUiState.Loading
|
||||
)
|
||||
|
||||
val customFoods: StateFlow<List<CustomFoodEntry>> = repository.observeCustomFoods()
|
||||
.catch { error ->
|
||||
val firestoreError = generateSequence(error) { it.cause }
|
||||
.filterIsInstance<FirebaseFirestoreException>()
|
||||
.firstOrNull()
|
||||
if (firestoreError == null && error is CancellationException) throw error
|
||||
emit(emptyList())
|
||||
}
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000L),
|
||||
initialValue = emptyList()
|
||||
)
|
||||
|
||||
private val _themeMode = MutableStateFlow(AppThemeMode.DARK)
|
||||
val themeMode: StateFlow<AppThemeMode> = _themeMode.asStateFlow()
|
||||
|
||||
fun setThemeMode(mode: AppThemeMode) {
|
||||
_themeMode.value = mode
|
||||
}
|
||||
|
||||
private fun enrichCatalogWithFoodItems(foodItems: List<MealFoodItem>) {
|
||||
val existingBuiltIn = DefaultFoodCatalog.builtInFoods.map { it.name.lowercase(Locale.FRENCH) }.toSet()
|
||||
val existingCustom = customFoods.value.map { it.name.lowercase(Locale.FRENCH) }.toSet()
|
||||
|
||||
viewModelScope.launch {
|
||||
foodItems.forEach { item ->
|
||||
val cleanName = item.name.trim()
|
||||
val normalized = cleanName.lowercase(Locale.FRENCH)
|
||||
if (cleanName.isNotEmpty() && normalized !in existingBuiltIn && normalized !in existingCustom) {
|
||||
repository.saveCustomFood(cleanName, item.unit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun addCustomFoodDirectly(name: String, unit: com.example.data.model.FoodUnit) {
|
||||
val cleanName = name.trim()
|
||||
if (cleanName.isEmpty()) return
|
||||
viewModelScope.launch {
|
||||
repository.saveCustomFood(cleanName, unit)
|
||||
}
|
||||
}
|
||||
|
||||
fun addMeal(
|
||||
mealType: MealType,
|
||||
mealTimestamp: Long,
|
||||
hungerBefore: Int,
|
||||
satietyAfter: Int,
|
||||
note: String,
|
||||
foodItems: List<MealFoodItem>,
|
||||
photosBase64: List<String>,
|
||||
onSuccess: () -> Unit = {},
|
||||
onError: (String) -> Unit = {}
|
||||
) {
|
||||
enrichCatalogWithFoodItems(foodItems)
|
||||
viewModelScope.launch {
|
||||
val entry = MealEntry(
|
||||
mealType = mealType.name,
|
||||
mealTimestamp = mealTimestamp,
|
||||
hungerBefore = hungerBefore.coerceIn(1, 10),
|
||||
satietyAfter = satietyAfter.coerceIn(1, 10),
|
||||
note = note.trim(),
|
||||
foodItemsSerialized = foodItems.map { it.serialize() }.take(30),
|
||||
photosBase64 = photosBase64
|
||||
)
|
||||
val result = repository.addMeal(entry)
|
||||
if (result.isSuccess) {
|
||||
onSuccess()
|
||||
} else {
|
||||
onError(result.exceptionOrNull()?.localizedMessage ?: "Erreur d'enregistrement")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateMeal(
|
||||
meal: MealEntry,
|
||||
foodItems: List<MealFoodItem>,
|
||||
onSuccess: () -> Unit = {},
|
||||
onError: (String) -> Unit = {}
|
||||
) {
|
||||
enrichCatalogWithFoodItems(foodItems)
|
||||
viewModelScope.launch {
|
||||
val sanitizedMeal = meal.copy(
|
||||
hungerBefore = meal.hungerBefore.coerceIn(1, 10),
|
||||
satietyAfter = meal.satietyAfter.coerceIn(1, 10),
|
||||
foodItemsSerialized = foodItems.map { it.serialize() }.take(30)
|
||||
)
|
||||
val result = repository.updateMeal(sanitizedMeal)
|
||||
if (result.isSuccess) {
|
||||
onSuccess()
|
||||
} else {
|
||||
onError(result.exceptionOrNull()?.localizedMessage ?: "Erreur de mise à jour")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteMeal(
|
||||
mealId: String,
|
||||
onSuccess: () -> Unit = {},
|
||||
onError: (String) -> Unit = {}
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
val result = repository.deleteMeal(mealId)
|
||||
if (result.isSuccess) {
|
||||
onSuccess()
|
||||
} else {
|
||||
onError(result.exceptionOrNull()?.localizedMessage ?: "Erreur de suppression")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun exportCsvToUri(
|
||||
targetUri: Uri,
|
||||
meals: List<MealEntry>,
|
||||
onSuccess: (Int) -> Unit,
|
||||
onError: (String) -> Unit
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
val result = CsvExporter.writeCsvToUri(context, targetUri, meals)
|
||||
result.fold(
|
||||
onSuccess = { count -> onSuccess(count) },
|
||||
onFailure = { err -> onError(err.localizedMessage ?: "Erreur lors de l'export CSV") }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun shareCsv(
|
||||
activityContext: Context,
|
||||
meals: List<MealEntry>,
|
||||
onIntentReady: (Intent) -> Unit,
|
||||
onError: (String) -> Unit
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
val result = CsvExporter.shareCsvFile(activityContext, meals)
|
||||
result.fold(
|
||||
onSuccess = { chooserIntent -> onIntentReady(chooserIntent) },
|
||||
onFailure = { err -> onError(err.localizedMessage ?: "Impossible de préparer le partage CSV") }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun scheduleMealReminder(mealType: MealType, hour: Int, minute: Int, enabled: Boolean) {
|
||||
MealNotificationManager.scheduleDailyReminder(context, mealType, hour, minute, enabled)
|
||||
}
|
||||
|
||||
fun triggerTestNotification(mealType: MealType) {
|
||||
MealNotificationManager.showMealNotification(context, mealType)
|
||||
}
|
||||
|
||||
fun computeWeeklyStats(meals: List<MealEntry>): WeeklyStats {
|
||||
val now = System.currentTimeMillis()
|
||||
val sevenDaysAgo = now - (7L * 24 * 60 * 60 * 1000)
|
||||
|
||||
val endOfToday = Calendar.getInstance().apply {
|
||||
set(Calendar.HOUR_OF_DAY, 23)
|
||||
set(Calendar.MINUTE, 59)
|
||||
set(Calendar.SECOND, 59)
|
||||
set(Calendar.MILLISECOND, 999)
|
||||
}.timeInMillis
|
||||
|
||||
val recentMeals = meals.filter { it.mealTimestamp in sevenDaysAgo..endOfToday }
|
||||
|
||||
val dayFormat = SimpleDateFormat("EEE", Locale.FRENCH)
|
||||
val fullDayFormat = SimpleDateFormat("d MMM", Locale.FRENCH)
|
||||
|
||||
val daysList = mutableListOf<DayStat>()
|
||||
|
||||
for (i in 6 downTo 0) {
|
||||
val dayCal = Calendar.getInstance().apply {
|
||||
timeInMillis = now
|
||||
add(Calendar.DAY_OF_YEAR, -i)
|
||||
set(Calendar.HOUR_OF_DAY, 0)
|
||||
set(Calendar.MINUTE, 0)
|
||||
set(Calendar.SECOND, 0)
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
}
|
||||
val startOfDay = dayCal.timeInMillis
|
||||
val endOfDay = startOfDay + (24 * 60 * 60 * 1000) - 1
|
||||
|
||||
val dayMeals = meals.filter { it.mealTimestamp in startOfDay..endOfDay }
|
||||
|
||||
val avgSatiety = if (dayMeals.isNotEmpty()) {
|
||||
dayMeals.map { it.satietyAfter }.average().toFloat()
|
||||
} else 0f
|
||||
|
||||
val avgHunger = if (dayMeals.isNotEmpty()) {
|
||||
dayMeals.map { it.hungerBefore }.average().toFloat()
|
||||
} else 0f
|
||||
|
||||
daysList.add(
|
||||
DayStat(
|
||||
dayLabel = fullDayFormat.format(dayCal.time),
|
||||
dayShortName = dayFormat.format(dayCal.time).replace(".", "").replaceFirstChar {
|
||||
if (it.isLowerCase()) it.titlecase(Locale.FRENCH) else it.toString()
|
||||
},
|
||||
timestampMillis = startOfDay,
|
||||
avgSatiety = avgSatiety,
|
||||
avgHunger = avgHunger,
|
||||
mealCount = dayMeals.size
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val allRecentSatieties = recentMeals.map { it.satietyAfter }
|
||||
val allRecentHungers = recentMeals.map { it.hungerBefore }
|
||||
|
||||
val avgSatiety = if (allRecentSatieties.isNotEmpty()) allRecentSatieties.average().toFloat() else 0f
|
||||
val avgHunger = if (allRecentHungers.isNotEmpty()) allRecentHungers.average().toFloat() else 0f
|
||||
|
||||
val optimalCount = recentMeals.count { it.satietyAfter in 6..8 }
|
||||
val optimalPercentage = if (recentMeals.isNotEmpty()) {
|
||||
((optimalCount.toFloat() / recentMeals.size) * 100).toInt()
|
||||
} else 0
|
||||
|
||||
val distribution = mutableMapOf<MealType, Int>()
|
||||
MealType.entries.forEach { type ->
|
||||
distribution[type] = recentMeals.count { it.mealType.equals(type.name, ignoreCase = true) }
|
||||
}
|
||||
|
||||
return WeeklyStats(
|
||||
dailyStats = daysList,
|
||||
averageSatiety = avgSatiety,
|
||||
averageHunger = avgHunger,
|
||||
optimalZonePercentage = optimalPercentage,
|
||||
totalMealsLast7Days = recentMeals.size,
|
||||
mealTypeDistribution = distribution
|
||||
)
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,509 @@
|
||||
package com.example.ui.settings
|
||||
|
||||
import android.Manifest
|
||||
import android.os.Build
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
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.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
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.lazy.LazyColumn
|
||||
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.filled.AccountCircle
|
||||
import androidx.compose.material.icons.filled.Brightness4
|
||||
import androidx.compose.material.icons.filled.CloudDone
|
||||
import androidx.compose.material.icons.filled.FileDownload
|
||||
import androidx.compose.material.icons.filled.Notifications
|
||||
import androidx.compose.material.icons.filled.NotificationsActive
|
||||
import androidx.compose.material.icons.filled.Share
|
||||
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.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FilterChip
|
||||
import androidx.compose.material3.FilterChipDefaults
|
||||
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.mutableStateMapOf
|
||||
import androidx.compose.runtime.remember
|
||||
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.credentials.ClearCredentialStateRequest
|
||||
import androidx.credentials.CredentialManager
|
||||
import com.example.data.model.MealType
|
||||
import com.example.ui.theme.AppThemeMode
|
||||
import com.google.firebase.Firebase
|
||||
import com.google.firebase.auth.auth
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SettingsScreen(
|
||||
currentThemeMode: AppThemeMode,
|
||||
totalMealsCount: Int,
|
||||
onThemeModeChange: (AppThemeMode) -> Unit,
|
||||
onScheduleReminder: (MealType, Int, Int, Boolean) -> Unit,
|
||||
onTestNotification: (MealType) -> Unit,
|
||||
onSaveCsvClick: () -> Unit,
|
||||
onShareCsvClick: () -> Unit,
|
||||
onSignOut: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val currentUser = Firebase.auth.currentUser
|
||||
|
||||
val reminderStates = remember {
|
||||
mutableStateMapOf(
|
||||
MealType.BREAKFAST to true,
|
||||
MealType.LUNCH to true,
|
||||
MealType.SNACK to false,
|
||||
MealType.DINNER to true
|
||||
)
|
||||
}
|
||||
|
||||
val permissionLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.RequestPermission()
|
||||
) { _ -> }
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
.testTag("settings_screen"),
|
||||
contentPadding = PaddingValues(horizontal = 20.dp, vertical = 20.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(18.dp)
|
||||
) {
|
||||
item {
|
||||
Column {
|
||||
Text(
|
||||
text = "Réglages & Préférences",
|
||||
style = MaterialTheme.typography.headlineMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Export CSV, rappels personnalisés, thème et compte",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.7f)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 1. CSV Export Card
|
||||
item {
|
||||
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.TableChart,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
modifier = Modifier.size(22.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "Exporter mes repas (CSV)",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "$totalMealsCount repas prêts à être exportés vers Excel ou Sheets",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
||||
) {
|
||||
Button(
|
||||
onClick = onSaveCsvClick,
|
||||
enabled = totalMealsCount > 0,
|
||||
modifier = Modifier
|
||||
.weight(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(6.dp))
|
||||
Text("Enregistrer")
|
||||
}
|
||||
|
||||
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(6.dp))
|
||||
Text("Partager")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Reminders Card
|
||||
item {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(18.dp))
|
||||
|
||||
MealType.entries.forEach { mealType ->
|
||||
val isEnabled = reminderStates[mealType] ?: false
|
||||
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 conseillée: ${mealType.defaultTime}",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Switch(
|
||||
checked = isEnabled,
|
||||
onCheckedChange = { checked ->
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && checked) {
|
||||
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
reminderStates[mealType] = checked
|
||||
val (hour, minute) = when (mealType) {
|
||||
MealType.BREAKFAST -> Pair(8, 0)
|
||||
MealType.LUNCH -> Pair(12, 30)
|
||||
MealType.SNACK -> Pair(16, 30)
|
||||
MealType.DINNER -> Pair(20, 0)
|
||||
}
|
||||
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(10.dp))
|
||||
|
||||
OutlinedButton(
|
||||
onClick = {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
||||
}
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Theme Selector Card
|
||||
item {
|
||||
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()}")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Account & Cloud Sync Card
|
||||
item {
|
||||
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.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 connecté",
|
||||
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 • Sauvegarde continue",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
Firebase.auth.signOut()
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val credManager = CredentialManager.create(context)
|
||||
credManager.clearCredentialState(ClearCredentialStateRequest())
|
||||
} catch (_: Exception) {}
|
||||
}
|
||||
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("Se déconnecter")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
item {
|
||||
Spacer(modifier = Modifier.height(64.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,512 @@
|
||||
package com.example.ui.stats
|
||||
|
||||
import androidx.compose.foundation.Canvas
|
||||
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.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
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.lazy.LazyColumn
|
||||
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.Share
|
||||
import androidx.compose.material.icons.filled.ThumbUp
|
||||
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.geometry.Offset
|
||||
import androidx.compose.ui.geometry.Size
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.Path
|
||||
import androidx.compose.ui.graphics.StrokeCap
|
||||
import androidx.compose.ui.graphics.drawscope.Stroke
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.example.data.model.MealType
|
||||
import com.example.ui.journal.DayStat
|
||||
import com.example.ui.journal.WeeklyStats
|
||||
import java.util.Locale
|
||||
|
||||
@Composable
|
||||
fun WeeklyStatsScreen(
|
||||
weeklyStats: WeeklyStats,
|
||||
totalMealsCount: Int,
|
||||
onSaveCsvClick: () -> Unit,
|
||||
onShareCsvClick: () -> Unit
|
||||
) {
|
||||
LazyColumn(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
.testTag("weekly_stats_screen"),
|
||||
contentPadding = PaddingValues(horizontal = 20.dp, vertical = 20.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp)
|
||||
) {
|
||||
item {
|
||||
Column {
|
||||
Text(
|
||||
text = "Suivi Hebdomadaire",
|
||||
style = MaterialTheme.typography.headlineMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = "Évolution de vos sensations (/10) sur les 7 derniers jours",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.7f)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// 7-day Line Chart Card (1 to 10 scale)
|
||||
item {
|
||||
WeeklyChartCard(dailyStats = weeklyStats.dailyStats)
|
||||
}
|
||||
|
||||
// Key metrics cards (/10)
|
||||
item {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
MetricCard(
|
||||
modifier = Modifier.weight(1f),
|
||||
title = "Satiété moyenne",
|
||||
value = if (weeklyStats.totalMealsLast7Days > 0) String.format(Locale.FRENCH, "%.1f", weeklyStats.averageSatiety) else "—",
|
||||
unit = "/ 10",
|
||||
subtitle = "Après repas",
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
|
||||
MetricCard(
|
||||
modifier = Modifier.weight(1f),
|
||||
title = "Faim moyenne",
|
||||
value = if (weeklyStats.totalMealsLast7Days > 0) String.format(Locale.FRENCH, "%.1f", weeklyStats.averageHunger) else "—",
|
||||
unit = "/ 10",
|
||||
subtitle = "Avant repas",
|
||||
color = MaterialTheme.colorScheme.secondary
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Optimal Comfort Zone Card (6 to 8 / 10)
|
||||
item {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(20.dp),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier.size(54.dp),
|
||||
shape = CircleShape,
|
||||
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.15f)
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.ThumbUp,
|
||||
contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.size(28.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.width(16.dp))
|
||||
|
||||
Column {
|
||||
Text(
|
||||
text = "${weeklyStats.optimalZonePercentage}% en zone de confort",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = "Part des repas terminés agréablement rassasié (entre 6 et 8 / 10), sans lourdeur ni faim résiduelle.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CSV Export Card (Save to device & Share)
|
||||
item {
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("stats_csv_export_card"),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(20.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Exporter les résultats en CSV",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = "Exportez vos $totalMealsCount repas enregistrés (dates, heures, notes sur 10 et commentaires) au format tableur CSV.",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
||||
) {
|
||||
Button(
|
||||
onClick = onSaveCsvClick,
|
||||
enabled = totalMealsCount > 0,
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.testTag("stats_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(6.dp))
|
||||
Text("Enregistrer")
|
||||
}
|
||||
|
||||
OutlinedButton(
|
||||
onClick = onShareCsvClick,
|
||||
enabled = totalMealsCount > 0,
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.testTag("stats_share_csv_button"),
|
||||
shape = RoundedCornerShape(12.dp)
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Default.Share,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(6.dp))
|
||||
Text("Partager")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Meal type breakdown
|
||||
item {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(20.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Répartition des repas sur 7 jours",
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.height(14.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
MealType.entries.forEach { type ->
|
||||
val count = weeklyStats.mealTypeDistribution[type] ?: 0
|
||||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
Surface(
|
||||
modifier = Modifier.size(44.dp),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.surfaceVariant
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Text(
|
||||
text = count.toString(),
|
||||
style = MaterialTheme.typography.titleSmall,
|
||||
fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier.height(6.dp))
|
||||
Text(
|
||||
text = type.displayName.take(7),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
item {
|
||||
Spacer(modifier = Modifier.height(64.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun WeeklyChartCard(dailyStats: List<DayStat>) {
|
||||
val satietyColor = MaterialTheme.colorScheme.primary
|
||||
val hungerColor = MaterialTheme.colorScheme.secondary
|
||||
val gridColor = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f)
|
||||
val optimalZoneColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.08f)
|
||||
|
||||
Card(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.testTag("weekly_chart_card"),
|
||||
shape = RoundedCornerShape(22.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(20.dp)
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Text(
|
||||
text = "Courbe 7 jours (sur 10)",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(14.dp)) {
|
||||
LegendItem(color = satietyColor, label = "Satiété")
|
||||
LegendItem(color = hungerColor, label = "Faim")
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(18.dp))
|
||||
|
||||
// Canvas Chart on 1..10 scale
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(180.dp)
|
||||
) {
|
||||
Canvas(modifier = Modifier.fillMaxSize()) {
|
||||
val width = size.width
|
||||
val height = size.height
|
||||
val bottomPadding = 30f
|
||||
val chartHeight = height - bottomPadding
|
||||
val stepX = if (dailyStats.size > 1) width / (dailyStats.size - 1) else width
|
||||
|
||||
// Draw Optimal Zone band (level 6 to 8 out of 10)
|
||||
val y8 = chartHeight - (8f / 10f) * chartHeight
|
||||
val y6 = chartHeight - (6f / 10f) * chartHeight
|
||||
drawRect(
|
||||
color = optimalZoneColor,
|
||||
topLeft = Offset(0f, y8),
|
||||
size = Size(width, y6 - y8)
|
||||
)
|
||||
|
||||
// Draw horizontal grid lines at 2, 4, 6, 8, 10
|
||||
for (level in listOf(2, 4, 6, 8, 10)) {
|
||||
val y = chartHeight - (level.toFloat() / 10f) * chartHeight
|
||||
drawLine(
|
||||
color = gridColor,
|
||||
start = Offset(0f, y),
|
||||
end = Offset(width, y),
|
||||
strokeWidth = 1.dp.toPx()
|
||||
)
|
||||
}
|
||||
|
||||
val satietyPath = Path()
|
||||
val hungerPath = Path()
|
||||
|
||||
var hasStartedSatiety = false
|
||||
var hasStartedHunger = false
|
||||
|
||||
dailyStats.forEachIndexed { index, stat ->
|
||||
val x = index * stepX
|
||||
|
||||
if (stat.mealCount > 0) {
|
||||
val ySat = chartHeight - (stat.avgSatiety.coerceIn(1f, 10f) / 10f) * chartHeight
|
||||
if (!hasStartedSatiety) {
|
||||
satietyPath.moveTo(x, ySat)
|
||||
hasStartedSatiety = true
|
||||
} else {
|
||||
satietyPath.lineTo(x, ySat)
|
||||
}
|
||||
drawCircle(
|
||||
color = satietyColor,
|
||||
radius = 4.dp.toPx(),
|
||||
center = Offset(x, ySat)
|
||||
)
|
||||
|
||||
val yHunger = chartHeight - (stat.avgHunger.coerceIn(1f, 10f) / 10f) * chartHeight
|
||||
if (!hasStartedHunger) {
|
||||
hungerPath.moveTo(x, yHunger)
|
||||
hasStartedHunger = true
|
||||
} else {
|
||||
hungerPath.lineTo(x, yHunger)
|
||||
}
|
||||
drawCircle(
|
||||
color = hungerColor,
|
||||
radius = 4.dp.toPx(),
|
||||
center = Offset(x, yHunger)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (hasStartedSatiety) {
|
||||
drawPath(
|
||||
path = satietyPath,
|
||||
color = satietyColor,
|
||||
style = Stroke(width = 3.dp.toPx(), cap = StrokeCap.Round)
|
||||
)
|
||||
}
|
||||
|
||||
if (hasStartedHunger) {
|
||||
drawPath(
|
||||
path = hungerPath,
|
||||
color = hungerColor,
|
||||
style = Stroke(width = 2.5.dp.toPx(), cap = StrokeCap.Round)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
dailyStats.forEach { stat ->
|
||||
Text(
|
||||
text = stat.dayShortName,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LegendItem(color: Color, label: String) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically) {
|
||||
Surface(
|
||||
modifier = Modifier.size(8.dp),
|
||||
shape = CircleShape,
|
||||
color = color
|
||||
) {}
|
||||
Spacer(modifier = Modifier.width(6.dp))
|
||||
Text(
|
||||
text = label,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun MetricCard(
|
||||
modifier: Modifier = Modifier,
|
||||
title: String,
|
||||
value: String,
|
||||
unit: String,
|
||||
subtitle: String,
|
||||
color: Color
|
||||
) {
|
||||
Card(
|
||||
modifier = modifier,
|
||||
shape = RoundedCornerShape(18.dp),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surface
|
||||
)
|
||||
) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
Text(
|
||||
text = title,
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
Spacer(modifier = Modifier.height(6.dp))
|
||||
Row(verticalAlignment = Alignment.Bottom) {
|
||||
Text(
|
||||
text = value,
|
||||
style = MaterialTheme.typography.displaySmall,
|
||||
color = color,
|
||||
fontWeight = FontWeight.Bold
|
||||
)
|
||||
Text(
|
||||
text = " $unit",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
color = color.copy(alpha = 0.8f),
|
||||
modifier = Modifier.padding(bottom = 4.dp)
|
||||
)
|
||||
}
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = subtitle,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.example.ui.theme
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
// Dark Palette (Default & Organic Elegant)
|
||||
val DarkPrimary = Color(0xFF8CD4A8)
|
||||
val DarkOnPrimary = Color(0xFF0F3820)
|
||||
val DarkPrimaryContainer = Color(0xFF1E482E)
|
||||
val DarkOnPrimaryContainer = Color(0xFFC5F2D7)
|
||||
|
||||
val DarkSecondary = Color(0xFFF0B87A)
|
||||
val DarkOnSecondary = Color(0xFF452B07)
|
||||
val DarkSecondaryContainer = Color(0xFF5D3E10)
|
||||
val DarkOnSecondaryContainer = Color(0xFFFCE7CB)
|
||||
|
||||
val DarkTertiary = Color(0xFF8DCED8)
|
||||
val DarkOnTertiary = Color(0xFF00363D)
|
||||
|
||||
val DarkBackground = Color(0xFF111513)
|
||||
val DarkOnBackground = Color(0xFFE2E7E2)
|
||||
val DarkSurface = Color(0xFF191F1C)
|
||||
val DarkOnSurface = Color(0xFFE2E7E2)
|
||||
val DarkSurfaceVariant = Color(0xFF242C27)
|
||||
val DarkOnSurfaceVariant = Color(0xFFBAC3BC)
|
||||
val DarkOutline = Color(0xFF536056)
|
||||
val DarkOutlineVariant = Color(0xFF2C362F)
|
||||
|
||||
// Light Palette (Warm Linen & Forest)
|
||||
val LightPrimary = Color(0xFF246944)
|
||||
val LightOnPrimary = Color(0xFFFFFFFF)
|
||||
val LightPrimaryContainer = Color(0xFFC5F2D7)
|
||||
val LightOnPrimaryContainer = Color(0xFF062112)
|
||||
|
||||
val LightSecondary = Color(0xFF865306)
|
||||
val LightOnSecondary = Color(0xFFFFFFFF)
|
||||
val LightSecondaryContainer = Color(0xFFFCE7CB)
|
||||
val LightOnSecondaryContainer = Color(0xFF2C1700)
|
||||
|
||||
val LightTertiary = Color(0xFF1A6773)
|
||||
val LightOnTertiary = Color(0xFFFFFFFF)
|
||||
|
||||
val LightBackground = Color(0xFFF8F9F5)
|
||||
val LightOnBackground = Color(0xFF191F1C)
|
||||
val LightSurface = Color(0xFFFFFFFF)
|
||||
val LightOnSurface = Color(0xFF191F1C)
|
||||
val LightSurfaceVariant = Color(0xFFE3E9E3)
|
||||
val LightOnSurfaceVariant = Color(0xFF424A44)
|
||||
val LightOutline = Color(0xFF727A73)
|
||||
val LightOutlineVariant = Color(0xFFC2C9C2)
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.example.ui.theme
|
||||
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.material3.dynamicDarkColorScheme
|
||||
import androidx.compose.material3.dynamicLightColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
|
||||
enum class AppThemeMode(val displayName: String) {
|
||||
DARK("Sombre élégant (défaut)"),
|
||||
LIGHT("Clair lin & forêt"),
|
||||
SYSTEM("Suivre le système")
|
||||
}
|
||||
|
||||
private val DarkColorScheme = darkColorScheme(
|
||||
primary = DarkPrimary,
|
||||
onPrimary = DarkOnPrimary,
|
||||
primaryContainer = DarkPrimaryContainer,
|
||||
onPrimaryContainer = DarkOnPrimaryContainer,
|
||||
secondary = DarkSecondary,
|
||||
onSecondary = DarkOnSecondary,
|
||||
secondaryContainer = DarkSecondaryContainer,
|
||||
onSecondaryContainer = DarkOnSecondaryContainer,
|
||||
tertiary = DarkTertiary,
|
||||
onTertiary = DarkOnTertiary,
|
||||
background = DarkBackground,
|
||||
onBackground = DarkOnBackground,
|
||||
surface = DarkSurface,
|
||||
onSurface = DarkOnSurface,
|
||||
surfaceVariant = DarkSurfaceVariant,
|
||||
onSurfaceVariant = DarkOnSurfaceVariant,
|
||||
outline = DarkOutline,
|
||||
outlineVariant = DarkOutlineVariant
|
||||
)
|
||||
|
||||
private val LightColorScheme = lightColorScheme(
|
||||
primary = LightPrimary,
|
||||
onPrimary = LightOnPrimary,
|
||||
primaryContainer = LightPrimaryContainer,
|
||||
onPrimaryContainer = LightOnPrimaryContainer,
|
||||
secondary = LightSecondary,
|
||||
onSecondary = LightOnSecondary,
|
||||
secondaryContainer = LightSecondaryContainer,
|
||||
onSecondaryContainer = LightOnSecondaryContainer,
|
||||
tertiary = LightTertiary,
|
||||
onTertiary = LightOnTertiary,
|
||||
background = LightBackground,
|
||||
onBackground = LightOnBackground,
|
||||
surface = LightSurface,
|
||||
onSurface = LightOnSurface,
|
||||
surfaceVariant = LightSurfaceVariant,
|
||||
onSurfaceVariant = LightOnSurfaceVariant,
|
||||
outline = LightOutline,
|
||||
outlineVariant = LightOutlineVariant
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun SatieaTheme(
|
||||
themeMode: AppThemeMode = AppThemeMode.DARK,
|
||||
dynamicColor: Boolean = false,
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
val darkTheme = when (themeMode) {
|
||||
AppThemeMode.DARK -> true
|
||||
AppThemeMode.LIGHT -> false
|
||||
AppThemeMode.SYSTEM -> isSystemInDarkTheme()
|
||||
}
|
||||
|
||||
val colorScheme = when {
|
||||
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||
val context = LocalContext.current
|
||||
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
|
||||
}
|
||||
darkTheme -> DarkColorScheme
|
||||
else -> LightColorScheme
|
||||
}
|
||||
|
||||
MaterialTheme(
|
||||
colorScheme = colorScheme,
|
||||
typography = Typography,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.example.ui.theme
|
||||
|
||||
import androidx.compose.material3.Typography
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.Font
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.example.R
|
||||
|
||||
val FrauncesFontFamily = FontFamily(
|
||||
Font(R.font.fraunces, FontWeight.Normal),
|
||||
Font(R.font.fraunces, FontWeight.SemiBold),
|
||||
Font(R.font.fraunces, FontWeight.Bold)
|
||||
)
|
||||
|
||||
val PlusJakartaSansFontFamily = FontFamily(
|
||||
Font(R.font.plus_jakarta_sans, FontWeight.Normal),
|
||||
Font(R.font.plus_jakarta_sans, FontWeight.Medium),
|
||||
Font(R.font.plus_jakarta_sans, FontWeight.SemiBold),
|
||||
Font(R.font.plus_jakarta_sans, FontWeight.Bold)
|
||||
)
|
||||
|
||||
val Typography = Typography(
|
||||
displayLarge = TextStyle(
|
||||
fontFamily = FrauncesFontFamily,
|
||||
fontWeight = FontWeight.Bold,
|
||||
fontSize = 40.sp,
|
||||
lineHeight = 48.sp,
|
||||
letterSpacing = (-0.5).sp
|
||||
),
|
||||
displayMedium = TextStyle(
|
||||
fontFamily = FrauncesFontFamily,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
fontSize = 32.sp,
|
||||
lineHeight = 38.sp
|
||||
),
|
||||
headlineLarge = TextStyle(
|
||||
fontFamily = FrauncesFontFamily,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
fontSize = 26.sp,
|
||||
lineHeight = 32.sp
|
||||
),
|
||||
headlineMedium = TextStyle(
|
||||
fontFamily = FrauncesFontFamily,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
fontSize = 22.sp,
|
||||
lineHeight = 28.sp
|
||||
),
|
||||
titleLarge = TextStyle(
|
||||
fontFamily = FrauncesFontFamily,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
fontSize = 19.sp,
|
||||
lineHeight = 24.sp
|
||||
),
|
||||
titleMedium = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
fontSize = 16.sp,
|
||||
lineHeight = 22.sp
|
||||
),
|
||||
titleSmall = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
fontSize = 14.sp,
|
||||
lineHeight = 20.sp
|
||||
),
|
||||
bodyLarge = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 16.sp,
|
||||
lineHeight = 24.sp
|
||||
),
|
||||
bodyMedium = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 14.sp,
|
||||
lineHeight = 20.sp
|
||||
),
|
||||
bodySmall = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 12.sp,
|
||||
lineHeight = 16.sp
|
||||
),
|
||||
labelLarge = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
fontSize = 14.sp,
|
||||
lineHeight = 20.sp
|
||||
),
|
||||
labelMedium = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 12.sp,
|
||||
lineHeight = 16.sp
|
||||
),
|
||||
labelSmall = TextStyle(
|
||||
fontFamily = PlusJakartaSansFontFamily,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 11.sp,
|
||||
lineHeight = 14.sp
|
||||
)
|
||||
)
|
||||
Reference in new issue
Block a user