feat: Implement socket-first message sending with REST fallback, enhance chat room re-joining on socket reconnect, and update 'Categories' to 'Specialization' in the hero section.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
@@ -28,6 +30,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
|
||||
int _previousMessageCount = 0;
|
||||
|
||||
final SocketService _socket = SocketService();
|
||||
StreamSubscription<bool>? _connectionSub;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -37,17 +40,17 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
|
||||
notifier.setActiveConversation(widget.conversationId);
|
||||
notifier.loadMessages(widget.conversationId);
|
||||
notifier.markAsRead(widget.conversationId);
|
||||
// Join socket room for real-time updates (socket is connected at auth time)
|
||||
// Join socket room for real-time updates
|
||||
if (_socket.isConnected) {
|
||||
_socket.joinConversation(widget.conversationId);
|
||||
} else {
|
||||
// Wait for socket connection then join
|
||||
_socket.onConnectionChange.first.then((connected) {
|
||||
if (connected && mounted) {
|
||||
_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) {
|
||||
if (connected && mounted) {
|
||||
_socket.joinConversation(widget.conversationId);
|
||||
}
|
||||
});
|
||||
});
|
||||
_scrollController.addListener(_onScroll);
|
||||
_messageController.addListener(_onTextChanged);
|
||||
@@ -55,6 +58,7 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_connectionSub?.cancel();
|
||||
// Stop typing and leave room before disposing
|
||||
if (_isComposing) {
|
||||
_socket.stopTyping(widget.conversationId);
|
||||
|
||||
Reference in New Issue
Block a user