Tell meal item suggestions apart by portion

This commit is contained in:
Roland Geider
2026-12-03 19:34:45 +01:00
parent 86af7ab9b1
commit 2b4dc2a55c
2 changed files with 27 additions and 3 deletions

View File

@@ -245,14 +245,17 @@ class NutritionalPlan with _$NutritionalPlan {
return out;
}
/// returns meal items across all meals
/// deduped by the combination of amount and ingredient ID
/// The meal items across all meals, one per combination of ingredient,
/// amount and weight unit.
List<MealItem> get dedupMealItems {
final List<MealItem> out = [];
for (final meal in meals) {
for (final mealItem in meal.mealItems) {
final found = out.firstWhereOrNull(
(e) => e.amount == mealItem.amount && e.ingredientId == mealItem.ingredientId,
(e) =>
e.amount == mealItem.amount &&
e.ingredientId == mealItem.ingredientId &&
e.weightUnitId == mealItem.weightUnitId,
);
if (found == null) {

View File

@@ -137,6 +137,27 @@ void main() {
expect(result[3], same(otherIngredient));
});
test('dedupMealItems keeps the same amount in another unit', () {
const cup = IngredientWeightUnit(
id: 1,
uuid: '00000000-0000-0000-0000-000000000001',
ingredientId: 1,
name: 'Cup',
grams: 80,
);
final grams = MealItem(ingredientId: 1, amount: 2);
final cups = MealItem(ingredientId: 1, amount: 2, weightUnitId: cup.id, weightUnitObj: cup);
final plan = NutritionalPlan(
description: 'portions',
startDate: DateTime(2026, 3, 1),
meals: [
Meal(planId: '', mealItems: [grams, cups]),
],
);
expect(plan.dedupMealItems, [grams, cups]);
});
group('dedupDiaryEntries', () {
const cup = IngredientWeightUnit(
id: 1,