wip(app): live New marks from pushed chat frames (run 151, tests and lint pending)
This commit is contained in:
@@ -7,18 +7,38 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|||||||
///
|
///
|
||||||
/// Titles, stars and the closed state are server-owned and deliberately absent.
|
/// Titles, stars and the closed state are server-owned and deliberately absent.
|
||||||
class ChatSessionLocal {
|
class ChatSessionLocal {
|
||||||
const ChatSessionLocal({this.hidden = const [], this.seenAt = const {}});
|
const ChatSessionLocal({
|
||||||
|
this.hidden = const [],
|
||||||
|
this.seenAt = const {},
|
||||||
|
this.unseen = const [],
|
||||||
|
});
|
||||||
|
|
||||||
final List<String> hidden;
|
final List<String> hidden;
|
||||||
final Map<String, int> seenAt;
|
final Map<String, int> seenAt;
|
||||||
|
final List<String> unseen;
|
||||||
|
|
||||||
bool isHidden(String id) => hidden.contains(id);
|
bool isHidden(String id) => hidden.contains(id);
|
||||||
|
|
||||||
int watermark(String id) => seenAt[id] ?? 0;
|
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) {
|
ChatSessionLocal hide(String id) {
|
||||||
if (id.isEmpty || hidden.contains(id)) return this;
|
if (id.isEmpty || hidden.contains(id)) return this;
|
||||||
return ChatSessionLocal(hidden: [...hidden, id], seenAt: seenAt);
|
return ChatSessionLocal(
|
||||||
|
hidden: [...hidden, id],
|
||||||
|
seenAt: seenAt,
|
||||||
|
unseen: unseen,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatSessionLocal restore(String id) {
|
ChatSessionLocal restore(String id) {
|
||||||
@@ -26,14 +46,22 @@ class ChatSessionLocal {
|
|||||||
return ChatSessionLocal(
|
return ChatSessionLocal(
|
||||||
hidden: hidden.where((entry) => entry != id).toList(),
|
hidden: hidden.where((entry) => entry != id).toList(),
|
||||||
seenAt: seenAt,
|
seenAt: seenAt,
|
||||||
|
unseen: unseen,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Advances only for a strictly newer recency, so a stale list read can never
|
/// Advances only for a strictly newer recency, so a stale list read can never
|
||||||
/// mark newer activity as already seen.
|
/// mark newer activity as already seen.
|
||||||
ChatSessionLocal read(String id, int updatedAt) {
|
ChatSessionLocal read(String id, int updatedAt) {
|
||||||
if (id.isEmpty || updatedAt <= watermark(id)) return this;
|
final nextUnseen = unseen.where((entry) => entry != id).toList();
|
||||||
return ChatSessionLocal(hidden: hidden, seenAt: {...seenAt, id: updatedAt});
|
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
|
/// The first sight of a session baselines it, so pre-existing chats are not
|
||||||
@@ -46,12 +74,14 @@ class ChatSessionLocal {
|
|||||||
next = ChatSessionLocal(
|
next = ChatSessionLocal(
|
||||||
hidden: next.hidden,
|
hidden: next.hidden,
|
||||||
seenAt: {...next.seenAt, row.id: row.updatedAt},
|
seenAt: {...next.seenAt, row.id: row.updatedAt},
|
||||||
|
unseen: next.unseen,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
String encode() => jsonEncode({'hidden': hidden, 'seenAt': seenAt});
|
String encode() =>
|
||||||
|
jsonEncode({'hidden': hidden, 'seenAt': seenAt, 'unseen': unseen});
|
||||||
|
|
||||||
static ChatSessionLocal decode(Object? value) {
|
static ChatSessionLocal decode(Object? value) {
|
||||||
try {
|
try {
|
||||||
@@ -59,6 +89,7 @@ class ChatSessionLocal {
|
|||||||
if (parsed is! Map) return const ChatSessionLocal();
|
if (parsed is! Map) return const ChatSessionLocal();
|
||||||
final hidden = parsed['hidden'];
|
final hidden = parsed['hidden'];
|
||||||
final seenAt = parsed['seenAt'];
|
final seenAt = parsed['seenAt'];
|
||||||
|
final unseen = parsed['unseen'];
|
||||||
return ChatSessionLocal(
|
return ChatSessionLocal(
|
||||||
hidden: hidden is List
|
hidden: hidden is List
|
||||||
? hidden.whereType<String>().where((id) => id.isNotEmpty).toList()
|
? hidden.whereType<String>().where((id) => id.isNotEmpty).toList()
|
||||||
@@ -70,6 +101,13 @@ class ChatSessionLocal {
|
|||||||
'${entry.key}': entry.value as int,
|
'${entry.key}': entry.value as int,
|
||||||
}
|
}
|
||||||
: const {},
|
: const {},
|
||||||
|
unseen: unseen is List
|
||||||
|
? unseen
|
||||||
|
.whereType<String>()
|
||||||
|
.where((id) => id.trim().isNotEmpty)
|
||||||
|
.toSet()
|
||||||
|
.toList()
|
||||||
|
: const [],
|
||||||
);
|
);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return const ChatSessionLocal();
|
return const ChatSessionLocal();
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ import 'chat_session_local.dart';
|
|||||||
class ChatSessionsController extends ChangeNotifier {
|
class ChatSessionsController extends ChangeNotifier {
|
||||||
ChatSessionsController(this.repository, {ChatSessionLocalStore? localStore})
|
ChatSessionsController(this.repository, {ChatSessionLocalStore? localStore})
|
||||||
: _localStore =
|
: _localStore =
|
||||||
localStore ?? const SharedPreferencesChatSessionLocalStore();
|
localStore ?? const SharedPreferencesChatSessionLocalStore() {
|
||||||
|
_listenFrames();
|
||||||
|
}
|
||||||
|
|
||||||
ChatRepository repository;
|
ChatRepository repository;
|
||||||
final ChatSessionLocalStore _localStore;
|
final ChatSessionLocalStore _localStore;
|
||||||
@@ -31,6 +33,42 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
Future<void>? _localLoading;
|
Future<void>? _localLoading;
|
||||||
bool _disposed = false;
|
bool _disposed = false;
|
||||||
int _generation = 0;
|
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 => [
|
List<JsonMap> get sessions => [
|
||||||
for (final session in _sessions)
|
for (final session in _sessions)
|
||||||
@@ -48,6 +86,7 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
|
|
||||||
bool isUnseen(JsonMap session) {
|
bool isUnseen(JsonMap session) {
|
||||||
final id = '${session['id'] ?? ''}';
|
final id = '${session['id'] ?? ''}';
|
||||||
|
if (id == activeSessionId) return false;
|
||||||
// Closed chats keep their recency from the close, but they are not new
|
// 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.
|
// work: both the web list and the Android list suppress unseen there.
|
||||||
if (!_localLoaded ||
|
if (!_localLoaded ||
|
||||||
@@ -57,6 +96,7 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
session['closed'] == true) {
|
session['closed'] == true) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (_local.isUnseen(id)) return true;
|
||||||
final updatedAt = chatSessionMillis(session['updated_at']);
|
final updatedAt = chatSessionMillis(session['updated_at']);
|
||||||
return updatedAt > 0 && updatedAt > _local.watermark(id);
|
return updatedAt > 0 && updatedAt > _local.watermark(id);
|
||||||
}
|
}
|
||||||
@@ -64,6 +104,7 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
void replaceRepository(ChatRepository next) {
|
void replaceRepository(ChatRepository next) {
|
||||||
if (identical(next.gateway, repository.gateway)) return;
|
if (identical(next.gateway, repository.gateway)) return;
|
||||||
repository = next;
|
repository = next;
|
||||||
|
_listenFrames();
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +186,7 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
Future<void> markOpened(JsonMap session) async {
|
Future<void> markOpened(JsonMap session) async {
|
||||||
final id = '${session['id'] ?? ''}';
|
final id = '${session['id'] ?? ''}';
|
||||||
if (id.isEmpty) return;
|
if (id.isEmpty) return;
|
||||||
|
activeSessionId = id;
|
||||||
await _ensureLocal();
|
await _ensureLocal();
|
||||||
final next = _local.read(id, chatSessionMillis(session['updated_at']));
|
final next = _local.read(id, chatSessionMillis(session['updated_at']));
|
||||||
if (identical(next, _local)) return;
|
if (identical(next, _local)) return;
|
||||||
@@ -236,6 +278,7 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
void dispose() {
|
void dispose() {
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
_generation++;
|
_generation++;
|
||||||
|
unawaited(_frameSubscription?.cancel());
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user