LifeMate — Personal, Family & Learning Companion

fix/local-task-persistence
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 332 lines 10.9 KB
Raw
1 import 'package:flutter/material.dart';
2 import 'package:flutter_localizations/flutter_localizations.dart';
3 import 'package:shared_preferences/shared_preferences.dart';
4
5 import 'api.dart';
6 import 'local_data_store.dart';
7 import 'local_only_api.dart';
8 import 'main.dart' show HomeShell;
9 import 'phase3_ui.dart' show localDataErrorText;
10
11 void main() => runApp(const LifeGuideRoleTestApp());
12
13 class LifeGuideRoleTestApp extends StatelessWidget {
14 const LifeGuideRoleTestApp({super.key});
15
16 @override
17 Widget build(BuildContext context) => MaterialApp(
18 debugShowCheckedModeBanner: false,
19 locale: const Locale('fa'),
20 supportedLocales: const [Locale('fa'), Locale('en')],
21 localizationsDelegates: const [
22 GlobalMaterialLocalizations.delegate,
23 GlobalWidgetsLocalizations.delegate,
24 GlobalCupertinoLocalizations.delegate,
25 ],
26 theme: ThemeData(
27 colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF4D86E8)),
28 scaffoldBackgroundColor: const Color(0xFFF8FAFD),
29 inputDecorationTheme:
30 const InputDecorationTheme(border: OutlineInputBorder()),
31 useMaterial3: true,
32 ),
33 home: const Directionality(
34 textDirection: TextDirection.rtl, child: RoleEntryGate()),
35 );
36 }
37
38 class LocalTestProfile {
39 const LocalTestProfile({required this.category, required this.displayName});
40 final String category;
41 final String displayName;
42 }
43
44 class LocalTestProfileStore {
45 static const _categoryKey = 'lifeguide.local_profile.category';
46 static const _displayNameKey = 'lifeguide.local_profile.display_name';
47
48 Future<LocalTestProfile?> load() async {
49 final saved = await LocalDataStore.readSavedProfile();
50 if (saved != null) {
51 return LocalTestProfile(
52 category: saved['profile_category'] as String,
53 displayName: saved['display_name'] as String);
54 }
55 final prefs = await SharedPreferences.getInstance();
56 await prefs.reload();
57 final category = prefs.getString(_categoryKey);
58 final displayName = prefs.getString(_displayNameKey);
59 if (!LocalDataStore.categories.contains(category) ||
60 displayName == null ||
61 displayName.trim().isEmpty) {
62 return null;
63 }
64 return LocalTestProfile(category: category!, displayName: displayName);
65 }
66
67 Future<void> save(LocalTestProfile profile) async {
68 final name = profile.displayName.trim();
69 if (name.isEmpty) throw ArgumentError('displayName cannot be empty');
70 if (!LocalDataStore.categories.contains(profile.category)) {
71 throw ArgumentError('invalid profile category');
72 }
73 await LocalDataStore(category: profile.category, displayName: name)
74 .mutate((data) {
75 final saved = data['profile'] as Map<String, dynamic>;
76 if (saved['profile_category'] != profile.category) {
77 throw const ApiException(
78 409, 'local_profile_change_requires_confirmation');
79 }
80 saved['display_name'] = name;
81 });
82 }
83 }
84
85 class RoleEntryGate extends StatefulWidget {
86 const RoleEntryGate({super.key});
87
88 @override
89 State<RoleEntryGate> createState() => _RoleEntryGateState();
90 }
91
92 class _RoleEntryGateState extends State<RoleEntryGate> {
93 final store = LocalTestProfileStore();
94 LocalTestProfile? profile;
95 bool loading = true;
96 String? error;
97
98 @override
99 void initState() {
100 super.initState();
101 _load();
102 }
103
104 Future<void> _load() async {
105 setState(() {
106 loading = true;
107 error = null;
108 });
109 try {
110 final saved = await store.load();
111 if (!mounted) return;
112 setState(() {
113 profile = saved;
114 loading = false;
115 });
116 } catch (failure) {
117 if (mounted) {
118 setState(() {
119 loading = false;
120 error = localDataErrorText(failure);
121 });
122 }
123 }
124 }
125
126 void _openHome(LocalTestProfile value) {
127 Navigator.of(context).pushReplacement(
128 MaterialPageRoute(
129 builder: (_) => HomeShell(
130 localOnly: true,
131 profileCategory: value.category,
132 api: LocalOnlyApi(
133 category: value.category, displayName: value.displayName),
134 ),
135 ),
136 );
137 }
138
139 @override
140 Widget build(BuildContext context) {
141 if (loading) {
142 return const Scaffold(body: Center(child: CircularProgressIndicator()));
143 }
144 if (error != null) {
145 return Scaffold(
146 body: Center(
147 child: Column(mainAxisSize: MainAxisSize.min, children: [
148 Text(error!, textAlign: TextAlign.center),
149 TextButton(onPressed: _load, child: const Text('تلاش دوباره')),
150 ])));
151 }
152 if (profile != null) {
153 WidgetsBinding.instance.addPostFrameCallback((_) {
154 if (mounted) _openHome(profile!);
155 });
156 return const Scaffold(body: SizedBox.shrink());
157 }
158 return RoleEntryPage(
159 onComplete: (value) async {
160 await store.save(value);
161 if (mounted) _openHome(value);
162 },
163 );
164 }
165 }
166
167 class RoleEntryPage extends StatefulWidget {
168 const RoleEntryPage({super.key, required this.onComplete});
169 final Future<void> Function(LocalTestProfile profile) onComplete;
170
171 @override
172 State<RoleEntryPage> createState() => _RoleEntryPageState();
173 }
174
175 class _RoleEntryPageState extends State<RoleEntryPage> {
176 bool busy = false;
177
178 Future<void> _choose(
179 BuildContext context, String category, String fallbackName) async {
180 if (busy) return;
181 setState(() => busy = true);
182 try {
183 final name = await showDialog<String>(
184 context: context,
185 builder: (_) => _ProfileNameDialog(initialName: fallbackName));
186 if (name == null || name.trim().isEmpty) return;
187 await widget.onComplete(
188 LocalTestProfile(category: category, displayName: name.trim()));
189 } catch (failure) {
190 if (context.mounted) {
191 ScaffoldMessenger.of(context).showSnackBar(
192 SnackBar(content: Text(localDataErrorText(failure))),
193 );
194 }
195 } finally {
196 if (mounted) setState(() => busy = false);
197 }
198 }
199
200 @override
201 Widget build(BuildContext context) => Scaffold(
202 body: SafeArea(
203 child: Center(
204 child: ConstrainedBox(
205 constraints: const BoxConstraints(maxWidth: 480),
206 child: ListView(
207 shrinkWrap: true,
208 padding: const EdgeInsets.all(28),
209 children: [
210 const Icon(Icons.route_rounded, size: 72),
211 const SizedBox(height: 16),
212 const Text('Life Guide',
213 textAlign: TextAlign.center,
214 style:
215 TextStyle(fontSize: 32, fontWeight: FontWeight.w700)),
216 const SizedBox(height: 8),
217 const Text('چه کسی وارد می‌شود؟',
218 textAlign: TextAlign.center,
219 style:
220 TextStyle(fontSize: 20, fontWeight: FontWeight.w600)),
221 const SizedBox(height: 8),
222 const Text(
223 'یک‌بار پروفایل را انتخاب و نام‌گذاری کن؛ دفعه‌های بعد مستقیم وارد فضای شخصی خودت می‌شوی.',
224 textAlign: TextAlign.center),
225 const SizedBox(height: 28),
226 _RoleButton(
227 icon: Icons.girl_rounded,
228 title: 'فرزند دختر',
229 subtitle: 'فضای شخصی و تحصیلی با تم دخترانه',
230 onTap: busy
231 ? null
232 : () => _choose(context, 'girl_minor', 'آرام')),
233 const SizedBox(height: 12),
234 _RoleButton(
235 icon: Icons.boy_rounded,
236 title: 'فرزند پسر',
237 subtitle: 'فضای شخصی و تحصیلی با تم پسرانه',
238 onTap: busy
239 ? null
240 : () => _choose(context, 'boy_minor', 'فرزند')),
241 const SizedBox(height: 12),
242 _RoleButton(
243 icon: Icons.person_rounded,
244 title: 'بزرگسال',
245 subtitle: 'فضای شخصی، خانواده و مدیریت',
246 onTap: busy
247 ? null
248 : () => _choose(context, 'adult', 'بزرگسال')),
249 const SizedBox(height: 20),
250 const Text('نسخه آزمایشی: ورود ایمیل/رمز موقتاً غیرفعال است.',
251 textAlign: TextAlign.center,
252 style: TextStyle(fontSize: 12)),
253 ],
254 ),
255 ),
256 ),
257 ),
258 );
259 }
260
261 class _RoleButton extends StatelessWidget {
262 const _RoleButton(
263 {required this.icon,
264 required this.title,
265 required this.subtitle,
266 required this.onTap});
267 final IconData icon;
268 final String title;
269 final String subtitle;
270 final VoidCallback? onTap;
271 @override
272 Widget build(BuildContext context) => Card(
273 child: ListTile(
274 contentPadding:
275 const EdgeInsets.symmetric(horizontal: 18, vertical: 10),
276 leading: Icon(icon, size: 36),
277 title:
278 Text(title, style: const TextStyle(fontWeight: FontWeight.w700)),
279 subtitle: Text(subtitle),
280 trailing: const Icon(Icons.chevron_left_rounded),
281 onTap: onTap,
282 ),
283 );
284 }
285
286 class _ProfileNameDialog extends StatefulWidget {
287 const _ProfileNameDialog({required this.initialName});
288 final String initialName;
289 @override
290 State<_ProfileNameDialog> createState() => _ProfileNameDialogState();
291 }
292
293 class _ProfileNameDialogState extends State<_ProfileNameDialog> {
294 final formKey = GlobalKey<FormState>();
295 late final name = TextEditingController(text: widget.initialName);
296
297 @override
298 void dispose() {
299 name.dispose();
300 super.dispose();
301 }
302
303 void submit() {
304 if (formKey.currentState!.validate()) {
305 Navigator.pop(context, name.text.trim());
306 }
307 }
308
309 @override
310 Widget build(BuildContext context) => AlertDialog(
311 title: const Text('اسم این پروفایل چیست؟'),
312 content: Form(
313 key: formKey,
314 child: TextFormField(
315 controller: name,
316 autofocus: true,
317 maxLength: 120,
318 textInputAction: TextInputAction.done,
319 decoration: const InputDecoration(labelText: 'نام نمایشی'),
320 validator: (value) => value == null || value.trim().isEmpty
321 ? 'نام را وارد کن.'
322 : null,
323 onFieldSubmitted: (_) => submit(),
324 )),
325 actions: [
326 TextButton(
327 onPressed: () => Navigator.pop(context),
328 child: const Text('انصراف')),
329 FilledButton(onPressed: submit, child: const Text('ادامه')),
330 ],
331 );
332 }