feat: add recipe management support

- Add `LocalRecipeEntity` and database migration 3->4
- Update `LocalCustomFoodEntity` with category support
- Include DAO methods for recipe persistence
This commit is contained in:
bleackstorm committed 2026-11-20 21:10:19 +01:00
1 parent 097d4b521e
commit d4d5d77d71
41 files changed
+5264 -1901

No files matched your search

@@ -0,0 +1,483 @@
package com.example.ui.recipes
import androidx.compose.foundation.BorderStroke
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.automirrored.filled.MenuBook
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.People
import androidx.compose.material.icons.filled.Schedule
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
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.saveable.rememberSaveable
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.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.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.example.data.model.CustomFoodEntry
import com.example.data.model.FoodCategory
import com.example.data.model.RecipeCategoryTag
import com.example.data.model.RecipeEntry
import com.example.ui.common.FoodCategoryColorDot
import com.example.ui.common.badgeBorderColor
import com.example.ui.common.badgeContainerColor
import com.example.ui.common.badgeTextColor
import com.example.ui.common.isSatieaDarkTheme
import java.util.Locale
@Composable
fun RecipesScreen(
recipes: List<RecipeEntry>,
customFoods: List<CustomFoodEntry>,
onAddRecipeClick: () -> Unit,
onRecipeClick: (RecipeEntry) -> Unit
) {
var searchQuery by rememberSaveable { mutableStateOf("") }
var selectedTag by rememberSaveable { mutableStateOf<String?>(null) }
val customCategoryLookup = remember(customFoods) {
customFoods.associate { it.name.lowercase(Locale.FRENCH).trim() to it.resolvedCategory() }
}
val filteredRecipes = remember(recipes, searchQuery, selectedTag) {
val query = searchQuery.trim().lowercase(Locale.FRENCH)
recipes.filter { recipe ->
val matchesTag = selectedTag == null || recipe.categoryTag.equals(selectedTag, ignoreCase = true)
val matchesQuery = query.isBlank() ||
recipe.title.lowercase(Locale.FRENCH).contains(query) ||
recipe.parsedIngredients().any { it.name.lowercase(Locale.FRENCH).contains(query) }
matchesTag && matchesQuery
}
}
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
.testTag("recipes_screen")
) {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.testTag("recipes_list"),
contentPadding = PaddingValues(
start = 20.dp,
end = 20.dp,
top = 16.dp,
bottom = 96.dp
),
verticalArrangement = Arrangement.spacedBy(14.dp)
) {
// Header
item {
Column {
Text(
text = "Mon Recueil de Recettes",
style = MaterialTheme.typography.headlineMedium,
color = MaterialTheme.colorScheme.onBackground,
fontWeight = FontWeight.Bold
)
Text(
text = if (recipes.isEmpty()) {
"Conservez vos recettes préférées et notez-les en 1 clic"
} else {
"${recipes.size} recette${if (recipes.size > 1) "s" else ""} • ingrédients colorés & portions ajustables"
},
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.7f)
)
}
}
// Search bar & Category filter chips
if (recipes.isNotEmpty()) {
item {
OutlinedTextField(
value = searchQuery,
onValueChange = { if (it.length <= 60) searchQuery = it },
modifier = Modifier
.fillMaxWidth()
.testTag("recipe_search_input"),
placeholder = { Text("Rechercher une recette ou un ingrédient...") },
leadingIcon = {
Icon(
imageVector = Icons.Default.Search,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary
)
},
trailingIcon = {
if (searchQuery.isNotEmpty()) {
IconButton(onClick = { searchQuery = "" }) {
Icon(
imageVector = Icons.Default.Close,
contentDescription = "Effacer la recherche"
)
}
}
},
singleLine = true,
shape = RoundedCornerShape(14.dp)
)
}
item {
LazyRow(
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.fillMaxWidth()
) {
item {
FilterChip(
selected = selectedTag == null,
onClick = { selectedTag = null },
label = { Text("Toutes") },
colors = FilterChipDefaults.filterChipColors(
selectedContainerColor = MaterialTheme.colorScheme.primaryContainer,
selectedLabelColor = MaterialTheme.colorScheme.onPrimaryContainer
)
)
}
items(RecipeCategoryTag.entries, key = { it.name }) { tag ->
FilterChip(
selected = selectedTag == tag.name,
onClick = {
selectedTag = if (selectedTag == tag.name) null else tag.name
},
label = { Text(tag.displayName) },
colors = FilterChipDefaults.filterChipColors(
selectedContainerColor = MaterialTheme.colorScheme.primaryContainer,
selectedLabelColor = MaterialTheme.colorScheme.onPrimaryContainer
)
)
}
}
}
}
// Content
if (recipes.isEmpty()) {
item {
EmptyRecipesView(onAddRecipeClick = onAddRecipeClick)
}
} else if (filteredRecipes.isEmpty()) {
item {
Surface(
shape = RoundedCornerShape(18.dp),
color = MaterialTheme.colorScheme.surface,
modifier = Modifier.fillMaxWidth()
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "Aucune recette ne correspond à ce filtre",
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.onSurface,
fontWeight = FontWeight.SemiBold
)
Spacer(modifier = Modifier.height(8.dp))
TextButton(
onClick = {
searchQuery = ""
selectedTag = null
}
) {
Text("Réinitialiser les filtres")
}
}
}
}
} else {
items(filteredRecipes, key = { it.id }) { recipe ->
RecipeCardItem(
recipe = recipe,
customCategoryLookup = customCategoryLookup,
onClick = { onRecipeClick(recipe) }
)
}
}
}
FloatingActionButton(
onClick = onAddRecipeClick,
modifier = Modifier
.align(Alignment.BottomEnd)
.padding(24.dp)
.testTag("add_recipe_fab"),
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary,
shape = CircleShape
) {
Icon(
imageVector = Icons.Default.Add,
contentDescription = "Créer une recette",
modifier = Modifier.size(28.dp)
)
}
}
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun RecipeCardItem(
recipe: RecipeEntry,
customCategoryLookup: Map<String, FoodCategory>,
onClick: () -> Unit
) {
val isDark = isSatieaDarkTheme()
val ingredients = remember(recipe.ingredientsSerialized) { recipe.parsedIngredients() }
val dominantCategories = remember(recipe.ingredientsSerialized, customCategoryLookup) {
recipe.dominantCategories(customCategoryLookup).take(4)
}
Card(
modifier = Modifier
.fillMaxWidth()
.clickable(onClick = onClick)
.testTag("recipe_card_${recipe.id}"),
shape = RoundedCornerShape(20.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surface
),
border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.3f))
) {
Column(modifier = Modifier.fillMaxWidth()) {
if (!recipe.photoUri.isNullOrBlank()) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(145.dp)
) {
AsyncImage(
model = recipe.photoUri,
contentDescription = "Photo de ${recipe.title}",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
Surface(
shape = RoundedCornerShape(10.dp),
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.9f),
modifier = Modifier
.align(Alignment.TopStart)
.padding(12.dp)
) {
Text(
text = recipe.categoryEnum().displayName,
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.primary,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp)
)
}
}
}
Column(
modifier = Modifier
.fillMaxWidth()
.padding(18.dp)
) {
if (recipe.photoUri.isNullOrBlank()) {
Surface(
shape = RoundedCornerShape(10.dp),
color = MaterialTheme.colorScheme.primaryContainer
) {
Text(
text = recipe.categoryEnum().displayName,
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onPrimaryContainer,
fontWeight = FontWeight.SemiBold,
modifier = Modifier.padding(horizontal = 10.dp, vertical = 4.dp)
)
}
Spacer(modifier = Modifier.height(8.dp))
}
Text(
text = recipe.title,
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onSurface,
fontWeight = FontWeight.Bold,
maxLines = 2,
overflow = TextOverflow.Ellipsis
)
Spacer(modifier = Modifier.height(8.dp))
// Metadata chips (prep time, servings, ingredient count)
Row(
horizontalArrangement = Arrangement.spacedBy(14.dp),
verticalAlignment = Alignment.CenterVertically
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector = Icons.Default.Schedule,
contentDescription = null,
tint = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.size(15.dp)
)
Spacer(modifier = Modifier.width(4.dp))
Text(
text = recipe.formatPrepTimeLabel(),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
imageVector = Icons.Default.People,
contentDescription = null,
tint = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier.size(15.dp)
)
Spacer(modifier = Modifier.width(4.dp))
Text(
text = "${recipe.servings} portion${if (recipe.servings > 1) "s" else ""}",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
Text(
text = "${ingredients.size} ingrédient${if (ingredients.size > 1) "s" else ""}",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
if (dominantCategories.isNotEmpty()) {
Spacer(modifier = Modifier.height(12.dp))
FlowRow(
horizontalArrangement = Arrangement.spacedBy(6.dp),
verticalArrangement = Arrangement.spacedBy(6.dp)
) {
dominantCategories.forEach { cat ->
Surface(
shape = RoundedCornerShape(9.dp),
color = cat.badgeContainerColor(isDark),
border = BorderStroke(1.dp, cat.badgeBorderColor(isDark))
) {
Row(
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
verticalAlignment = Alignment.CenterVertically
) {
FoodCategoryColorDot(category = cat, size = 7.dp)
Spacer(modifier = Modifier.width(5.dp))
Text(
text = cat.shortLabel,
style = MaterialTheme.typography.labelSmall,
color = cat.badgeTextColor(isDark),
fontWeight = FontWeight.Medium
)
}
}
}
}
}
}
}
}
}
@Composable
private fun EmptyRecipesView(onAddRecipeClick: () -> Unit) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 44.dp, horizontal = 20.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Surface(
modifier = Modifier.size(82.dp),
shape = CircleShape,
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
) {
Box(contentAlignment = Alignment.Center) {
Icon(
imageVector = Icons.AutoMirrored.Filled.MenuBook,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(38.dp)
)
}
}
Spacer(modifier = Modifier.height(20.dp))
Text(
text = "Votre recueil est prêt",
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onBackground,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = "Ajoutez vos plats maison avec photo, ingrédients colorés et étapes, puis notez une portion dans votre journal en un clic.",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.7f),
textAlign = TextAlign.Center
)
Spacer(modifier = Modifier.height(20.dp))
TextButton(
onClick = onAddRecipeClick,
modifier = Modifier.testTag("empty_recipes_add_button")
) {
Text(
text = "+ Créer ma première recette",
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.primary
)
}
}
}