LifeMate — Personal, Family & Learning Companion
fix: add durable device-only planner storage
Codex committed
Oct 8, 2026 at 03:35 UTC
e4df409c02ce842fe94fe0b02d3231e251fe355f
7 files changed
+942
.gitignore
+1
@@ -12,6 +12,7 @@ build/
12
.DS_Store
13
14
# Secrets
15
+node_modules/
16
.env
17
.env.*
18
!.env.example
apps/lifemate/lib/local_data_store.dart
new
+157
@@ -0,0 +1,157 @@
1
+import 'dart:convert';
2
+import 'dart:math';
3
+
4
+import 'package:shared_preferences/shared_preferences.dart';
5
+
6
+import 'api.dart';
7
+
8
+/// Device-only data for the explicitly separate, unauthenticated test entrypoint.
9
+/// Never used as an authorization boundary or as a production sync cache.
10
+class LocalDataStore {
11
+ LocalDataStore({required this.category, required this.displayName});
12
+
13
+ final String category;
14
+ final String displayName;
15
+ static const key = 'lifeguide.local_data.v1';
16
+ static const categories = {'girl_minor', 'boy_minor', 'adult'};
17
+ static const themes = {'girl_pink', 'boy_blue', 'adult_blue'};
18
+ static const kinds = {
19
+ 'task',
20
+ 'assignment',
21
+ 'exam',
22
+ 'event',
23
+ 'routine',
24
+ 'goal',
25
+ 'study_session'
26
+ };
27
+ static const statuses = {'planned', 'in_progress', 'completed', 'cancelled'};
28
+ // All instances share the writer lock: read/modify/write cannot lose a record.
29
+ static Future<void> _tail = Future.value();
30
+
31
+ static Future<T> _locked<T>(Future<T> Function() operation) {
32
+ final task = _tail.then((_) => operation());
33
+ _tail = task.then<void>((_) {}, onError: (Object _, StackTrace __) {});
34
+ return task;
35
+ }
36
+
37
+ static String newId() {
38
+ final random = Random.secure();
39
+ return List.generate(
40
+ 16, (_) => random.nextInt(256).toRadixString(16).padLeft(2, '0'))
41
+ .join();
42
+ }
43
+
44
+ static Map<String, dynamic> _copy(Map<String, dynamic> value) =>
45
+ jsonDecode(jsonEncode(value)) as Map<String, dynamic>;
46
+
47
+ static Map<String, dynamic> _decode(String raw) {
48
+ try {
49
+ final data = jsonDecode(raw) as Map<String, dynamic>;
50
+ final profile = data['profile'] as Map<String, dynamic>;
51
+ final items = data['items'] as List<dynamic>;
52
+ if (data['version'] != 1 ||
53
+ !categories.contains(profile['profile_category']) ||
54
+ profile['user_id'] is! String ||
55
+ (profile['user_id'] as String).isEmpty ||
56
+ profile['display_name'] is! String ||
57
+ (profile['display_name'] as String).trim().isEmpty ||
58
+ !themes.contains(profile['theme_preference'])) {
59
+ throw const FormatException();
60
+ }
61
+ final ids = <String>{};
62
+ for (final rawItem in items) {
63
+ final item = rawItem as Map<String, dynamic>;
64
+ if (item['id'] is! String ||
65
+ (item['id'] as String).isEmpty ||
66
+ !ids.add(item['id'] as String) ||
67
+ item['owner_user_id'] != profile['user_id'] ||
68
+ item['title'] is! String ||
69
+ (item['title'] as String).trim().isEmpty ||
70
+ !kinds.contains(item['kind']) ||
71
+ !statuses.contains(item['status']) ||
72
+ item['visibility'] != 'private') throw const FormatException();
73
+ for (final field in ['starts_at', 'due_at']) {
74
+ if (item[field] != null &&
75
+ DateTime.tryParse(item[field].toString()) == null) {
76
+ throw const FormatException();
77
+ }
78
+ }
79
+ }
80
+ return data;
81
+ } catch (_) {
82
+ throw const ApiException(500, 'local_data_corrupt');
83
+ }
84
+ }
85
+
86
+ static Future<SharedPreferences> _preferences() async {
87
+ try {
88
+ final prefs = await SharedPreferences.getInstance();
89
+ // Legacy SharedPreferences caches a value even if the platform write fails.
90
+ // Always reload the durable platform state before reading it.
91
+ await prefs.reload();
92
+ return prefs;
93
+ } catch (_) {
94
+ throw const ApiException(500, 'local_storage_failed');
95
+ }
96
+ }
97
+
98
+ static Future<void> _write(
99
+ SharedPreferences prefs, Map<String, dynamic> data) async {
100
+ try {
101
+ final raw = jsonEncode(data);
102
+ // Bounded test storage; fail explicitly rather than risk silent truncation.
103
+ if (utf8.encode(raw).length > 1024 * 1024 ||
104
+ !await prefs.setString(key, raw)) {
105
+ throw const ApiException(500, 'local_storage_failed');
106
+ }
107
+ } catch (_) {
108
+ throw const ApiException(500, 'local_storage_failed');
109
+ }
110
+ }
111
+
112
+ static Future<Map<String, dynamic>?> readSavedProfile() => _locked(() async {
113
+ final prefs = await _preferences();
114
+ final raw = prefs.getString(key);
115
+ if (raw == null) return null;
116
+ return _copy(_decode(raw)['profile'] as Map<String, dynamic>);
117
+ });
118
+
119
+ Future<Map<String, dynamic>> _load(SharedPreferences prefs) async {
120
+ final raw = prefs.getString(key);
121
+ if (raw != null) return _decode(raw);
122
+ if (!categories.contains(category) || displayName.trim().isEmpty) {
123
+ throw const ApiException(400, 'local_invalid_profile');
124
+ }
125
+ return {
126
+ 'version': 1,
127
+ 'profile': {
128
+ 'user_id': newId(),
129
+ 'profile_category': category,
130
+ 'display_name': displayName.trim(),
131
+ 'theme_preference': category == 'girl_minor'
132
+ ? 'girl_pink'
133
+ : category == 'boy_minor'
134
+ ? 'boy_blue'
135
+ : 'adult_blue',
136
+ },
137
+ 'items': <Map<String, dynamic>>[],
138
+ };
139
+ }
140
+
141
+ Future<Map<String, dynamic>> read() => _locked(() async {
142
+ final prefs = await _preferences();
143
+ final missing = prefs.getString(key) == null;
144
+ final data = await _load(prefs);
145
+ if (missing) await _write(prefs, data);
146
+ return _copy(data);
147
+ });
148
+
149
+ Future<T> mutate<T>(T Function(Map<String, dynamic> data) change) =>
150
+ _locked(() async {
151
+ final prefs = await _preferences();
152
+ final data = await _load(prefs);
153
+ final result = change(data);
154
+ await _write(prefs, data);
155
+ return result;
156
+ });
157
+}
apps/lifemate/lib/local_only_api.dart
new
+173
@@ -0,0 +1,173 @@
1
+import 'api.dart';
2
+import 'local_data_store.dart';
3
+
4
+/// Real device storage, without login, networking, family sharing or server sync.
5
+/// The authenticated application continues to use HttpIdentityApi exclusively.
6
+class LocalOnlyApi implements IdentityApi {
7
+ LocalOnlyApi({required String category, required String displayName})
8
+ : store = LocalDataStore(category: category, displayName: displayName);
9
+
10
+ final LocalDataStore store;
11
+
12
+ @override
13
+ String? get accessToken => null;
14
+
15
+ @override
16
+ Future<Map<String, dynamic>> getProfile() async =>
17
+ Map<String, dynamic>.from((await store.read())['profile'] as Map);
18
+
19
+ @override
20
+ Future<Map<String, dynamic>> updateProfile({
21
+ String? displayName,
22
+ String? birthDate,
23
+ String? themePreference,
24
+ }) async {
25
+ if (birthDate != null ||
26
+ (displayName != null &&
27
+ (displayName.trim().isEmpty || displayName.trim().length > 120)) ||
28
+ (themePreference != null &&
29
+ !LocalDataStore.themes.contains(themePreference))) {
30
+ throw const ApiException(400, 'local_invalid_profile');
31
+ }
32
+ return store.mutate((data) {
33
+ final profile = data['profile'] as Map<String, dynamic>;
34
+ if (displayName != null) profile['display_name'] = displayName.trim();
35
+ if (themePreference != null)
36
+ profile['theme_preference'] = themePreference;
37
+ return Map<String, dynamic>.from(profile);
38
+ });
39
+ }
40
+
41
+ @override
42
+ Future<List<Map<String, dynamic>>> listFamilies() async => [];
43
+
44
+ Map<String, dynamic> _fields(Map<String, dynamic> input) {
45
+ const allowed = {
46
+ 'kind',
47
+ 'title',
48
+ 'notes',
49
+ 'status',
50
+ 'priority',
51
+ 'startsAt',
52
+ 'dueAt',
53
+ 'visibility'
54
+ };
55
+ if (input.isEmpty ||
56
+ input.keys.any((key) => !allowed.contains(key)) ||
57
+ (input.containsKey('title') &&
58
+ (input['title'] is! String ||
59
+ (input['title'] as String).trim().isEmpty ||
60
+ (input['title'] as String).trim().length > 240)) ||
61
+ (input.containsKey('kind') &&
62
+ !LocalDataStore.kinds.contains(input['kind'])) ||
63
+ (input.containsKey('status') &&
64
+ !LocalDataStore.statuses.contains(input['status'])) ||
65
+ (input.containsKey('priority') &&
66
+ !{'low', 'normal', 'high', 'urgent'}.contains(input['priority'])) ||
67
+ (input.containsKey('visibility') && input['visibility'] != 'private') ||
68
+ (input['notes'] != null &&
69
+ (input['notes'] is! String ||
70
+ (input['notes'] as String).length > 10000))) {
71
+ throw const ApiException(400, 'local_invalid_item');
72
+ }
73
+ final fields = <String, dynamic>{};
74
+ for (final entry in input.entries) {
75
+ if (entry.key == 'startsAt' || entry.key == 'dueAt') {
76
+ final value = entry.value;
77
+ final date = value == null ? null : DateTime.tryParse(value.toString());
78
+ if (value != null && date == null)
79
+ throw const ApiException(400, 'local_invalid_item');
80
+ fields[entry.key == 'startsAt' ? 'starts_at' : 'due_at'] =
81
+ date?.toUtc().toIso8601String();
82
+ } else {
83
+ fields[entry.key] =
84
+ entry.key == 'title' ? (entry.value as String).trim() : entry.value;
85
+ }
86
+ }
87
+ return fields;
88
+ }
89
+
90
+ void _validateDates(Map<String, dynamic> item) {
91
+ final start = DateTime.tryParse(item['starts_at']?.toString() ?? '');
92
+ final due = DateTime.tryParse(item['due_at']?.toString() ?? '');
93
+ if (start != null && due != null && due.isBefore(start)) {
94
+ throw const ApiException(400, 'local_invalid_item');
95
+ }
96
+ }
97
+
98
+ @override
99
+ Future<Map<String, dynamic>> createPlanItem(
100
+ Map<String, dynamic> input) async {
101
+ final fields = _fields(input);
102
+ if (!fields.containsKey('title') || !fields.containsKey('kind')) {
103
+ throw const ApiException(400, 'local_invalid_item');
104
+ }
105
+ _validateDates(fields);
106
+ return store.mutate((data) {
107
+ final now = DateTime.now().toUtc().toIso8601String();
108
+ final item = <String, dynamic>{
109
+ 'id': LocalDataStore.newId(),
110
+ 'owner_user_id': (data['profile'] as Map)['user_id'],
111
+ 'status': 'planned',
112
+ 'priority': 'normal',
113
+ 'visibility': 'private',
114
+ 'created_at': now,
115
+ 'updated_at': now,
116
+ ...fields,
117
+ };
118
+ (data['items'] as List).add(item);
119
+ return Map<String, dynamic>.from(item);
120
+ });
121
+ }
122
+
123
+ @override
124
+ Future<Map<String, dynamic>> updatePlanItem(
125
+ String itemId, Map<String, dynamic> input) async {
126
+ final fields = _fields(input);
127
+ return store.mutate((data) {
128
+ final items = data['items'] as List;
129
+ final index = items.indexWhere((item) => (item as Map)['id'] == itemId);
130
+ if (index < 0) throw const ApiException(404, 'local_item_not_found');
131
+ final item = <String, dynamic>{
132
+ ...Map<String, dynamic>.from(items[index] as Map),
133
+ ...fields
134
+ };
135
+ _validateDates(item);
136
+ item['updated_at'] = DateTime.now().toUtc().toIso8601String();
137
+ items[index] = item;
138
+ return Map<String, dynamic>.from(item);
139
+ });
140
+ }
141
+
142
+ @override
143
+ Future<List<Map<String, dynamic>>> listPlanItems(
144
+ {DateTime? from, DateTime? to, String? kind}) async {
145
+ final data = await store.read();
146
+ final items = (data['items'] as List)
147
+ .map((item) => Map<String, dynamic>.from(item as Map));
148
+ return items.where((item) {
149
+ if (item['status'] == 'cancelled' ||
150
+ (kind != null && kind != item['kind'])) return false;
151
+ final date = DateTime.tryParse(
152
+ (item['due_at'] ?? item['starts_at'])?.toString() ?? '');
153
+ // Undated records remain visible in the planner; dates use [from, to).
154
+ if (date == null) return true;
155
+ return (from == null || !date.isBefore(from)) &&
156
+ (to == null || date.isBefore(to));
157
+ }).toList();
158
+ }
159
+
160
+ @override
161
+ Future<List<Map<String, dynamic>>> getToday(
162
+ {required DateTime from, required DateTime to}) async =>
163
+ (await listPlanItems(from: from, to: to))
164
+ .where((item) =>
165
+ item['status'] != 'completed' &&
166
+ (item['due_at'] != null || item['starts_at'] != null))
167
+ .toList();
168
+
169
+ // Unsupported flows fail visibly instead of manufacturing successful records.
170
+ @override
171
+ dynamic noSuchMethod(Invocation invocation) =>
172
+ throw const ApiException(409, 'local_feature_unavailable');
173
+}
apps/lifemate/pubspec.lock
new
+414
@@ -0,0 +1,414 @@
1
+# Generated by pub
2
+# See https://dart.dev/tools/pub/glossary#lockfile
3
+packages:
4
+ async:
5
+ dependency: transitive
6
+ description:
7
+ name: async
8
+ sha256: e2eb0491ba5ddb6177742d2da23904574082139b07c1e33b8503b9f46f3e1a37
9
+ url: "https://pub.dev"
10
+ source: hosted
11
+ version: "2.13.1"
12
+ boolean_selector:
13
+ dependency: transitive
14
+ description:
15
+ name: boolean_selector
16
+ sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea"
17
+ url: "https://pub.dev"
18
+ source: hosted
19
+ version: "2.1.2"
20
+ characters:
21
+ dependency: transitive
22
+ description:
23
+ name: characters
24
+ sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b
25
+ url: "https://pub.dev"
26
+ source: hosted
27
+ version: "1.4.1"
28
+ clock:
29
+ dependency: transitive
30
+ description:
31
+ name: clock
32
+ sha256: e51d50bca3217c9a9fa2b41a30e4a38971133f5f9ec7a3d57bae095007f1d28e
33
+ url: "https://pub.dev"
34
+ source: hosted
35
+ version: "1.1.3"
36
+ collection:
37
+ dependency: transitive
38
+ description:
39
+ name: collection
40
+ sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
41
+ url: "https://pub.dev"
42
+ source: hosted
43
+ version: "1.19.1"
44
+ fake_async:
45
+ dependency: transitive
46
+ description:
47
+ name: fake_async
48
+ sha256: "5368f224a74523e8d2e7399ea1638b37aecfca824a3cc4dfdf77bf1fa905ac44"
49
+ url: "https://pub.dev"
50
+ source: hosted
51
+ version: "1.3.3"
52
+ ffi:
53
+ dependency: transitive
54
+ description:
55
+ name: ffi
56
+ sha256: "6d7fd89431262d8f3125e81b50d3847a091d846eafcd4fdb88dd06f36d705a45"
57
+ url: "https://pub.dev"
58
+ source: hosted
59
+ version: "2.2.0"
60
+ file:
61
+ dependency: transitive
62
+ description:
63
+ name: file
64
+ sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4
65
+ url: "https://pub.dev"
66
+ source: hosted
67
+ version: "7.0.1"
68
+ flutter:
69
+ dependency: "direct main"
70
+ description: flutter
71
+ source: sdk
72
+ version: "0.0.0"
73
+ flutter_driver:
74
+ dependency: transitive
75
+ description: flutter
76
+ source: sdk
77
+ version: "0.0.0"
78
+ flutter_lints:
79
+ dependency: "direct dev"
80
+ description:
81
+ name: flutter_lints
82
+ sha256: "3105dc8492f6183fb076ccf1f351ac3d60564bff92e20bfc4af9cc1651f4e7e1"
83
+ url: "https://pub.dev"
84
+ source: hosted
85
+ version: "6.0.0"
86
+ flutter_localizations:
87
+ dependency: "direct main"
88
+ description: flutter
89
+ source: sdk
90
+ version: "0.0.0"
91
+ flutter_test:
92
+ dependency: "direct dev"
93
+ description: flutter
94
+ source: sdk
95
+ version: "0.0.0"
96
+ flutter_web_plugins:
97
+ dependency: transitive
98
+ description: flutter
99
+ source: sdk
100
+ version: "0.0.0"
101
+ fuchsia_remote_debug_protocol:
102
+ dependency: transitive
103
+ description: flutter
104
+ source: sdk
105
+ version: "0.0.0"
106
+ http:
107
+ dependency: "direct main"
108
+ description:
109
+ name: http
110
+ sha256: "87721a4a50b19c7f1d49001e51409bddc46303966ce89a65af4f4e6004896412"
111
+ url: "https://pub.dev"
112
+ source: hosted
113
+ version: "1.6.0"
114
+ http_parser:
115
+ dependency: transitive
116
+ description:
117
+ name: http_parser
118
+ sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571"
119
+ url: "https://pub.dev"
120
+ source: hosted
121
+ version: "4.1.2"
122
+ integration_test:
123
+ dependency: "direct dev"
124
+ description: flutter
125
+ source: sdk
126
+ version: "0.0.0"
127
+ intl:
128
+ dependency: "direct main"
129
+ description:
130
+ name: intl
131
+ sha256: "1ca20c894b1717686a2319b8548763d812bc0aabdac580420a44c5178c57a867"
132
+ url: "https://pub.dev"
133
+ source: hosted
134
+ version: "0.20.3"
135
+ leak_tracker:
136
+ dependency: transitive
137
+ description:
138
+ name: leak_tracker
139
+ sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de"
140
+ url: "https://pub.dev"
141
+ source: hosted
142
+ version: "11.0.2"
143
+ leak_tracker_flutter_testing:
144
+ dependency: transitive
145
+ description:
146
+ name: leak_tracker_flutter_testing
147
+ sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1"
148
+ url: "https://pub.dev"
149
+ source: hosted
150
+ version: "3.0.10"
151
+ leak_tracker_testing:
152
+ dependency: transitive
153
+ description:
154
+ name: leak_tracker_testing
155
+ sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1"
156
+ url: "https://pub.dev"
157
+ source: hosted
158
+ version: "3.0.2"
159
+ lints:
160
+ dependency: transitive
161
+ description:
162
+ name: lints
163
+ sha256: "12f842a479589fea194fe5c5a3095abc7be0c1f2ddfa9a0e76aed1dbd26a87df"
164
+ url: "https://pub.dev"
165
+ source: hosted
166
+ version: "6.1.0"
167
+ matcher:
168
+ dependency: transitive
169
+ description:
170
+ name: matcher
171
+ sha256: "31bd099b47c10cd1aeb55146a2d46ce0277630ecef3f7dae54ad7873f36696cd"
172
+ url: "https://pub.dev"
173
+ source: hosted
174
+ version: "0.12.20"
175
+ material_color_utilities:
176
+ dependency: transitive
177
+ description:
178
+ name: material_color_utilities
179
+ sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b"
180
+ url: "https://pub.dev"
181
+ source: hosted
182
+ version: "0.13.0"
183
+ meta:
184
+ dependency: transitive
185
+ description:
186
+ name: meta
187
+ sha256: "307249ce4ff29d58a18e97f6345f539382eb9c9c29ecda628900f31de0443dd9"
188
+ url: "https://pub.dev"
189
+ source: hosted
190
+ version: "1.19.0"
191
+ path:
192
+ dependency: transitive
193
+ description:
194
+ name: path
195
+ sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
196
+ url: "https://pub.dev"
197
+ source: hosted
198
+ version: "1.9.1"
199
+ path_provider_linux:
200
+ dependency: transitive
201
+ description:
202
+ name: path_provider_linux
203
+ sha256: "58c2005f147315b11e9b4a7bc889cd5203e250cba8e3f012dae259b4972b5c16"
204
+ url: "https://pub.dev"
205
+ source: hosted
206
+ version: "2.2.2"
207
+ path_provider_platform_interface:
208
+ dependency: transitive
209
+ description:
210
+ name: path_provider_platform_interface
211
+ sha256: "484838772624c3a4b94f1e44a3e19897fee738f2d5c4ce448443b0417f7c9dda"
212
+ url: "https://pub.dev"
213
+ source: hosted
214
+ version: "2.1.3"
215
+ path_provider_windows:
216
+ dependency: transitive
217
+ description:
218
+ name: path_provider_windows
219
+ sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7
220
+ url: "https://pub.dev"
221
+ source: hosted
222
+ version: "2.3.0"
223
+ platform:
224
+ dependency: transitive
225
+ description:
226
+ name: platform
227
+ sha256: a36d119c13416516a7b5913fbe8af8531e11633d784c550b2125f76c758524ec
228
+ url: "https://pub.dev"
229
+ source: hosted
230
+ version: "3.2.0"
231
+ plugin_platform_interface:
232
+ dependency: transitive
233
+ description:
234
+ name: plugin_platform_interface
235
+ sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
236
+ url: "https://pub.dev"
237
+ source: hosted
238
+ version: "2.1.8"
239
+ process:
240
+ dependency: transitive
241
+ description:
242
+ name: process
243
+ sha256: "4242ba3508d37e01808bdf71ad1d5bb93a8d671bf2e7450e6b1b353fb0808891"
244
+ url: "https://pub.dev"
245
+ source: hosted
246
+ version: "5.0.6"
247
+ shared_preferences:
248
+ dependency: "direct main"
249
+ description:
250
+ name: shared_preferences
251
+ sha256: "1e85e0dccf5b4a016077fea7dfbb4a7cb39c759ac1596fcf60c06deadb7da91f"
252
+ url: "https://pub.dev"
253
+ source: hosted
254
+ version: "2.5.6"
255
+ shared_preferences_android:
256
+ dependency: transitive
257
+ description:
258
+ name: shared_preferences_android
259
+ sha256: "1e12aafe408aa50da80edfd679a2a6bf63ba7ab37c7fa98286da459a757b3399"
260
+ url: "https://pub.dev"
261
+ source: hosted
262
+ version: "2.4.28"
263
+ shared_preferences_foundation:
264
+ dependency: transitive
265
+ description:
266
+ name: shared_preferences_foundation
267
+ sha256: "2ec3934efa51e46117f23031cc141b8fc878e8525b94ec1ea4f7f586cf1b47ea"
268
+ url: "https://pub.dev"
269
+ source: hosted
270
+ version: "2.5.7"
271
+ shared_preferences_linux:
272
+ dependency: transitive
273
+ description:
274
+ name: shared_preferences_linux
275
+ sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f"
276
+ url: "https://pub.dev"
277
+ source: hosted
278
+ version: "2.4.1"
279
+ shared_preferences_platform_interface:
280
+ dependency: "direct dev"
281
+ description:
282
+ name: shared_preferences_platform_interface
283
+ sha256: "649dc798a33931919ea356c4305c2d1f81619ea6e92244070b520187b5140ef9"
284
+ url: "https://pub.dev"
285
+ source: hosted
286
+ version: "2.4.2"
287
+ shared_preferences_web:
288
+ dependency: transitive
289
+ description:
290
+ name: shared_preferences_web
291
+ sha256: c49bd060261c9a3f0ff445892695d6212ff603ef3115edbb448509d407600019
292
+ url: "https://pub.dev"
293
+ source: hosted
294
+ version: "2.4.3"
295
+ shared_preferences_windows:
296
+ dependency: transitive
297
+ description:
298
+ name: shared_preferences_windows
299
+ sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1"
300
+ url: "https://pub.dev"
301
+ source: hosted
302
+ version: "2.4.1"
303
+ sky_engine:
304
+ dependency: transitive
305
+ description: flutter
306
+ source: sdk
307
+ version: "0.0.0"
308
+ source_span:
309
+ dependency: transitive
310
+ description:
311
+ name: source_span
312
+ sha256: "56a02f1f4cd1a2d96303c0144c93bd6d909eea6bee6bf5a0e0b685edbd4c47ab"
313
+ url: "https://pub.dev"
314
+ source: hosted
315
+ version: "1.10.2"
316
+ stack_trace:
317
+ dependency: transitive
318
+ description:
319
+ name: stack_trace
320
+ sha256: "277654b3034d17ac6f9f1cb5595db011b1d5d41e8806866db28e0abaa101c490"
321
+ url: "https://pub.dev"
322
+ source: hosted
323
+ version: "1.12.2"
324
+ stream_channel:
325
+ dependency: transitive
326
+ description:
327
+ name: stream_channel
328
+ sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d"
329
+ url: "https://pub.dev"
330
+ source: hosted
331
+ version: "2.1.4"
332
+ string_scanner:
333
+ dependency: transitive
334
+ description:
335
+ name: string_scanner
336
+ sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43"
337
+ url: "https://pub.dev"
338
+ source: hosted
339
+ version: "1.4.1"
340
+ sync_http:
341
+ dependency: transitive
342
+ description:
343
+ name: sync_http
344
+ sha256: "7f0cd72eca000d2e026bcd6f990b81d0ca06022ef4e32fb257b30d3d1014a961"
345
+ url: "https://pub.dev"
346
+ source: hosted
347
+ version: "0.3.1"
348
+ term_glyph:
349
+ dependency: transitive
350
+ description:
351
+ name: term_glyph
352
+ sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e"
353
+ url: "https://pub.dev"
354
+ source: hosted
355
+ version: "1.2.2"
356
+ test_api:
357
+ dependency: transitive
358
+ description:
359
+ name: test_api
360
+ sha256: "2a122cbe059f8b610d3a5415f42e255b6c17b1f21eee1d960f31080237fb4f11"
361
+ url: "https://pub.dev"
362
+ source: hosted
363
+ version: "0.7.12"
364
+ typed_data:
365
+ dependency: transitive
366
+ description:
367
+ name: typed_data
368
+ sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
369
+ url: "https://pub.dev"
370
+ source: hosted
371
+ version: "1.4.0"
372
+ vector_math:
373
+ dependency: transitive
374
+ description:
375
+ name: vector_math
376
+ sha256: "92b9910f66ed1057fd4da7b040ae7c74cafacf885bdc81be496928d5049b032d"
377
+ url: "https://pub.dev"
378
+ source: hosted
379
+ version: "2.4.3"
380
+ vm_service:
381
+ dependency: transitive
382
+ description:
383
+ name: vm_service
384
+ sha256: "5f37239c4851efcef929cea7824e76df7f2f0970aef85d66bbc430afa40e72f0"
385
+ url: "https://pub.dev"
386
+ source: hosted
387
+ version: "15.3.0"
388
+ web:
389
+ dependency: transitive
390
+ description:
391
+ name: web
392
+ sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
393
+ url: "https://pub.dev"
394
+ source: hosted
395
+ version: "1.1.1"
396
+ webdriver:
397
+ dependency: transitive
398
+ description:
399
+ name: webdriver
400
+ sha256: "28b82ec894fed45dd71c23ba62d1af973ed97dd59a4f5790a4d38b0b13e5657e"
401
+ url: "https://pub.dev"
402
+ source: hosted
403
+ version: "3.2.0"
404
+ xdg_directories:
405
+ dependency: transitive
406
+ description:
407
+ name: xdg_directories
408
+ sha256: "7a3f37b05d989967cdddcbb571f1ea834867ae2faa29725fd085180e0883aa15"
409
+ url: "https://pub.dev"
410
+ source: hosted
411
+ version: "1.1.0"
412
+sdks:
413
+ dart: ">=3.12.0 <4.0.0"
414
+ flutter: ">=3.44.0"
apps/lifemate/pubspec.yaml
+3
@@ -13,8 +13,11 @@ dependencies:
13
http: ^1.5.0
14
shared_preferences: ^2.5.3
15
dev_dependencies:
16
+ integration_test:
17
+ sdk: flutter
18
flutter_test:
19
sdk: flutter
20
flutter_lints: ^6.0.0
21
+ shared_preferences_platform_interface: ^2.4.2
22
flutter:
23
uses-material-design: true
apps/lifemate/test/local_persistence_test.dart
new
+136
@@ -0,0 +1,136 @@
1
+import 'package:flutter_test/flutter_test.dart';
2
+import 'package:lifemate/local_only_api.dart';
3
+import 'package:lifemate/api.dart';
4
+import 'package:lifemate/local_data_store.dart';
5
+import 'package:shared_preferences/shared_preferences.dart';
6
+import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
7
+
8
+class FailedWrites extends InMemorySharedPreferencesStore {
9
+ FailedWrites(Map<String, Object> data) : super.withData(data);
10
+
11
+ @override
12
+ Future<bool> setValue(String valueType, String key, Object value) async =>
13
+ false;
14
+}
15
+
16
+void main() {
17
+ setUp(() => SharedPreferences.setMockInitialValues({}));
18
+
19
+ LocalOnlyApi open() =>
20
+ LocalOnlyApi(displayName: 'آرام', category: 'girl_minor');
21
+
22
+ test('assignment survives recreation, update and safe archive', () async {
23
+ final created = await open().createPlanItem({
24
+ 'kind': 'assignment',
25
+ 'title': 'تمرین ریاضی',
26
+ 'dueAt': DateTime.now().toUtc().toIso8601String(),
27
+ 'visibility': 'private',
28
+ });
29
+ final reopened = open();
30
+ final records = await reopened.listPlanItems();
31
+ expect(records, hasLength(1));
32
+ expect(records.single['id'], created['id']);
33
+ expect(records.single['title'], 'تمرین ریاضی');
34
+ await reopened
35
+ .updatePlanItem(created['id'].toString(), {'title': 'تمرین علوم'});
36
+ expect((await open().listPlanItems()).single['title'], 'تمرین علوم');
37
+ await open()
38
+ .updatePlanItem(created['id'].toString(), {'status': 'cancelled'});
39
+ expect(await open().listPlanItems(), isEmpty);
40
+ });
41
+
42
+ test('edited profile is restored by a new API', () async {
43
+ await open()
44
+ .updateProfile(displayName: 'سارا', themePreference: 'boy_blue');
45
+ final restored = await open().getProfile();
46
+ expect(restored['display_name'], 'سارا');
47
+ expect(restored['theme_preference'], 'boy_blue');
48
+ });
49
+
50
+ test('concurrent writers retain every item with unique identifiers',
51
+ () async {
52
+ final records = await Future.wait(List.generate(
53
+ 20,
54
+ (i) => open().createPlanItem({
55
+ 'kind': 'task',
56
+ 'title': 'کار $i',
57
+ })));
58
+ expect(records.map((item) => item['id']).toSet(), hasLength(20));
59
+ expect(await open().listPlanItems(), hasLength(20));
60
+ });
61
+
62
+ test('invalid changes do not replace persisted records', () async {
63
+ final item =
64
+ await open().createPlanItem({'kind': 'assignment', 'title': 'علوم'});
65
+ for (final change in <Map<String, dynamic>>[
66
+ {'title': ' '},
67
+ {'status': 'wrong'},
68
+ {'visibility': 'family'},
69
+ {'dueAt': 'not-a-date'},
70
+ {'id': 'spoofed'},
71
+ ]) {
72
+ await expectLater(open().updatePlanItem(item['id'] as String, change),
73
+ throwsA(isA<ApiException>()));
74
+ }
75
+ expect((await open().listPlanItems()).single['title'], 'علوم');
76
+ await expectLater(open().updatePlanItem('missing', {'title': 'x'}),
77
+ throwsA(isA<ApiException>()));
78
+ await expectLater(
79
+ open().updateProfile(displayName: ' '), throwsA(isA<ApiException>()));
80
+ });
81
+
82
+ test('date window includes due boundary and excludes upper boundary',
83
+ () async {
84
+ final from = DateTime.utc(2026, 10, 8);
85
+ final to = from.add(const Duration(days: 1));
86
+ await open().createPlanItem({
87
+ 'kind': 'assignment',
88
+ 'title': 'داخل',
89
+ 'dueAt': from.toIso8601String()
90
+ });
91
+ await open().createPlanItem(
92
+ {'kind': 'task', 'title': 'بعد', 'dueAt': to.toIso8601String()});
93
+ final result = await open().getToday(from: from, to: to);
94
+ expect(result.map((item) => item['title']), ['داخل']);
95
+ await open()
96
+ .updatePlanItem(result.single['id'] as String, {'status': 'completed'});
97
+ expect(await open().getToday(from: from, to: to), isEmpty);
98
+ });
99
+
100
+ test('corrupt data raises error and is never overwritten', () async {
101
+ const bad = '{invalid';
102
+ SharedPreferences.setMockInitialValues({LocalDataStore.key: bad});
103
+ await expectLater(
104
+ open().listPlanItems(),
105
+ throwsA(isA<ApiException>()
106
+ .having((e) => e.code, 'code', 'local_data_corrupt')));
107
+ await expectLater(open().createPlanItem({'kind': 'task', 'title': 'new'}),
108
+ throwsA(isA<ApiException>()));
109
+ expect(
110
+ (await SharedPreferences.getInstance()).getString(LocalDataStore.key),
111
+ bad);
112
+ });
113
+
114
+ test('failed write reports error and preserves last durable version',
115
+ () async {
116
+ final item = await open().createPlanItem({'kind': 'task', 'title': 'اول'});
117
+ final raw =
118
+ (await SharedPreferences.getInstance()).getString(LocalDataStore.key)!;
119
+ SharedPreferencesStorePlatform.instance =
120
+ FailedWrites({'flutter.${LocalDataStore.key}': raw});
121
+ await expectLater(
122
+ open().updatePlanItem(item['id'] as String, {'title': 'دوم'}),
123
+ throwsA(isA<ApiException>()
124
+ .having((e) => e.code, 'code', 'local_storage_failed')));
125
+ expect((await open().listPlanItems()).single['title'], 'اول');
126
+ });
127
+
128
+ test('unimplemented server features cannot fabricate success', () async {
129
+ expect(open().accessToken, isNull);
130
+ expect(
131
+ () => open().createAiSession('study'),
132
+ throwsA(isA<ApiException>()
133
+ .having((e) => e.code, 'code', 'local_feature_unavailable')));
134
+ expect(() => open().createFamily('خانواده'), throwsA(isA<ApiException>()));
135
+ });
136
+}
docs/superpowers/plans/2026-10-08-local-persistence-repair.md
new
+58
@@ -0,0 +1,58 @@
1
+# برنامه اصلاح ماندگاری داده محلی آرام
2
+
3
+> روش اجرا: اجرای مستقیم در همین نشست و بازبینی مستقل؛ دستور مالک برای آغاز توسعه و تصمیمهای برگشتپذیر مبنای اجراست.
4
+
5
+**هدف:** مسیر واقعی ایجاد تکلیف → اجرای مجدد → خواندن → ویرایش → خواندن → بایگانی بدون پاسخ ساختگی.
6
+
7
+**معماری:** حالت بدون Login فقط روی دستگاه ذخیره میکند و به سرور/خانواده واقعی متصل نیست. یک سند JSON نسخهدار در SharedPreferences با ذخیره سریالی و بررسی موفقیت نوشتن جایگزین پاسخهای نمایشی برنامه میشود. API و مجوزهای Production دستنخورده میمانند؛ حذف در این بسته بایگانی با `cancelled` است.
8
+
9
+**فناوری:** Flutter/Dart، shared_preferences، Flutter tests؛ CI خصوصی برای APK.
10
+
11
+**مبنای نیاز:** سند انتقال مالک و `2026-10-07-personalized-profile-guidance-design.md`؛ اولویت سلامت/AI برنامه قبلی تا تثبیت داده تعویق میافتد.
12
+
13
+## محدودیتها و معیارهای پذیرش
14
+
15
+- دسته پروفایل `girl_minor | boy_minor | adult` از نقش خانواده مستقل است.
16
+- حالت محلی: ذخیره روی دستگاه؛ همگامسازی/پشتیبان سرور ندارد؛ حذف برنامه میتواند داده را حذف کند.
17
+- نتیجه موفق فقط پس از تأیید نوشتن؛ خرابی سند نباید به لیست خالی یا بازنویسی خودکار تبدیل شود.
18
+- نام، تم و شناسه پروفایل پایدار؛ بزرگسال بدون خانواده هم فضای بزرگسال دارد.
19
+- خانواده، آموزش ساختاریافته، check-in و AI در حالت محلی این بسته غیرفعال؛ هیچ موفقیت ساختگی.
20
+
21
+## تمرکز بازبینی
22
+
23
+- نوشتن ناموفق و JSON خراب: خطای روشن، حفظ نسخه قبلی.
24
+- چند ثبت همزمان: بدون گمشدن رکورد؛ شناسههای متفاوت.
25
+- زمان و نوع نامعتبر، نام خالی: رد بدون نوشتن.
26
+- قطع اینترنت: عملیات محلی بدون شبکه؛ خطای محلی به صف sync سرور منتقل نشود.
27
+- بایگانی و بازگشایی: رکورد دوباره در امروز/برنامه ظاهر نشود.
28
+
29
+### ۱. ذخیره و API محلی — مسئول اصلی: هماهنگکننده
30
+
31
+فایلها: `lib/local_data_store.dart` و `lib/local_only_api.dart` جدید؛ `lib/role_entry_main.dart` اصلاح.
32
+
33
+رابط: `LocalOnlyApi implements IdentityApi`؛ نگاشت ورودی camelCase به رکورد snake_case مطابق Backend؛ `createPlanItem/updatePlanItem/listPlanItems/getToday/getProfile/updateProfile`.
34
+
35
+- [ ] آزمون فعلی LocalRoleTestApi: ثبت تکلیف و بازیابی از نمونه جدید باید شکست بخورد.
36
+- [ ] پیادهسازی سند مستقل `lifeguide.local_data.v1`، مهاجرت غیرمخرب نام/دسته قبلی، نوشتن سریالی با SharedPreferences با reload و بررسی نتیجه نوشتن.
37
+- [ ] آزمون ایجاد/بازسازی API/خواندن/ویرایش/بازسازی/بایگانی؛ اعتبارسنجی، همزمانی، نوشتن ناموفق و سند خراب.
38
+- [ ] اجرای آزمونهای متمرکز و commit کوچک.
39
+
40
+### ۲. رابط عملیاتی برنامه و پروفایل — مسئول اصلی: عامل رابط
41
+
42
+فایلها: `main.dart` و `phase3_ui.dart`؛ آزمون `local_persistence_ui_test.dart`.
43
+
44
+رابط مصرفی: همان IdentityApi؛ `HomeShell(localOnly: true, profileCategory: category)`؛ غیرفعالسازی کش/صف Production در صفحات محلی.
45
+
46
+- [ ] آزمون RED رابط ثبت و بازیابی تکلیف، ویرایش عنوان، بایگانی، خطای ذخیره.
47
+- [ ] فرم دارای اعتبارسنجی، وضعیت درحال ذخیره و خطا؛ جلوگیری از ثبت تکراری؛ ویرایش و بایگانی با تأیید.
48
+- [ ] نمایش محل داده و عدم Sync در حالت محلی؛ صفحات ناموجود غیرفعال با توضیح فارسی.
49
+- [ ] آزمون مسیریابی دستهها و ماندگاری نام/تم؛ commit کوچک.
50
+
51
+### ۳. ممیزی و تحویل — مسئول اصلی: هماهنگکننده؛ بازبینی: عامل مستقل
52
+
53
+- [ ] جدول چهار وضعیت قابلیتها همراه محل داده؛ اصلاح ادعاهای قدیمی PROJECT_STATE.md با حفظ تاریخچه.
54
+- [ ] اجرای واقعی full Flutter tests/analyze و Node/SQL مطابق امکانات محیط؛ ثبت موانع دقیق.
55
+- [ ] CI ساخت APK محلی فقط بهصورت Artifact خصوصی؛ بدون انتشار Release یا تغییر سرویس.
56
+- [ ] بازبینی مستقل امنیت/داده، اصلاح یافتهها و گزارش SHA/شاخه/آزمون/Artifact.
57
+
58
+آزمونهای SharedPreferences در unit/widget از پلاگین شبیهسازیشده استفاده میکنند؛ بازسازی API معادل اثبات force-stop روی Android نیست. شواهد دستگاه جداگانه ثبت شود و محدودیت آن پنهان نشود.