/// Wires "Fill all with catering" into the log form. /// /// Its own file because `log_meal_screen.dart` and /// `log_meal_kuchnia_mixin.dart` are both at the repo's 250-line ceiling. The /// rule lives in `kuchnia_fill.dart` and the two-tap state in /// `kuchnia_fill_flow.dart`; this only connects them to the screen. library; import 'package:diet_guard_app/screens/log_meal_future_mixin.dart'; import 'package:diet_guard_app/screens/log_meal_kuchnia_mixin.dart'; import 'package:diet_guard_app/screens/log_meal_sync_mixin.dart'; import 'package:diet_guard_app/services/kuchnia_fill_flow.dart'; import 'package:diet_guard_app/services/kuchnia_queue.dart'; import 'package:diet_guard_app/services/kuchnia_spread.dart'; import 'package:flutter/material.dart'; /// Drives the screen's [FillAllFlow] and tidies the screen after a fill. mixin LogMealFillMixin on LogMealSyncMixin, LogMealKuchniaMixin, LogMealFutureMixin { /// The flow behind the button. Per-screen, so leaving the screen drops an /// armed proposal along with it. FillAllFlow fillFlow = FillAllFlow(); /// Shows [message] in the form's status line. Supplied by the screen. void showFillStatus(String message); /// A proposal belongs to the date it was fetched for; picking another (or /// going back to today) must not leave "Confirm (N)" offering it. @override void onLogDateChanged() { super.onLogDateChanged(); fillFlow.cancel(); } /// Runs one tap: the first proposes, a second on the same day confirms. /// Fills the "Log for later" date when one is picked, else today. Future onFillAll() async { final day = futureDate; final pending = fillFlow.tap(day: day); setState(() {}); // show the busy label while the fetch/writes run final FillOutcome? outcome; try { outcome = await pending; } on Exception catch (error) { if (!mounted) return; setState(() {}); // A confirm can fail partway through its writes. Whatever did land is // a real meal, so it is published like any other rather than waiting // for the next tick, and the user is told the batch did not finish. await publishAfterLog(); await refreshSlots(); if (mounted) showFillStatus('Fill all failed: $error'); return; } if (!mounted) return; setState(() {}); if (outcome == null) return; if (outcome.written.isNotEmpty) { await _afterFill(outcome.written, later: day != null); } if (!mounted) return; showFillStatus(outcome.message); } /// Disarms a proposal without writing anything. void onFillCancel() { fillFlow.cancel(); showFillStatus('Fill all cancelled — nothing logged.'); } /// Publishes the batch once and drops the filled dishes from the delivery /// queue, so "Today's delivery (N more to go)" and the prefilled form do /// not keep offering meals that were just logged. /// /// A [later] fill logged another day's delivery: today's queue and form are /// left alone, and the screen goes back to today, as after a manual /// "Log for later" -- the picked day's slot row would otherwise be stale. Future _afterFill( List written, { required bool later, }) async { if (later) { resetFutureLog(); await publishAfterLog(); await refreshSlots(); return; } if (KuchniaQueueService.isInitialized) { for (final item in written) { KuchniaQueueService.instance.markLogged(item.dish); } } final offered = offeredDish; if (offered != null && written.any((item) => item.dish.bankKey == offered.bankKey)) { // The prefilled dish was just logged by the fill; leaving it in the // form would invite a second, duplicate submit. descController.clear(); macroControllers.clear(); } prefillNextDish(); await publishAfterLog(); await refreshSlots(); } }