fix: update active conversation state and read status based on app lifecycle changes

This commit is contained in:
pradeepkumar
2026-04-09 20:40:58 +05:30
parent 63288fa06a
commit b97113b10a

View File

@@ -101,8 +101,18 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
// App came back to foreground — reconnect socket and refresh messages
if (state == AppLifecycleState.paused || state == AppLifecycleState.inactive) {
// App went to background/recent apps — clear active conversation so
// incoming messages DON'T auto-mark as read while user isn't looking.
// Also clear push suppression so notifications still show.
ref.read(messagingProvider.notifier).setActiveConversation(null);
PushNotificationService().activeConversationId = null;
} else if (state == AppLifecycleState.resumed) {
// App came back to foreground — restore active conversation, reconnect
// socket, mark unread messages as read, and refresh.
ref.read(messagingProvider.notifier).setActiveConversation(widget.conversationId);
PushNotificationService().activeConversationId = widget.conversationId;
_socket.ensureConnected().then((_) {
if (_socket.isConnected && mounted) {
_socket.joinConversation(widget.conversationId);
@@ -110,6 +120,8 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
});
// Refresh messages to catch anything missed while in background
ref.read(messagingProvider.notifier).loadMessages(widget.conversationId);
// Now that user is actively viewing, mark as read
ref.read(messagingProvider.notifier).markAsRead(widget.conversationId);
}
}