markAsRead method
Mark single message as read (optimistic update).
Implementation
Future<void> markAsRead(String messageId) async {
if (!_initialized) return;
// Find message
final index = _state.messages.indexWhere((m) => m.id == messageId);
if (index == -1) return;
final message = _state.messages[index];
if (message.read) return; // Already read
// Snapshot for rollback
final originalRead = message.read;
final originalCount = _state.unreadCount;
// Optimistic update
message.read = true;
_state = _state.copyWith(
unreadCount: (_state.unreadCount - 1).clamp(0, 999999),
);
notifyListeners();
try {
await _client!.inboxMarkAsRead(messageId);
await _persistState();
} catch (e) {
// Revert on failure
message.read = originalRead;
_state = _state.copyWith(unreadCount: originalCount);
notifyListeners();
debugPrint('Error marking message as read: $e');
}
}