fix: implement safe map casting for socket events and add support for conversation deletion updates

This commit is contained in:
pradeepkumar
2026-04-09 16:02:31 +05:30
parent f9c6d5e5da
commit 44278b7eb0
3 changed files with 63 additions and 16 deletions

View File

@@ -240,6 +240,27 @@ class MessagingNotifier extends StateNotifier<MessagingState> {
state = state.copyWith(conversations: updatedConversations, messages: newMessages);
}));
// Conversation deleted by other party — remove from local state
_subscriptions.add(_socket.onConversationDeleted.listen((data) {
if (!mounted) return;
final convId = data['conversationId'] as String?;
if (convId == null) return;
final updatedMessages =
Map<String, List<ChatMessage>>.from(state.messages)..remove(convId);
final updatedConversations =
state.conversations.where((c) => c.id != convId).toList();
final totalUnread = updatedConversations.fold<int>(
0, (sum, c) => sum + c.unreadCount,
);
state = state.copyWith(
messages: updatedMessages,
conversations: updatedConversations,
unreadCount: totalUnread,
clearActiveConversation: state.activeConversationId == convId,
);
}));
// Reconnection: re-join active conversation room and catch up missed messages
_subscriptions.add(_socket.onConnectionChange.listen((connected) {
if (!mounted || !connected) return;