Skip to content

Commit a4bdf70

Browse files
committed
consistent, improved deduplication behavior across mealitems and logs
deduplicate by ingredient AND amount: so if you use the same ingredient, but in a different amount, the different amount will show up this increases the list of suggestions for editing meals, and decreases the list logging an ingredient (significantly, if you often log the same ingredient in the same amount)
1 parent 68799b0 commit a4bdf70

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

lib/models/nutrition/nutritional_plan.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
import 'package:collection/collection.dart';
1920
import 'package:flutter/widgets.dart';
2021
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2122
import 'package:json_annotation/json_annotation.dart';
@@ -191,23 +192,37 @@ class NutritionalPlan {
191192
return out;
192193
}
193194

194-
/// Helper that returns all meal items for the current plan
195-
///
196-
/// Duplicated ingredients are removed
197-
List<MealItem> get allMealItems {
195+
/// returns meal items across all meals
196+
/// deduped by the combination of amount and ingredient ID
197+
List<MealItem> get dedupMealItems {
198198
final List<MealItem> out = [];
199199
for (final meal in meals) {
200200
for (final mealItem in meal.mealItems) {
201-
final ingredientInList = out.where((e) => e.ingredientId == mealItem.ingredientId);
201+
final found = out.firstWhereOrNull(
202+
(e) => e.amount == mealItem.amount && e.ingredientId == mealItem.ingredientId);
202203

203-
if (ingredientInList.isEmpty) {
204+
if (found == null) {
204205
out.add(mealItem);
205206
}
206207
}
207208
}
208209
return out;
209210
}
210211

212+
/// returns diary entries
213+
/// deduped by the combination of amount and ingredient ID
214+
List<Log> get dedupDiaryEntries {
215+
final out = <Log>[];
216+
for (final log in diaryEntries) {
217+
final found =
218+
out.firstWhereOrNull((e) => e.amount == log.amount && e.ingredientId == log.ingredientId);
219+
if (found == null) {
220+
out.add(log);
221+
}
222+
}
223+
return out;
224+
}
225+
211226
Meal pseudoMealOthers(String name) {
212227
return Meal(
213228
id: PSEUDO_MEAL_ID,

lib/widgets/nutrition/forms.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Widget MealItemForm(Meal meal, List<MealItem> recent, [String? barcode, bool? te
131131

132132
Widget IngredientLogForm(NutritionalPlan plan) {
133133
return IngredientForm(
134-
recent: plan.diaryEntries,
134+
recent: plan.dedupDiaryEntries,
135135
onSave: (BuildContext context, MealItem mealItem, DateTime? dt) {
136136
Provider.of<NutritionPlansProvider>(context, listen: false)
137137
.logIngredientToDiary(mealItem, plan.id!, dt);

lib/widgets/nutrition/meal.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ enum viewMode {
3939

4040
class MealWidget extends StatefulWidget {
4141
final Meal _meal;
42-
final List<MealItem> _listMealItems;
42+
final List<MealItem> _recentMealItems;
4343

4444
const MealWidget(
4545
this._meal,
46-
this._listMealItems,
46+
this._recentMealItems,
4747
);
4848

4949
@override
@@ -108,7 +108,7 @@ class _MealWidgetState extends State<MealWidget> {
108108
FormScreen.routeName,
109109
arguments: FormScreenArguments(
110110
AppLocalizations.of(context).addIngredient,
111-
MealItemForm(widget._meal, widget._listMealItems),
111+
MealItemForm(widget._meal, widget._recentMealItems),
112112
hasListView: true,
113113
),
114114
);

lib/widgets/nutrition/nutritional_plan_detail.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
5656
const SizedBox(height: 10),
5757
..._nutritionalPlan.meals.map((meal) => MealWidget(
5858
meal,
59-
_nutritionalPlan.allMealItems,
59+
_nutritionalPlan.dedupMealItems,
6060
)),
6161
MealWidget(
6262
_nutritionalPlan.pseudoMealOthers('Other logs'),
63-
_nutritionalPlan.allMealItems,
63+
_nutritionalPlan.dedupMealItems,
6464
),
6565
if (!_nutritionalPlan.onlyLogging)
6666
Padding(

test/nutrition/nutritional_plan_model_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void main() {
101101
});
102102

103103
test('Test that the getter returns all meal items for a plan', () {
104-
expect(plan.allMealItems, plan.meals[0].mealItems + plan.meals[1].mealItems);
104+
expect(plan.dedupMealItems, plan.meals[0].mealItems + plan.meals[1].mealItems);
105105
});
106106
});
107107
}

0 commit comments

Comments
 (0)