markAllAsRead method
Mark all messages as read (optimistic update).
Implementation
Future<void> markAllAsRead() async {
if (!_initialized) return;
// Snapshot for rollback
final originalReadStates =
_state.messages.map((m) => MapEntry(m.id, m.read)).toList();
final originalCount = _state.unreadCount;
// Optimistic update
for (final message in _state.messages) {
message.read = true;
}
_state = _state.copyWith(unreadCount: 0);
notifyListeners();
try {
await _client!.inboxMarkAllAsRead();
await _persistState();
} catch (e) {
// Revert on failure
for (final entry in originalReadStates) {
final msg = _state.messages.firstWhereOrNull(
(m) => m.id == entry.key,
);
if (msg != null) {
msg.read = entry.value;
}
}
_state = _state.copyWith(unreadCount: originalCount);
notifyListeners();
debugPrint('Error marking all messages as read: $e');
}
}