fix: improve chat message deduplication, add conversation refresh logic, and update UI components for consistency

This commit is contained in:
pradeepkumar
2026-04-18 11:34:48 +05:30
parent 164d590d31
commit afa1f3489a
6 changed files with 47 additions and 8 deletions

View File

@@ -585,6 +585,24 @@ class MessagingNotifier extends StateNotifier<MessagingState> {
}
}
/// Refetch a single conversation — used when opening chat to get fresh
/// presence (isOnline/lastSeenAt) since the list may be stale. Mirrors
/// web's selectConversation which calls getConversation on open.
Future<void> refreshConversation(String conversationId) async {
try {
final fresh = await _repository.getConversation(conversationId);
if (!mounted) return;
final updated = state.conversations.map((c) {
return c.id == conversationId
? c.copyWith(otherParty: fresh.otherParty)
: c;
}).toList();
state = state.copyWith(conversations: updated);
} catch (_) {
// Non-fatal — existing cached data remains in place
}
}
Future<void> loadUnreadCount() async {
try {
final count = await _repository.getUnreadCount();

View File

@@ -54,6 +54,9 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
final notifier = ref.read(messagingProvider.notifier);
notifier.setActiveConversation(widget.conversationId);
notifier.loadMessages(widget.conversationId);
// Refetch conversation to get fresh presence (isOnline/lastSeenAt) —
// the list snapshot may be stale. Matches web's selectConversation flow.
notifier.refreshConversation(widget.conversationId);
// Initialize mute/star state from conversation data
final conversations = ref.read(messagingProvider).conversations;
final conv = conversations.where((c) => c.id == widget.conversationId).firstOrNull;
@@ -120,6 +123,9 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
});
// Refresh messages to catch anything missed while in background
ref.read(messagingProvider.notifier).loadMessages(widget.conversationId);
// Refresh conversation to pick up any presence changes that happened
// while the app was backgrounded (socket status events were missed)
ref.read(messagingProvider.notifier).refreshConversation(widget.conversationId);
// Now that user is actively viewing, mark as read
ref.read(messagingProvider.notifier).markAsRead(widget.conversationId);
}