@davoodmehraban89 / LifeMate / commits / 831c9f5
Fork 0

LifeMate — Personal, Family & Learning Companion

fix(planner): bound recurring editor date horizon

Quinn White committed Oct 18, 2026 at 21:48 UTC 831c9f5b4c86057862acfe61472dd89792014466
2 files changed +88 -5
apps/lifemate/lib/planner_series_ui.dart
+34 -4
@@ -19,11 +19,13 @@ class PlannerSeriesDialog extends StatefulWidget {
19 required this.api,
20 this.initialDate,
21 this.onCreate,
22 + this.clock = DateTime.now,
23 });
24
25 final HttpIdentityApi api;
26 final DateTime? initialDate;
27 final CreatePlannerSeries? onCreate;
28 + final DateTime Function() clock;
29
30 @override
31 State<PlannerSeriesDialog> createState() => _PlannerSeriesDialogState();
@@ -70,10 +72,23 @@ class _PlannerSeriesDialogState extends State<PlannerSeriesDialog> {
72
73 bool get editable => !busy && frozenPayload == null;
74
75 + // The server accepts ±730 Tehran calendar days. A UTC reference and a one-day
76 + // margin avoid offering an unsupported boundary near midnight or a time-zone
77 + // change. This is a UI guard; the API remains authoritative.
78 + DateTime get referenceDay {
79 + final now = widget.clock().toUtc();
80 + return DateTime(now.year, now.month, now.day);
81 + }
82 +
83 + DateTime get firstDate => referenceDay.subtract(const Duration(days: 729));
84 + DateTime get lastDate => referenceDay.add(const Duration(days: 729));
85 + bool supportedDate(DateTime value) =>
86 + !value.isBefore(firstDate) && !value.isAfter(lastDate);
87 +
88 @override
89 void initState() {
90 super.initState();
76 - final date = widget.initialDate ?? DateTime.now();
91 + final date = widget.initialDate ?? widget.clock();
92 startsOn = DateTime(date.year, date.month, date.day);
93 untilOn = startsOn.add(const Duration(days: 90));
94 selected.add(startsOn.weekday);
@@ -88,11 +103,18 @@ class _PlannerSeriesDialogState extends State<PlannerSeriesDialog> {
103
104 Future<void> pickDate({required bool end}) async {
105 if (!editable) return;
106 + final selectedDate = end ? untilOn : startsOn;
107 + final first = firstDate, last = lastDate;
108 + final initial = selectedDate.isBefore(first)
109 + ? first
110 + : selectedDate.isAfter(last)
111 + ? last
112 + : selectedDate;
113 final date = await showDatePicker(
114 context: context,
93 - initialDate: end ? untilOn : startsOn,
94 - firstDate: DateTime(2020),
95 - lastDate: DateTime(2100, 12, 31),
115 + initialDate: initial,
116 + firstDate: first,
117 + lastDate: last,
118 );
119 if (date != null && mounted && editable) {
120 setState(() => end ? untilOn = date : startsOn = date);
@@ -125,6 +147,14 @@ class _PlannerSeriesDialogState extends State<PlannerSeriesDialog> {
147 error = 'عنوان را وارد کن.';
148 return null;
149 }
150 + if (!supportedDate(startsOn)) {
151 + error = 'تاریخ شروع باید در بازهٔ مجاز دو ساله باشد.';
152 + return null;
153 + }
154 + if (hasEnd && !supportedDate(untilOn)) {
155 + error = 'تاریخ پایان باید در بازهٔ مجاز دو ساله باشد.';
156 + return null;
157 + }
158 if (hasEnd && untilOn.isBefore(startsOn)) {
159 error = 'تاریخ پایان نباید پیش از تاریخ شروع باشد.';
160 return null;
apps/lifemate/test/planner_series_ui_test.dart
+54 -1
@@ -21,6 +21,7 @@ void main() {
21 ValueChanged<PlannerSeriesOutcome?>? onClosed,
22 Size size = const Size(390, 844),
23 double textScale = 1,
24 + DateTime? initialDate,
25 }) async {
26 tester.view.physicalSize = size;
27 tester.view.devicePixelRatio = 1;
@@ -44,7 +45,8 @@ void main() {
45 textDirection: TextDirection.rtl,
46 child: PlannerSeriesDialog(
47 api: HttpIdentityApi(baseUrl: 'https://example.invalid'),
47 - initialDate: DateTime(2026, 10, 19),
48 + initialDate: initialDate ?? DateTime(2026, 10, 19),
49 + clock: () => DateTime.utc(2026, 10, 18, 12),
50 onCreate: create,
51 ),
52 ),
@@ -249,4 +251,55 @@ void main() {
251 expect(tester.getSize(find.byKey(const ValueKey('series-save'))).height,
252 greaterThanOrEqualTo(48));
253 });
254 +
255 + testWidgets('unsupported start stays visible and cannot be submitted',
256 + (tester) async {
257 + final today = DateTime(2026, 10, 18);
258 + final unsupported = today.add(const Duration(days: 800));
259 + var submitted = 0;
260 + await open(tester, (payload, {id}) async {
261 + submitted++;
262 + return PlannerSeriesOutcome(queued: false, id: id!);
263 + }, initialDate: unsupported);
264 + await tester.enterText(find.byKey(const ValueKey('series-title')), 'نمونه');
265 + await tester.tap(find.byKey(const ValueKey('series-save')));
266 + await tester.pumpAndSettle();
267 + expect(find.text('تاریخ شروع باید در بازهٔ مجاز دو ساله باشد.'),
268 + findsOneWidget);
269 + expect(submitted, 0);
270 + await tapVisible(tester, find.byKey(const ValueKey('series-start-date')));
271 + final picker =
272 + tester.widget<DatePickerDialog>(find.byType(DatePickerDialog));
273 + expect(picker.firstDate, today.subtract(const Duration(days: 729)));
274 + expect(picker.lastDate, today.add(const Duration(days: 729)));
275 + expect(picker.initialDate, picker.lastDate);
276 + await tester.tap(find.text('Cancel'));
277 + await tester.pumpAndSettle();
278 + expect(find.textContaining('${unsupported.year}-'), findsOneWidget);
279 + expect(tester.takeException(), isNull);
280 + });
281 +
282 + testWidgets('optional end beyond horizon is rejected before enqueue',
283 + (tester) async {
284 + final today = DateTime(2026, 10, 18);
285 + var submitted = 0;
286 + await open(tester, (payload, {id}) async {
287 + submitted++;
288 + return PlannerSeriesOutcome(queued: false, id: id!);
289 + }, initialDate: today.add(const Duration(days: 720)));
290 + await tester.enterText(find.byKey(const ValueKey('series-title')), 'نمونه');
291 + await tapVisible(tester, find.byKey(const ValueKey('series-has-end')));
292 + await tester.tap(find.byKey(const ValueKey('series-save')));
293 + await tester.pumpAndSettle();
294 + expect(find.text('تاریخ پایان باید در بازهٔ مجاز دو ساله باشد.'),
295 + findsOneWidget);
296 + expect(submitted, 0);
297 + await tapVisible(tester, find.byKey(const ValueKey('series-end-date')));
298 + final picker =
299 + tester.widget<DatePickerDialog>(find.byType(DatePickerDialog));
300 + expect(picker.firstDate, today.subtract(const Duration(days: 729)));
301 + expect(picker.lastDate, today.add(const Duration(days: 729)));
302 + expect(picker.initialDate, picker.lastDate);
303 + expect(tester.takeException(), isNull);
304 + });
305 }