feat: Enhance socket reconnection and chat screen lifecycle management, and refine agent detail and home screen UI.
This commit is contained in:
@@ -68,9 +68,9 @@ class SocketService {
|
||||
.setAuth({'token': token})
|
||||
.disableAutoConnect()
|
||||
.enableReconnection()
|
||||
.setReconnectionAttempts(5)
|
||||
.setReconnectionAttempts(double.maxFinite.toInt())
|
||||
.setReconnectionDelay(1000)
|
||||
.setReconnectionDelayMax(5000)
|
||||
.setReconnectionDelayMax(10000)
|
||||
.build(),
|
||||
);
|
||||
|
||||
@@ -97,6 +97,18 @@ class SocketService {
|
||||
_connectionController.add(false);
|
||||
});
|
||||
|
||||
_socket!.onReconnect((_) {
|
||||
_log.i('Socket reconnected successfully');
|
||||
});
|
||||
|
||||
_socket!.onReconnectAttempt((attempt) {
|
||||
_log.w('Socket reconnection attempt #$attempt');
|
||||
});
|
||||
|
||||
_socket!.onReconnectError((error) {
|
||||
_log.e('Socket reconnection error: $error');
|
||||
});
|
||||
|
||||
// Listen for events
|
||||
_socket!.on('new_message', (data) {
|
||||
try {
|
||||
@@ -172,6 +184,12 @@ class SocketService {
|
||||
_socket?.connect();
|
||||
}
|
||||
|
||||
/// Ensure socket is connected — reconnect if needed.
|
||||
Future<void> ensureConnected() async {
|
||||
if (_socket?.connected == true) return;
|
||||
await connect();
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
_intentionalDisconnect = true;
|
||||
_socket?.disconnect();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user