Skip to content

Commit

Permalink
Merge branch 'log-fractional-meals'
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed May 29, 2024
2 parents e043b29 + 53c674b commit cebb53c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
27 changes: 21 additions & 6 deletions lib/models/nutrition/nutritional_plan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import 'package:collection/collection.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:json_annotation/json_annotation.dart';
Expand Down Expand Up @@ -201,23 +202,37 @@ class NutritionalPlan {
return out;
}

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

if (ingredientInList.isEmpty) {
if (found == null) {
out.add(mealItem);
}
}
}
return out;
}

/// returns diary entries
/// deduped by the combination of amount and ingredient ID
List<Log> get dedupDiaryEntries {
final out = <Log>[];
for (final log in diaryEntries) {
final found =
out.firstWhereOrNull((e) => e.amount == log.amount && e.ingredientId == log.ingredientId);
if (found == null) {
out.add(log);
}
}
return out;
}

Meal pseudoMealOthers(String name) {
return Meal(
id: PSEUDO_MEAL_ID,
Expand Down
27 changes: 18 additions & 9 deletions lib/widgets/nutrition/forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Widget MealItemForm(Meal meal, List<MealItem> recent, [String? barcode, bool? te

Widget IngredientLogForm(NutritionalPlan plan) {
return IngredientForm(
recent: plan.diaryEntries,
recent: plan.dedupDiaryEntries,
onSave: (BuildContext context, MealItem mealItem, DateTime? dt) {
Provider.of<NutritionPlansProvider>(context, listen: false)
.logIngredientToDiary(mealItem, plan.id!, dt);
Expand Down Expand Up @@ -168,6 +168,7 @@ class IngredientFormState extends State<IngredientForm> {
final _dateController = TextEditingController(); // optional
final _timeController = TextEditingController(); // optional
final _mealItem = MealItem.empty();
var _searchQuery = ''; // copy from typeahead. for filtering suggestions

@override
void initState() {
Expand Down Expand Up @@ -201,10 +202,18 @@ class IngredientFormState extends State<IngredientForm> {
});
}

void updateSearchQuery(String query) {
setState(() {
_searchQuery = query;
});
}

@override
Widget build(BuildContext context) {
final String unit = AppLocalizations.of(context).g;

final queryLower = _searchQuery.toLowerCase();
final suggestions =
widget.recent.where((e) => e.ingredient.name.toLowerCase().contains(queryLower)).toList();
return Container(
margin: const EdgeInsets.all(20),
child: Form(
Expand All @@ -218,6 +227,7 @@ class IngredientFormState extends State<IngredientForm> {
test: widget.test,
selectIngredient: selectIngredient,
unSelectIngredient: unSelectIngredient,
updateSearchQuery: updateSearchQuery,
),
Row(
children: [
Expand Down Expand Up @@ -370,27 +380,26 @@ class IngredientFormState extends State<IngredientForm> {
Navigator.of(context).pop();
},
),
if (widget.recent.isNotEmpty) const SizedBox(height: 10.0),
if (suggestions.isNotEmpty) const SizedBox(height: 10.0),
Container(
padding: const EdgeInsets.all(10.0),
child: Text(AppLocalizations.of(context).recentlyUsedIngredients),
),
Expanded(
child: ListView.builder(
itemCount: widget.recent.length,
itemCount: suggestions.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {
final ingredient = widget.recent[index].ingredient;
selectIngredient(
ingredient.id, ingredient.name, widget.recent[index].amount);
final ingredient = suggestions[index].ingredient;
selectIngredient(ingredient.id, ingredient.name, suggestions[index].amount);
},
title: Text(
'${widget.recent[index].ingredient.name} (${widget.recent[index].amount.toStringAsFixed(0)}$unit)'),
'${suggestions[index].ingredient.name} (${suggestions[index].amount.toStringAsFixed(0)}$unit)'),
subtitle: Text(getShortNutritionValues(
widget.recent[index].ingredient.nutritionalValues, context)),
suggestions[index].ingredient.nutritionalValues, context)),
trailing: const Icon(Icons.copy),
),
);
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/nutrition/meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ enum viewMode {

class MealWidget extends StatefulWidget {
final Meal _meal;
final List<MealItem> _listMealItems;
final List<MealItem> _recentMealItems;

const MealWidget(
this._meal,
this._listMealItems,
this._recentMealItems,
);

@override
Expand Down Expand Up @@ -108,7 +108,7 @@ class _MealWidgetState extends State<MealWidget> {
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).addIngredient,
MealItemForm(widget._meal, widget._listMealItems),
MealItemForm(widget._meal, widget._recentMealItems),
hasListView: true,
),
);
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/nutrition/nutritional_plan_detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
const SizedBox(height: 10),
..._nutritionalPlan.meals.map((meal) => MealWidget(
meal,
_nutritionalPlan.allMealItems,
_nutritionalPlan.dedupMealItems,
)),
MealWidget(
_nutritionalPlan.pseudoMealOthers('Other logs'),
_nutritionalPlan.allMealItems,
_nutritionalPlan.dedupMealItems,
),
if (!_nutritionalPlan.onlyLogging)
Padding(
Expand Down
3 changes: 3 additions & 0 deletions lib/widgets/nutrition/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class IngredientTypeahead extends StatefulWidget {

final Function(int id, String name, num? amount) selectIngredient;
final Function() unSelectIngredient;
final Function(String query) updateSearchQuery;

const IngredientTypeahead(
this._ingredientIdController,
Expand All @@ -74,6 +75,7 @@ class IngredientTypeahead extends StatefulWidget {
this.barcode = '',
required this.selectIngredient,
required this.unSelectIngredient,
required this.updateSearchQuery,
});

@override
Expand Down Expand Up @@ -125,6 +127,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
return null;
},
onChanged: (value) {
widget.updateSearchQuery(value);
// unselect to start a new search
widget.unSelectIngredient();
},
Expand Down
2 changes: 1 addition & 1 deletion test/nutrition/nutritional_plan_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void main() {
});

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

0 comments on commit cebb53c

Please sign in to comment.