|
| 1 | +import 'dart:developer' as developer; |
| 2 | + |
1 | 3 | import 'package:flutter_riverpod/flutter_riverpod.dart'; |
2 | 4 | import 'package:supabase_flutter/supabase_flutter.dart'; |
3 | 5 | import '../models/chat_thread_model.dart'; |
@@ -25,19 +27,31 @@ class ThreadMessagesNotifier extends Notifier<List<MessageModel>> { |
25 | 27 | Future<void> init(String threadId) async { |
26 | 28 | _threadId = threadId; |
27 | 29 |
|
| 30 | + // Clear immediately so a fast thread switch never shows the prior thread. |
| 31 | + state = []; |
| 32 | + _channel?.unsubscribe(); |
| 33 | + _channel = null; |
| 34 | + |
28 | 35 | // Load initial messages |
29 | 36 | try { |
30 | 37 | final messages = await chatRepository.fetchMessages(threadId); |
| 38 | + if (_threadId != threadId) return; |
31 | 39 | state = messages; |
32 | | - } catch (_) { |
33 | | - state = []; |
| 40 | + } catch (e, st) { |
| 41 | + developer.log( |
| 42 | + 'fetchMessages failed', |
| 43 | + name: 'ThreadMessagesNotifier', |
| 44 | + error: e, |
| 45 | + stackTrace: st, |
| 46 | + ); |
| 47 | + if (_threadId == threadId) state = []; |
34 | 48 | } |
35 | 49 |
|
36 | 50 | // Subscribe to real-time updates |
37 | | - _channel?.unsubscribe(); |
38 | 51 | _channel = chatRepository.subscribeToMessages( |
39 | 52 | threadId: threadId, |
40 | 53 | onMessage: (message) { |
| 54 | + if (_threadId != threadId) return; |
41 | 55 | if (!state.any((m) => m.id == message.id)) { |
42 | 56 | state = [...state, message]; |
43 | 57 | } |
@@ -73,7 +87,13 @@ class ThreadMessagesNotifier extends Notifier<List<MessageModel>> { |
73 | 87 |
|
74 | 88 | // Message notifications are handled by the DB trigger |
75 | 89 | // (notify_on_new_message) to avoid duplicates. |
76 | | - } catch (_) { |
| 90 | + } catch (e, st) { |
| 91 | + developer.log( |
| 92 | + 'sendMessage failed', |
| 93 | + name: 'ThreadMessagesNotifier', |
| 94 | + error: e, |
| 95 | + stackTrace: st, |
| 96 | + ); |
77 | 97 | // Rollback optimistic message |
78 | 98 | state = state.where((m) => m.id != tempId).toList(); |
79 | 99 | } |
@@ -197,6 +217,42 @@ class ChatController extends Notifier<ChatState> { |
197 | 217 | } |
198 | 218 | } |
199 | 219 |
|
| 220 | + /// When navigating directly to `/chat/:id`, the thread may not yet appear in |
| 221 | + /// [state.threads]. Fetch that row (with participant pets) and merge it in. |
| 222 | + Future<void> ensureThreadLoaded(String threadId) async { |
| 223 | + if (state.threads.any((t) => t.id == threadId)) return; |
| 224 | + |
| 225 | + final activePet = ref.read(activePetProvider); |
| 226 | + if (activePet == null) return; |
| 227 | + |
| 228 | + try { |
| 229 | + final thread = |
| 230 | + await chatRepository.fetchThreadById(threadId, activePet.id); |
| 231 | + if (thread == null) return; |
| 232 | + |
| 233 | + final unreadCounts = await chatRepository.fetchUnreadCountsForThreads( |
| 234 | + [threadId], |
| 235 | + activePet.id, |
| 236 | + ); |
| 237 | + final merged = thread.copyWith( |
| 238 | + unreadCount: unreadCounts[threadId] ?? thread.unreadCount, |
| 239 | + ); |
| 240 | + |
| 241 | + if (state.threads.any((t) => t.id == threadId)) return; |
| 242 | + state = state.copyWith( |
| 243 | + threads: [merged, ...state.threads], |
| 244 | + ); |
| 245 | + } catch (e, st) { |
| 246 | + developer.log( |
| 247 | + 'ensureThreadLoaded failed', |
| 248 | + name: 'ChatController', |
| 249 | + error: e, |
| 250 | + stackTrace: st, |
| 251 | + ); |
| 252 | + state = state.copyWith(error: 'Could not load conversation header.'); |
| 253 | + } |
| 254 | + } |
| 255 | + |
200 | 256 | Future<void> markThreadAsRead(String threadId) async { |
201 | 257 | final activePet = ref.read(activePetProvider); |
202 | 258 | if (activePet == null) return; |
|
0 commit comments