mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-27 18:09:13 +01:00
Some checks are pending
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Todo: c5ee9e59
124 lines
4.2 KiB
Dart
124 lines
4.2 KiB
Dart
/// One delivered catering dish, with its portion macros.
|
|
///
|
|
/// The Dart mirror of `diet_guard/_kuchnia_parse.py`'s `Dish`. Every macro is
|
|
/// the total *for this portion*, not per 100 g -- the same convention
|
|
/// [Nutrition] uses.
|
|
///
|
|
/// KEEP IN SYNC WITH `diet_guard/_kuchnia_parse.py`. Both sides are gated by
|
|
/// `tests/fixtures/kuchnia_day.json`; see `kuchnia_parity_test.dart`.
|
|
library;
|
|
|
|
/// A dish the caterer delivered on a given day.
|
|
class KuchniaDish {
|
|
/// Creates a dish from its already-validated fields.
|
|
const KuchniaDish({
|
|
required this.name,
|
|
required this.kcal,
|
|
required this.proteinG,
|
|
required this.carbsG,
|
|
required this.fatG,
|
|
required this.grams,
|
|
required this.priority,
|
|
required this.slotLabel,
|
|
this.dishId = '',
|
|
});
|
|
|
|
/// Rebuilds a dish stored by [toJson]; throws on a malformed record.
|
|
factory KuchniaDish.fromJson(Map<String, Object?> json) => KuchniaDish(
|
|
name: json['name']! as String,
|
|
kcal: (json['kcal']! as num).toDouble(),
|
|
proteinG: (json['protein_g']! as num).toDouble(),
|
|
carbsG: (json['carbs_g']! as num).toDouble(),
|
|
fatG: (json['fat_g']! as num).toDouble(),
|
|
grams: (json['grams']! as num).toDouble(),
|
|
priority: json['priority']! as int,
|
|
slotLabel: json['slot_label']! as String,
|
|
dishId: json['dish_id'] as String? ?? '',
|
|
);
|
|
|
|
/// The stored form; the keys follow the PC's `kuchnia_delivery.json`.
|
|
Map<String, Object?> toJson() => {
|
|
'name': name,
|
|
'kcal': kcal,
|
|
'protein_g': proteinG,
|
|
'carbs_g': carbsG,
|
|
'fat_g': fatG,
|
|
'grams': grams,
|
|
'priority': priority,
|
|
'slot_label': slotLabel,
|
|
'dish_id': dishId,
|
|
};
|
|
|
|
/// The dish itself (`menuMealName`), e.g. "Kaszotto grzybowe...".
|
|
///
|
|
/// Not `mealName`, which is the slot label ("Kolacja") and would collapse
|
|
/// every Monday dinner onto one food-bank entry.
|
|
final String name;
|
|
|
|
/// Calories for this portion.
|
|
final double kcal;
|
|
|
|
/// Protein in grams, for this portion.
|
|
final double proteinG;
|
|
|
|
/// Carbohydrate in grams, for this portion.
|
|
final double carbsG;
|
|
|
|
/// Fat in grams, for this portion.
|
|
final double fatG;
|
|
|
|
/// The portion weight in grams (`nutrition.weight`).
|
|
final double grams;
|
|
|
|
/// The caterer's own eating order, 1..N.
|
|
///
|
|
/// Falls back to the dish's 1-based position in the payload when
|
|
/// `mealPriority` is missing.
|
|
final int priority;
|
|
|
|
/// The caterer's slot label ("Śniadanie", "Obiad", ...), for display only.
|
|
final String slotLabel;
|
|
|
|
/// The panel's own id for this meal (`deliveryMealId`), as a string, or ''
|
|
/// when the payload carried none.
|
|
///
|
|
/// Never written to the bank record or the log: it only feeds [autologKey].
|
|
final String dishId;
|
|
|
|
/// The dish half of the auto-log entry id: [dishId], else [bankKey].
|
|
///
|
|
/// Mirrors the fixture `tests/fixtures/kuchnia_autolog_ids.json`.
|
|
String get autologKey => dishId.isNotEmpty ? dishId : bankKey;
|
|
|
|
/// The curated-bank record for this dish.
|
|
///
|
|
/// Mirrors `_kuchnia_import.dish_to_record`. `count` is 0: the bank's count
|
|
/// ranks foods by how often they were *eaten*, and a delivered dish has not
|
|
/// been eaten yet.
|
|
///
|
|
/// Every macro is declared `double` on this class rather than `num`, which
|
|
/// is what keeps the encoded record byte-identical to Python's.
|
|
/// `jsonEncode(435)` emits `435` where Python emits `435.0`, and a
|
|
/// byte-different record for the same dish makes every refresh look like a
|
|
/// change -- which republishes the whole curated bank to every peer. A
|
|
/// `.toDouble()` here would be a no-op the analyzer flags; the field types
|
|
/// are the guarantee, and `kuchnia_parity_test.dart` asserts the encoding.
|
|
Map<String, dynamic> toBankRecord() => {
|
|
'desc': name,
|
|
'kcal': kcal,
|
|
'protein_g': proteinG,
|
|
'carbs_g': carbsG,
|
|
'fat_g': fatG,
|
|
'grams': grams,
|
|
'count': 0,
|
|
};
|
|
|
|
/// The curated bank's key for this dish.
|
|
///
|
|
/// Python uses `str.casefold()` and this uses `String.toLowerCase()`. They
|
|
/// agree across the whole Polish alphabet and diverge only on `ß`,
|
|
/// ligatures and final sigma; rekeying to unify them would strand every
|
|
/// existing entry, so the agreement is pinned by test instead.
|
|
String get bankKey => name.trim().toLowerCase();
|
|
}
|