wip(app): live New marks from pushed chat frames (run 151, tests and lint pending)

This commit is contained in:
2026-10-22 18:36:47 +01:00
parent 1bcc26a4fd
commit faa5cc003b
2 changed files with 87 additions and 6 deletions
@@ -17,7 +17,9 @@ import 'chat_session_local.dart';
class ChatSessionsController extends ChangeNotifier {
ChatSessionsController(this.repository, {ChatSessionLocalStore? localStore})
: _localStore =
localStore ?? const SharedPreferencesChatSessionLocalStore();
localStore ?? const SharedPreferencesChatSessionLocalStore() {
_listenFrames();
}
ChatRepository repository;
final ChatSessionLocalStore _localStore;
@@ -31,6 +33,42 @@ class ChatSessionsController extends ChangeNotifier {
Future<void>? _localLoading;
bool _disposed = false;
int _generation = 0;
StreamSubscription<JsonMap>? _frameSubscription;
String? activeSessionId;
void _listenFrames() {
_frameSubscription?.cancel();
_frameSubscription = repository.gateway.frames.listen(_onFrame);
}
void _onFrame(JsonMap frame) {
final type = frame['type'];
String? id;
if (type == 'chat_event') {
final event = frame['chat_event'];
if (event is! Map || event['kind'] == 'usage') return;
id = event['session_id'] is String ? event['session_id'] as String : null;
} else if (type == 'chat_permission') {
final permission = frame['chat_permission'];
if (permission is! Map) return;
id = permission['session_id'] is String
? permission['session_id'] as String
: null;
} else {
return;
}
if (id == null || id.isEmpty || id == activeSessionId) return;
unawaited(_markUnseen(id));
}
Future<void> _markUnseen(String id) async {
await _ensureLocal();
final next = _local.markUnseen(id);
if (identical(next, _local)) return;
_local = next;
_notify();
await _persist();
}
List<JsonMap> get sessions => [
for (final session in _sessions)
@@ -48,6 +86,7 @@ class ChatSessionsController extends ChangeNotifier {
bool isUnseen(JsonMap session) {
final id = '${session['id'] ?? ''}';
if (id == activeSessionId) return false;
// Closed chats keep their recency from the close, but they are not new
// work: both the web list and the Android list suppress unseen there.
if (!_localLoaded ||
@@ -57,6 +96,7 @@ class ChatSessionsController extends ChangeNotifier {
session['closed'] == true) {
return false;
}
if (_local.isUnseen(id)) return true;
final updatedAt = chatSessionMillis(session['updated_at']);
return updatedAt > 0 && updatedAt > _local.watermark(id);
}
@@ -64,6 +104,7 @@ class ChatSessionsController extends ChangeNotifier {
void replaceRepository(ChatRepository next) {
if (identical(next.gateway, repository.gateway)) return;
repository = next;
_listenFrames();
refresh();
}
@@ -145,6 +186,7 @@ class ChatSessionsController extends ChangeNotifier {
Future<void> markOpened(JsonMap session) async {
final id = '${session['id'] ?? ''}';
if (id.isEmpty) return;
activeSessionId = id;
await _ensureLocal();
final next = _local.read(id, chatSessionMillis(session['updated_at']));
if (identical(next, _local)) return;
@@ -236,6 +278,7 @@ class ChatSessionsController extends ChangeNotifier {
void dispose() {
_disposed = true;
_generation++;
unawaited(_frameSubscription?.cancel());
super.dispose();
}
}