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 {}, this.unseen = const [], }); final List hidden; final Map seenAt; final List unseen; bool isHidden(String id) => hidden.contains(id); int watermark(String id) => seenAt[id] ?? 0; bool isUnseen(String id) => unseen.contains(id); ChatSessionLocal markUnseen(String id) { if (id.trim().isEmpty || unseen.contains(id)) return this; return ChatSessionLocal( hidden: hidden, seenAt: seenAt, unseen: [...unseen, id], ); } ChatSessionLocal hide(String id) { if (id.isEmpty || hidden.contains(id)) return this; return ChatSessionLocal( hidden: [...hidden, id], seenAt: seenAt, unseen: unseen, ); } ChatSessionLocal restore(String id) { if (!hidden.contains(id)) return this; return ChatSessionLocal( hidden: hidden.where((entry) => entry != id).toList(), seenAt: seenAt, unseen: unseen, ); } /// 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) { final nextUnseen = unseen.where((entry) => entry != id).toList(); if (id.isEmpty || (updatedAt <= watermark(id) && nextUnseen.length == unseen.length)) { return this; } return ChatSessionLocal( hidden: hidden, seenAt: updatedAt > watermark(id) ? {...seenAt, id: updatedAt} : seenAt, unseen: nextUnseen, ); } /// 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}, unseen: next.unseen, ); } return next; } String encode() => jsonEncode({'hidden': hidden, 'seenAt': seenAt, 'unseen': unseen}); 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']; final unseen = parsed['unseen']; 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 {}, unseen: unseen is List ? unseen .whereType() .where((id) => id.trim().isNotEmpty) .toSet() .toList() : 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. } } }