@davoodmehraban89 / LifeMate / commits / 3a4e30b
Fork 0

LifeMate — Personal, Family & Learning Companion

feat: add offline cache and mutation queue

davoodmehraban89 committed Oct 5, 2026 at 15:07 UTC 3a4e30b615eb4614eecbe73c041b3553e4c7662d
1 file changed +60
apps/lifemate/lib/offline_store.dart new
+60
@@ -0,0 +1,60 @@
1 +import 'dart:convert';
2 +
3 +import 'package:shared_preferences/shared_preferences.dart';
4 +
5 +class OfflineStore {
6 + OfflineStore._();
7 + static final OfflineStore instance = OfflineStore._();
8 +
9 + static const _todayKey = 'phase3.today.cache';
10 + static const _plannerKey = 'phase3.planner.cache';
11 + static const _schoolKey = 'phase3.school.cache';
12 + static const _queueKey = 'phase3.sync.queue';
13 +
14 + Future<void> writeList(String key, List<Map<String, dynamic>> value) async {
15 + final prefs = await SharedPreferences.getInstance();
16 + await prefs.setString(key, jsonEncode(value));
17 + }
18 +
19 + Future<List<Map<String, dynamic>>> readList(String key) async {
20 + final prefs = await SharedPreferences.getInstance();
21 + final raw = prefs.getString(key);
22 + if (raw == null || raw.isEmpty) return [];
23 + try {
24 + final decoded = jsonDecode(raw) as List<dynamic>;
25 + return decoded
26 + .map((item) => Map<String, dynamic>.from(item as Map))
27 + .toList();
28 + } catch (_) {
29 + return [];
30 + }
31 + }
32 +
33 + Future<void> cacheToday(List<Map<String, dynamic>> items) =>
34 + writeList(_todayKey, items);
35 + Future<List<Map<String, dynamic>>> readToday() => readList(_todayKey);
36 +
37 + Future<void> cachePlanner(List<Map<String, dynamic>> items) =>
38 + writeList(_plannerKey, items);
39 + Future<List<Map<String, dynamic>>> readPlanner() => readList(_plannerKey);
40 +
41 + Future<void> cacheSchool(List<Map<String, dynamic>> items) =>
42 + writeList(_schoolKey, items);
43 + Future<List<Map<String, dynamic>>> readSchool() => readList(_schoolKey);
44 +
45 + Future<List<Map<String, dynamic>>> readQueue() => readList(_queueKey);
46 +
47 + Future<void> enqueue(Map<String, dynamic> mutation) async {
48 + final queue = await readQueue();
49 + queue.add(mutation);
50 + await writeList(_queueKey, queue);
51 + }
52 +
53 + Future<void> removeQueued(Set<String> ids) async {
54 + final queue = await readQueue();
55 + await writeList(
56 + _queueKey,
57 + queue.where((item) => !ids.contains(item['id']?.toString())).toList(),
58 + );
59 + }
60 +}