From ed8203de093be3e09ad10045a28dbd06ebe9e726 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 31 Mar 2026 11:46:38 +0200 Subject: [PATCH] Expose units in the nutritional plan --- .../nutrition/ingredient_weight_unit.dart | 16 +----- .../nutrition/ingredient_weight_unit.g.dart | 19 ++----- lib/models/nutrition/log.dart | 4 +- lib/models/nutrition/meal_item.dart | 18 +----- lib/models/nutrition/weight_unit.dart | 36 ------------ lib/models/nutrition/weight_unit.g.dart | 20 ------- lib/providers/nutrition.dart | 17 +++++- ...trition_ingredient_filters_riverpod.g.dart | 2 +- lib/widgets/nutrition/forms.dart | 56 ++++++++++++++++++- lib/widgets/nutrition/meal.dart | 23 ++++---- lib/widgets/nutrition/nutrition_tiles.dart | 4 +- test/core/settings_test.mocks.dart | 49 ++++++++++------ test/core/validators_test.mocks.dart | 33 +++++++++++ .../nutritional_meal_form_test.mocks.dart | 19 ++++++- .../nutritional_meal_item_form_test.dart | 2 + .../nutritional_plan_form_test.mocks.dart | 19 ++++++- .../nutritional_plan_model_test.dart | 31 ++++++++++ .../nutritional_plan_screen_test.dart | 16 +++--- .../nutritional_plan_screen_test.mocks.dart | 8 +++ .../nutritional_plans_screen_test.mocks.dart | 8 +++ test/routine/gym_mode/session_page_test.dart | 3 +- test/weight/weight_screen_test.mocks.dart | 13 +++++ 22 files changed, 259 insertions(+), 157 deletions(-) delete mode 100644 lib/models/nutrition/weight_unit.dart delete mode 100644 lib/models/nutrition/weight_unit.g.dart diff --git a/lib/models/nutrition/ingredient_weight_unit.dart b/lib/models/nutrition/ingredient_weight_unit.dart index cfbb9fd61..edad5dbb3 100644 --- a/lib/models/nutrition/ingredient_weight_unit.dart +++ b/lib/models/nutrition/ingredient_weight_unit.dart @@ -17,8 +17,6 @@ */ import 'package:json_annotation/json_annotation.dart'; -import 'package:wger/models/nutrition/ingredient.dart'; -import 'package:wger/models/nutrition/weight_unit.dart'; part 'ingredient_weight_unit.g.dart'; @@ -27,24 +25,16 @@ class IngredientWeightUnit { @JsonKey(required: true) final int id; - @JsonKey(required: true, name: 'weight_unit') - final WeightUnit weightUnit; - @JsonKey(required: true) - final Ingredient ingredient; + final String name; - @JsonKey(required: true) + @JsonKey(required: true, name: 'gram') final int grams; - @JsonKey(required: true) - final double amount; - const IngredientWeightUnit({ required this.id, - required this.weightUnit, - required this.ingredient, + required this.name, required this.grams, - required this.amount, }); // Boilerplate diff --git a/lib/models/nutrition/ingredient_weight_unit.g.dart b/lib/models/nutrition/ingredient_weight_unit.g.dart index dcc3e2f10..4ed185293 100644 --- a/lib/models/nutrition/ingredient_weight_unit.g.dart +++ b/lib/models/nutrition/ingredient_weight_unit.g.dart @@ -7,18 +7,11 @@ part of 'ingredient_weight_unit.dart'; // ************************************************************************** IngredientWeightUnit _$IngredientWeightUnitFromJson(Map json) { - $checkKeys( - json, - requiredKeys: const ['id', 'weight_unit', 'ingredient', 'grams', 'amount'], - ); + $checkKeys(json, requiredKeys: const ['id', 'name', 'gram']); return IngredientWeightUnit( id: (json['id'] as num).toInt(), - weightUnit: WeightUnit.fromJson( - json['weight_unit'] as Map, - ), - ingredient: Ingredient.fromJson(json['ingredient'] as Map), - grams: (json['grams'] as num).toInt(), - amount: (json['amount'] as num).toDouble(), + name: json['name'] as String, + grams: (json['gram'] as num).toInt(), ); } @@ -26,8 +19,6 @@ Map _$IngredientWeightUnitToJson( IngredientWeightUnit instance, ) => { 'id': instance.id, - 'weight_unit': instance.weightUnit, - 'ingredient': instance.ingredient, - 'grams': instance.grams, - 'amount': instance.amount, + 'name': instance.name, + 'gram': instance.grams, }; diff --git a/lib/models/nutrition/log.dart b/lib/models/nutrition/log.dart index 118b62549..e8fb8ae3d 100644 --- a/lib/models/nutrition/log.dart +++ b/lib/models/nutrition/log.dart @@ -84,9 +84,7 @@ class Log { NutritionalValues get nutritionalValues { // This is already done on the server. It might be better to read it from there. - final weight = weightUnitObj == null - ? amount - : amount * weightUnitObj!.amount * weightUnitObj!.grams; + final weight = weightUnitObj == null ? amount : amount * weightUnitObj!.grams; return ingredient.nutritionalValues / (100 / weight); } diff --git a/lib/models/nutrition/meal_item.dart b/lib/models/nutrition/meal_item.dart index 410e43064..9b17797ce 100644 --- a/lib/models/nutrition/meal_item.dart +++ b/lib/models/nutrition/meal_item.dart @@ -72,24 +72,10 @@ class MealItem { Map toJson() => _$MealItemToJson(this); /// Calculations - /// TODO why does this not consider weightUnitObj ? should we do the same as Log.nutritionalValues here? NutritionalValues get nutritionalValues { - // This is already done on the server. It might be better to read it from there. - final out = NutritionalValues(); + final weight = weightUnitObj == null ? amount : amount * weightUnitObj!.grams; - //final weight = this.weightUnit == null ? amount : amount * weightUnit.amount * weightUnit.grams; - final weight = amount; - - out.energy = ingredient.energy * weight / 100; - out.protein = ingredient.protein * weight / 100; - out.carbohydrates = ingredient.carbohydrates * weight / 100; - out.carbohydratesSugar = ingredient.carbohydratesSugar * weight / 100; - out.fat = ingredient.fat * weight / 100; - out.fatSaturated = ingredient.fatSaturated * weight / 100; - out.fiber = ingredient.fiber * weight / 100; - out.sodium = ingredient.sodium * weight / 100; - - return out; + return ingredient.nutritionalValues / (100 / weight); } MealItem copyWith({ diff --git a/lib/models/nutrition/weight_unit.dart b/lib/models/nutrition/weight_unit.dart deleted file mode 100644 index 2fab3e0af..000000000 --- a/lib/models/nutrition/weight_unit.dart +++ /dev/null @@ -1,36 +0,0 @@ -/* - * This file is part of wger Workout Manager . - * Copyright (C) 2020, 2021 wger Team - * - * wger Workout Manager is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -import 'package:json_annotation/json_annotation.dart'; - -part 'weight_unit.g.dart'; - -@JsonSerializable() -class WeightUnit { - @JsonKey(required: true) - final int id; - - @JsonKey(required: true) - final String name; - - const WeightUnit({required this.id, required this.name}); - - // Boilerplate - factory WeightUnit.fromJson(Map json) => _$WeightUnitFromJson(json); - Map toJson() => _$WeightUnitToJson(this); -} diff --git a/lib/models/nutrition/weight_unit.g.dart b/lib/models/nutrition/weight_unit.g.dart deleted file mode 100644 index a8f86e6de..000000000 --- a/lib/models/nutrition/weight_unit.g.dart +++ /dev/null @@ -1,20 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'weight_unit.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -WeightUnit _$WeightUnitFromJson(Map json) { - $checkKeys(json, requiredKeys: const ['id', 'name']); - return WeightUnit( - id: (json['id'] as num).toInt(), - name: json['name'] as String, - ); -} - -Map _$WeightUnitToJson(WeightUnit instance) => { - 'id': instance.id, - 'name': instance.name, -}; diff --git a/lib/providers/nutrition.dart b/lib/providers/nutrition.dart index 3761dfe28..bb751d5be 100644 --- a/lib/providers/nutrition.dart +++ b/lib/providers/nutrition.dart @@ -28,6 +28,7 @@ import 'package:wger/database/ingredients/ingredients_database.dart'; import 'package:wger/helpers/consts.dart'; import 'package:wger/models/nutrition/ingredient.dart'; import 'package:wger/models/nutrition/ingredient_image.dart'; +import 'package:wger/models/nutrition/ingredient_weight_unit.dart'; import 'package:wger/models/nutrition/log.dart'; import 'package:wger/models/nutrition/meal.dart'; import 'package:wger/models/nutrition/meal_item.dart'; @@ -49,6 +50,7 @@ class NutritionPlansProvider with ChangeNotifier { static const _mealItemPath = 'mealitem'; static const _ingredientInfoPath = 'ingredientinfo'; static const _nutritionDiaryPath = 'nutritiondiary'; + static const _ingredientWeightUnitPath = 'ingredientweightunit'; final WgerBaseProvider baseProvider; late IngredientDatabase database; @@ -380,6 +382,17 @@ class NutritionPlansProvider with ChangeNotifier { return ingredient; } + /// Fetch the available weight units for an ingredient + Future> fetchWeightUnits(int ingredientId) async { + final data = await baseProvider.fetchPaginated( + baseProvider.makeUrl( + _ingredientWeightUnitPath, + query: {'ingredient': ingredientId.toString()}, + ), + ); + return data.map((e) => IngredientWeightUnit.fromJson(e)).toList(); + } + /// Loads the available ingredients from the local cache Future fetchIngredientsFromCache() async { final ingredientDb = await database.select(database.ingredients).get(); @@ -400,8 +413,6 @@ class NutritionPlansProvider with ChangeNotifier { if (name.length <= 1) { return []; } - print("isVegan in search result is $isVegan"); - print("isVegetarian in search result is $isVegetarian"); final List languages = []; switch (searchLanguage) { @@ -435,7 +446,7 @@ class NutritionPlansProvider with ChangeNotifier { if (isVegetarian) { query['is_vegetarian'] = 'true'; } - print("query is $query"); + // Send the request _logger.info('Fetching ingredients from server'); final response = await baseProvider.fetch( diff --git a/lib/providers/nutrition_ingredient_filters_riverpod.g.dart b/lib/providers/nutrition_ingredient_filters_riverpod.g.dart index 1e61dc17d..5b9c50c23 100644 --- a/lib/providers/nutrition_ingredient_filters_riverpod.g.dart +++ b/lib/providers/nutrition_ingredient_filters_riverpod.g.dart @@ -33,7 +33,7 @@ final class IngredientFiltersNotifierProvider IngredientFiltersNotifier create() => IngredientFiltersNotifier(); } -String _$ingredientFiltersNotifierHash() => r'e9a4717c081f85ed6daef56f06c620b08bee4e0c'; +String _$ingredientFiltersNotifierHash() => r'839633dd545b6a9a6cbb8840098fe15f7c827897'; abstract class _$IngredientFiltersNotifier extends $AsyncNotifier { FutureOr build(); diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index c8a791458..4921525b9 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/forms.dart @@ -23,6 +23,7 @@ import 'package:wger/helpers/consts.dart'; import 'package:wger/helpers/json.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; import 'package:wger/models/nutrition/ingredient.dart'; +import 'package:wger/models/nutrition/ingredient_weight_unit.dart'; import 'package:wger/models/nutrition/log.dart'; import 'package:wger/models/nutrition/meal.dart'; import 'package:wger/models/nutrition/meal_item.dart'; @@ -184,6 +185,8 @@ class IngredientFormState extends State { final _timeController = TextEditingController(text: ''); // optional final _mealItem = MealItem.empty(); var _searchQuery = ''; // copy from typeahead. for filtering suggestions + List _weightUnits = []; + IngredientWeightUnit? _selectedWeightUnit; @override void dispose() { @@ -208,6 +211,16 @@ class IngredientFormState extends State { _amountController.text = amount.toStringAsFixed(0); _mealItem.amount = amount; } + _selectedWeightUnit = null; + _mealItem.weightUnitId = null; + _mealItem.weightUnitObj = null; + }); + + // Load weight units for this ingredient + Provider.of(context, listen: false).fetchWeightUnits(id).then((units) { + setState(() { + _weightUnits = units; + }); }); } @@ -268,8 +281,43 @@ class IngredientFormState extends State { Expanded( child: TextFormField( key: const Key('field-weight'), - // needed ? - decoration: InputDecoration(labelText: i18n.weight), + decoration: InputDecoration( + labelText: i18n.weight, + suffix: _weightUnits.isNotEmpty + ? DropdownButton( + value: _selectedWeightUnit?.id, + underline: const SizedBox(), + isDense: true, + items: [ + DropdownMenuItem( + value: null, + child: Text(i18n.g), + ), + ..._weightUnits.map( + (unit) => DropdownMenuItem( + value: unit.id, + child: Text('${unit.name} (${unit.grams}g)'), + ), + ), + ], + onChanged: (value) { + setState(() { + if (value == null) { + _selectedWeightUnit = null; + _mealItem.weightUnitId = null; + _mealItem.weightUnitObj = null; + } else { + _selectedWeightUnit = _weightUnits.firstWhere( + (u) => u.id == value, + ); + _mealItem.weightUnitId = value; + _mealItem.weightUnitObj = _selectedWeightUnit; + } + }); + }, + ) + : Text(i18n.g), + ), controller: _amountController, keyboardType: textInputTypeDecimal, onChanged: (value) { @@ -443,7 +491,9 @@ class IngredientFormState extends State { child: ListTile( onTap: select, title: Text( - '${suggestions[index].ingredient.name} (${suggestions[index].amount.toStringAsFixed(0)}$unit)', + suggestions[index].weightUnitObj != null + ? '${suggestions[index].ingredient.name} (${suggestions[index].amount.toStringAsFixed(0)} × ${suggestions[index].weightUnitObj!.name})' + : '${suggestions[index].ingredient.name} (${suggestions[index].amount.toStringAsFixed(0)}$unit)', ), subtitle: Text( getShortNutritionValues( diff --git a/lib/widgets/nutrition/meal.dart b/lib/widgets/nutrition/meal.dart index 4ba99cf63..f155975ca 100644 --- a/lib/widgets/nutrition/meal.dart +++ b/lib/widgets/nutrition/meal.dart @@ -1,13 +1,13 @@ /* * This file is part of wger Workout Manager . - * Copyright (C) 2020, 2021 wger Team + * Copyright (c) 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * wger Workout Manager is distributed in the hope that it will be useful, + * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. @@ -229,20 +229,17 @@ class MealItemEditableFullTile extends StatelessWidget { @override Widget build(BuildContext context) { - // TODO(x): add real support for weight units - /* - String unit = _item.weightUnitId == null - ? AppLocalizations.of(context).g - : _item.weightUnitObj!.weightUnit.name; - - */ - final String unit = AppLocalizations.of(context).g; final values = _item.nutritionalValues; + final i18n = AppLocalizations.of(context); + + final String amountText = _item.weightUnitObj != null + ? '${_item.amount.toStringAsFixed(0)} × ${_item.weightUnitObj!.name}' + : i18n.gValue(_item.amount.toStringAsFixed(0)); return NutritionTile( leading: IngredientAvatar(ingredient: _item.ingredient), title: Text( - '${_item.amount.toStringAsFixed(0)}$unit ${_item.ingredient.name}', + '$amountText ${_item.ingredient.name}', overflow: TextOverflow.ellipsis, textAlign: TextAlign.left, ), @@ -252,7 +249,7 @@ class MealItemEditableFullTile extends StatelessWidget { trailing: _editing ? IconButton( icon: const Icon(Icons.delete, size: ICON_SIZE_SMALL), - tooltip: AppLocalizations.of(context).delete, + tooltip: i18n.delete, iconSize: ICON_SIZE_SMALL, onPressed: () { // Delete the meal item @@ -262,7 +259,7 @@ class MealItemEditableFullTile extends StatelessWidget { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( - AppLocalizations.of(context).successfullyDeleted, + i18n.successfullyDeleted, textAlign: TextAlign.center, ), ), diff --git a/lib/widgets/nutrition/nutrition_tiles.dart b/lib/widgets/nutrition/nutrition_tiles.dart index 36f20155b..a9ea2d3a3 100644 --- a/lib/widgets/nutrition/nutrition_tiles.dart +++ b/lib/widgets/nutrition/nutrition_tiles.dart @@ -70,7 +70,9 @@ class DiaryEntryTile extends StatelessWidget { style: const TextStyle(fontWeight: FontWeight.bold), ), title: Text( - '${AppLocalizations.of(context).gValue(diaryEntry.amount.toStringAsFixed(0))} ${diaryEntry.ingredient.name}', + diaryEntry.weightUnitObj != null + ? '${diaryEntry.amount.toStringAsFixed(0)} × ${diaryEntry.weightUnitObj!.name} ${diaryEntry.ingredient.name}' + : '${AppLocalizations.of(context).gValue(diaryEntry.amount.toStringAsFixed(0))} ${diaryEntry.ingredient.name}', overflow: TextOverflow.ellipsis, ), subtitle: getNutritionRow( diff --git a/test/core/settings_test.mocks.dart b/test/core/settings_test.mocks.dart index b2e735a57..37fd79eb4 100644 --- a/test/core/settings_test.mocks.dart +++ b/test/core/settings_test.mocks.dart @@ -6,7 +6,7 @@ import 'dart:async' as _i18; import 'dart:ui' as _i19; -import 'package:flutter/material.dart' as _i22; +import 'package:flutter/material.dart' as _i23; import 'package:http/http.dart' as _i16; import 'package:mockito/mockito.dart' as _i1; import 'package:shared_preferences/shared_preferences.dart' as _i14; @@ -18,15 +18,16 @@ import 'package:wger/models/exercises/exercise.dart' as _i4; import 'package:wger/models/exercises/language.dart' as _i8; import 'package:wger/models/exercises/muscle.dart' as _i7; import 'package:wger/models/nutrition/ingredient.dart' as _i13; +import 'package:wger/models/nutrition/ingredient_weight_unit.dart' as _i21; import 'package:wger/models/nutrition/meal.dart' as _i11; import 'package:wger/models/nutrition/meal_item.dart' as _i12; import 'package:wger/models/nutrition/nutritional_plan.dart' as _i10; -import 'package:wger/models/user/profile.dart' as _i23; +import 'package:wger/models/user/profile.dart' as _i24; import 'package:wger/providers/auth.dart' as _i15; import 'package:wger/providers/base_provider.dart' as _i2; import 'package:wger/providers/exercises.dart' as _i17; import 'package:wger/providers/nutrition.dart' as _i20; -import 'package:wger/providers/user.dart' as _i21; +import 'package:wger/providers/user.dart' as _i22; // ignore_for_file: type=lint // ignore_for_file: avoid_redundant_argument_values @@ -801,6 +802,18 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i20.NutritionPlans ) as _i18.Future<_i13.Ingredient>); + @override + _i18.Future> fetchWeightUnits( + int? ingredientId, + ) => + (super.noSuchMethod( + Invocation.method(#fetchWeightUnits, [ingredientId]), + returnValue: _i18.Future>.value( + <_i21.IngredientWeightUnit>[], + ), + ) + as _i18.Future>); + @override _i18.Future fetchIngredientsFromCache() => (super.noSuchMethod( @@ -915,18 +928,18 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i20.NutritionPlans /// A class which mocks [UserProvider]. /// /// See the documentation for Mockito's code generation for more information. -class MockUserProvider extends _i1.Mock implements _i21.UserProvider { +class MockUserProvider extends _i1.Mock implements _i22.UserProvider { MockUserProvider() { _i1.throwOnMissingStub(this); } @override - _i22.ThemeMode get themeMode => + _i23.ThemeMode get themeMode => (super.noSuchMethod( Invocation.getter(#themeMode), - returnValue: _i22.ThemeMode.system, + returnValue: _i23.ThemeMode.system, ) - as _i22.ThemeMode); + as _i23.ThemeMode); @override _i2.WgerBaseProvider get baseProvider => @@ -951,23 +964,23 @@ class MockUserProvider extends _i1.Mock implements _i21.UserProvider { as _i14.SharedPreferencesAsync); @override - List<_i21.DashboardWidget> get dashboardWidgets => + List<_i22.DashboardWidget> get dashboardWidgets => (super.noSuchMethod( Invocation.getter(#dashboardWidgets), - returnValue: <_i21.DashboardWidget>[], + returnValue: <_i22.DashboardWidget>[], ) - as List<_i21.DashboardWidget>); + as List<_i22.DashboardWidget>); @override - List<_i21.DashboardWidget> get allDashboardWidgets => + List<_i22.DashboardWidget> get allDashboardWidgets => (super.noSuchMethod( Invocation.getter(#allDashboardWidgets), - returnValue: <_i21.DashboardWidget>[], + returnValue: <_i22.DashboardWidget>[], ) - as List<_i21.DashboardWidget>); + as List<_i22.DashboardWidget>); @override - set themeMode(_i22.ThemeMode? value) => super.noSuchMethod( + set themeMode(_i23.ThemeMode? value) => super.noSuchMethod( Invocation.setter(#themeMode, value), returnValueForMissingStub: null, ); @@ -979,7 +992,7 @@ class MockUserProvider extends _i1.Mock implements _i21.UserProvider { ); @override - set profile(_i23.Profile? value) => super.noSuchMethod( + set profile(_i24.Profile? value) => super.noSuchMethod( Invocation.setter(#profile, value), returnValueForMissingStub: null, ); @@ -995,7 +1008,7 @@ class MockUserProvider extends _i1.Mock implements _i21.UserProvider { ); @override - bool isDashboardWidgetVisible(_i21.DashboardWidget? key) => + bool isDashboardWidgetVisible(_i22.DashboardWidget? key) => (super.noSuchMethod( Invocation.method(#isDashboardWidgetVisible, [key]), returnValue: false, @@ -1004,7 +1017,7 @@ class MockUserProvider extends _i1.Mock implements _i21.UserProvider { @override _i18.Future setDashboardWidgetVisible( - _i21.DashboardWidget? key, + _i22.DashboardWidget? key, bool? visible, ) => (super.noSuchMethod( @@ -1024,7 +1037,7 @@ class MockUserProvider extends _i1.Mock implements _i21.UserProvider { as _i18.Future); @override - void setThemeMode(_i22.ThemeMode? mode) => super.noSuchMethod( + void setThemeMode(_i23.ThemeMode? mode) => super.noSuchMethod( Invocation.method(#setThemeMode, [mode]), returnValueForMissingStub: null, ); diff --git a/test/core/validators_test.mocks.dart b/test/core/validators_test.mocks.dart index 3992c34b7..809142890 100644 --- a/test/core/validators_test.mocks.dart +++ b/test/core/validators_test.mocks.dart @@ -2677,6 +2677,17 @@ class MockAppLocalizations extends _i1.Mock implements _i2.AppLocalizations { ) as String); + @override + String get serverUpdateTitle => + (super.noSuchMethod( + Invocation.getter(#serverUpdateTitle), + returnValue: _i3.dummyValue( + this, + Invocation.getter(#serverUpdateTitle), + ), + ) + as String); + @override String get productFound => (super.noSuchMethod( @@ -3920,6 +3931,17 @@ class MockAppLocalizations extends _i1.Mock implements _i2.AppLocalizations { ) as String); + @override + String setNr(String? nr) => + (super.noSuchMethod( + Invocation.method(#setNr, [nr]), + returnValue: _i3.dummyValue( + this, + Invocation.method(#setNr, [nr]), + ), + ) + as String); + @override String topSet(String? value) => (super.noSuchMethod( @@ -4063,6 +4085,17 @@ class MockAppLocalizations extends _i1.Mock implements _i2.AppLocalizations { ) as String); + @override + String serverUpdateContent(String? minVersion) => + (super.noSuchMethod( + Invocation.method(#serverUpdateContent, [minVersion]), + returnValue: _i3.dummyValue( + this, + Invocation.method(#serverUpdateContent, [minVersion]), + ), + ) + as String); + @override String productFoundDescription(String? productName) => (super.noSuchMethod( diff --git a/test/nutrition/nutritional_meal_form_test.mocks.dart b/test/nutrition/nutritional_meal_form_test.mocks.dart index fa16966bd..42a82a330 100644 --- a/test/nutrition/nutritional_meal_form_test.mocks.dart +++ b/test/nutrition/nutritional_meal_form_test.mocks.dart @@ -4,11 +4,12 @@ // ignore_for_file: no_leading_underscores_for_library_prefixes import 'dart:async' as _i9; -import 'dart:ui' as _i10; +import 'dart:ui' as _i11; import 'package:mockito/mockito.dart' as _i1; import 'package:wger/database/ingredients/ingredients_database.dart' as _i3; import 'package:wger/models/nutrition/ingredient.dart' as _i7; +import 'package:wger/models/nutrition/ingredient_weight_unit.dart' as _i10; import 'package:wger/models/nutrition/meal.dart' as _i5; import 'package:wger/models/nutrition/meal_item.dart' as _i6; import 'package:wger/models/nutrition/nutritional_plan.dart' as _i4; @@ -318,6 +319,18 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i8.NutritionPlansP ) as _i9.Future<_i7.Ingredient>); + @override + _i9.Future> fetchWeightUnits( + int? ingredientId, + ) => + (super.noSuchMethod( + Invocation.method(#fetchWeightUnits, [ingredientId]), + returnValue: _i9.Future>.value( + <_i10.IngredientWeightUnit>[], + ), + ) + as _i9.Future>); + @override _i9.Future fetchIngredientsFromCache() => (super.noSuchMethod( @@ -405,13 +418,13 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i8.NutritionPlansP as _i9.Future); @override - void addListener(_i10.VoidCallback? listener) => super.noSuchMethod( + void addListener(_i11.VoidCallback? listener) => super.noSuchMethod( Invocation.method(#addListener, [listener]), returnValueForMissingStub: null, ); @override - void removeListener(_i10.VoidCallback? listener) => super.noSuchMethod( + void removeListener(_i11.VoidCallback? listener) => super.noSuchMethod( Invocation.method(#removeListener, [listener]), returnValueForMissingStub: null, ); diff --git a/test/nutrition/nutritional_meal_item_form_test.dart b/test/nutrition/nutritional_meal_item_form_test.dart index 536e8dfbe..bfb18bd5e 100644 --- a/test/nutrition/nutritional_meal_item_form_test.dart +++ b/test/nutrition/nutritional_meal_item_form_test.dart @@ -108,6 +108,8 @@ void main() { ); when(mockNutrition.addMealItem(any, meal1)).thenAnswer((_) => Future.value(mealItem)); + when(mockNutrition.fetchWeightUnits(1)).thenAnswer((_) => Future.value([])); + when(mockNutrition.fetchWeightUnits(2)).thenAnswer((_) => Future.value([])); }); Widget createMealItemFormScreen(Meal meal, String code, bool test, {locale = 'en'}) { diff --git a/test/nutrition/nutritional_plan_form_test.mocks.dart b/test/nutrition/nutritional_plan_form_test.mocks.dart index 525bf8298..161cacdee 100644 --- a/test/nutrition/nutritional_plan_form_test.mocks.dart +++ b/test/nutrition/nutritional_plan_form_test.mocks.dart @@ -4,11 +4,12 @@ // ignore_for_file: no_leading_underscores_for_library_prefixes import 'dart:async' as _i9; -import 'dart:ui' as _i10; +import 'dart:ui' as _i11; import 'package:mockito/mockito.dart' as _i1; import 'package:wger/database/ingredients/ingredients_database.dart' as _i3; import 'package:wger/models/nutrition/ingredient.dart' as _i7; +import 'package:wger/models/nutrition/ingredient_weight_unit.dart' as _i10; import 'package:wger/models/nutrition/meal.dart' as _i5; import 'package:wger/models/nutrition/meal_item.dart' as _i6; import 'package:wger/models/nutrition/nutritional_plan.dart' as _i4; @@ -318,6 +319,18 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i8.NutritionPlansP ) as _i9.Future<_i7.Ingredient>); + @override + _i9.Future> fetchWeightUnits( + int? ingredientId, + ) => + (super.noSuchMethod( + Invocation.method(#fetchWeightUnits, [ingredientId]), + returnValue: _i9.Future>.value( + <_i10.IngredientWeightUnit>[], + ), + ) + as _i9.Future>); + @override _i9.Future fetchIngredientsFromCache() => (super.noSuchMethod( @@ -405,13 +418,13 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i8.NutritionPlansP as _i9.Future); @override - void addListener(_i10.VoidCallback? listener) => super.noSuchMethod( + void addListener(_i11.VoidCallback? listener) => super.noSuchMethod( Invocation.method(#addListener, [listener]), returnValueForMissingStub: null, ); @override - void removeListener(_i10.VoidCallback? listener) => super.noSuchMethod( + void removeListener(_i11.VoidCallback? listener) => super.noSuchMethod( Invocation.method(#removeListener, [listener]), returnValueForMissingStub: null, ); diff --git a/test/nutrition/nutritional_plan_model_test.dart b/test/nutrition/nutritional_plan_model_test.dart index 40069d162..0ece7f208 100644 --- a/test/nutrition/nutritional_plan_model_test.dart +++ b/test/nutrition/nutritional_plan_model_test.dart @@ -17,6 +17,8 @@ */ import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/models/nutrition/ingredient_weight_unit.dart'; +import 'package:wger/models/nutrition/meal_item.dart'; import 'package:wger/models/nutrition/nutritional_goals.dart'; import 'package:wger/models/nutrition/nutritional_plan.dart'; import 'package:wger/models/nutrition/nutritional_values.dart'; @@ -112,4 +114,33 @@ void main() { expect(plan.dedupMealItems, plan.meals[0].mealItems + plan.meals[1].mealItems); }); }); + + group('weight unit tests', () { + test('MealItem nutritionalValues without weight unit uses grams', () { + final item = MealItem(ingredientId: 1, amount: 200); + item.ingredient = ingredient1; + // ingredient1: energy=500 per 100g, so 200g = 1000 + expect(item.nutritionalValues.energy, closeTo(1000, 0.01)); + expect(item.nutritionalValues.protein, closeTo(10, 0.01)); + }); + + test('MealItem nutritionalValues with weight unit', () { + final unit = const IngredientWeightUnit(id: 1, name: 'Serving', grams: 50); + final item = MealItem(ingredientId: 1, amount: 3); + item.ingredient = ingredient1; + item.weightUnitObj = unit; + // 3 servings * 50g = 150g, energy = 500 * 150 / 100 = 750 + expect(item.nutritionalValues.energy, closeTo(750, 0.01)); + expect(item.nutritionalValues.protein, closeTo(7.5, 0.01)); + }); + + test('MealItem nutritionalValues with weight unit amount 1', () { + final unit = const IngredientWeightUnit(id: 2, name: 'Cup', grams: 240); + final item = MealItem(ingredientId: 1, amount: 1); + item.ingredient = ingredient1; + item.weightUnitObj = unit; + // 1 * 240g, energy = 500 * 240 / 100 = 1200 + expect(item.nutritionalValues.energy, closeTo(1200, 0.01)); + }); + }); } diff --git a/test/nutrition/nutritional_plan_screen_test.dart b/test/nutrition/nutritional_plan_screen_test.dart index 71b87d860..af8b456ca 100644 --- a/test/nutrition/nutritional_plan_screen_test.dart +++ b/test/nutrition/nutritional_plan_screen_test.dart @@ -1,13 +1,13 @@ /* * This file is part of wger Workout Manager . - * Copyright (C) 2020, 2021 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * wger Workout Manager is distributed in the hope that it will be useful, + * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. @@ -108,8 +108,8 @@ void main() { expect(find.text('Less fat, more protein'), findsOneWidget); expect(find.byIcon(Icons.info_outline), findsNWidgets(3)); // 2 meals, 1 "other logs" expect(find.byIcon(Icons.info), findsNothing); - expect(find.text('100g Water'), findsNothing); - expect(find.text('75g Burger soup'), findsNothing); + expect(find.text('100 g Water'), findsNothing); + expect(find.text('75 g Burger soup'), findsNothing); // tap the first info button changes it and reveals ingredients for the first meal var infoOutlineButtons = find.byIcon(Icons.info_outline); @@ -124,8 +124,8 @@ void main() { } // Ingredients show up now - expect(find.text('100g Water'), findsOneWidget); - expect(find.text('75g Burger soup'), findsOneWidget); + expect(find.text('100 g Water'), findsOneWidget); + expect(find.text('75 g Burger soup'), findsOneWidget); // .. and the button icon has changed expect(find.byIcon(Icons.info_outline), findsNWidgets(2)); @@ -136,7 +136,7 @@ void main() { infoOutlineButtons = find.byIcon(Icons.info_outline); await tester.scrollUntilVisible(infoOutlineButtons.first, 30); - expect(find.text('300g Broccoli cake'), findsNothing); + expect(find.text('300 g Broccoli cake'), findsNothing); await tester.tap(infoOutlineButtons.first); await tester.pumpAndSettle(); @@ -152,7 +152,7 @@ void main() { expect(find.byIcon(Icons.info), findsNWidgets(2)); await tester.scrollUntilVisible(find.text('300g Broccoli cake'), 30); - expect(find.text('300g Broccoli cake'), findsOneWidget); + expect(find.text('300 g Broccoli cake'), findsOneWidget); expect(find.byType(Card), findsNWidgets(3)); diff --git a/test/nutrition/nutritional_plan_screen_test.mocks.dart b/test/nutrition/nutritional_plan_screen_test.mocks.dart index 2f72550c6..774ecba72 100644 --- a/test/nutrition/nutritional_plan_screen_test.mocks.dart +++ b/test/nutrition/nutritional_plan_screen_test.mocks.dart @@ -327,6 +327,14 @@ class MockAuthProvider extends _i1.Mock implements _i2.AuthProvider { ) as _i5.Future); + @override + bool serverUpdateRequired([String? version]) => + (super.noSuchMethod( + Invocation.method(#serverUpdateRequired, [version]), + returnValue: false, + ) + as bool); + @override _i5.Future<_i2.LoginActions> register({ required String? username, diff --git a/test/nutrition/nutritional_plans_screen_test.mocks.dart b/test/nutrition/nutritional_plans_screen_test.mocks.dart index ccea3a560..79c80bd21 100644 --- a/test/nutrition/nutritional_plans_screen_test.mocks.dart +++ b/test/nutrition/nutritional_plans_screen_test.mocks.dart @@ -177,6 +177,14 @@ class MockAuthProvider extends _i1.Mock implements _i3.AuthProvider { ) as _i5.Future); + @override + bool serverUpdateRequired([String? version]) => + (super.noSuchMethod( + Invocation.method(#serverUpdateRequired, [version]), + returnValue: false, + ) + as bool); + @override _i5.Future<_i3.LoginActions> register({ required String? username, diff --git a/test/routine/gym_mode/session_page_test.dart b/test/routine/gym_mode/session_page_test.dart index 60382d66f..d62ce44a1 100644 --- a/test/routine/gym_mode/session_page_test.dart +++ b/test/routine/gym_mode/session_page_test.dart @@ -1,6 +1,6 @@ /* * This file is part of wger Workout Manager . - * Copyright (c) 2020 - 2025 wger Team + * Copyright (c) 2020 - 2026 wger Team * * wger Workout Manager is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -90,7 +90,6 @@ void main() { await tester.pumpWidget(renderSessionPage()); await tester.pumpAndSettle(); - debugDumpApp(); expect(find.text('10:00'), findsOneWidget); expect(find.text('12:34'), findsOneWidget); expect(find.text('This is a note'), findsOneWidget); diff --git a/test/weight/weight_screen_test.mocks.dart b/test/weight/weight_screen_test.mocks.dart index 7da919bf8..038f0df7b 100644 --- a/test/weight/weight_screen_test.mocks.dart +++ b/test/weight/weight_screen_test.mocks.dart @@ -12,6 +12,7 @@ import 'package:shared_preferences/shared_preferences.dart' as _i4; import 'package:wger/database/ingredients/ingredients_database.dart' as _i5; import 'package:wger/models/body_weight/weight_entry.dart' as _i3; import 'package:wger/models/nutrition/ingredient.dart' as _i9; +import 'package:wger/models/nutrition/ingredient_weight_unit.dart' as _i17; import 'package:wger/models/nutrition/meal.dart' as _i7; import 'package:wger/models/nutrition/meal_item.dart' as _i8; import 'package:wger/models/nutrition/nutritional_plan.dart' as _i6; @@ -623,6 +624,18 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i16.NutritionPlans ) as _i11.Future<_i9.Ingredient>); + @override + _i11.Future> fetchWeightUnits( + int? ingredientId, + ) => + (super.noSuchMethod( + Invocation.method(#fetchWeightUnits, [ingredientId]), + returnValue: _i11.Future>.value( + <_i17.IngredientWeightUnit>[], + ), + ) + as _i11.Future>); + @override _i11.Future fetchIngredientsFromCache() => (super.noSuchMethod(