mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-01 02:02:35 +01:00
296 lines
9.2 KiB
Dart
296 lines
9.2 KiB
Dart
/*
|
|
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
|
* 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.
|
|
*
|
|
* 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:collection/collection.dart';
|
|
import 'package:drift/drift.dart' as drift;
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:wger/core/consts.dart';
|
|
import 'package:wger/database/powersync/database.dart';
|
|
import 'package:wger/features/nutrition/models/log.dart';
|
|
import 'package:wger/features/nutrition/models/meal.dart';
|
|
import 'package:wger/features/nutrition/models/meal_item.dart';
|
|
import 'package:wger/features/nutrition/models/nutritional_goals.dart';
|
|
import 'package:wger/features/nutrition/models/nutritional_values.dart';
|
|
import 'package:wger/l10n/generated/app_localizations.dart';
|
|
|
|
class NutritionalPlan {
|
|
/// Inclusive upper bound for [description]
|
|
static const maxDescriptionChars = 80;
|
|
|
|
/// Inclusive upper bounds for the nutritional goal fields
|
|
static const maxGoalEnergy = 6000;
|
|
static const maxGoalProtein = 500;
|
|
static const maxGoalCarbohydrates = 750;
|
|
static const maxGoalFat = 500;
|
|
static const maxGoalFiber = 500;
|
|
|
|
final _logger = Logger('NutritionalPlan Model');
|
|
|
|
/// Client-generated UUID, is `null` only before the first persist
|
|
String? id;
|
|
|
|
late String description;
|
|
|
|
late DateTime creationDate;
|
|
|
|
late DateTime startDate;
|
|
|
|
late DateTime? endDate;
|
|
|
|
late bool onlyLogging;
|
|
|
|
late num? goalEnergy;
|
|
|
|
late num? goalProtein;
|
|
|
|
late num? goalCarbohydrates;
|
|
|
|
late num? goalFat;
|
|
|
|
late num? goalFiber;
|
|
|
|
late bool hasGoalCalories;
|
|
|
|
List<Meal> meals = [];
|
|
|
|
List<LogItem> diaryEntries = [];
|
|
|
|
NutritionalPlan({
|
|
this.id,
|
|
required this.description,
|
|
DateTime? creationDate,
|
|
required this.startDate,
|
|
this.endDate,
|
|
this.onlyLogging = false,
|
|
this.hasGoalCalories = false,
|
|
this.goalEnergy,
|
|
this.goalProtein,
|
|
this.goalCarbohydrates,
|
|
this.goalFat,
|
|
this.goalFiber,
|
|
List<Meal>? meals,
|
|
List<LogItem>? diaryEntries,
|
|
}) : creationDate = creationDate ?? DateTime.now() {
|
|
this.meals = meals ?? [];
|
|
this.diaryEntries = diaryEntries ?? [];
|
|
|
|
if (endDate != null && endDate!.isBefore(startDate)) {
|
|
_logger.warning(
|
|
'The end date of a nutritional plan is before the start. Setting to null! '
|
|
'PlanId: $id, startDate: $startDate, endDate: $endDate',
|
|
);
|
|
endDate = null;
|
|
}
|
|
}
|
|
|
|
NutritionalPlan.empty() {
|
|
creationDate = DateTime.now();
|
|
startDate = DateTime.now();
|
|
endDate = null;
|
|
description = '';
|
|
onlyLogging = false;
|
|
hasGoalCalories = false;
|
|
goalEnergy = null;
|
|
goalProtein = null;
|
|
goalCarbohydrates = null;
|
|
goalFiber = null;
|
|
goalFat = null;
|
|
}
|
|
|
|
/// Drift companion for inserts/updates against `nutrition_nutritionplan`.
|
|
///
|
|
/// If [id] is null, Drift's `clientDefault` mints a fresh UUID on insert.
|
|
/// If set, the value round-trips into the row as-is.
|
|
NutritionalPlanTableCompanion toCompanion() {
|
|
return NutritionalPlanTableCompanion(
|
|
id: id != null ? drift.Value(id!) : const drift.Value.absent(),
|
|
description: drift.Value(description),
|
|
creationDate: drift.Value(creationDate.toUtc()),
|
|
// `start`/`end` are `DateField` server-side
|
|
startDate: drift.Value(DateTime.utc(startDate.year, startDate.month, startDate.day)),
|
|
endDate: endDate == null
|
|
? const drift.Value.absent()
|
|
: drift.Value(DateTime.utc(endDate!.year, endDate!.month, endDate!.day)),
|
|
onlyLogging: drift.Value(onlyLogging),
|
|
hasGoalCalories: drift.Value(hasGoalCalories),
|
|
goalEnergy: goalEnergy == null
|
|
? const drift.Value.absent()
|
|
: drift.Value(goalEnergy!.toInt()),
|
|
goalProtein: goalProtein == null
|
|
? const drift.Value.absent()
|
|
: drift.Value(goalProtein!.toInt()),
|
|
goalCarbohydrates: goalCarbohydrates == null
|
|
? const drift.Value.absent()
|
|
: drift.Value(goalCarbohydrates!.toInt()),
|
|
goalFiber: goalFiber == null ? const drift.Value.absent() : drift.Value(goalFiber!.toInt()),
|
|
goalFat: goalFat == null ? const drift.Value.absent() : drift.Value(goalFat!.toInt()),
|
|
);
|
|
}
|
|
|
|
String getLabel(BuildContext context) {
|
|
return description != '' ? description : AppLocalizations.of(context).nutritionalPlan;
|
|
}
|
|
|
|
bool get hasAnyGoals {
|
|
return goalEnergy != null ||
|
|
goalFat != null ||
|
|
goalProtein != null ||
|
|
goalCarbohydrates != null;
|
|
}
|
|
|
|
bool get hasAnyAdvancedGoals {
|
|
return goalFiber != null;
|
|
}
|
|
|
|
/// Calculations
|
|
///
|
|
/// note that (some of) this is already done on the server. It might be better
|
|
/// to read it from there, but on the other hand we might want to do more locally
|
|
/// so that a mostly offline mode is possible.
|
|
NutritionalGoals get nutritionalGoals {
|
|
// If there are set goals, they take preference over any meals
|
|
if (hasAnyGoals || hasAnyAdvancedGoals) {
|
|
return NutritionalGoals(
|
|
energy: goalEnergy?.toDouble(),
|
|
fat: goalFat?.toDouble(),
|
|
protein: goalProtein?.toDouble(),
|
|
carbohydrates: goalCarbohydrates?.toDouble(),
|
|
fiber: goalFiber?.toDouble(),
|
|
);
|
|
}
|
|
// if there are no set goals and no defined meals, the goals are still undefined
|
|
if (meals.isEmpty) {
|
|
return NutritionalGoals();
|
|
}
|
|
// otherwise, add up all the nutritional values of the meals and use that as goals
|
|
final sumValues = meals.fold(
|
|
NutritionalValues(),
|
|
(a, b) => a + b.plannedNutritionalValues,
|
|
);
|
|
return NutritionalGoals(
|
|
energy: sumValues.energy,
|
|
fat: sumValues.fat,
|
|
protein: sumValues.protein,
|
|
carbohydrates: sumValues.carbohydrates,
|
|
carbohydratesSugar: sumValues.carbohydratesSugar,
|
|
fatSaturated: sumValues.fatSaturated,
|
|
fiber: sumValues.fiber,
|
|
sodium: sumValues.sodium,
|
|
);
|
|
}
|
|
|
|
NutritionalValues get loggedNutritionalValuesToday {
|
|
final now = DateTime.now();
|
|
final today = DateTime(now.year, now.month, now.day);
|
|
|
|
return logEntriesValues.containsKey(today) ? logEntriesValues[today]! : NutritionalValues();
|
|
}
|
|
|
|
NutritionalValues get loggedNutritionalValues7DayAvg {
|
|
final currentDate = DateTime.now();
|
|
final sevenDaysAgo = currentDate.subtract(const Duration(days: 7));
|
|
|
|
return diaryEntries
|
|
.where((obj) => obj.datetime.isAfter(sevenDaysAgo))
|
|
.fold(NutritionalValues(), (a, b) => a + b.nutritionalValues) /
|
|
7;
|
|
}
|
|
|
|
Map<DateTime, NutritionalValues> get logEntriesValues {
|
|
final out = <DateTime, NutritionalValues>{};
|
|
for (final log in diaryEntries) {
|
|
final date = DateTime(log.datetime.year, log.datetime.month, log.datetime.day);
|
|
|
|
if (!out.containsKey(date)) {
|
|
out[date] = NutritionalValues();
|
|
}
|
|
|
|
out[date]!.add(log.nutritionalValues);
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/// Returns the nutritional values for the given date
|
|
NutritionalValues? getValuesForDate(DateTime date) {
|
|
final values = logEntriesValues;
|
|
final dateKey = DateTime(date.year, date.month, date.day);
|
|
|
|
return values.containsKey(dateKey) ? values[dateKey] : null;
|
|
}
|
|
|
|
/// Returns the nutritional logs for the given date
|
|
List<LogItem> getLogsForDate(DateTime date) {
|
|
final List<LogItem> out = [];
|
|
for (final log in diaryEntries) {
|
|
final dateKey = DateTime(date.year, date.month, date.day);
|
|
final logKey = DateTime(log.datetime.year, log.datetime.month, log.datetime.day);
|
|
|
|
if (dateKey == logKey) {
|
|
out.add(log);
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
/// 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 found = out.firstWhereOrNull(
|
|
(e) => e.amount == mealItem.amount && e.ingredientId == mealItem.ingredientId,
|
|
);
|
|
|
|
if (found == null) {
|
|
out.add(mealItem);
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/// returns diary entries
|
|
/// deduped by the combination of amount and ingredient ID
|
|
List<LogItem> get dedupDiaryEntries {
|
|
final out = <LogItem>[];
|
|
for (final log in diaryEntries) {
|
|
final found = out.firstWhereOrNull(
|
|
(e) => e.amount == log.amount && e.ingredientId == log.ingredientId,
|
|
);
|
|
if (found == null) {
|
|
out.add(log);
|
|
}
|
|
}
|
|
out.sort((a, b) => b.datetime.compareTo(a.datetime));
|
|
return out;
|
|
}
|
|
|
|
Meal pseudoMealOthers(String name) {
|
|
return Meal(
|
|
id: PSEUDO_MEAL_ID,
|
|
plan: id,
|
|
name: name,
|
|
diaryEntries: diaryEntries.where((e) => e.mealId == null).toList(),
|
|
);
|
|
}
|
|
}
|