Files
flutter/lib/features/nutrition/models/log.dart
2026-07-01 17:57:49 +02:00

113 lines
4.4 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* 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.
*
* 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:drift/drift.dart' as drift;
import 'package:wger/database/powersync/database.dart';
import 'package:wger/features/nutrition/models/ingredient.dart';
import 'package:wger/features/nutrition/models/ingredient_weight_unit.dart';
import 'package:wger/features/nutrition/models/meal_item.dart';
import 'package:wger/features/nutrition/models/nutritional_values.dart';
/// Diary log entry, one ingredient consumed at a point in time.
///
/// Lives entirely in PowerSync/Drift now: row IDs are client-generated UUIDs
/// (so we can write before the server has assigned an integer PK), and the
/// `Ingredient` / `IngredientWeightUnit` relations are hydrated from the same
/// local Drift database by the repository's watch. No JSON round-trip.
///
/// Named `LogItem` to mirror the Django model and Drift table name, and to
/// avoid clashing with the workouts `Log`.
class LogItem {
/// Client-generated UUID, is `null` only before the first persist
String? id;
String planId;
/// Optional parent meal (`null` for free-form diary entries that don't belong to a meal)
String? mealId;
int ingredientId;
int? weightUnitId;
late DateTime datetime;
late num amount;
String? comment;
/// Nullable because of the way powersync works. This will be only the case
/// in the time between setting the ingredient ID (e.g. while editing a meal)
/// and when the sync comes from the server and the provider can load the
/// ingredient.Callers must treat null as "data still arriving" rather than
/// as an error.
Ingredient? ingredient;
/// Optional weight unit reference; `null` means the amount is in grams.
IngredientWeightUnit? weightUnitObj;
LogItem({
this.id,
this.mealId,
required this.ingredientId,
this.weightUnitId,
required this.amount,
required this.planId,
required this.datetime,
this.comment,
});
/// Builds a new log row from a meal-plan item, e.g. when the user taps
/// "log this meal". The `id` stays null and is assigned by Drift on insert.
LogItem.fromMealItem(MealItem mealItem, this.planId, this.mealId, [DateTime? dateTime])
: ingredientId = mealItem.ingredientId,
weightUnitId = mealItem.weightUnitId {
ingredient = mealItem.ingredient;
datetime = dateTime ?? DateTime.now();
amount = mealItem.amount;
}
/// Drift companion for inserts/updates against `nutrition_logitem`.
///
/// If [id] is null, Drift's `clientDefault` mints a fresh UUID on insert.
/// If set, the value round-trips into the row as-is.
LogItemTableCompanion toCompanion() {
return LogItemTableCompanion(
id: id != null ? drift.Value(id!) : const drift.Value.absent(),
planId: drift.Value(planId),
mealId: mealId == null ? const drift.Value.absent() : drift.Value(mealId),
ingredientId: drift.Value(ingredientId),
weightUnitId: weightUnitId == null ? const drift.Value.absent() : drift.Value(weightUnitId),
datetime: drift.Value(datetime),
amount: drift.Value(amount.toDouble()),
comment: comment == null ? const drift.Value.absent() : drift.Value(comment),
);
}
/// Total nutritional contribution of this entry. Server pre-computes the
/// same value, but doing it locally keeps offline mode honest.
///
/// Returns all-zero values while [ingredient] is still being hydrated
/// (see the field doc) so totals and renderers keep working through the
/// brief sync gap.
NutritionalValues get nutritionalValues {
final ing = ingredient;
if (ing == null) {
return NutritionalValues();
}
final weight = weightUnitObj == null ? amount : amount * weightUnitObj!.grams;
return ing.nutritionalValues / (100 / weight);
}
}