@@ -37,9 +37,9 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
3737 final MessagesRepository messagesRepository;
3838 final UserRepository userRepository;
3939
40- StreamSubscription <ConversationModel >? updateConversationStreamSubscription;
4140 StreamSubscription <ChatMessage >? incomingMessagesSubscription;
4241 StreamSubscription <MessageSendStatus >? statusMessagesSubscription;
42+ StreamSubscription <ConversationModel ?>? conversationWatcher;
4343
4444 ConversationBloc ({
4545 required this .currentConversation,
@@ -48,7 +48,7 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
4848 required this .userRepository,
4949 }) : super (ConversationState (
5050 conversation: currentConversation,
51- participants: currentConversation.participants. toSet ( ))) {
51+ participants: Set . of ( currentConversation.participants))) {
5252 on < MessagesRequested > (_onMessagesRequested);
5353 on < MessagesMoreRequested > (
5454 _onMessagesMoreRequested,
@@ -79,16 +79,6 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
7979
8080 add (const ParticipantsReceived ());
8181
82- updateConversationStreamSubscription =
83- conversationRepository.updateConversationStream.listen ((chat) async {
84- if (chat.id != currentConversation.id) return ;
85-
86- if (currentConversation != chat) {
87- currentConversation = currentConversation.copyWithItem (item: chat);
88- add (_ConversationUpdated (currentConversation));
89- }
90- });
91-
9282 incomingMessagesSubscription =
9383 messagesRepository.incomingMessagesStream.listen ((message) async {
9484 if (message.cid != currentConversation.id) return ;
@@ -117,6 +107,15 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
117107 break ;
118108 }
119109 });
110+
111+ conversationWatcher = messagesRepository.localDatasource
112+ .watchedConversation (currentConversation.id)
113+ .listen ((chat) {
114+ if (chat != null && chat != currentConversation) {
115+ currentConversation = currentConversation.copyWithItem (item: chat);
116+ add (_ConversationUpdated (currentConversation));
117+ }
118+ });
120119 }
121120
122121 Future <void > _onMessagesRequested (
@@ -132,7 +131,7 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
132131 status: ConversationStatus .success,
133132 messages: messages,
134133 hasReachedMax: false ,
135- participants: currentConversation.participants. toSet ( ),
134+ participants: Set . of ( currentConversation.participants),
136135 initial: true ),
137136 );
138137 add (const MessagesRequested ());
@@ -188,8 +187,8 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
188187
189188 Future <void > _onParticipantsReceived (
190189 ParticipantsReceived event, Emitter <ConversationState > emit) async {
191- var participants =
192- await conversationRepository .updateParticipants (currentConversation);
190+ var participants = await conversationRepository
191+ .updateParticipants (currentConversation. copyWith () );
193192 emit (state.copyWith (participants: Set .of (participants)));
194193 }
195194
@@ -277,9 +276,9 @@ class ConversationBloc extends Bloc<ConversationEvent, ConversationState> {
277276
278277 @override
279278 Future <void > close () {
280- updateConversationStreamSubscription? .cancel ();
281279 incomingMessagesSubscription? .cancel ();
282280 statusMessagesSubscription? .cancel ();
281+ conversationWatcher? .cancel ();
283282 return super .close ();
284283 }
285284}
0 commit comments