fix: unify date limits, fix ISO week calculation, refactor home screen plan button

- Fix _isoWeek: correct Sunday shift (-3 instead of +4), use floor+1 formula,
  match jan1 timezone to input — planned meals now appear correctly for UTC+ users
- Add kPlanningHorizonDays=28 / kMenuPastWeeks=8 constants; apply to home date
  strip, plan picker (strip + calendar), and menu screen prev/next navigation
- Menu screen week nav: disable arrows at min/max limits using compareTo
- Home screen: replace _GenerateActionCard/_WeekPlannedChip conditional with
  always-visible _FutureDayPlanButton(dateString); show _DayPlannedChip only
  when the specific day has planned meals; remove standalone _PlanMenuButton
- _FutureDayPlanButton uses selected date as defaultStart instead of lastPlanned+1
- Rename weekPlannedLabel -> dayPlannedLabel across all 12 locales

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-22 23:25:46 +02:00
parent edf587e798
commit 9a6b7800a3
30 changed files with 106 additions and 167 deletions

View File

@@ -12,6 +12,7 @@ import '../../core/storage/local_preferences_provider.dart';
import '../../core/theme/app_colors.dart';
import '../../shared/models/diary_entry.dart';
import '../../shared/models/home_summary.dart';
import '../../shared/constants/date_limits.dart';
import '../../shared/models/meal_type.dart';
import '../../shared/models/menu.dart';
import '../diary/food_search_sheet.dart';
@@ -128,8 +129,6 @@ class HomeScreen extends ConsumerWidget {
],
const SizedBox(height: 16),
_QuickActionsRow(),
const SizedBox(height: 8),
_PlanMenuButton(),
if (!isFutureDate && recommendations.isNotEmpty) ...[
const SizedBox(height: 20),
_SectionTitle(l10n.recommendCook),
@@ -191,10 +190,10 @@ class _DateSelector extends StatefulWidget {
}
class _DateSelectorState extends State<_DateSelector> {
// Strip covers 7 future days + today + 364 past days = 372 items total.
// Strip covers kPlanningHorizonDays future days + today + 364 past days.
// With reverse: true, index 0 is rendered at the RIGHT edge (newest).
// index 0 = today + 7, index 7 = today, index 371 = today - 364.
static const _futureDays = 7;
// index 0 = today + kPlanningHorizonDays, index kPlanningHorizonDays = today.
static const _futureDays = kPlanningHorizonDays;
static const _pastDays = 364;
static const _totalDays = _futureDays + 1 + _pastDays; // 372
static const _pillWidth = 48.0;
@@ -1156,125 +1155,26 @@ class _FutureDayHeader extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final l10n = AppLocalizations.of(context)!;
final date = DateTime.parse(dateString);
final weekString = isoWeekString(date);
final menuState = ref.watch(menuProvider(weekString));
final plannedMeals = ref.watch(plannedMealsProvider(dateString));
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_PlanningBanner(dateString: dateString),
const SizedBox(height: 8),
menuState.when(
loading: () => _GenerateLoadingCard(l10n: l10n),
error: (_, __) => _GenerateActionCard(
l10n: l10n,
onGenerate: () =>
ref.read(menuProvider(weekString).notifier).generate(),
),
data: (plan) => plan == null
? _GenerateActionCard(
l10n: l10n,
onGenerate: () =>
ref.read(menuProvider(weekString).notifier).generate(),
)
: _WeekPlannedChip(l10n: l10n),
),
_FutureDayPlanButton(dateString: dateString),
if (plannedMeals.isNotEmpty) ...[
const SizedBox(height: 8),
_DayPlannedChip(l10n: l10n),
],
],
);
}
}
class _GenerateActionCard extends StatelessWidget {
class _DayPlannedChip extends StatelessWidget {
final AppLocalizations l10n;
final VoidCallback onGenerate;
const _GenerateActionCard({required this.l10n, required this.onGenerate});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerLow,
borderRadius: BorderRadius.circular(12),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.auto_awesome,
color: theme.colorScheme.primary, size: 20),
const SizedBox(width: 8),
Expanded(
child: Text(
l10n.generateWeekLabel,
style: theme.textTheme.titleSmall?.copyWith(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.w600,
),
),
),
],
),
const SizedBox(height: 4),
Text(
l10n.generateWeekSubtitle,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 12),
FilledButton(
onPressed: onGenerate,
child: Text(l10n.generateWeekLabel),
),
],
),
);
}
}
class _GenerateLoadingCard extends StatelessWidget {
final AppLocalizations l10n;
const _GenerateLoadingCard({required this.l10n});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
decoration: BoxDecoration(
color: theme.colorScheme.surfaceContainerLow,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator.adaptive(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation(theme.colorScheme.primary),
),
),
const SizedBox(width: 12),
Text(
l10n.generatingMenu,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
),
);
}
}
class _WeekPlannedChip extends StatelessWidget {
final AppLocalizations l10n;
const _WeekPlannedChip({required this.l10n});
const _DayPlannedChip({required this.l10n});
@override
Widget build(BuildContext context) {
@@ -1292,7 +1192,7 @@ class _WeekPlannedChip extends StatelessWidget {
color: theme.colorScheme.onSecondaryContainer, size: 18),
const SizedBox(width: 8),
Text(
l10n.weekPlannedLabel,
l10n.dayPlannedLabel,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSecondaryContainer,
fontWeight: FontWeight.w600,
@@ -1647,8 +1547,9 @@ class _ActionButton extends StatelessWidget {
// ── Plan menu button ──────────────────────────────────────────
class _PlanMenuButton extends ConsumerWidget {
const _PlanMenuButton();
class _FutureDayPlanButton extends ConsumerWidget {
final String dateString;
const _FutureDayPlanButton({required this.dateString});
@override
Widget build(BuildContext context, WidgetRef ref) {
@@ -1682,16 +1583,13 @@ class _PlanMenuButton extends ConsumerWidget {
}
void _openPlanSheet(BuildContext context, WidgetRef ref) {
final defaultStart = DateTime.parse(dateString);
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
useSafeArea: true,
builder: (_) => PlanMenuSheet(
onModeSelected: (mode) {
final lastPlanned = ref.read(lastPlannedDateProvider);
final defaultStart = lastPlanned != null
? lastPlanned.add(const Duration(days: 1))
: DateTime.now().add(const Duration(days: 1));
showModalBottomSheet<void>(
context: context,
isScrollControlled: true,

View File

@@ -16,22 +16,27 @@ final menuServiceProvider = Provider<MenuService>((ref) {
/// The ISO week string for the currently displayed week, e.g. "2026-W08".
final currentWeekProvider = StateProvider<String>((ref) {
final now = DateTime.now().toUtc();
final now = DateTime.now();
final (y, w) = _isoWeek(now);
return '$y-W${w.toString().padLeft(2, '0')}';
});
(int year, int week) _isoWeek(DateTime dt) {
// Shift to Thursday to get ISO week year.
final thu = dt.add(Duration(days: 4 - (dt.weekday == 7 ? 0 : dt.weekday)));
final jan1 = DateTime.utc(thu.year, 1, 1);
final week = ((thu.difference(jan1).inDays) / 7).ceil();
// Shift to Thursday of the same ISO week.
// Monday=1…Saturday=6 → add (4 - weekday) days; Sunday=7 → subtract 3 days.
final int shift = dt.weekday == 7 ? -3 : 4 - dt.weekday;
final thu = dt.add(Duration(days: shift));
// Use the same timezone as the input to avoid offset drift on the difference.
final jan1 = dt.isUtc
? DateTime.utc(thu.year, 1, 1)
: DateTime(thu.year, 1, 1);
final week = (thu.difference(jan1).inDays ~/ 7) + 1;
return (thu.year, week);
}
/// Returns the ISO 8601 week string for [date], e.g. "2026-W12".
String isoWeekString(DateTime date) {
final (year, week) = _isoWeek(date.toUtc());
final (year, week) = _isoWeek(date);
return '$year-W${week.toString().padLeft(2, '0')}';
}

View File

@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../shared/constants/date_limits.dart';
import '../../shared/models/menu.dart';
import 'menu_provider.dart';
@@ -95,31 +96,48 @@ class _WeekNavBar extends StatelessWidget implements PreferredSizeWidget {
}
(int, int) _isoWeekOf(DateTime dt) {
final thu = dt.add(Duration(days: 4 - (dt.weekday == 7 ? 0 : dt.weekday)));
final jan1 = DateTime.utc(thu.year, 1, 1);
final w = ((thu.difference(jan1).inDays) / 7).ceil();
final int shift = dt.weekday == 7 ? -3 : 4 - dt.weekday;
final thu = dt.add(Duration(days: shift));
final jan1 = dt.isUtc
? DateTime.utc(thu.year, 1, 1)
: DateTime(thu.year, 1, 1);
final w = (thu.difference(jan1).inDays ~/ 7) + 1;
return (thu.year, w);
}
String _currentWeekString(DateTime date) {
final (y, w) = _isoWeekOf(date);
return '$y-W${w.toString().padLeft(2, '0')}';
}
@override
Widget build(BuildContext context) {
final now = DateTime.now();
final maxWeek = _currentWeekString(
now.add(const Duration(days: kPlanningHorizonDays)));
final minWeek = _currentWeekString(
now.subtract(Duration(days: kMenuPastWeeks * 7)));
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.chevron_left),
onPressed: () {
ref.read(currentWeekProvider.notifier).state =
_offsetWeek(week, -1);
},
onPressed: week.compareTo(minWeek) <= 0
? null
: () {
ref.read(currentWeekProvider.notifier).state =
_offsetWeek(week, -1);
},
),
Text(_weekLabel(week), style: Theme.of(context).textTheme.bodyMedium),
IconButton(
icon: const Icon(Icons.chevron_right),
onPressed: () {
ref.read(currentWeekProvider.notifier).state =
_offsetWeek(week, 1);
},
onPressed: week.compareTo(maxWeek) >= 0
? null
: () {
ref.read(currentWeekProvider.notifier).state =
_offsetWeek(week, 1);
},
),
],
);

View File

@@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:food_ai/l10n/app_localizations.dart';
import 'package:intl/intl.dart';
import '../../shared/constants/date_limits.dart';
import '../../shared/models/meal_type.dart';
import '../profile/profile_provider.dart';
import 'menu_provider.dart';
@@ -279,8 +280,8 @@ class _DateStripSelector extends StatefulWidget {
class _DateStripSelectorState extends State<_DateStripSelector> {
late final ScrollController _scrollController;
// Show 30 upcoming days (today excluded, starts tomorrow).
static const _futureDays = 30;
// Show upcoming days up to the planning horizon (today excluded, starts tomorrow).
static const _futureDays = kPlanningHorizonDays;
static const _itemWidth = 64.0;
DateTime get _tomorrow =>
@@ -407,12 +408,22 @@ class _CalendarRangePickerState extends State<_CalendarRangePicker> {
}
void _nextMonth() {
final horizon = DateTime.now().add(const Duration(days: kPlanningHorizonDays));
final limitMonth = DateTime(horizon.year, horizon.month);
if (!DateTime(_displayMonth.year, _displayMonth.month).isBefore(limitMonth)) return;
setState(() {
_displayMonth =
DateTime(_displayMonth.year, _displayMonth.month + 1);
});
}
bool _isBeyondHorizon(DateTime date) {
final today = DateTime.now();
final horizon = DateTime(today.year, today.month, today.day)
.add(const Duration(days: kPlanningHorizonDays));
return date.isAfter(horizon);
}
bool _isInRange(DateTime date) {
final dayOnly = DateTime(date.year, date.month, date.day);
final start =
@@ -510,11 +521,13 @@ class _CalendarRangePickerState extends State<_CalendarRangePicker> {
final isStart = _isRangeStart(date);
final isEnd = _isRangeEnd(date);
final isPast = _isPast(date);
final isBeyond = _isBeyondHorizon(date);
final isDisabled = isPast || isBeyond;
Color bgColor = Colors.transparent;
Color textColor = theme.colorScheme.onSurface;
if (isPast) {
if (isDisabled) {
// ignore: deprecated_member_use
textColor = theme.colorScheme.onSurface.withOpacity(0.3);
} else if (isStart || isEnd) {
@@ -526,7 +539,7 @@ class _CalendarRangePickerState extends State<_CalendarRangePicker> {
}
return GestureDetector(
onTap: isPast ? null : () => widget.onDayTapped(date),
onTap: isDisabled ? null : () => widget.onDayTapped(date),
child: Container(
decoration: BoxDecoration(
color: bgColor,