LifeMate — Personal, Family & Learning Companion

docs/offline-data-rescue-options
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
dart 348 lines 13 KB
Raw
1 import 'package:flutter/material.dart';
2
3 import 'model.dart';
4 import 'store.dart';
5
6 const _localNotice =
7 'نسخهٔ شخصی این گوشی · بدون حساب و سرور\nبرنامه‌ها فقط روی همین گوشی نگه‌داری می‌شوند. همگام‌سازی، مشاهدهٔ خانواده و بازیابی از سرور ندارد. حذف برنامه یا پاک کردن داده‌های آن، برنامه‌های ذخیره‌شده را از بین می‌برد.';
8
9 String categoryLabel(StandaloneProfileCategory category) => switch (category) {
10 StandaloneProfileCategory.girlMinor => 'دختر',
11 StandaloneProfileCategory.boyMinor => 'پسر',
12 StandaloneProfileCategory.adult => 'بزرگسال',
13 };
14
15 class StandaloneOnboarding extends StatefulWidget {
16 const StandaloneOnboarding(
17 {super.key, required this.store, required this.onSaved});
18 final StandaloneStore store;
19 final Future<void> Function() onSaved;
20
21 @override
22 State<StandaloneOnboarding> createState() => _StandaloneOnboardingState();
23 }
24
25 class _StandaloneOnboardingState extends State<StandaloneOnboarding> {
26 final _name = TextEditingController();
27 StandaloneProfileCategory? _category;
28 bool _busy = false;
29 bool _committed = false;
30 String? _error;
31
32 @override
33 void dispose() {
34 _name.dispose();
35 super.dispose();
36 }
37
38 Future<void> _save() async {
39 if (_busy) return;
40 if (_name.text.trim().isEmpty || _category == null) {
41 setState(() => _error = 'نام دلخواه و گروه خودت را انتخاب کن.');
42 return;
43 }
44 setState(() {
45 _busy = true;
46 _error = null;
47 });
48 try {
49 if (!_committed) {
50 await widget.store.createProfile(
51 displayName: _name.text.trim(), category: _category!);
52 _committed = true;
53 }
54 } catch (_) {
55 if (mounted) {
56 setState(() => _error =
57 'نام روی این گوشی ذخیره نشد. فضای خالی گوشی را بررسی کن و دوباره تلاش کن.');
58 }
59 return;
60 } finally {
61 if (mounted) setState(() => _busy = false);
62 }
63 try {
64 await widget.onSaved();
65 } catch (_) {
66 if (mounted) {
67 setState(() => _error =
68 'نام روی این گوشی ذخیره شد؛ باز کردن برنامه انجام نشد. دوباره تلاش کن.');
69 }
70 }
71 }
72
73 @override
74 Widget build(BuildContext context) => Scaffold(
75 body: SafeArea(
76 child: Center(
77 child: ConstrainedBox(
78 constraints: const BoxConstraints(maxWidth: 560),
79 child: ListView(
80 padding: const EdgeInsets.all(24),
81 children: [
82 const SizedBox(height: 24),
83 Icon(Icons.spa_outlined,
84 size: 56, color: Theme.of(context).colorScheme.primary),
85 const SizedBox(height: 20),
86 Text('جای کوچکی برای برنامه‌های تو',
87 style: Theme.of(context).textTheme.headlineSmall,
88 textAlign: TextAlign.center),
89 const SizedBox(height: 12),
90 const Text(
91 'قدم‌های امروزت را بنویس و هر کدام را که انجام دادی تیک بزن.',
92 textAlign: TextAlign.center),
93 const SizedBox(height: 24),
94 const _LocalNoticeCard(),
95 const SizedBox(height: 24),
96 TextField(
97 controller: _name,
98 enabled: !_busy && !_committed,
99 maxLength: 60,
100 textInputAction: TextInputAction.done,
101 decoration: const InputDecoration(
102 labelText: 'دوست داری چه صدایت کنیم؟',
103 hintText: 'نام دلخواه'),
104 ),
105 const SizedBox(height: 16),
106 DropdownButtonFormField<StandaloneProfileCategory>(
107 initialValue: _category,
108 itemHeight: null,
109 isExpanded: true,
110 decoration: const InputDecoration(labelText: 'گروه من'),
111 items: StandaloneProfileCategory.values
112 .map((category) => DropdownMenuItem(
113 value: category,
114 child: Text(categoryLabel(category))))
115 .toList(),
116 onChanged: _busy || _committed
117 ? null
118 : (value) => setState(() => _category = value),
119 ),
120 const SizedBox(height: 10),
121 const Text(
122 'این انتخاب فقط برای شخصی‌سازی است؛ نقش خانوادگی یا حساب کاربری ایجاد نمی‌کند.'),
123 if (_error != null) ...[
124 const SizedBox(height: 16),
125 Text(_error!,
126 key: const Key('onboarding-error'),
127 style: TextStyle(
128 color: Theme.of(context).colorScheme.error)),
129 ],
130 const SizedBox(height: 24),
131 FilledButton(
132 onPressed: _busy ? null : _save,
133 child: Text(_busy
134 ? 'در حال ذخیره…'
135 : _committed
136 ? 'باز کردن برنامهٔ من'
137 : 'شروع برنامهٔ من'),
138 ),
139 ],
140 ),
141 ),
142 ),
143 ),
144 );
145 }
146
147 class StandaloneSettings extends StatefulWidget {
148 const StandaloneSettings({
149 super.key,
150 required this.store,
151 required this.profile,
152 required this.onSaved,
153 this.reminderSettings,
154 this.onBeforeChange,
155 this.onAfterChange,
156 });
157 final StandaloneStore store;
158 final StandaloneProfile profile;
159 final Future<void> Function() onSaved;
160 final Widget? reminderSettings;
161 final Future<void> Function()? onBeforeChange;
162 final Future<void> Function()? onAfterChange;
163
164 @override
165 State<StandaloneSettings> createState() => _StandaloneSettingsState();
166 }
167
168 class _StandaloneSettingsState extends State<StandaloneSettings> {
169 late final TextEditingController _name;
170 late final TextEditingController _companion;
171 late StandaloneProfileCategory _category;
172 late String _theme;
173 bool _busy = false;
174 String? _error;
175
176 @override
177 void initState() {
178 super.initState();
179 _name = TextEditingController(text: widget.profile.displayName);
180 _companion = TextEditingController(text: widget.profile.companionName);
181 _category = widget.profile.category;
182 _theme = StandaloneProfile.themeKeys.contains(widget.profile.themeKey)
183 ? widget.profile.themeKey
184 : 'violet';
185 }
186
187 @override
188 void dispose() {
189 _name.dispose();
190 _companion.dispose();
191 super.dispose();
192 }
193
194 Future<void> _save() async {
195 if (_busy) return;
196 if (_name.text.trim().isEmpty) {
197 setState(() => _error = 'نام دلخواهت را بنویس.');
198 return;
199 }
200 if (_companion.text.trim().isEmpty) {
201 setState(() => _error = 'یک نام برای همراه انتخاب کن.');
202 return;
203 }
204 setState(() {
205 _busy = true;
206 _error = null;
207 });
208 try {
209 await widget.onBeforeChange?.call();
210 await widget.store.updateProfile(
211 displayName: _name.text.trim(),
212 companionName: _companion.text.trim(),
213 category: _category,
214 themeKey: _theme,
215 );
216 } catch (_) {
217 try {
218 await widget.onAfterChange?.call();
219 } catch (_) {/* Original save error remains visible. */}
220 if (mounted) {
221 setState(() {
222 _busy = false;
223 _error = 'تغییرها ذخیره نشد. فضای گوشی را بررسی کن و دوباره تلاش کن.';
224 });
225 }
226 return;
227 }
228 // A reminder failure after the durable write must never invite duplicate saves.
229 bool reminderFailed = false;
230 try {
231 await widget.onAfterChange?.call();
232 } catch (_) {
233 reminderFailed = true;
234 }
235 bool refreshFailed = false;
236 try {
237 await widget.onSaved();
238 } catch (_) {
239 refreshFailed = true;
240 }
241 if (!mounted) return;
242 setState(() => _busy = false);
243 ScaffoldMessenger.of(context).showSnackBar(SnackBar(
244 content: Text(refreshFailed
245 ? 'تغییرها ذخیره شد؛ برای دیدن تغییرها برنامه را دوباره باز کن.'
246 : reminderFailed
247 ? 'تغییرها ذخیره شد؛ وضعیت یادآوری را در تنظیمات بررسی کن.'
248 : 'تغییرها روی این گوشی ذخیره شد.')));
249 }
250
251 @override
252 Widget build(BuildContext context) => ListView(
253 padding: const EdgeInsets.all(20),
254 children: [
255 Text('تنظیمات من', style: Theme.of(context).textTheme.headlineSmall),
256 const SizedBox(height: 16),
257 const _LocalNoticeCard(),
258 const SizedBox(height: 24),
259 TextField(
260 controller: _name,
261 enabled: !_busy,
262 maxLength: 60,
263 decoration: const InputDecoration(labelText: 'نام من')),
264 const SizedBox(height: 16),
265 TextField(
266 controller: _companion,
267 enabled: !_busy,
268 maxLength: 40,
269 decoration: const InputDecoration(
270 labelText: 'نام دلخواه همراه',
271 helperText: 'فقط یک نام شخصی روی همین گوشی است.')),
272 const SizedBox(height: 16),
273 DropdownButtonFormField<StandaloneProfileCategory>(
274 initialValue: _category,
275 itemHeight: null,
276 isExpanded: true,
277 decoration: const InputDecoration(labelText: 'گروه من'),
278 items: StandaloneProfileCategory.values
279 .map((category) => DropdownMenuItem(
280 value: category, child: Text(categoryLabel(category))))
281 .toList(),
282 onChanged:
283 _busy ? null : (value) => setState(() => _category = value!),
284 ),
285 const SizedBox(height: 20),
286 const Text('رنگ دلخواه؛ برای همهٔ گروه‌ها'),
287 const SizedBox(height: 8),
288 Wrap(spacing: 12, runSpacing: 8, children: [
289 ChoiceChip(
290 avatar: const CircleAvatar(backgroundColor: Color(0xff76609a)),
291 label: const Text('بنفش آرام'),
292 selected: _theme == 'violet',
293 onSelected:
294 _busy ? null : (_) => setState(() => _theme = 'violet')),
295 ChoiceChip(
296 avatar: const CircleAvatar(backgroundColor: Color(0xff276e83)),
297 label: const Text('آبی دریا'),
298 selected: _theme == 'blue',
299 onSelected:
300 _busy ? null : (_) => setState(() => _theme = 'blue')),
301 ChoiceChip(
302 avatar: const CircleAvatar(backgroundColor: Color(0xff397b65)),
303 label: const Text('سبز نعنایی'),
304 selected: _theme == 'mint',
305 onSelected:
306 _busy ? null : (_) => setState(() => _theme = 'mint')),
307 ChoiceChip(
308 avatar: const CircleAvatar(backgroundColor: Color(0xffad5c4c)),
309 label: const Text('مرجانی گرم'),
310 selected: _theme == 'coral',
311 onSelected:
312 _busy ? null : (_) => setState(() => _theme = 'coral')),
313 ]),
314 if (_error != null) ...[
315 const SizedBox(height: 16),
316 Text(_error!,
317 style: TextStyle(color: Theme.of(context).colorScheme.error)),
318 ],
319 const SizedBox(height: 20),
320 FilledButton(
321 onPressed: _busy ? null : _save,
322 child: Text(_busy ? 'در حال ذخیره…' : 'ذخیرهٔ تغییرها')),
323 const SizedBox(height: 24),
324 if (widget.reminderSettings != null) widget.reminderSettings!,
325 const SizedBox(height: 24),
326 const Text(
327 'تغییر نام، گروه یا رنگ، برنامه‌های قبلی را پاک نمی‌کند. برای حفظ داده‌ها، برنامه یا داده‌های آن را از تنظیمات گوشی حذف نکن.'),
328 ],
329 );
330 }
331
332 class _LocalNoticeCard extends StatelessWidget {
333 const _LocalNoticeCard();
334
335 @override
336 Widget build(BuildContext context) => Card(
337 margin: EdgeInsets.zero,
338 child: Padding(
339 padding: const EdgeInsets.all(16),
340 child: Row(crossAxisAlignment: CrossAxisAlignment.start, children: [
341 Icon(Icons.phone_android_outlined,
342 color: Theme.of(context).colorScheme.primary),
343 const SizedBox(width: 12),
344 const Expanded(child: Text(_localNotice)),
345 ]),
346 ),
347 );
348 }