@davoodmehraban89 / LifeMate / commits / c5cb308
Fork 0

LifeMate — Personal, Family & Learning Companion

fix(personal): show and select Persian dates without moving stored schedules

Quinn White committed Oct 21, 2026 at 03:57 UTC c5cb3084f665cb5b1546447e34e35963e5ea5597
8 files changed +452 -9
apps/lifemate/lib/standalone/personal_date_picker.dart new
+166
@@ -0,0 +1,166 @@
1 +import 'package:flutter/material.dart';
2 +import 'package:shamsi_date/shamsi_date.dart';
3 +
4 +import 'personal_dates.dart';
5 +
6 +/// Presentation-only calendar: the result is still a local Gregorian DateTime.
7 +/// No stored date, weekday or notification timestamp is converted in place.
8 +Future<DateTime?> showPersonalDatePicker({
9 + required BuildContext context,
10 + required DateTime initialDate,
11 + required DateTime firstDate,
12 + required DateTime lastDate,
13 +}) {
14 + final first = DateUtils.dateOnly(firstDate);
15 + final last = DateUtils.dateOnly(lastDate);
16 + if (last.isBefore(first)) throw ArgumentError('Invalid calendar date range.');
17 + final initial = DateUtils.dateOnly(initialDate);
18 + return showDialog<DateTime>(
19 + context: context,
20 + builder: (_) => _PersianDateDialog(
21 + initial: initial.isBefore(first)
22 + ? first
23 + : initial.isAfter(last)
24 + ? last
25 + : initial,
26 + first: first,
27 + last: last,
28 + ),
29 + );
30 +}
31 +
32 +class _PersianDateDialog extends StatefulWidget {
33 + const _PersianDateDialog({
34 + required this.initial,
35 + required this.first,
36 + required this.last,
37 + });
38 + final DateTime initial;
39 + final DateTime first;
40 + final DateTime last;
41 +
42 + @override
43 + State<_PersianDateDialog> createState() => _PersianDateDialogState();
44 +}
45 +
46 +class _PersianDateDialogState extends State<_PersianDateDialog> {
47 + late final _first = Jalali.fromDateTime(widget.first);
48 + late final _last = Jalali.fromDateTime(widget.last);
49 + late Jalali _selected = Jalali.fromDateTime(widget.initial);
50 +
51 + int _firstMonth(int year) => year == _first.year ? _first.month : 1;
52 + int _lastMonth(int year) => year == _last.year ? _last.month : 12;
53 + int _firstDay(int year, int month) =>
54 + year == _first.year && month == _first.month ? _first.day : 1;
55 + int _lastDay(int year, int month) =>
56 + year == _last.year && month == _last.month
57 + ? _last.day
58 + : Jalali(year, month).monthLength;
59 +
60 + void _change({int? year, int? month, int? day}) {
61 + final y = (year ?? _selected.year).clamp(_first.year, _last.year);
62 + final m = (month ?? _selected.month).clamp(_firstMonth(y), _lastMonth(y));
63 + final d = (day ?? _selected.day).clamp(_firstDay(y, m), _lastDay(y, m));
64 + setState(() => _selected = Jalali(y, m, d));
65 + }
66 +
67 + Widget _field({
68 + required String id,
69 + required String label,
70 + required int value,
71 + required int first,
72 + required int last,
73 + required String Function(int) text,
74 + required ValueChanged<int> changed,
75 + }) =>
76 + InputDecorator(
77 + key: ValueKey('personal-date-$id'),
78 + decoration: InputDecoration(labelText: label),
79 + child: DropdownButtonHideUnderline(
80 + child: ConstrainedBox(
81 + constraints: const BoxConstraints(minHeight: 48),
82 + child: DropdownButton<int>(
83 + value: value,
84 + isExpanded: true,
85 + itemHeight: null,
86 + menuMaxHeight: MediaQuery.sizeOf(context).height * .6,
87 + items: [
88 + for (var item = first; item <= last; item++)
89 + DropdownMenuItem(
90 + value: item,
91 + child: ConstrainedBox(
92 + constraints: const BoxConstraints(minHeight: 48),
93 + child: Align(
94 + alignment: AlignmentDirectional.centerStart,
95 + child: Text(text(item))),
96 + ),
97 + ),
98 + ],
99 + onChanged: (selected) {
100 + if (selected != null) changed(selected);
101 + },
102 + ),
103 + ),
104 + ),
105 + );
106 +
107 + @override
108 + Widget build(BuildContext context) => AlertDialog(
109 + key: const Key('personal-date-dialog'),
110 + insetPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
111 + title: const Text('انتخاب تاریخ شمسی'),
112 + content: SizedBox(
113 + width: 320,
114 + child: SingleChildScrollView(
115 + child: Column(
116 + mainAxisSize: MainAxisSize.min,
117 + crossAxisAlignment: CrossAxisAlignment.stretch,
118 + children: [
119 + const SizedBox(height: 8),
120 + _field(
121 + id: 'year',
122 + label: 'سال',
123 + value: _selected.year,
124 + first: _first.year,
125 + last: _last.year,
126 + text: personalDateDigits,
127 + changed: (value) => _change(year: value)),
128 + const SizedBox(height: 20),
129 + _field(
130 + id: 'month',
131 + label: 'ماه',
132 + value: _selected.month,
133 + first: _firstMonth(_selected.year),
134 + last: _lastMonth(_selected.year),
135 + text: (month) => Jalali(_selected.year, month).formatter.mN,
136 + changed: (value) => _change(month: value)),
137 + const SizedBox(height: 20),
138 + _field(
139 + id: 'day',
140 + label: 'روز',
141 + value: _selected.day,
142 + first: _firstDay(_selected.year, _selected.month),
143 + last: _lastDay(_selected.year, _selected.month),
144 + text: personalDateDigits,
145 + changed: (value) => _change(day: value)),
146 + const SizedBox(height: 20),
147 + Text(personalDateLabel(_selected.toDateTime()),
148 + key: const Key('personal-date-preview'),
149 + style: Theme.of(context).textTheme.titleMedium),
150 + const SizedBox(height: 12),
151 + const Text(
152 + 'اگر روز انتخابی در ماه یا سال جدید نباشد، آخرین روز مجاز انتخاب می‌شود.'),
153 + ],
154 + ),
155 + ),
156 + ),
157 + actions: [
158 + TextButton(
159 + onPressed: () => Navigator.pop(context),
160 + child: const Text('انصراف')),
161 + FilledButton(
162 + onPressed: () => Navigator.pop(context, _selected.toDateTime()),
163 + child: const Text('انتخاب تاریخ')),
164 + ],
165 + );
166 +}
apps/lifemate/lib/standalone/personal_dates.dart new
+13
@@ -0,0 +1,13 @@
1 +import 'package:shamsi_date/shamsi_date.dart';
2 +
3 +// Display calendar only. SQLite/notification dates remain local Gregorian dates.
4 +String personalDateDigits(Object value) => value.toString().replaceAllMapped(
5 + RegExp('[0-9]'), (match) => '۰۱۲۳۴۵۶۷۸۹'[int.parse(match[0]!)]);
6 +
7 +String personalDateLabel(DateTime value) {
8 + final date = Jalali.fromDateTime(value);
9 + return '${personalDateDigits(date.day)} ${date.formatter.mN} ${personalDateDigits(date.year)} شمسی';
10 +}
11 +
12 +String personalDayLabel(DateTime value) =>
13 + personalDateDigits(Jalali.fromDateTime(value).day);
apps/lifemate/lib/standalone/planner_ui.dart
+6 -5
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
2
3 import 'app.dart';
4 import 'model.dart';
5 +import 'personal_dates.dart';
6 +import 'personal_date_picker.dart';
7 import 'settings_ui.dart';
8 import 'store.dart';
9
@@ -24,8 +26,7 @@ const _weekdays = <int, String>{
26 4: 'پنجشنبه',
27 5: 'جمعه',
28 };
27 -String _date(DateTime value) =>
28 - '${_digits(value.year)}/${_digits(value.month)}/${_digits(value.day)} میلادی';
29 +String _date(DateTime value) => personalDateLabel(value);
30 String _kind(StandalonePlanKind value) => switch (value) {
31 StandalonePlanKind.task => 'کار',
32 StandalonePlanKind.event => 'رویداد',
@@ -339,7 +340,7 @@ class _StandalonePlannerShellState extends State<StandalonePlannerShell>
340 },
341 icon: const Icon(Icons.chevron_left)),
342 ]),
342 - const Text('تاریخ‌ها میلادی‌اند.', textAlign: TextAlign.center),
343 + const Text('تاریخ‌ها شمسی‌اند.', textAlign: TextAlign.center),
344 const SizedBox(height: 16),
345 Wrap(spacing: 8, runSpacing: 8, children: [
346 for (int index = 0; index < 7; index++)
@@ -347,7 +348,7 @@ class _StandalonePlannerShellState extends State<StandalonePlannerShell>
348 label: Padding(
349 padding: const EdgeInsets.symmetric(vertical: 8),
350 child: Text(
350 - '${_weekdays[_addDays(start, index).weekday]}\n${_digits(_addDays(start, index).day)}',
351 + '${_weekdays[_addDays(start, index).weekday]}\n${personalDayLabel(_addDays(start, index))}',
352 textAlign: TextAlign.center),
353 ),
354 selected: _addDays(start, index) == _selectedDate,
@@ -773,7 +774,7 @@ class _PlanEditorState extends State<_PlanEditor> {
774 super.dispose();
775 }
776
776 - Future<DateTime?> _pickDate(DateTime initial) => showDatePicker(
777 + Future<DateTime?> _pickDate(DateTime initial) => showPersonalDatePicker(
778 context: context,
779 initialDate: initial,
780 firstDate: DateTime(2020),
apps/lifemate/pubspec.lock
+8
@@ -429,6 +429,14 @@ packages:
429 url: "https://pub.dev"
430 source: hosted
431 version: "1.1.1"
432 + shamsi_date:
433 + dependency: "direct main"
434 + description:
435 + name: shamsi_date
436 + sha256: "0383fddc9bce91e9e08de0c909faf93c3ab3a0e532abd271fb0dcf5d0617487b"
437 + url: "https://pub.dev"
438 + source: hosted
439 + version: "1.1.1"
440 shared_preferences:
441 dependency: "direct main"
442 description:
apps/lifemate/pubspec.yaml
+1
@@ -11,6 +11,7 @@ dependencies:
11 sdk: flutter
12 cupertino_icons: ^1.0.8
13 intl: ^0.20.2
14 + shamsi_date: 1.1.1
15 http: ^1.5.0
16 shared_preferences: ^2.5.3
17 flutter_secure_storage: ^10.0.0
apps/lifemate/test/standalone_date_picker_test.dart new
+105
@@ -0,0 +1,105 @@
1 +import 'package:flutter/material.dart';
2 +import 'package:flutter_localizations/flutter_localizations.dart';
3 +import 'package:flutter_test/flutter_test.dart';
4 +import 'package:lifemate/standalone/personal_date_picker.dart';
5 +
6 +void main() {
7 + Future<void> choose(WidgetTester tester, String field, String text,
8 + {double delta = -180}) async {
9 + final input = find.byKey(ValueKey('personal-date-$field'));
10 + await tester.ensureVisible(input);
11 + await tester.tap(input);
12 + await tester.pumpAndSettle();
13 + final option = find.text(text);
14 + if (option.evaluate().isEmpty) {
15 + await tester.scrollUntilVisible(option, delta,
16 + scrollable: find.byType(Scrollable).last, maxScrolls: 100);
17 + }
18 + await tester.ensureVisible(option.last);
19 + await tester.tap(option.last);
20 + await tester.pumpAndSettle();
21 + }
22 +
23 + Future<void> launch(
24 + WidgetTester tester, DateTime initial, ValueChanged<DateTime?> result,
25 + {DateTime? first, DateTime? last}) async {
26 + await tester.pumpWidget(MaterialApp(
27 + locale: const Locale('fa'),
28 + supportedLocales: const [Locale('fa')],
29 + localizationsDelegates: const [
30 + GlobalMaterialLocalizations.delegate,
31 + GlobalWidgetsLocalizations.delegate,
32 + GlobalCupertinoLocalizations.delegate,
33 + ],
34 + home: Builder(
35 + builder: (context) => Scaffold(
36 + body: FilledButton(
37 + onPressed: () async => result(await showPersonalDatePicker(
38 + context: context,
39 + initialDate: initial,
40 + firstDate: first ?? DateTime(2020),
41 + lastDate: last ?? DateTime(2100))),
42 + child: const Text('باز کردن')))),
43 + ));
44 + await tester.tap(find.text('باز کردن'));
45 + await tester.pumpAndSettle();
46 + }
47 +
48 + Future<void> confirm(WidgetTester tester) async {
49 + await tester.tap(find.text('انتخاب تاریخ'));
50 + await tester.pumpAndSettle();
51 + }
52 +
53 + testWidgets('leap Esfand day30 becomes29 when year changes to nonleap',
54 + (tester) async {
55 + DateTime? selected;
56 + await launch(tester, DateTime(2025, 3, 20), (value) => selected = value);
57 + expect(find.text('۳۰ اسفند ۱۴۰۳ شمسی'), findsOneWidget);
58 + await choose(tester, 'year', '۱۴۰۴');
59 + expect(find.text('۲۹ اسفند ۱۴۰۴ شمسی'), findsOneWidget);
60 + await confirm(tester);
61 + expect(selected, DateTime(2026, 3, 20));
62 + expect(selected!.isUtc, isFalse);
63 + expect(tester.takeException(), isNull);
64 + });
65 +
66 + testWidgets('day31 changes to30 in a thirty-day Persian month',
67 + (tester) async {
68 + DateTime? selected;
69 + await launch(tester, DateTime(2026, 9, 22), (value) => selected = value);
70 + expect(find.text('۳۱ شهریور ۱۴۰۵ شمسی'), findsOneWidget);
71 + await choose(tester, 'month', 'مهر', delta: 180);
72 + expect(find.text('۳۰ مهر ۱۴۰۵ شمسی'), findsOneWidget);
73 + await confirm(tester);
74 + expect(selected, DateTime(2026, 10, 22));
75 + expect(tester.takeException(), isNull);
76 + });
77 +
78 + testWidgets('partial first and last month bounds remain inclusive',
79 + (tester) async {
80 + DateTime? selected;
81 + await launch(tester, DateTime(2025, 1, 1), (value) => selected = value,
82 + first: DateTime(2025, 1, 1), last: DateTime(2025, 1, 2));
83 + expect(find.text('۱۲ دی ۱۴۰۳ شمسی'), findsOneWidget);
84 + await choose(tester, 'day', '۱۳', delta: 180);
85 + await confirm(tester);
86 + expect(selected, DateTime(2025, 1, 2));
87 + expect(tester.takeException(), isNull);
88 + });
89 +
90 + testWidgets(
91 + 'existing2020to2100 bounds clamp initial dates without leaving range',
92 + (tester) async {
93 + for (final pair in [
94 + (DateTime(2019, 12, 31), DateTime(2020)),
95 + (DateTime(2100, 1, 2), DateTime(2100)),
96 + ]) {
97 + DateTime? selected;
98 + await launch(tester, pair.$1, (value) => selected = value);
99 + await confirm(tester);
100 + expect(selected, pair.$2);
101 + expect(tester.takeException(), isNull);
102 + await tester.pumpWidget(const SizedBox());
103 + }
104 + });
105 +}
apps/lifemate/test/standalone_dates_test.dart new
+26
@@ -0,0 +1,26 @@
1 +import 'package:flutter_test/flutter_test.dart';
2 +import 'package:lifemate/standalone/personal_dates.dart';
3 +
4 +void main() {
5 + final references = <DateTime, String>{
6 + DateTime(2024, 3, 20): '۱ فروردین ۱۴۰۳ شمسی',
7 + DateTime(2025, 3, 20): '۳۰ اسفند ۱۴۰۳ شمسی',
8 + DateTime(2025, 3, 21): '۱ فروردین ۱۴۰۴ شمسی',
9 + DateTime(2026, 10, 20): '۲۸ مهر ۱۴۰۵ شمسی',
10 + DateTime(2027, 3, 21): '۱ فروردین ۱۴۰۶ شمسی',
11 + };
12 + for (final entry in references.entries) {
13 + test('Persian date for ${entry.key.toIso8601String()}', () {
14 + expect(personalDateLabel(entry.key), entry.value);
15 + });
16 + }
17 + test('week chip day follows Persian month and does not use Gregorian day',
18 + () {
19 + expect(personalDayLabel(DateTime(2025, 3, 20)), '۳۰');
20 + expect(personalDayLabel(DateTime(2025, 3, 21)), '۱');
21 + });
22 + test('display uses the supplied calendar fields without moving UTC midnight',
23 + () {
24 + expect(personalDateLabel(DateTime.utc(2027, 3, 21)), '۱ فروردین ۱۴۰۶ شمسی');
25 + });
26 +}
apps/lifemate/test/standalone_ui_test.dart
+127 -4
@@ -1,6 +1,8 @@
1 import 'dart:io';
2 +import 'dart:ui' as ui;
3
4 import 'package:flutter/material.dart';
5 +import 'package:flutter/rendering.dart';
6 import 'package:flutter/services.dart';
7 import 'package:flutter_test/flutter_test.dart';
8 import 'package:lifemate/standalone/app.dart';
@@ -19,6 +21,9 @@ void main() {
21 await (FontLoader('LifeGuidePersian')
22 ..addFont(rootBundle.load('assets/fonts/LifeGuidePersian.ttf')))
23 .load();
24 + await (FontLoader('MaterialIcons')
25 + ..addFont(rootBundle.load('fonts/MaterialIcons-Regular.otf')))
26 + .load();
27 });
28 setUp(() async {
29 directory = await Directory.systemTemp.createTemp('lifeguide-ui-');
@@ -50,10 +55,12 @@ void main() {
55 Future<void> open(WidgetTester tester,
56 {Future<void> Function(StandaloneStore)? before,
57 Future<void> Function(StandaloneStore)? after}) async {
53 - await tester.pumpWidget(StandaloneApp(
54 - openStore: () async => store,
55 - onBeforePlansChange: before,
56 - onPlansChanged: after));
58 + await tester.pumpWidget(RepaintBoundary(
59 + key: const Key('personal-test-surface'),
60 + child: StandaloneApp(
61 + openStore: () async => store,
62 + onBeforePlansChange: before,
63 + onPlansChanged: after)));
64 await idle(tester);
65 }
66
@@ -169,6 +176,122 @@ void main() {
176 });
177 });
178
179 + testWidgets(
180 + 'personal week shows Persian calendar without rewriting stored days',
181 + (tester) async {
182 + await tester.runAsync(() async {
183 + await seedDaily();
184 + final original = (await store.listPlans()).single;
185 + await open(tester);
186 + await tap(
187 + tester,
188 + find.descendant(
189 + of: find.byType(NavigationBar), matching: find.text('هفته')));
190 + expect(find.text('تاریخ‌ها شمسی‌اند.'), findsOneWidget);
191 + expect(find.textContaining('میلادی'), findsNothing);
192 + expect((await store.listPlans()).single.startDate, original.startDate);
193 + expect(tester.takeException(), isNull);
194 + });
195 + });
196 +
197 + testWidgets(
198 + 'Persian picker saves next-year date and keeps ISO SQLite after reopen',
199 + (tester) async {
200 + tester.view.physicalSize = const Size(390, 844);
201 + tester.view.devicePixelRatio = 1;
202 + addTearDown(() {
203 + tester.view.resetPhysicalSize();
204 + tester.view.resetDevicePixelRatio();
205 + });
206 + await tester.runAsync(() async {
207 + await profile();
208 + await open(tester);
209 + await tap(tester, find.text('برنامهٔ تازه'));
210 + await tester.enterText(
211 + find.byKey(const Key('plan-title')), 'نوروز ساختگی');
212 + await tap(tester, find.textContaining('تاریخ:'));
213 + expect(find.text('انتخاب تاریخ شمسی'), findsOneWidget);
214 + await tap(tester, find.byKey(const Key('personal-date-year')));
215 + await tap(tester, find.text('۱۴۰۶').last);
216 + await tap(tester, find.byKey(const Key('personal-date-month')));
217 + await tester.scrollUntilVisible(find.text('فروردین'), -150,
218 + scrollable: find.byType(Scrollable).last);
219 + await tap(tester, find.text('فروردین').last);
220 + await tap(tester, find.byKey(const Key('personal-date-day')));
221 + await tester.scrollUntilVisible(find.text('۱'), -150,
222 + scrollable: find.byType(Scrollable).last);
223 + await tap(tester, find.text('۱').last);
224 + final evidence = Platform.environment['LIFEGUIDE_UI_EVIDENCE_DIR'];
225 + if (evidence != null) {
226 + final boundary = tester.renderObject<RenderRepaintBoundary>(
227 + find.byKey(const Key('personal-test-surface')));
228 + final image = await boundary.toImage(pixelRatio: 2);
229 + final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
230 + await Directory(evidence).create(recursive: true);
231 + await File('$evidence/personal-jalali-picker.png')
232 + .writeAsBytes(bytes!.buffer.asUint8List());
233 + image.dispose();
234 + }
235 + await tap(tester, find.text('انتخاب تاریخ'));
236 + expect(find.text('تاریخ: ۱ فروردین ۱۴۰۶ شمسی'), findsOneWidget);
237 + await tap(tester, find.text('ذخیرهٔ برنامه'));
238 + final original = (await store.listPlans()).single;
239 + await tester.pumpWidget(const SizedBox());
240 + await store.close();
241 + final independent = await databaseFactoryFfiNoIsolate.openDatabase(path,
242 + options: OpenDatabaseOptions(readOnly: true, singleInstance: false));
243 + expect((await independent.query('local_plans')).single['start_date'],
244 + '2027-03-21');
245 + await independent.close();
246 + store = await StandaloneStore.open(
247 + path: path, factory: databaseFactoryFfiNoIsolate);
248 + await open(tester);
249 + expect((await store.listPlans()).single.id, original.id);
250 + expect((await store.listPlans()).single.startDate, DateTime(2027, 3, 21));
251 + expect(tester.takeException(), isNull);
252 + });
253 + });
254 +
255 + testWidgets('Persian picker confirms and cancels at320px and200percent text',
256 + (tester) async {
257 + tester.view.physicalSize = const Size(320, 900);
258 + tester.view.devicePixelRatio = 1;
259 + tester.platformDispatcher.textScaleFactorTestValue = 2;
260 + addTearDown(() {
261 + tester.view.resetPhysicalSize();
262 + tester.view.resetDevicePixelRatio();
263 + tester.platformDispatcher.clearTextScaleFactorTestValue();
264 + });
265 + await tester.runAsync(() async {
266 + await profile();
267 + await open(tester);
268 + await tap(tester, find.text('برنامهٔ تازه'));
269 + await tap(tester, find.textContaining('تاریخ:'));
270 + expect(find.text('انتخاب تاریخ شمسی'), findsOneWidget);
271 + await tap(tester, find.byKey(const Key('personal-date-year')));
272 + await tap(tester, find.text('۱۴۰۶').last);
273 + await tap(tester, find.byKey(const Key('personal-date-month')));
274 + await tester.scrollUntilVisible(find.text('فروردین'), -150,
275 + scrollable: find.byType(Scrollable).last);
276 + await tap(tester, find.text('فروردین').last);
277 + await tap(tester, find.byKey(const Key('personal-date-day')));
278 + await tester.scrollUntilVisible(find.text('۱'), -150,
279 + scrollable: find.byType(Scrollable).last);
280 + await tap(tester, find.text('۱').last);
281 + await tap(tester, find.text('انتخاب تاریخ'));
282 + expect(find.text('تاریخ: ۱ فروردین ۱۴۰۶ شمسی'), findsOneWidget);
283 + final originalLabel =
284 + tester.widget<Text>(find.textContaining('تاریخ:')).data;
285 + await tap(tester, find.textContaining('تاریخ:'));
286 + await tap(tester, find.byKey(const Key('personal-date-year')));
287 + await tap(tester, find.text('۱۴۰۵').last);
288 + await tap(tester, find.text('انصراف').last);
289 + expect(find.text(originalLabel!), findsOneWidget);
290 + expect(await store.listPlans(), isEmpty);
291 + expect(tester.takeException(), isNull);
292 + });
293 + });
294 +
295 testWidgets('weekly editor persists different hours for selected weekdays',
296 (tester) async {
297 await tester.runAsync(() async {