mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 00:52:21 +01:00
The phone now prefills the log form with today's delivered dishes, in the caterer's own meal order. It still never logs anything itself -- a delivered meal is not an eaten meal, so the user taps for each dish. Two guards carried over from the PC, both of which fail silently if dropped: - **The queue survives a submit.** `prefillNextDish` has two callers: the initial load and the post-submit advance. On the PC the second briefly did not exist, which made "(N more to go)" a dead letter -- every dish after the first waited behind another tap. The test asserts the *form controller* holds dish 2 after dish 1 is logged, not that a dish is "available"; the latter passes throughout that bug. - **The automatic refresh is guarded** by a device-local last-fetched date, mirroring `refresh_delivery_once`. The log-flow trigger can fire after every meal and each unguarded walk is a login plus three requests. The settings button deliberately stays unguarded: an explicit ask always goes and looks, matching `diet-guard kuchnia`. Only a clean fetch records the day, so an outage retries. Dishes are ordered against `MealScheduleService.current`, never a hardcoded four slots -- offering a dish for a slot the user's schedule does not have is the "checkpoint that can never be satisfied" failure in a new costume. Editing the description before submitting leaves the dish queued: what was logged was not the delivered dish, and marking it eaten would lose a real delivery silently. Two bugs the tests caught: - `dishesStillQueued` excluded the offered dish, so the status line read "0 more" while a real dish sat in the form. The offered dish is prefilled, not logged; it counts. - An end-to-end test through the real `LogMealScreen` never terminated -- that screen's `initState` fans out into sync, notifications and the food bank, and pumping it to quiescence hangs. Rather than keep fighting it, the behaviour is tested at the mixin that owns it, through a minimal host. The guarantee asserted is the same one: form contents after a submit. `log_meal_screen.dart` is back under the 250-line cap by moving the nutrition build into `log_meal_actions.dart` and inlining a one-line alias. 694 Dart tests, 1040 Python tests, 100% Python branch coverage. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX
112 lines
4.4 KiB
Dart
112 lines
4.4 KiB
Dart
/// Assigns catering dishes to meal slots, in the provider's own order.
|
|
///
|
|
/// The Dart mirror of `diet_guard/_kuchnia_spread.py`.
|
|
///
|
|
/// The panel returns each dish with a `mealPriority` (1..N: Śniadanie, II
|
|
/// śniadanie, Obiad, Podwieczorek, Kolacja) -- the order the catering intends
|
|
/// them to be eaten in. That beats inferring an order from the dish names, and
|
|
/// it beats spreading them arithmetically.
|
|
///
|
|
/// Counts rarely match: a 5-meal plan against the default 4 slots means two
|
|
/// dishes share the first slot. The mapping is `i * S ~/ N`, which is
|
|
/// monotonic non-decreasing, keeps the first dish on the first slot and the
|
|
/// last on the last, and doubles up the earliest slots rather than dropping
|
|
/// anything.
|
|
///
|
|
/// **Integer `~/` only, never `round()`.** Repo convention for slot arithmetic
|
|
/// (`docs/meal-schedule.md`): Python's `round` is banker's and Dart's is
|
|
/// half-away-from-zero, so a float path silently desyncs the two devices --
|
|
/// and a slot one device offers while the other does not is a checkpoint that
|
|
/// can never be satisfied, i.e. a permanent lock. `index`, `span` and `count`
|
|
/// are all `int`, so `~/` matches Python's `//` exactly (`~/` on doubles
|
|
/// truncates toward zero where `//` floors).
|
|
///
|
|
/// KEEP IN SYNC WITH `diet_guard/_kuchnia_spread.py`, gated by
|
|
/// `tests/fixtures/kuchnia_day.json`.
|
|
library;
|
|
|
|
import 'package:diet_guard_app/models/kuchnia_dish.dart';
|
|
|
|
/// A dish paired with the meal-slot hour it was assigned to.
|
|
class SlottedDish {
|
|
/// Pairs [dish] with the [slot] hour it should be logged against.
|
|
const SlottedDish({required this.dish, required this.slot});
|
|
|
|
/// The dish itself.
|
|
final KuchniaDish dish;
|
|
|
|
/// The slot hour, e.g. `12` for the 12:00 checkpoint.
|
|
final int slot;
|
|
}
|
|
|
|
/// Pairs each dish with a slot hour, following the provider's meal order.
|
|
///
|
|
/// Returns an empty list when either input is empty -- a day with no delivery
|
|
/// and a schedule with no slots are both "nothing to assign", not errors.
|
|
List<SlottedDish> assignSlots(List<KuchniaDish> dishes, List<int> slots) {
|
|
if (dishes.isEmpty || slots.isEmpty) return const [];
|
|
|
|
// Ties fall back to name so the result is deterministic; a bank import that
|
|
// reshuffled between runs would look like a change and re-stamp every
|
|
// record.
|
|
//
|
|
// The payload index is the final tiebreak because `List.sort` is *not*
|
|
// stable in Dart while Python's `sorted` is: two dishes sharing both
|
|
// priority and name would otherwise be free to swap here and not on the PC.
|
|
// With the index in the comparator the ordering is total, so both languages
|
|
// agree without relying on either sort's stability.
|
|
final indexed = <(int, KuchniaDish)>[
|
|
for (var i = 0; i < dishes.length; i++) (i, dishes[i]),
|
|
]..sort((a, b) {
|
|
final byPriority = a.$2.priority.compareTo(b.$2.priority);
|
|
if (byPriority != 0) return byPriority;
|
|
final byName = a.$2.name.compareTo(b.$2.name);
|
|
if (byName != 0) return byName;
|
|
return a.$1.compareTo(b.$1);
|
|
});
|
|
|
|
final count = indexed.length;
|
|
final span = slots.length;
|
|
return [
|
|
for (var index = 0; index < count; index++)
|
|
SlottedDish(
|
|
dish: indexed[index].$2,
|
|
slot: slots[index * span ~/ count],
|
|
),
|
|
];
|
|
}
|
|
|
|
/// Returns [dishes] ordered by the provider's own meal priority.
|
|
///
|
|
/// So the dish offered for the slot being filled is the one the catering
|
|
/// actually intends for it, rather than whatever order the payload arrived in.
|
|
List<KuchniaDish> dishesInSlotOrder(
|
|
List<KuchniaDish> dishes,
|
|
List<int> slots,
|
|
) => [for (final item in assignSlots(dishes, slots)) item.dish];
|
|
|
|
/// Returns `(portionGrams, (kcal, protein, carbs, fat))` as form strings.
|
|
///
|
|
/// Mirrors `_kuchnia_spread.dish_field_values`. Formatted here rather than in
|
|
/// the widget so the numeric formatting is covered by a test that needs no
|
|
/// display.
|
|
///
|
|
/// Python uses `f"{value:g}"`, which drops a trailing `.0`; Dart's
|
|
/// `toStringAsFixed` would keep it and `toString` would render `435.0`. So a
|
|
/// whole number is printed as an integer and anything else keeps its
|
|
/// decimals -- the form should read "435", not "435.0".
|
|
(String, (String, String, String, String)) dishFieldValues(KuchniaDish dish) {
|
|
String format(double value) => value == value.roundToDouble()
|
|
? value.toInt().toString()
|
|
: value.toString();
|
|
return (
|
|
format(dish.grams),
|
|
(
|
|
format(dish.kcal),
|
|
format(dish.proteinG),
|
|
format(dish.carbsG),
|
|
format(dish.fatG),
|
|
),
|
|
);
|
|
}
|