feat: Enhance socket reconnection and chat screen lifecycle management, and refine agent detail and home screen UI.

This commit is contained in:
pradeepkumar
2026-03-19 03:58:03 +05:30
parent 6936c29c13
commit 21e677bf27
5 changed files with 75 additions and 22 deletions

View File

@@ -23,7 +23,8 @@ class ChatScreen extends ConsumerStatefulWidget {
ConsumerState<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends ConsumerState<ChatScreen> {
class _ChatScreenState extends ConsumerState<ChatScreen>
with WidgetsBindingObserver {
final TextEditingController _messageController = TextEditingController();
final ScrollController _scrollController = ScrollController();
final FocusNode _messageFocusNode = FocusNode();
@@ -36,6 +37,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
WidgetsBinding.instance.addPostFrameCallback((_) {
final notifier = ref.read(messagingProvider.notifier);
notifier.setActiveConversation(widget.conversationId);
@@ -43,10 +45,12 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
notifier.markAsRead(widget.conversationId);
// Suppress push notifications for this conversation while viewing
PushNotificationService().activeConversationId = widget.conversationId;
// Join socket room for real-time updates
if (_socket.isConnected) {
_socket.joinConversation(widget.conversationId);
}
// Ensure socket is connected and join room for real-time updates
_socket.ensureConnected().then((_) {
if (_socket.isConnected) {
_socket.joinConversation(widget.conversationId);
}
});
// Listen for ALL connection changes (initial connect + reconnects)
// so we re-join the room after any reconnection
_connectionSub = _socket.onConnectionChange.listen((connected) {
@@ -59,8 +63,23 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
_messageController.addListener(_onTextChanged);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
// App came back to foreground — reconnect socket and refresh messages
_socket.ensureConnected().then((_) {
if (_socket.isConnected && mounted) {
_socket.joinConversation(widget.conversationId);
}
});
// Refresh messages to catch anything missed while in background
ref.read(messagingProvider.notifier).loadMessages(widget.conversationId);
}
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_connectionSub?.cancel();
// Stop typing and leave room before disposing
if (_isComposing) {