Sync Needs-you dismissals and chat read state across devices
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../../../core/servers/server_scope.dart';
|
||||
import '../../../../core/transport/connection_state.dart';
|
||||
import '../../../attention/runtime/attention_ack_sync.dart';
|
||||
import '../../data/chat_repository.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_session_local.dart';
|
||||
@@ -16,18 +18,40 @@ import 'session_attention.dart';
|
||||
/// discarded, so a slow reply can never replace fresher state. Accepted
|
||||
/// mutations always re-read the server list; only this device's hidden ids and
|
||||
/// read watermark are local.
|
||||
///
|
||||
/// With an [AttentionAckSync] (bound to the gateway by the app, or passed in),
|
||||
/// reading is shared: opening a chat acknowledges a `chat_seen` watermark to
|
||||
/// every device, and a watermark acknowledged elsewhere that is newer than this
|
||||
/// device's own clears the chat's New badge here. The local store remains the
|
||||
/// offline cache of both.
|
||||
class ChatSessionsController extends ChangeNotifier {
|
||||
ChatSessionsController(this.repository, {ChatSessionLocalStore? localStore})
|
||||
: _localStore =
|
||||
localStore ??
|
||||
SharedPreferencesChatSessionLocalStore(
|
||||
serverId: serverStorageScope(serverIdentity(repository.gateway)),
|
||||
) {
|
||||
ChatSessionsController(
|
||||
this.repository, {
|
||||
ChatSessionLocalStore? localStore,
|
||||
AttentionAckSync? ackSync,
|
||||
int Function()? nowMillis,
|
||||
this.activeAckDelay = const Duration(milliseconds: 1500),
|
||||
}) : _localStore =
|
||||
localStore ??
|
||||
SharedPreferencesChatSessionLocalStore(
|
||||
serverId: serverStorageScope(serverIdentity(repository.gateway)),
|
||||
),
|
||||
_nowMillis = nowMillis ?? _wallClockMillis {
|
||||
_listenFrames();
|
||||
_bindAckSync(ackSync ?? attentionAckSyncOf(repository.gateway));
|
||||
}
|
||||
|
||||
static int _wallClockMillis() => DateTime.now().millisecondsSinceEpoch;
|
||||
|
||||
/// How long the open chat stays quiet before its new content is
|
||||
/// acknowledged to the other devices; streaming text arrives in many frames.
|
||||
final Duration activeAckDelay;
|
||||
|
||||
ChatRepository repository;
|
||||
final ChatSessionLocalStore _localStore;
|
||||
final int Function() _nowMillis;
|
||||
AttentionAckSync? _ackSync;
|
||||
Timer? _activeAck;
|
||||
ChatSessionLocal _local = const ChatSessionLocal();
|
||||
final Map<String, String> _actionErrors = {};
|
||||
List<JsonMap> _sessions = const [];
|
||||
@@ -59,6 +83,59 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
if (row['closed_at'] != null || row['closed'] == true) return;
|
||||
if (effect.running != null) _setRunning(id, effect.running!);
|
||||
if (effect.attention && id != activeSessionId) unawaited(_markUnseen(id));
|
||||
if (effect.attention && id == activeSessionId) _scheduleActiveAck(id);
|
||||
}
|
||||
|
||||
void _bindAckSync(AttentionAckSync? sync) {
|
||||
if (identical(sync, _ackSync)) return;
|
||||
_ackSync?.removeListener(_ackChanged);
|
||||
_ackSync = sync;
|
||||
sync?.addListener(_ackChanged);
|
||||
_ackChanged();
|
||||
}
|
||||
|
||||
/// Content arriving in the chat this device has open is read here, so it is
|
||||
/// acknowledged to the other devices once the stream goes quiet.
|
||||
void _scheduleActiveAck(String id) {
|
||||
if (_ackSync == null) return;
|
||||
_activeAck?.cancel();
|
||||
_activeAck = Timer(activeAckDelay, () {
|
||||
if (_disposed || id != activeSessionId) return;
|
||||
unawaited(_acknowledgeSeen(id, 0));
|
||||
});
|
||||
}
|
||||
|
||||
/// Applies watermarks acknowledged on any device: one newer than this
|
||||
/// device's own marks the chat read here (clearing its New badge) and raises
|
||||
/// the local watermark, so a later badge is not cleared by the same ack.
|
||||
void _ackChanged() {
|
||||
final seen = _ackSync?.acks.chatSeen;
|
||||
if (_disposed || !_localLoaded || seen == null) return;
|
||||
var next = _local;
|
||||
seen.forEach((id, at) {
|
||||
if (at > next.watermark(id)) next = next.read(id, at);
|
||||
});
|
||||
if (identical(next, _local)) return;
|
||||
_local = next;
|
||||
_notify();
|
||||
unawaited(_persist());
|
||||
}
|
||||
|
||||
/// Marks [id] read up to now here and acknowledges it to every device. The
|
||||
/// value is at least the row's own recency; the server lifts it further to
|
||||
/// the session's registry recency.
|
||||
Future<void> _acknowledgeSeen(String id, int updatedAt) async {
|
||||
await _ensureLocal();
|
||||
if (_disposed) return;
|
||||
final at = max(updatedAt, _nowMillis());
|
||||
final next = _local.read(id, at);
|
||||
if (!identical(next, _local)) {
|
||||
_local = next;
|
||||
_notify();
|
||||
await _persist();
|
||||
}
|
||||
if (_disposed) return;
|
||||
await _ackSync?.acknowledge(chatSeen: {id: at});
|
||||
}
|
||||
|
||||
void _setRunning(String id, bool running) {
|
||||
@@ -126,6 +203,7 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
repository = next;
|
||||
_runningFrames.clear();
|
||||
_listenFrames();
|
||||
_bindAckSync(attentionAckSyncOf(next.gateway) ?? _ackSync);
|
||||
refresh();
|
||||
}
|
||||
|
||||
@@ -242,7 +320,9 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
activeSessionId = id;
|
||||
await _ensureLocal();
|
||||
if (_disposed) return;
|
||||
final next = _local.read(id, chatSessionMillis(session['updated_at']));
|
||||
final updatedAt = chatSessionMillis(session['updated_at']);
|
||||
if (_ackSync != null) return _acknowledgeSeen(id, updatedAt);
|
||||
final next = _local.read(id, updatedAt);
|
||||
if (identical(next, _local)) return;
|
||||
_local = next;
|
||||
_notify();
|
||||
@@ -313,6 +393,7 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
}
|
||||
_localLoaded = true;
|
||||
_seedLocal();
|
||||
_ackChanged();
|
||||
_notify();
|
||||
}
|
||||
|
||||
@@ -351,6 +432,8 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
_disposed = true;
|
||||
_generation++;
|
||||
unawaited(_frameSubscription?.cancel());
|
||||
_activeAck?.cancel();
|
||||
_ackSync?.removeListener(_ackChanged);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user