import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; /// Device-local list preferences, mirroring the web client's C13 behaviour: /// hidden ids and the newest session recency this device has read. /// /// Titles, stars and the closed state are server-owned and deliberately absent. class ChatSessionLocal { const ChatSessionLocal({this.hidden = const [], this.seenAt = const {}}); final List hidden; final Map seenAt; bool isHidden(String id) => hidden.contains(id); int watermark(String id) => seenAt[id] ?? 0; ChatSessionLocal hide(String id) { if (id.isEmpty || hidden.contains(id)) return this; return ChatSessionLocal(hidden: [...hidden, id], seenAt: seenAt); } ChatSessionLocal restore(String id) { if (!hidden.contains(id)) return this; return ChatSessionLocal( hidden: hidden.where((entry) => entry != id).toList(), seenAt: seenAt, ); } /// Advances only for a strictly newer recency, so a stale list read can never /// mark newer activity as already seen. ChatSessionLocal read(String id, int updatedAt) { if (id.isEmpty || updatedAt <= watermark(id)) return this; return ChatSessionLocal(hidden: hidden, seenAt: {...seenAt, id: updatedAt}); } /// The first sight of a session baselines it, so pre-existing chats are not /// all reported as new. ChatSessionLocal seed(Iterable<({String id, int updatedAt})> rows) { var next = this; for (final row in rows) { if (row.id.isEmpty || row.updatedAt <= 0) continue; if (next.watermark(row.id) != 0) continue; next = ChatSessionLocal( hidden: next.hidden, seenAt: {...next.seenAt, row.id: row.updatedAt}, ); } return next; } String encode() => jsonEncode({'hidden': hidden, 'seenAt': seenAt}); static ChatSessionLocal decode(Object? value) { try { final parsed = jsonDecode('$value'); if (parsed is! Map) return const ChatSessionLocal(); final hidden = parsed['hidden']; final seenAt = parsed['seenAt']; return ChatSessionLocal( hidden: hidden is List ? hidden.whereType().where((id) => id.isNotEmpty).toList() : const [], seenAt: seenAt is Map ? { for (final entry in seenAt.entries) if (entry.key is String && entry.value is int) '${entry.key}': entry.value as int, } : const {}, ); } catch (_) { return const ChatSessionLocal(); } } } abstract interface class ChatSessionLocalStore { Future load(); Future save(ChatSessionLocal state); } /// `shared_preferences`-backed storage; unavailable storage degrades to the /// in-memory choice instead of failing the list. class SharedPreferencesChatSessionLocalStore implements ChatSessionLocalStore { const SharedPreferencesChatSessionLocalStore(); static const key = 'pipelines.chat.local.v1'; Future _preferences() async { try { return await SharedPreferences.getInstance(); } catch (_) { return null; } } @override Future load() async { final preferences = await _preferences(); try { return ChatSessionLocal.decode(preferences?.getString(key)); } catch (_) { return const ChatSessionLocal(); } } @override Future save(ChatSessionLocal state) async { final preferences = await _preferences(); try { await preferences?.setString(key, state.encode()); } catch (_) { // Storage may be unavailable; the in-memory choice still applies. } } }