@davoodmehraban89 / LifeMate / commits / 796d6be
Fork 0

LifeMate — Personal, Family & Learning Companion

feat: persist personalized local profiles

davoodmehraban89 committed Oct 7, 2026 at 22:30 UTC 796d6bec16841020323bf67275f60c38048a0374
1 file changed +132 -11
apps/lifemate/lib/role_entry_main.dart
+132 -11
@@ -1,5 +1,6 @@
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 'main.dart' show HomeShell;
@@ -25,20 +26,137 @@ class LifeGuideRoleTestApp extends StatelessWidget {
26 inputDecorationTheme: const InputDecorationTheme(border: OutlineInputBorder()),
27 useMaterial3: true,
28 ),
28 - home: const Directionality(textDirection: TextDirection.rtl, child: RoleEntryPage()),
29 + home: const Directionality(textDirection: TextDirection.rtl, child: RoleEntryGate()),
30 );
31 }
32
32 -class RoleEntryPage extends StatelessWidget {
33 - const RoleEntryPage({super.key});
33 +class LocalTestProfile {
34 + const LocalTestProfile({required this.category, required this.displayName});
35 + final String category;
36 + final String displayName;
37 +}
38 +
39 +class LocalTestProfileStore {
40 + static const _categoryKey = 'lifeguide.local_profile.category';
41 + static const _displayNameKey = 'lifeguide.local_profile.display_name';
42 +
43 + Future<LocalTestProfile?> load() async {
44 + final prefs = await SharedPreferences.getInstance();
45 + final category = prefs.getString(_categoryKey);
46 + final displayName = prefs.getString(_displayNameKey);
47 + if (category == null || displayName == null || displayName.trim().isEmpty) {
48 + return null;
49 + }
50 + return LocalTestProfile(category: category, displayName: displayName);
51 + }
52 +
53 + Future<void> save(LocalTestProfile profile) async {
54 + final name = profile.displayName.trim();
55 + if (name.isEmpty) throw ArgumentError('displayName cannot be empty');
56 + final prefs = await SharedPreferences.getInstance();
57 + await prefs.setString(_categoryKey, profile.category);
58 + await prefs.setString(_displayNameKey, name);
59 + }
60 +}
61 +
62 +class RoleEntryGate extends StatefulWidget {
63 + const RoleEntryGate({super.key});
64 +
65 + @override
66 + State<RoleEntryGate> createState() => _RoleEntryGateState();
67 +}
68 +
69 +class _RoleEntryGateState extends State<RoleEntryGate> {
70 + final store = LocalTestProfileStore();
71 + LocalTestProfile? profile;
72 + bool loading = true;
73 +
74 + @override
75 + void initState() {
76 + super.initState();
77 + _load();
78 + }
79 +
80 + Future<void> _load() async {
81 + final saved = await store.load();
82 + if (!mounted) return;
83 + setState(() {
84 + profile = saved;
85 + loading = false;
86 + });
87 + }
88
35 - void enter(BuildContext context, String role, String label) {
36 - final api = LocalRoleTestApi(role: role, label: label);
89 + void _openHome(LocalTestProfile value) {
90 + final role = value.category == 'adult' ? 'parent_guardian' : 'teen_minor';
91 Navigator.of(context).pushReplacement(
38 - MaterialPageRoute(builder: (_) => HomeShell(api: api)),
92 + MaterialPageRoute(
93 + builder: (_) => HomeShell(
94 + api: LocalRoleTestApi(
95 + role: role,
96 + label: value.displayName,
97 + category: value.category,
98 + ),
99 + ),
100 + ),
101 );
102 }
103
104 + @override
105 + Widget build(BuildContext context) {
106 + if (loading) {
107 + return const Scaffold(body: Center(child: CircularProgressIndicator()));
108 + }
109 + if (profile != null) {
110 + WidgetsBinding.instance.addPostFrameCallback((_) {
111 + if (mounted) _openHome(profile!);
112 + });
113 + return const Scaffold(body: SizedBox.shrink());
114 + }
115 + return RoleEntryPage(
116 + onComplete: (value) async {
117 + await store.save(value);
118 + if (mounted) _openHome(value);
119 + },
120 + );
121 + }
122 +}
123 +
124 +class RoleEntryPage extends StatelessWidget {
125 + const RoleEntryPage({super.key, required this.onComplete});
126 + final Future<void> Function(LocalTestProfile profile) onComplete;
127 +
128 + Future<void> _choose(BuildContext context, String category, String fallbackName) async {
129 + final controller = TextEditingController(text: fallbackName);
130 + final name = await showDialog<String>(
131 + context: context,
132 + builder: (dialogContext) => AlertDialog(
133 + title: const Text('اسم این پروفایل چیست؟'),
134 + content: TextField(
135 + controller: controller,
136 + autofocus: true,
137 + textInputAction: TextInputAction.done,
138 + decoration: const InputDecoration(labelText: 'نام نمایشی'),
139 + onSubmitted: (value) {
140 + if (value.trim().isNotEmpty) Navigator.pop(dialogContext, value.trim());
141 + },
142 + ),
143 + actions: [
144 + TextButton(onPressed: () => Navigator.pop(dialogContext), child: const Text('انصراف')),
145 + FilledButton(
146 + onPressed: () {
147 + final value = controller.text.trim();
148 + if (value.isNotEmpty) Navigator.pop(dialogContext, value);
149 + },
150 + child: const Text('ادامه'),
151 + ),
152 + ],
153 + ),
154 + );
155 + controller.dispose();
156 + if (name == null || name.trim().isEmpty) return;
157 + await onComplete(LocalTestProfile(category: category, displayName: name.trim()));
158 + }
159 +
160 @override
161 Widget build(BuildContext context) => Scaffold(
162 body: SafeArea(
@@ -54,12 +172,14 @@ class RoleEntryPage extends StatelessWidget {
172 const Text('Life Guide', textAlign: TextAlign.center, style: TextStyle(fontSize: 32, fontWeight: FontWeight.w700)),
173 const SizedBox(height: 8),
174 const Text('چه کسی وارد می‌شود؟', textAlign: TextAlign.center, style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600)),
175 + const SizedBox(height: 8),
176 + const Text('یک‌بار پروفایل را انتخاب و نام‌گذاری کن؛ دفعه‌های بعد مستقیم وارد فضای شخصی خودت می‌شوی.', textAlign: TextAlign.center),
177 const SizedBox(height: 28),
58 - _RoleButton(icon: Icons.school_rounded, title: 'فرزند / دانش‌آموز', subtitle: 'ورود به فضای شخصی و تحصیلی', onTap: () => enter(context, 'teen_minor', 'فرزند')),
178 + _RoleButton(icon: Icons.girl_rounded, title: 'فرزند دختر', subtitle: 'فضای شخصی و تحصیلی با تم دخترانه', onTap: () => _choose(context, 'girl_minor', 'آرام')),
179 const SizedBox(height: 12),
60 - _RoleButton(icon: Icons.woman_rounded, title: 'مادر', subtitle: 'ورود به فضای والد و خانواده', onTap: () => enter(context, 'parent_guardian', 'مادر')),
180 + _RoleButton(icon: Icons.boy_rounded, title: 'فرزند پسر', subtitle: 'فضای شخصی و تحصیلی با تم پسرانه', onTap: () => _choose(context, 'boy_minor', 'فرزند')),
181 const SizedBox(height: 12),
62 - _RoleButton(icon: Icons.man_rounded, title: 'پدر', subtitle: 'ورود به فضای والد و خانواده', onTap: () => enter(context, 'parent_guardian', 'پدر')),
182 + _RoleButton(icon: Icons.person_rounded, title: 'بزرگسال', subtitle: 'فضای شخصی، خانواده و مدیریت', onTap: () => _choose(context, 'adult', 'بزرگسال')),
183 const SizedBox(height: 20),
184 const Text('نسخه آزمایشی: ورود ایمیل/رمز موقتاً غیرفعال است.', textAlign: TextAlign.center, style: TextStyle(fontSize: 12)),
185 ],
@@ -90,9 +210,10 @@ class _RoleButton extends StatelessWidget {
210 }
211
212 class LocalRoleTestApi implements IdentityApi {
93 - LocalRoleTestApi({required this.role, required this.label});
213 + LocalRoleTestApi({required this.role, required this.label, this.category});
214 final String role;
215 final String label;
216 + final String? category;
217 bool familyCreated = false;
218 String familyName = 'خانواده من';
219 @override String? accessToken = 'local-role-test';
@@ -102,7 +223,7 @@ class LocalRoleTestApi implements IdentityApi {
223 @override Future<void> forgotPassword(String email) async {}
224 @override Future<void> resetPassword(String token, String newPassword) async {}
225 @override Future<void> changePassword(String currentPassword, String newPassword) async {}
105 - @override Future<Map<String,dynamic>> getProfile() async => {'display_name': label, 'email_normalized':'test@lifeguide.local', 'user_id': role == 'teen_minor' ? 'student-test' : 'parent-test', 'theme_preference': role == 'teen_minor' ? 'girl_pink' : 'adult_blue'};
226 + @override Future<Map<String,dynamic>> getProfile() async => {'display_name': label, 'email_normalized':'test@lifeguide.local', 'user_id': role == 'teen_minor' ? 'student-test' : 'parent-test', 'theme_preference': category == 'girl_minor' ? 'girl_pink' : category == 'boy_minor' ? 'boy_blue' : 'adult_blue'};
227 @override Future<Map<String,dynamic>> updateProfile({String? displayName,String? birthDate,String? themePreference}) async => {'display_name':displayName ?? label,'theme_preference':themePreference ?? 'adult_blue'};
228 @override Future<List<Map<String,dynamic>>> listFamilies() async => familyCreated ? [{'id':'family-test','name':familyName,'role':role,'is_admin':role == 'parent_guardian'}] : [];
229 @override Future<List<Map<String,dynamic>>> listFamilyMembers(String familyId) async => [];