@@ -24,63 +24,100 @@ import 'dart:async';
2424import 'package:flutter/material.dart' ;
2525
2626import '../models/chat_view_list_item.dart' ;
27+ import '../values/typedefs.dart' ;
2728
2829class ChatViewListController {
2930 ChatViewListController ({
30- required this . initialChatList,
31+ required List < ChatViewListItem > initialChatList,
3132 required this .scrollController,
3233 this .disposeOtherResources = true ,
3334 }) {
34- // Add the initial chat list to the stream controller after the first frame.
35+ final chatListLength = initialChatList.length;
36+
37+ final chatsMap = {
38+ for (var i = 0 ; i < chatListLength; i++ )
39+ if (initialChatList[i] case final chat) chat.id: chat,
40+ };
41+
42+ chatListMap = chatsMap;
43+
44+ // Adds the current chat map to the stream controller
45+ // after the first frame render.
3546 Future .delayed (
3647 Duration .zero,
37- () => _chatListStreamController.add (initialChatList ),
48+ () => _chatListStreamController.add (chatListMap ),
3849 );
3950 }
4051
41- /// Represents initial chat list.
42- List <ChatViewListItem > initialChatList = [];
52+ /// Stores and manages chat items by their unique IDs.
53+ /// A map is used for efficient lookup, update, and removal of chats
54+ /// by their unique id.
55+ Map <String , ChatViewListItem > chatListMap = {};
4356
4457 /// Provides scroll controller for chat list.
4558 ScrollController scrollController;
4659
4760 final bool disposeOtherResources;
4861
49- /// Represents chat list user stream
50- final StreamController <List <ChatViewListItem >> _chatListStreamController =
51- StreamController .broadcast ();
62+ /// Stream controller to manage the chat list stream.
63+ final StreamController <Map <String , ChatViewListItem >>
64+ _chatListStreamController =
65+ StreamController <Map <String , ChatViewListItem >>.broadcast ();
5266
5367 late final Stream <List <ChatViewListItem >> chatListStream =
54- _chatListStreamController.stream;
68+ _chatListStreamController.stream.map (
69+ (chatMap) => chatMap.values.toList (),
70+ );
5571
5672 /// Adds a chat to the chat list.
5773 void addChat (ChatViewListItem chat) {
58- initialChatList. add ( chat) ;
74+ chatListMap[chat.id] = chat;
5975 if (_chatListStreamController.isClosed) return ;
60- _chatListStreamController.sink. add (initialChatList );
76+ _chatListStreamController.add (chatListMap );
6177 }
6278
6379 /// Function for loading data while pagination.
6480 void loadMoreChats (List <ChatViewListItem > chatList) {
65- initialChatList.addAll (chatList);
81+ final chatListLength = chatList.length;
82+ chatListMap.addAll (
83+ {
84+ for (var i = 0 ; i < chatListLength; i++ )
85+ if (chatList[i] case final chat) chat.id: chat,
86+ },
87+ );
6688 if (_chatListStreamController.isClosed) return ;
67- _chatListStreamController.sink.add (initialChatList);
89+ _chatListStreamController.add (chatListMap);
90+ }
91+
92+ /// Updates the chat entry in [chatListMap] for the given [chatId] using
93+ /// the provided [newChat] callback.
94+ ///
95+ /// If the chat with [chatId] does not exist, the method returns without
96+ /// making changes.
97+ void updateChat (String chatId, UpdateChatCallback newChat) {
98+ final chat = chatListMap[chatId];
99+ if (chat == null ) return ;
100+
101+ chatListMap[chatId] = newChat (chat);
102+ if (_chatListStreamController.isClosed) return ;
103+ _chatListStreamController.add (chatListMap);
68104 }
69105
70106 /// Adds the given chat search results to the stream after the current frame.
71107 void setSearchChats (List <ChatViewListItem > searchResults) {
72- WidgetsBinding .instance.addPostFrameCallback (
73- (_) {
74- if (_chatListStreamController.isClosed) return ;
75- _chatListStreamController.sink.add (searchResults);
76- },
77- );
108+ final searchResultLength = searchResults.length;
109+ final searchResultMap = {
110+ for (var i = 0 ; i < searchResultLength; i++ )
111+ if (searchResults[i] case final chat) chat.id: chat,
112+ };
113+ if (_chatListStreamController.isClosed) return ;
114+ _chatListStreamController.add (searchResultMap);
78115 }
79116
80117 /// Function to clear the search results and show the original chat list.
81118 void clearSearch () {
82119 if (_chatListStreamController.isClosed) return ;
83- _chatListStreamController.sink. add (initialChatList );
120+ _chatListStreamController.add (chatListMap );
84121 }
85122
86123 /// Used to dispose ValueNotifiers and Streams.
0 commit comments