mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
feat(planning): add expiration dates, alerts, and AI menu adaptation
This commit is contained in:
1 parent
e534cfa7b0
commit
381fe9ab72
24 files changed
+2253
-54
No files matched your search
@@ -1,8 +1,59 @@
|
||||
package com.example.data.model
|
||||
|
||||
import com.example.domain.IngredientMatcher
|
||||
import java.time.LocalDate
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Locale
|
||||
import java.util.UUID
|
||||
|
||||
enum class ExpirationStatus {
|
||||
EXPIRED,
|
||||
URGENT,
|
||||
SOON,
|
||||
FRESH,
|
||||
NONE
|
||||
}
|
||||
|
||||
object ExpirationDefaults {
|
||||
/**
|
||||
* Suggests a realistic shelf-life in days based on category and storage location,
|
||||
* or null for long-shelf-life pantry/frozen items where no short-term alert is needed.
|
||||
*/
|
||||
fun suggestedShelfLifeDays(category: FoodCategory, location: StorageLocation): Long? {
|
||||
return when (location) {
|
||||
StorageLocation.FRIDGE -> when (category) {
|
||||
FoodCategory.PROTEIN -> 3L
|
||||
FoodCategory.VEGETABLE -> 5L
|
||||
FoodCategory.FRUIT -> 5L
|
||||
FoodCategory.DAIRY -> 7L
|
||||
FoodCategory.CHEESE -> 14L
|
||||
FoodCategory.BREAD -> 4L
|
||||
FoodCategory.STARCH -> 4L
|
||||
FoodCategory.OTHER -> 5L
|
||||
FoodCategory.FAT_SEASONING,
|
||||
FoodCategory.NUTS,
|
||||
FoodCategory.PLEASURE -> 14L
|
||||
}
|
||||
StorageLocation.PANTRY -> when (category) {
|
||||
FoodCategory.BREAD -> 3L
|
||||
FoodCategory.FRUIT -> 5L
|
||||
FoodCategory.VEGETABLE -> 7L
|
||||
else -> null
|
||||
}
|
||||
StorageLocation.FREEZER -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun suggestedExpirationEpochDay(
|
||||
category: FoodCategory,
|
||||
location: StorageLocation,
|
||||
today: LocalDate = LocalDate.now()
|
||||
): Long? {
|
||||
val days = suggestedShelfLifeDays(category, location) ?: return null
|
||||
return today.plusDays(days).toEpochDay()
|
||||
}
|
||||
}
|
||||
|
||||
enum class StorageLocation(val displayName: String) {
|
||||
FRIDGE("Frigo"),
|
||||
FREEZER("Congélateur"),
|
||||
@@ -79,9 +130,45 @@ data class InventoryItem(
|
||||
val unit: FoodUnit = FoodUnit.G,
|
||||
val category: FoodCategory = FoodCategory.OTHER,
|
||||
val location: StorageLocation = StorageLocation.PANTRY,
|
||||
val expirationDateEpochDay: Long? = null,
|
||||
val updatedAt: Long = System.currentTimeMillis()
|
||||
) {
|
||||
fun formatQuantity(): String = IngredientMatcher.formatQuantity(quantity, unit)
|
||||
|
||||
fun daysUntilExpiration(today: LocalDate = LocalDate.now()): Long? {
|
||||
val exp = expirationDateEpochDay ?: return null
|
||||
return exp - today.toEpochDay()
|
||||
}
|
||||
|
||||
fun expirationStatus(
|
||||
alertThresholdDays: Int = 3,
|
||||
today: LocalDate = LocalDate.now()
|
||||
): ExpirationStatus {
|
||||
val days = daysUntilExpiration(today) ?: return ExpirationStatus.NONE
|
||||
val safeThreshold = alertThresholdDays.coerceIn(1, 7).toLong()
|
||||
return when {
|
||||
days < 0L -> ExpirationStatus.EXPIRED
|
||||
days <= 1L -> ExpirationStatus.URGENT
|
||||
days <= safeThreshold -> ExpirationStatus.SOON
|
||||
else -> ExpirationStatus.FRESH
|
||||
}
|
||||
}
|
||||
|
||||
fun formatExpirationBadge(today: LocalDate = LocalDate.now()): String? {
|
||||
val exp = expirationDateEpochDay ?: return null
|
||||
val days = exp - today.toEpochDay()
|
||||
return when {
|
||||
days < -1L -> "Périmé (${-days} j)"
|
||||
days == -1L -> "Périmé hier"
|
||||
days == 0L -> "Aujourd'hui"
|
||||
days == 1L -> "Demain"
|
||||
days <= 7L -> "Dans $days j"
|
||||
else -> {
|
||||
val fmt = DateTimeFormatter.ofPattern("d MMM", Locale.FRENCH)
|
||||
LocalDate.ofEpochDay(exp).format(fmt)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class ManualShoppingItem(
|
||||
@@ -135,6 +222,7 @@ data class ScannedReceiptItem(
|
||||
val unit: FoodUnit = FoodUnit.PORTION,
|
||||
val category: FoodCategory = FoodCategory.OTHER,
|
||||
val location: StorageLocation = StorageLocation.PANTRY,
|
||||
val expirationDateEpochDay: Long? = null,
|
||||
val selected: Boolean = true
|
||||
)
|
||||
|
||||
@@ -204,3 +292,45 @@ sealed interface WeekPredictionUiState {
|
||||
) : WeekPredictionUiState
|
||||
}
|
||||
|
||||
data class MenuAdaptationProposal(
|
||||
val id: String = "adapt_${UUID.randomUUID().toString().take(8)}",
|
||||
val dateEpochDay: Long,
|
||||
val mealType: MealType,
|
||||
val existingPlannedMealId: String? = null,
|
||||
val existingMealTitle: String? = null,
|
||||
val recipeId: String? = null,
|
||||
val title: String,
|
||||
val servings: Double = 1.0,
|
||||
val items: List<MealFoodItem> = emptyList(),
|
||||
val usedExpiringNames: List<String> = emptyList(),
|
||||
val reason: String = "",
|
||||
val selected: Boolean = true
|
||||
) {
|
||||
fun toPlannedMealEntry(): PlannedMealEntry {
|
||||
return PlannedMealEntry(
|
||||
id = existingPlannedMealId.orEmpty(),
|
||||
dateEpochDay = dateEpochDay,
|
||||
mealType = mealType,
|
||||
recipeId = recipeId,
|
||||
customTitle = title.trim(),
|
||||
servings = servings.coerceAtLeast(0.5),
|
||||
itemsSerialized = if (recipeId.isNullOrBlank()) {
|
||||
items.map { it.serialize() }
|
||||
} else {
|
||||
emptyList()
|
||||
},
|
||||
isConsumed = false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
sealed interface MenuAdaptationUiState {
|
||||
data object Idle : MenuAdaptationUiState
|
||||
data object Loading : MenuAdaptationUiState
|
||||
data class Preview(
|
||||
val proposals: List<MenuAdaptationProposal>,
|
||||
val expiringItems: List<InventoryItem>
|
||||
) : MenuAdaptationUiState
|
||||
data class Error(val message: String) : MenuAdaptationUiState
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user