mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 00:12:22 +01:00
Action row 1A, due mode, catering auto-log rows with undo, relay interface, photo input. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
168 lines
5.9 KiB
Dart
168 lines
5.9 KiB
Dart
/// Lets the user pre-log a meal against a future date + slot instead of now.
|
|
///
|
|
/// Its own file because `log_meal_screen.dart` is at the repo's 250-line cap.
|
|
///
|
|
/// Scoped to dates strictly after today: the date picker's `firstDate` is
|
|
/// tomorrow.
|
|
library;
|
|
|
|
import 'package:diet_guard_app/models/food_entry.dart';
|
|
import 'package:diet_guard_app/models/local_time.dart';
|
|
import 'package:diet_guard_app/models/meal_schedule.dart';
|
|
import 'package:diet_guard_app/models/slot.dart';
|
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
|
import 'package:diet_guard_app/services/meal_schedule_service.dart';
|
|
import 'package:diet_guard_app/ui/theme.dart';
|
|
import 'package:diet_guard_app/widgets/pickers/pickers.dart';
|
|
import 'package:diet_guard_app/widgets/slot_selector_row.dart';
|
|
import 'package:material_ui/material_ui.dart';
|
|
|
|
/// Adds a future-date meal-log picker to [LogMealScreen]'s state.
|
|
mixin LogMealFutureMixin<T extends StatefulWidget> on State<T> {
|
|
DateTime? _futureDate;
|
|
int? _futureSlot;
|
|
Set<int> _futureLoggedSlots = {};
|
|
MealSchedule? _futureSchedule;
|
|
|
|
/// The date currently chosen for a future log, or null when logging for
|
|
/// today through the screen's normal flow.
|
|
DateTime? get futureDate => _futureDate;
|
|
|
|
/// The slot chosen for [futureDate], or null until one is picked.
|
|
int? get futureSlot => _futureSlot;
|
|
|
|
/// Called after the picked date changes (including back to today), for
|
|
/// state keyed to the old date to drop itself. No-op unless overridden.
|
|
void onLogDateChanged() {}
|
|
|
|
/// Clears back to "log for today", dropping the picked date and slot.
|
|
void resetFutureLog() {
|
|
setState(() {
|
|
_futureDate = null;
|
|
_futureSlot = null;
|
|
_futureLoggedSlots = {};
|
|
_futureSchedule = null;
|
|
});
|
|
onLogDateChanged();
|
|
}
|
|
|
|
Future<void> _pickFutureDate() async {
|
|
final today = DateTime.now();
|
|
final tomorrow = DateTime(today.year, today.month, today.day + 1);
|
|
final picked = await pickDate(
|
|
context: context,
|
|
firstDate: tomorrow,
|
|
lastDate: tomorrow.add(const Duration(days: 365)),
|
|
initialDate: _futureDate ?? tomorrow,
|
|
);
|
|
if (picked == null || !mounted) return;
|
|
await selectLogDate(picked);
|
|
}
|
|
|
|
/// Switches the form to log against [picked] (strictly after today): what
|
|
/// the date picker does once a date is chosen.
|
|
Future<void> selectLogDate(DateTime picked) async {
|
|
final log = await LogStorageService.instance.readLog();
|
|
if (!mounted) return;
|
|
final day = localDateKey(picked);
|
|
final schedule = MealScheduleService.history.forDay(day);
|
|
// Snapped against the schedule for *that* day, the same rule as
|
|
// `loggedSlotsToday`: an exact match would show a pre-logged meal's slot
|
|
// as open again after a schedule edit, inviting a duplicate.
|
|
final logged = satisfiedSlots([
|
|
for (final entry in log[day] ?? const <FoodEntry>[])
|
|
if (!entry.deleted) ?entry.slotMinute,
|
|
], schedule);
|
|
setState(() {
|
|
_futureDate = picked;
|
|
_futureSchedule = schedule;
|
|
_futureLoggedSlots = logged;
|
|
_futureSlot = null;
|
|
});
|
|
onLogDateChanged();
|
|
}
|
|
|
|
/// The "When" row: a "Log for later" button, or the picked date with a
|
|
/// button to change it and one to clear back to today.
|
|
Widget buildWhenRow(BuildContext context) {
|
|
final date = _futureDate;
|
|
final scheme = Theme.of(context).colorScheme;
|
|
return Row(
|
|
children: [
|
|
// Today is the default and shows nothing; only a picked date (data)
|
|
// is shown, marked by the calendar glyph.
|
|
if (date != null) ...[
|
|
Icon(
|
|
Icons.event,
|
|
size: 18,
|
|
color: scheme.onSurfaceVariant,
|
|
semanticLabel: 'Log date',
|
|
),
|
|
const SizedBox(width: AppSpacing.xs),
|
|
],
|
|
Expanded(
|
|
child: date == null ? const SizedBox() : Text(localDateKey(date)),
|
|
),
|
|
IconButton(
|
|
// Not Icons.calendar_month: the app bar's own "Calendar" nav button
|
|
// already uses that, and a second one on screen makes
|
|
// `find.byIcon` ambiguous for widget tests (and taps) alike.
|
|
icon: Icon(
|
|
Icons.date_range,
|
|
size: 18,
|
|
semanticLabel: date == null ? 'Log for later' : 'Change date',
|
|
),
|
|
onPressed: _pickFutureDate,
|
|
),
|
|
if (date != null)
|
|
IconButton(
|
|
icon: const Icon(Icons.close, semanticLabel: 'Back to today'),
|
|
onPressed: resetFutureLog,
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// The slot row for a picked future date, else nothing.
|
|
///
|
|
/// Today has no slot row any more: the screen derives today's slot (the
|
|
/// oldest due one, else the nearest). A *future* date has no due slots to
|
|
/// derive from, so the row survives for exactly that case.
|
|
Widget buildSlotRow() {
|
|
final date = _futureDate;
|
|
if (date == null) return const SizedBox.shrink();
|
|
return SlotSelectorRow(
|
|
now: DateTime(date.year, date.month, date.day),
|
|
schedule: _futureSchedule,
|
|
loggedSlots: _futureLoggedSlots,
|
|
selectedSlot: _futureSlot,
|
|
onSlotSelected: (slot) => setState(() => _futureSlot = slot),
|
|
);
|
|
}
|
|
|
|
/// Resolves the slot/time to log against: the future pick when one is
|
|
/// active, otherwise [todaySlot] for now. `error` is set instead when a
|
|
/// future date is chosen but no slot on it has been picked yet.
|
|
({String? error, int? slot, DateTime? when}) resolveLogTarget(
|
|
int? todaySlot,
|
|
) {
|
|
final date = _futureDate;
|
|
if (date == null) return (error: null, slot: todaySlot, when: null);
|
|
final slot = _futureSlot;
|
|
if (slot == null) {
|
|
return (
|
|
error: 'Pick a slot for that date first.',
|
|
slot: null,
|
|
when: null,
|
|
);
|
|
}
|
|
return (
|
|
error: null,
|
|
slot: slot,
|
|
// The slot is a minute of day, not an hour: passing it as the hour
|
|
// argument would roll 720 (12:00) forward a whole month.
|
|
when: DateTime(date.year, date.month, date.day, slot ~/ 60, slot % 60),
|
|
);
|
|
}
|
|
}
|