mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
- Add Planning module with UI and ViewModel - Update database schema to support planned meals and inventory - Add camera permission for future features - Refactor food category handling to support multiple tags
473 lines
21 KiB
Kotlin
473 lines
21 KiB
Kotlin
package com.example.ui.planning
|
||
|
||
import androidx.compose.foundation.BorderStroke
|
||
import androidx.compose.foundation.layout.Arrangement
|
||
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.lazy.items
|
||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||
import androidx.compose.material.icons.Icons
|
||
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft
|
||
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
|
||
import androidx.compose.material.icons.filled.Add
|
||
import androidx.compose.material.icons.filled.CheckCircle
|
||
import androidx.compose.material.icons.filled.DeleteOutline
|
||
import androidx.compose.material.icons.filled.Edit
|
||
import androidx.compose.material.icons.filled.Restaurant
|
||
import androidx.compose.material.icons.filled.Today
|
||
import androidx.compose.material3.AssistChip
|
||
import androidx.compose.material3.Card
|
||
import androidx.compose.material3.CardDefaults
|
||
import androidx.compose.material3.FilledTonalButton
|
||
import androidx.compose.material3.HorizontalDivider
|
||
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.remember
|
||
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.text.style.TextDecoration
|
||
import androidx.compose.ui.text.style.TextOverflow
|
||
import androidx.compose.ui.unit.dp
|
||
import com.example.data.model.CustomFoodEntry
|
||
import com.example.data.model.MealType
|
||
import com.example.data.model.PlannedMealEntry
|
||
import com.example.data.model.RecipeEntry
|
||
import com.example.ui.common.FoodCategoryColorDotsRow
|
||
import java.time.LocalDate
|
||
import java.time.format.DateTimeFormatter
|
||
import java.util.Locale
|
||
|
||
@Composable
|
||
fun WeeklyPlanTab(
|
||
weekStartDate: LocalDate,
|
||
plannedMeals: List<PlannedMealEntry>,
|
||
recipesById: Map<String, RecipeEntry>,
|
||
customFoods: List<CustomFoodEntry>,
|
||
onPreviousWeek: () -> Unit,
|
||
onNextWeek: () -> Unit,
|
||
onGoToCurrentWeek: () -> Unit,
|
||
onPlanMealClick: (date: LocalDate, mealType: MealType) -> Unit,
|
||
onEditPlannedMeal: (PlannedMealEntry) -> Unit,
|
||
onDeletePlannedMeal: (String) -> Unit,
|
||
onMarkAsEaten: (PlannedMealEntry) -> Unit
|
||
) {
|
||
val today = remember { LocalDate.now() }
|
||
val currentMonday = remember { PlanningViewModel.currentMonday() }
|
||
val isCurrentWeek = weekStartDate == currentMonday
|
||
|
||
val weekDays = remember(weekStartDate) {
|
||
(0L..6L).map { weekStartDate.plusDays(it) }
|
||
}
|
||
val mealsByDayAndType = remember(plannedMeals) {
|
||
plannedMeals.groupBy { it.dateEpochDay to it.mealType }
|
||
}
|
||
val customCategoriesLookup = remember(customFoods) {
|
||
CustomFoodEntry.buildCategoriesLookup(customFoods)
|
||
}
|
||
|
||
val rangeFormatter = remember { DateTimeFormatter.ofPattern("d MMM", Locale.FRENCH) }
|
||
val dayTitleFormatter = remember { DateTimeFormatter.ofPattern("EEEE d MMMM", Locale.FRENCH) }
|
||
val weekRangeLabel = remember(weekStartDate) {
|
||
val end = weekStartDate.plusDays(6)
|
||
"${weekStartDate.format(rangeFormatter)} – ${end.format(rangeFormatter)}"
|
||
}
|
||
|
||
val consumedCount = plannedMeals.count { it.isConsumed }
|
||
|
||
LazyColumn(
|
||
modifier = Modifier
|
||
.fillMaxSize()
|
||
.testTag("weekly_plan_tab_list"),
|
||
contentPadding = PaddingValues(start = 20.dp, end = 20.dp, top = 8.dp, bottom = 96.dp),
|
||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||
) {
|
||
// Week navigation bar
|
||
item {
|
||
Card(
|
||
shape = RoundedCornerShape(18.dp),
|
||
colors = CardDefaults.cardColors(
|
||
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
|
||
)
|
||
) {
|
||
Column(
|
||
modifier = Modifier
|
||
.fillMaxWidth()
|
||
.padding(horizontal = 12.dp, vertical = 10.dp),
|
||
verticalArrangement = Arrangement.spacedBy(6.dp)
|
||
) {
|
||
Row(
|
||
modifier = Modifier.fillMaxWidth(),
|
||
verticalAlignment = Alignment.CenterVertically,
|
||
horizontalArrangement = Arrangement.SpaceBetween
|
||
) {
|
||
IconButton(
|
||
onClick = onPreviousWeek,
|
||
modifier = Modifier.testTag("previous_week_button")
|
||
) {
|
||
Icon(
|
||
imageVector = Icons.AutoMirrored.Filled.KeyboardArrowLeft,
|
||
contentDescription = "Semaine précédente"
|
||
)
|
||
}
|
||
|
||
Column(horizontalAlignment = Alignment.CenterHorizontally) {
|
||
Text(
|
||
text = "Semaine du $weekRangeLabel",
|
||
style = MaterialTheme.typography.titleSmall,
|
||
fontWeight = FontWeight.Bold
|
||
)
|
||
Text(
|
||
text = if (plannedMeals.isEmpty()) {
|
||
"Aucun repas planifié cette semaine"
|
||
} else {
|
||
"${plannedMeals.size} repas planifié${if (plannedMeals.size > 1) "s" else ""} • $consumedCount consommé${if (consumedCount > 1) "s" else ""}"
|
||
},
|
||
style = MaterialTheme.typography.labelSmall,
|
||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||
)
|
||
}
|
||
|
||
IconButton(
|
||
onClick = onNextWeek,
|
||
modifier = Modifier.testTag("next_week_button")
|
||
) {
|
||
Icon(
|
||
imageVector = Icons.AutoMirrored.Filled.KeyboardArrowRight,
|
||
contentDescription = "Semaine suivante"
|
||
)
|
||
}
|
||
}
|
||
|
||
if (!isCurrentWeek) {
|
||
Row(
|
||
modifier = Modifier.fillMaxWidth(),
|
||
horizontalArrangement = Arrangement.Center
|
||
) {
|
||
AssistChip(
|
||
onClick = onGoToCurrentWeek,
|
||
leadingIcon = {
|
||
Icon(
|
||
imageVector = Icons.Default.Today,
|
||
contentDescription = null,
|
||
modifier = Modifier.size(16.dp)
|
||
)
|
||
},
|
||
label = { Text("Revenir à cette semaine") },
|
||
modifier = Modifier.testTag("current_week_button")
|
||
)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 7 Day Cards
|
||
items(weekDays, key = { it.toEpochDay() }) { day ->
|
||
val isToday = day == today
|
||
val formattedDay = day.format(dayTitleFormatter).replaceFirstChar {
|
||
it.titlecase(Locale.FRENCH)
|
||
}
|
||
val dayPlannedCount = MealType.entries.sumOf { type ->
|
||
mealsByDayAndType[day.toEpochDay() to type]?.size ?: 0
|
||
}
|
||
|
||
Card(
|
||
shape = RoundedCornerShape(18.dp),
|
||
colors = CardDefaults.cardColors(
|
||
containerColor = if (isToday) {
|
||
MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.28f)
|
||
} else {
|
||
MaterialTheme.colorScheme.surface
|
||
}
|
||
),
|
||
border = BorderStroke(
|
||
width = if (isToday) 1.5.dp else 1.dp,
|
||
color = if (isToday) {
|
||
MaterialTheme.colorScheme.primary.copy(alpha = 0.6f)
|
||
} else {
|
||
MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f)
|
||
}
|
||
),
|
||
modifier = Modifier
|
||
.fillMaxWidth()
|
||
.testTag("plan_day_card_${day.toEpochDay()}")
|
||
) {
|
||
Column(
|
||
modifier = Modifier
|
||
.fillMaxWidth()
|
||
.padding(14.dp),
|
||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||
) {
|
||
Row(
|
||
modifier = Modifier.fillMaxWidth(),
|
||
horizontalArrangement = Arrangement.SpaceBetween,
|
||
verticalAlignment = Alignment.CenterVertically
|
||
) {
|
||
Row(
|
||
verticalAlignment = Alignment.CenterVertically,
|
||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||
) {
|
||
Text(
|
||
text = formattedDay,
|
||
style = MaterialTheme.typography.titleSmall,
|
||
fontWeight = FontWeight.Bold,
|
||
color = if (isToday) {
|
||
MaterialTheme.colorScheme.primary
|
||
} else {
|
||
MaterialTheme.colorScheme.onSurface
|
||
}
|
||
)
|
||
if (isToday) {
|
||
Surface(
|
||
shape = RoundedCornerShape(8.dp),
|
||
color = MaterialTheme.colorScheme.primary
|
||
) {
|
||
Text(
|
||
text = "Aujourd'hui",
|
||
style = MaterialTheme.typography.labelSmall,
|
||
color = MaterialTheme.colorScheme.onPrimary,
|
||
modifier = Modifier.padding(horizontal = 7.dp, vertical = 2.dp)
|
||
)
|
||
}
|
||
}
|
||
}
|
||
|
||
if (dayPlannedCount > 0) {
|
||
Text(
|
||
text = "$dayPlannedCount/4",
|
||
style = MaterialTheme.typography.labelSmall,
|
||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||
)
|
||
}
|
||
}
|
||
|
||
MealType.entries.forEachIndexed { index, mealType ->
|
||
if (index > 0) {
|
||
HorizontalDivider(
|
||
color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.35f)
|
||
)
|
||
}
|
||
val slotEntries = mealsByDayAndType[day.toEpochDay() to mealType].orEmpty()
|
||
MealSlotRow(
|
||
mealType = mealType,
|
||
entries = slotEntries,
|
||
recipesById = recipesById,
|
||
customCategoriesLookup = customCategoriesLookup,
|
||
onAddClick = { onPlanMealClick(day, mealType) },
|
||
onEditEntry = onEditPlannedMeal,
|
||
onDeleteEntry = onDeletePlannedMeal,
|
||
onMarkAsEaten = onMarkAsEaten
|
||
)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
@Composable
|
||
private fun MealSlotRow(
|
||
mealType: MealType,
|
||
entries: List<PlannedMealEntry>,
|
||
recipesById: Map<String, RecipeEntry>,
|
||
customCategoriesLookup: Map<String, List<com.example.data.model.FoodCategory>>,
|
||
onAddClick: () -> Unit,
|
||
onEditEntry: (PlannedMealEntry) -> Unit,
|
||
onDeleteEntry: (String) -> Unit,
|
||
onMarkAsEaten: (PlannedMealEntry) -> Unit
|
||
) {
|
||
Column(
|
||
modifier = Modifier.fillMaxWidth(),
|
||
verticalArrangement = Arrangement.spacedBy(6.dp)
|
||
) {
|
||
Row(
|
||
modifier = Modifier.fillMaxWidth(),
|
||
verticalAlignment = Alignment.CenterVertically,
|
||
horizontalArrangement = Arrangement.SpaceBetween
|
||
) {
|
||
Text(
|
||
text = mealType.displayName,
|
||
style = MaterialTheme.typography.labelMedium,
|
||
fontWeight = FontWeight.SemiBold,
|
||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||
)
|
||
|
||
TextButton(
|
||
onClick = onAddClick,
|
||
contentPadding = PaddingValues(horizontal = 10.dp, vertical = 4.dp),
|
||
modifier = Modifier.testTag("add_plan_${mealType.name.lowercase()}")
|
||
) {
|
||
Icon(
|
||
imageVector = Icons.Default.Add,
|
||
contentDescription = "Planifier ${mealType.displayName}",
|
||
modifier = Modifier.size(16.dp)
|
||
)
|
||
Spacer(modifier = Modifier.width(4.dp))
|
||
Text(
|
||
text = if (entries.isEmpty()) "Planifier" else "Ajouter",
|
||
style = MaterialTheme.typography.labelMedium
|
||
)
|
||
}
|
||
}
|
||
|
||
entries.forEach { entry ->
|
||
val title = entry.resolvedTitle(recipesById)
|
||
val resolvedItems = entry.resolvedFoodItems(recipesById)
|
||
val categoryDots = remember(resolvedItems, customCategoriesLookup) {
|
||
resolvedItems
|
||
.flatMap { it.resolvedCategories(customCategoriesLookup) }
|
||
.distinct()
|
||
.take(5)
|
||
}
|
||
|
||
Surface(
|
||
shape = RoundedCornerShape(12.dp),
|
||
color = if (entry.isConsumed) {
|
||
MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.35f)
|
||
} else {
|
||
MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.6f)
|
||
},
|
||
modifier = Modifier.fillMaxWidth()
|
||
) {
|
||
Column(
|
||
modifier = Modifier
|
||
.fillMaxWidth()
|
||
.padding(horizontal = 10.dp, vertical = 8.dp),
|
||
verticalArrangement = Arrangement.spacedBy(6.dp)
|
||
) {
|
||
Row(
|
||
modifier = Modifier.fillMaxWidth(),
|
||
verticalAlignment = Alignment.CenterVertically
|
||
) {
|
||
if (categoryDots.isNotEmpty()) {
|
||
FoodCategoryColorDotsRow(
|
||
categories = categoryDots,
|
||
dotSize = 8.dp,
|
||
spacing = 3.dp
|
||
)
|
||
Spacer(modifier = Modifier.width(8.dp))
|
||
}
|
||
|
||
Column(modifier = Modifier.weight(1f)) {
|
||
Text(
|
||
text = title,
|
||
style = MaterialTheme.typography.bodyMedium,
|
||
fontWeight = FontWeight.SemiBold,
|
||
textDecoration = if (entry.isConsumed) TextDecoration.LineThrough else null,
|
||
color = if (entry.isConsumed) {
|
||
MaterialTheme.colorScheme.onSurfaceVariant
|
||
} else {
|
||
MaterialTheme.colorScheme.onSurface
|
||
},
|
||
maxLines = 2,
|
||
overflow = TextOverflow.Ellipsis
|
||
)
|
||
|
||
val subtitleParts = buildList {
|
||
if (!entry.recipeId.isNullOrBlank()) {
|
||
val formattedServings = if (entry.servings % 1.0 == 0.0) {
|
||
entry.servings.toInt().toString()
|
||
} else {
|
||
entry.servings.toString()
|
||
}
|
||
add("$formattedServings part${if (entry.servings > 1.0) "s" else ""}")
|
||
}
|
||
if (resolvedItems.isNotEmpty()) {
|
||
add("${resolvedItems.size} ingrédient${if (resolvedItems.size > 1) "s" else ""}")
|
||
}
|
||
}
|
||
if (subtitleParts.isNotEmpty()) {
|
||
Text(
|
||
text = subtitleParts.joinToString(" • "),
|
||
style = MaterialTheme.typography.labelSmall,
|
||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||
)
|
||
}
|
||
}
|
||
|
||
IconButton(
|
||
onClick = { onEditEntry(entry) },
|
||
modifier = Modifier.size(36.dp)
|
||
) {
|
||
Icon(
|
||
imageVector = Icons.Default.Edit,
|
||
contentDescription = "Modifier $title",
|
||
tint = MaterialTheme.colorScheme.onSurfaceVariant,
|
||
modifier = Modifier.size(18.dp)
|
||
)
|
||
}
|
||
|
||
IconButton(
|
||
onClick = { onDeleteEntry(entry.id) },
|
||
modifier = Modifier.size(36.dp)
|
||
) {
|
||
Icon(
|
||
imageVector = Icons.Default.DeleteOutline,
|
||
contentDescription = "Retirer $title",
|
||
tint = MaterialTheme.colorScheme.onSurfaceVariant,
|
||
modifier = Modifier.size(18.dp)
|
||
)
|
||
}
|
||
}
|
||
|
||
if (entry.isConsumed) {
|
||
Row(
|
||
verticalAlignment = Alignment.CenterVertically,
|
||
horizontalArrangement = Arrangement.spacedBy(5.dp)
|
||
) {
|
||
Icon(
|
||
imageVector = Icons.Default.CheckCircle,
|
||
contentDescription = null,
|
||
tint = MaterialTheme.colorScheme.primary,
|
||
modifier = Modifier.size(16.dp)
|
||
)
|
||
Text(
|
||
text = "Noté au journal",
|
||
style = MaterialTheme.typography.labelSmall,
|
||
color = MaterialTheme.colorScheme.primary,
|
||
fontWeight = FontWeight.Medium
|
||
)
|
||
}
|
||
} else {
|
||
FilledTonalButton(
|
||
onClick = { onMarkAsEaten(entry) },
|
||
shape = RoundedCornerShape(10.dp),
|
||
contentPadding = PaddingValues(horizontal = 12.dp, vertical = 6.dp),
|
||
modifier = Modifier
|
||
.height(36.dp)
|
||
.testTag("mark_eaten_${entry.id}")
|
||
) {
|
||
Icon(
|
||
imageVector = Icons.Default.Restaurant,
|
||
contentDescription = null,
|
||
modifier = Modifier.size(15.dp)
|
||
)
|
||
Spacer(modifier = Modifier.width(6.dp))
|
||
Text(
|
||
text = "Marquer comme mangé",
|
||
style = MaterialTheme.typography.labelMedium
|
||
)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|