mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 17:52:35 +01:00
Expose units in the nutritional plan
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -7,18 +7,11 @@ part of 'ingredient_weight_unit.dart';
|
||||
// **************************************************************************
|
||||
|
||||
IngredientWeightUnit _$IngredientWeightUnitFromJson(Map<String, dynamic> 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<String, dynamic>,
|
||||
),
|
||||
ingredient: Ingredient.fromJson(json['ingredient'] as Map<String, dynamic>),
|
||||
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<String, dynamic> _$IngredientWeightUnitToJson(
|
||||
IngredientWeightUnit instance,
|
||||
) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'weight_unit': instance.weightUnit,
|
||||
'ingredient': instance.ingredient,
|
||||
'grams': instance.grams,
|
||||
'amount': instance.amount,
|
||||
'name': instance.name,
|
||||
'gram': instance.grams,
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -72,24 +72,10 @@ class MealItem {
|
||||
Map<String, dynamic> 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({
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<String, dynamic> json) => _$WeightUnitFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$WeightUnitToJson(this);
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'weight_unit.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
WeightUnit _$WeightUnitFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json, requiredKeys: const ['id', 'name']);
|
||||
return WeightUnit(
|
||||
id: (json['id'] as num).toInt(),
|
||||
name: json['name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$WeightUnitToJson(WeightUnit instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'name': instance.name,
|
||||
};
|
||||
@@ -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<List<IngredientWeightUnit>> 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<void> 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<String> 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(
|
||||
|
||||
@@ -33,7 +33,7 @@ final class IngredientFiltersNotifierProvider
|
||||
IngredientFiltersNotifier create() => IngredientFiltersNotifier();
|
||||
}
|
||||
|
||||
String _$ingredientFiltersNotifierHash() => r'e9a4717c081f85ed6daef56f06c620b08bee4e0c';
|
||||
String _$ingredientFiltersNotifierHash() => r'839633dd545b6a9a6cbb8840098fe15f7c827897';
|
||||
|
||||
abstract class _$IngredientFiltersNotifier extends $AsyncNotifier<IngredientFilters> {
|
||||
FutureOr<IngredientFilters> build();
|
||||
|
||||
@@ -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<IngredientForm> {
|
||||
final _timeController = TextEditingController(text: ''); // optional
|
||||
final _mealItem = MealItem.empty();
|
||||
var _searchQuery = ''; // copy from typeahead. for filtering suggestions
|
||||
List<IngredientWeightUnit> _weightUnits = [];
|
||||
IngredientWeightUnit? _selectedWeightUnit;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
@@ -208,6 +211,16 @@ class IngredientFormState extends State<IngredientForm> {
|
||||
_amountController.text = amount.toStringAsFixed(0);
|
||||
_mealItem.amount = amount;
|
||||
}
|
||||
_selectedWeightUnit = null;
|
||||
_mealItem.weightUnitId = null;
|
||||
_mealItem.weightUnitObj = null;
|
||||
});
|
||||
|
||||
// Load weight units for this ingredient
|
||||
Provider.of<NutritionPlansProvider>(context, listen: false).fetchWeightUnits(id).then((units) {
|
||||
setState(() {
|
||||
_weightUnits = units;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -268,8 +281,43 @@ class IngredientFormState extends State<IngredientForm> {
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
key: const Key('field-weight'),
|
||||
// needed ?
|
||||
decoration: InputDecoration(labelText: i18n.weight),
|
||||
decoration: InputDecoration(
|
||||
labelText: i18n.weight,
|
||||
suffix: _weightUnits.isNotEmpty
|
||||
? DropdownButton<int?>(
|
||||
value: _selectedWeightUnit?.id,
|
||||
underline: const SizedBox(),
|
||||
isDense: true,
|
||||
items: [
|
||||
DropdownMenuItem<int?>(
|
||||
value: null,
|
||||
child: Text(i18n.g),
|
||||
),
|
||||
..._weightUnits.map(
|
||||
(unit) => DropdownMenuItem<int?>(
|
||||
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<IngredientForm> {
|
||||
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(
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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,
|
||||
),
|
||||
),
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<List<_i21.IngredientWeightUnit>> fetchWeightUnits(
|
||||
int? ingredientId,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchWeightUnits, [ingredientId]),
|
||||
returnValue: _i18.Future<List<_i21.IngredientWeightUnit>>.value(
|
||||
<_i21.IngredientWeightUnit>[],
|
||||
),
|
||||
)
|
||||
as _i18.Future<List<_i21.IngredientWeightUnit>>);
|
||||
|
||||
@override
|
||||
_i18.Future<void> 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<void> 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<void>);
|
||||
|
||||
@override
|
||||
void setThemeMode(_i22.ThemeMode? mode) => super.noSuchMethod(
|
||||
void setThemeMode(_i23.ThemeMode? mode) => super.noSuchMethod(
|
||||
Invocation.method(#setThemeMode, [mode]),
|
||||
returnValueForMissingStub: null,
|
||||
);
|
||||
|
||||
@@ -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<String>(
|
||||
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<String>(
|
||||
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<String>(
|
||||
this,
|
||||
Invocation.method(#serverUpdateContent, [minVersion]),
|
||||
),
|
||||
)
|
||||
as String);
|
||||
|
||||
@override
|
||||
String productFoundDescription(String? productName) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
@@ -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<List<_i10.IngredientWeightUnit>> fetchWeightUnits(
|
||||
int? ingredientId,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchWeightUnits, [ingredientId]),
|
||||
returnValue: _i9.Future<List<_i10.IngredientWeightUnit>>.value(
|
||||
<_i10.IngredientWeightUnit>[],
|
||||
),
|
||||
)
|
||||
as _i9.Future<List<_i10.IngredientWeightUnit>>);
|
||||
|
||||
@override
|
||||
_i9.Future<void> fetchIngredientsFromCache() =>
|
||||
(super.noSuchMethod(
|
||||
@@ -405,13 +418,13 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i8.NutritionPlansP
|
||||
as _i9.Future<void>);
|
||||
|
||||
@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,
|
||||
);
|
||||
|
||||
@@ -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'}) {
|
||||
|
||||
@@ -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<List<_i10.IngredientWeightUnit>> fetchWeightUnits(
|
||||
int? ingredientId,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchWeightUnits, [ingredientId]),
|
||||
returnValue: _i9.Future<List<_i10.IngredientWeightUnit>>.value(
|
||||
<_i10.IngredientWeightUnit>[],
|
||||
),
|
||||
)
|
||||
as _i9.Future<List<_i10.IngredientWeightUnit>>);
|
||||
|
||||
@override
|
||||
_i9.Future<void> fetchIngredientsFromCache() =>
|
||||
(super.noSuchMethod(
|
||||
@@ -405,13 +418,13 @@ class MockNutritionPlansProvider extends _i1.Mock implements _i8.NutritionPlansP
|
||||
as _i9.Future<void>);
|
||||
|
||||
@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,
|
||||
);
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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));
|
||||
|
||||
|
||||
@@ -327,6 +327,14 @@ class MockAuthProvider extends _i1.Mock implements _i2.AuthProvider {
|
||||
)
|
||||
as _i5.Future<bool>);
|
||||
|
||||
@override
|
||||
bool serverUpdateRequired([String? version]) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#serverUpdateRequired, [version]),
|
||||
returnValue: false,
|
||||
)
|
||||
as bool);
|
||||
|
||||
@override
|
||||
_i5.Future<_i2.LoginActions> register({
|
||||
required String? username,
|
||||
|
||||
@@ -177,6 +177,14 @@ class MockAuthProvider extends _i1.Mock implements _i3.AuthProvider {
|
||||
)
|
||||
as _i5.Future<bool>);
|
||||
|
||||
@override
|
||||
bool serverUpdateRequired([String? version]) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#serverUpdateRequired, [version]),
|
||||
returnValue: false,
|
||||
)
|
||||
as bool);
|
||||
|
||||
@override
|
||||
_i5.Future<_i3.LoginActions> register({
|
||||
required String? username,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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);
|
||||
|
||||
@@ -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<List<_i17.IngredientWeightUnit>> fetchWeightUnits(
|
||||
int? ingredientId,
|
||||
) =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchWeightUnits, [ingredientId]),
|
||||
returnValue: _i11.Future<List<_i17.IngredientWeightUnit>>.value(
|
||||
<_i17.IngredientWeightUnit>[],
|
||||
),
|
||||
)
|
||||
as _i11.Future<List<_i17.IngredientWeightUnit>>);
|
||||
|
||||
@override
|
||||
_i11.Future<void> fetchIngredientsFromCache() =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
Reference in New Issue
Block a user