fix: update active conversation state and read status based on app lifecycle changes
This commit is contained in:
@@ -101,8 +101,18 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
if (state == AppLifecycleState.resumed) {
|
if (state == AppLifecycleState.paused || state == AppLifecycleState.inactive) {
|
||||||
// App came back to foreground — reconnect socket and refresh messages
|
// 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((_) {
|
_socket.ensureConnected().then((_) {
|
||||||
if (_socket.isConnected && mounted) {
|
if (_socket.isConnected && mounted) {
|
||||||
_socket.joinConversation(widget.conversationId);
|
_socket.joinConversation(widget.conversationId);
|
||||||
@@ -110,6 +120,8 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
|
|||||||
});
|
});
|
||||||
// Refresh messages to catch anything missed while in background
|
// Refresh messages to catch anything missed while in background
|
||||||
ref.read(messagingProvider.notifier).loadMessages(widget.conversationId);
|
ref.read(messagingProvider.notifier).loadMessages(widget.conversationId);
|
||||||
|
// Now that user is actively viewing, mark as read
|
||||||
|
ref.read(messagingProvider.notifier).markAsRead(widget.conversationId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user