Files
flutter/lib/models/nutrition/log.dart
2024-05-20 22:06:47 +02:00

104 lines
3.2 KiB
Dart

/*
* 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';
import 'package:wger/helpers/json.dart';
import 'package:wger/models/nutrition/ingredient.dart';
import 'package:wger/models/nutrition/ingredient_weight_unit.dart';
import 'package:wger/models/nutrition/meal_item.dart';
import 'package:wger/models/nutrition/nutritional_values.dart';
part 'log.g.dart';
@JsonSerializable()
class Log {
@JsonKey(required: true)
int? id;
@JsonKey(required: false, name: 'meal')
int? mealId;
@JsonKey(required: true, name: 'plan')
int planId;
@JsonKey(required: true)
late DateTime datetime;
String? comment;
@JsonKey(required: true, name: 'ingredient')
late int ingredientId;
@JsonKey(includeFromJson: false, includeToJson: false)
late Ingredient ingredient;
@JsonKey(required: true, name: 'weight_unit')
int? weightUnitId;
@JsonKey(includeFromJson: false, includeToJson: false)
IngredientWeightUnit? weightUnitObj;
@JsonKey(required: true, fromJson: stringToNum)
late num amount;
Log({
this.id,
required this.mealId,
required this.ingredientId,
required this.weightUnitId,
required this.amount,
required this.planId,
required this.datetime,
this.comment,
});
Log.fromMealItem(MealItem mealItem, this.planId, this.mealId, [DateTime? dateTime]) {
ingredientId = mealItem.ingredientId;
ingredient = mealItem.ingredient;
weightUnitId = mealItem.weightUnitId;
datetime = dateTime ?? DateTime.now();
amount = mealItem.amount;
}
// Boilerplate
factory Log.fromJson(Map<String, dynamic> json) => _$LogFromJson(json);
Map<String, dynamic> toJson() => _$LogToJson(this);
/// Calculations
NutritionalValues get nutritionalValues {
// This is already done on the server. It might be better to read it from there.
final out = NutritionalValues();
//final weight = amount;
final weight =
weightUnitObj == null ? amount : amount * weightUnitObj!.amount * weightUnitObj!.grams;
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.fibers = ingredient.fibers * weight / 100;
out.sodium = ingredient.sodium * weight / 100;
return out;
}
}