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 198 lines 8.67 KB
Raw
1 import 'package:flutter/material.dart';
2 import 'api.dart';
3 import 'device_reminder_coordinator.dart';
4 import 'device_reminder_preferences.dart';
5 import 'local_reminders.dart';
6
7 class DeviceReminderSettingsPage extends StatefulWidget {
8 const DeviceReminderSettingsPage(
9 {super.key, required this.api, this.coordinator});
10 final HttpIdentityApi api;
11 final DeviceReminderCoordinator? coordinator;
12 @override
13 State<DeviceReminderSettingsPage> createState() =>
14 _DeviceReminderSettingsPageState();
15 }
16
17 class _DeviceReminderSettingsPageState extends State<DeviceReminderSettingsPage>
18 with WidgetsBindingObserver {
19 final _nickname = TextEditingController();
20 final _preferences = DeviceReminderPreferences();
21 LocalReminderStatus? _status;
22 String? _error;
23 bool _busy = true;
24 DeviceReminderCoordinator get _coordinator =>
25 widget.coordinator ?? DeviceReminderCoordinator(widget.api);
26 @override
27 void initState() {
28 super.initState();
29 WidgetsBinding.instance.addObserver(this);
30 _run(_load);
31 }
32
33 @override
34 void dispose() {
35 WidgetsBinding.instance.removeObserver(this);
36 _nickname.dispose();
37 super.dispose();
38 }
39
40 @override
41 void didChangeAppLifecycleState(AppLifecycleState state) {
42 if (state == AppLifecycleState.resumed && !_busy) _run(_refresh);
43 }
44
45 Future<void> _load() async {
46 final scope = widget.api.cacheNamespace;
47 if (scope == null) throw const ApiException(401, 'session_expired');
48 final name = await _preferences.read(scope);
49 final status = await _coordinator.status();
50 if (!mounted || widget.api.cacheNamespace != scope) return;
51 _nickname.text = name;
52 _status = status;
53 }
54
55 Future<void> _refresh() async {
56 _status = await _coordinator.reconcile();
57 }
58
59 Future<void> _run(Future<void> Function() work) async {
60 if (mounted) {
61 setState(() {
62 _busy = true;
63 _error = null;
64 });
65 }
66 try {
67 await work();
68 } catch (error) {
69 if (mounted) {
70 _error = error is ArgumentError
71 ? 'نام همراه باید حداکثر ۴۰ نویسه باشد و کاراکتر کنترلی نداشته باشد.'
72 : error is ApiException && error.statusCode == 429
73 ? 'درخواست‌ها زیاد است؛ کمی بعد دوباره تلاش کنید.'
74 : error is ApiException && error.statusCode == 401
75 ? 'نشست پایان یافته؛ دوباره وارد شوید.'
76 : 'یادآوری آماده نشد. اتصال، مجوز اعلان و تنظیمات دستگاه را بررسی کنید؛ مهلت قبلی تمدید نشد.';
77 }
78 } finally {
79 if (mounted) {
80 setState(() {
81 _busy = false;
82 });
83 }
84 }
85 }
86
87 Future<void> _saveNickname() async {
88 final scope = widget.api.cacheNamespace;
89 if (scope == null) throw const ApiException(401, 'session_expired');
90 await _preferences.write(scope, _nickname.text);
91 _status = await _coordinator.reconcile();
92 }
93
94 String _localTime(DateTime value) {
95 final local = value.toLocal();
96 String two(int n) => '$n'.padLeft(2, '0');
97 return '${local.year}/${two(local.month)}/${two(local.day)}، ${two(local.hour)}:${two(local.minute)}';
98 }
99
100 @override
101 Widget build(BuildContext context) {
102 final status = _status;
103 final owned = status?.activeScope == widget.api.cacheNamespace;
104 final enabled = status?.active == true && owned;
105 final supported = status?.supported == true;
106 return Directionality(
107 textDirection: TextDirection.rtl,
108 child: Scaffold(
109 appBar: AppBar(title: const Text('یادآوری و نام همراه')),
110 body: ListView(padding: const EdgeInsets.all(16), children: [
111 const Text('همراهِ خودت',
112 style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
113 const SizedBox(height: 12),
114 TextField(
115 key: const ValueKey('companion-nickname'),
116 controller: _nickname,
117 maxLength: 40,
118 enabled: !_busy,
119 decoration: const InputDecoration(
120 labelText: 'نام همراه',
121 hintText: 'LifeGuide',
122 helperText:
123 'این نام بالای یادآوری می‌آید؛ نام رسمی برنامه لایف‌گاید می‌ماند.')),
124 Align(
125 alignment: Alignment.centerRight,
126 child: FilledButton(
127 onPressed: _busy ? null : () => _run(_saveNickname),
128 child: const Text('ذخیرهٔ نام همراه'))),
129 const Divider(height: 32),
130 SwitchListTile(
131 contentPadding: EdgeInsets.zero,
132 title: const Text('یادآوری روی همین دستگاه'),
133 subtitle: const Text(
134 'فعال‌سازی با اجازهٔ شما؛ عنوان و یادداشت خصوصی در اعلان نمایش داده نمی‌شود.'),
135 value: enabled,
136 onChanged: _busy || !supported
137 ? null
138 : (value) => _run(() async {
139 _status = value
140 ? await _coordinator.enable(
141 companionName: _nickname.text.trim().isEmpty
142 ? 'LifeGuide'
143 : _nickname.text.trim())
144 : await _coordinator.disable();
145 })),
146 if (status != null && !supported)
147 const Text('اعلان پس‌زمینه در این دستگاه پشتیبانی نمی‌شود.'),
148 if (supported && !status!.permissionGranted)
149 const Text(
150 'مجوز اعلان یا کانال یادآوری در تنظیمات دستگاه بسته است.'),
151 if (supported)
152 Text(enabled
153 ? '${status!.scheduledCount} یادآوری برای این دستگاه آماده است.'
154 : 'یادآوری این حساب روی این دستگاه خاموش است.'),
155 if (enabled && status?.validUntil != null)
156 Padding(
157 padding: const EdgeInsets.only(top: 12),
158 child: Text(
159 'مهلت یادآوری: ${_localTime(status!.validUntil!)}\nتمدید فقط با اتصال و دریافت تازه از سرور؛ دادهٔ آفلاین مهلت را تمدید نمی‌کند.')),
160 if (supported)
161 Padding(
162 padding: const EdgeInsets.only(top: 12),
163 child: Text(status!.timing == 'exact'
164 ? 'مجوز زمان‌بندی دقیق برقرار است. تنظیمات باتری، بی‌صدا و مزاحم نشوید همچنان اثر دارند.'
165 : 'زمان‌بندی تقریبی است و ممکن است اعلان دیر برسد.')),
166 if (supported &&
167 status!.exactAlarmSupported &&
168 !status.canScheduleExactAlarms)
169 OutlinedButton(
170 onPressed: _busy
171 ? null
172 : () => _run(() async {
173 _status =
174 await _coordinator.requestExactAlarmAccess();
175 }),
176 child: const Text('اجازهٔ زمان‌بندی دقیق')),
177 if (supported)
178 OutlinedButton.icon(
179 onPressed: _busy ? null : () => _run(_refresh),
180 icon: const Icon(Icons.refresh),
181 label: const Text('دریافت تازه و آماده‌سازی یادآوری')),
182 const SizedBox(height: 16),
183 const Text(
184 'بستن اجباری برنامه (Force stop)، خاموشی گوشی یا محدودیت باتری می‌تواند اعلان را متوقف کند. صدا و نمایش روی قفل در کنترل تنظیمات گوشی است. لمس اعلان، خانهٔ برنامه را باز می‌کند؛ برای تیک انجام، برنامه را باز کنید.'),
185 if (_busy)
186 const Padding(
187 padding: EdgeInsets.all(16),
188 child: Center(child: CircularProgressIndicator())),
189 if (_error != null)
190 Padding(
191 padding: const EdgeInsets.only(top: 16),
192 child: Text(_error!,
193 style: TextStyle(
194 color: Theme.of(context).colorScheme.error))),
195 ]),
196 ));
197 }
198 }