Sync Needs-you dismissals and chat read state across devices
This commit is contained in:
@@ -6,6 +6,7 @@ import '../../../core/api_gateway.dart';
|
||||
import '../../runs/domain/run_events.dart';
|
||||
import '../domain/attention_models.dart';
|
||||
import '../domain/attention_transitions.dart';
|
||||
import 'attention_ack_sync.dart';
|
||||
import 'attention_store.dart';
|
||||
|
||||
/// The live `Needs you` state behind the Runs-list bell.
|
||||
@@ -20,11 +21,22 @@ import 'attention_store.dart';
|
||||
/// [AttentionStore] without blocking the caller. A failed read or write leaves
|
||||
/// the in-memory set in force for this session; neither direction can throw into
|
||||
/// the UI.
|
||||
///
|
||||
/// With a [sync], dismissals are shared with every device: Dismiss and Clear
|
||||
/// all are also acknowledged to the server, and identities dismissed elsewhere
|
||||
/// (the server's set and pushed deltas) are folded in and cached locally, so
|
||||
/// the local store doubles as the offline copy.
|
||||
class AttentionController extends ChangeNotifier {
|
||||
AttentionController({required this.store, DateTime Function()? now})
|
||||
: _now = now ?? DateTime.now;
|
||||
AttentionController({
|
||||
required this.store,
|
||||
this.sync,
|
||||
DateTime Function()? now,
|
||||
}) : _now = now ?? DateTime.now {
|
||||
sync?.addListener(_syncChanged);
|
||||
}
|
||||
|
||||
final AttentionStore store;
|
||||
final AttentionAckSync? sync;
|
||||
final DateTime Function() _now;
|
||||
final AttentionTransitionDetector _detector = AttentionTransitionDetector();
|
||||
final List<AttentionTransition> _transitions = [];
|
||||
@@ -75,6 +87,16 @@ class AttentionController extends ChangeNotifier {
|
||||
// older stored value; the in-memory set is the operator's latest intent.
|
||||
if (!_dirty) _dismissed = stored;
|
||||
notifyListeners();
|
||||
_syncChanged();
|
||||
}
|
||||
|
||||
/// Folds identities acknowledged on any device into this set. A change is
|
||||
/// cached in the local store; nothing is sent back to the server.
|
||||
void _syncChanged() {
|
||||
final shared = sync?.acks.dismissed;
|
||||
if (_disposed || !_loaded || shared == null) return;
|
||||
if (_dismissed.containsAll(shared)) return;
|
||||
unawaited(_commit({..._dismissed, ...shared}));
|
||||
}
|
||||
|
||||
/// Replaces the drawn rows with an authoritative read.
|
||||
@@ -149,12 +171,25 @@ class AttentionController extends ChangeNotifier {
|
||||
/// The in-memory set and the bell update before the store is touched, so a
|
||||
/// slow or failed write never blocks the sheet. The returned future reports
|
||||
/// whether the write succeeded; callers that do not need it may ignore it.
|
||||
Future<bool> dismiss(AttentionEntry entry) =>
|
||||
_commit(attentionDismiss(_dismissed, entry.key));
|
||||
Future<bool> dismiss(AttentionEntry entry) {
|
||||
_share([entry.key]);
|
||||
return _commit(attentionDismiss(_dismissed, entry.key));
|
||||
}
|
||||
|
||||
/// Applies only the identities captured when the sheet opened.
|
||||
Future<bool> clearAll(Iterable<AttentionEntry> snapshot) =>
|
||||
_commit(attentionDismissAll(_dismissed, snapshot));
|
||||
Future<bool> clearAll(Iterable<AttentionEntry> snapshot) {
|
||||
final entries = snapshot.toList();
|
||||
_share([for (final entry in entries) entry.key]);
|
||||
return _commit(attentionDismissAll(_dismissed, entries));
|
||||
}
|
||||
|
||||
/// Acknowledges [keys] to every device. The sync queues them while offline,
|
||||
/// and sending never blocks or fails the local dismissal.
|
||||
void _share(List<String> keys) {
|
||||
final shared = sync;
|
||||
if (_disposed || shared == null || keys.isEmpty) return;
|
||||
unawaited(shared.acknowledge(dismissed: keys));
|
||||
}
|
||||
|
||||
Future<bool> _commit(Set<String> next) async {
|
||||
final unchanged =
|
||||
@@ -174,6 +209,7 @@ class AttentionController extends ChangeNotifier {
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
sync?.removeListener(_syncChanged);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user