@@ -24,56 +24,102 @@ import 'dart:async';
2424import 'package:flutter/material.dart' ;
2525
2626import 'models/chat_view_list_tile.dart' ;
27+ import 'values/typedefs.dart' ;
2728
2829class ChatViewListController {
2930 ChatViewListController ({
30- required this .initialUsersList ,
31+ required List < ChatViewListModel > initialChatList ,
3132 required this .scrollController,
32- });
33+ }) {
34+ final chatListLength = initialChatList.length;
3335
34- /// Represents initial chat list users.
35- List <ChatViewListModel > initialUsersList = [];
36+ final chatsMap = {
37+ for (var i = 0 ; i < chatListLength; i++ )
38+ if (initialChatList[i] case final chat) chat.id: chat,
39+ };
40+
41+ chatListMap = chatsMap;
42+
43+ // Adds the current chat map to the stream controller
44+ // after the first frame render.
45+ Future .delayed (
46+ Duration .zero,
47+ () => _chatListStreamController.add (chatListMap),
48+ );
49+ }
50+
51+ /// Stores and manages chat items by their unique IDs.
52+ /// A map is used for efficient lookup, update, and removal of chats
53+ /// by their unique id.
54+ Map <String , ChatViewListModel > chatListMap = {};
3655
3756 /// Provides scroll controller for chat list.
3857 ScrollController scrollController;
3958
40- /// Represents chat list user stream
41- StreamController <List <ChatViewListModel >> chatListStreamController =
42- StreamController .broadcast ();
59+ /// Stream controller to manage the chat list stream.
60+ final StreamController <Map <String , ChatViewListModel >>
61+ _chatListStreamController =
62+ StreamController <Map <String , ChatViewListModel >>.broadcast ();
4363
44- /// Used to add user in the chat list.
45- void addUser (ChatViewListModel user) {
46- initialUsersList.add (user);
47- if (chatListStreamController.isClosed) return ;
48- chatListStreamController.sink.add (initialUsersList);
49- }
64+ late final Stream <List <ChatViewListModel >> chatListStream =
65+ _chatListStreamController.stream.map (
66+ (chatMap) => chatMap.values.toList (),
67+ );
5068
51- /// Function for loading data while pagination .
52- void loadMoreUsers ( List < ChatViewListModel > userList ) {
53- initialUsersList. addAll (userList) ;
54- if (chatListStreamController .isClosed) return ;
55- chatListStreamController.sink. add (initialUsersList );
69+ /// Adds a chat to the chat list .
70+ void addChat ( ChatViewListModel chat ) {
71+ chatListMap[chat.id] = chat ;
72+ if (_chatListStreamController .isClosed) return ;
73+ _chatListStreamController. add (chatListMap );
5674 }
5775
58- /// Function to add search results of the chat list in the stream.
59- void updateChatList (List <ChatViewListModel > searchResults) {
60- WidgetsBinding .instance.addPostFrameCallback (
61- (_) {
62- if (chatListStreamController.isClosed) return ;
63- chatListStreamController.sink.add (searchResults);
76+ /// Function for loading data while pagination.
77+ void loadMoreChats (List <ChatViewListModel > chatList) {
78+ final chatListLength = chatList.length;
79+ chatListMap.addAll (
80+ {
81+ for (var i = 0 ; i < chatListLength; i++ )
82+ if (chatList[i] case final chat) chat.id: chat,
6483 },
6584 );
85+ if (_chatListStreamController.isClosed) return ;
86+ _chatListStreamController.add (chatListMap);
87+ }
88+
89+ /// Updates the chat entry in [chatListMap] for the given [chatId] using
90+ /// the provided [newChat] callback.
91+ ///
92+ /// If the chat with [chatId] does not exist, the method returns without
93+ /// making changes.
94+ void updateChat (String chatId, UpdateChatCallback newChat) {
95+ final chat = chatListMap[chatId];
96+ if (chat == null ) return ;
97+
98+ chatListMap[chatId] = newChat (chat);
99+ if (_chatListStreamController.isClosed) return ;
100+ _chatListStreamController.add (chatListMap);
101+ }
102+
103+ /// Adds the given chat search results to the stream after the current frame.
104+ void setSearchChats (List <ChatViewListModel > searchResults) {
105+ final searchResultLength = searchResults.length;
106+ final searchResultMap = {
107+ for (var i = 0 ; i < searchResultLength; i++ )
108+ if (searchResults[i] case final chat) chat.id: chat,
109+ };
110+ if (_chatListStreamController.isClosed) return ;
111+ _chatListStreamController.add (searchResultMap);
66112 }
67113
68114 /// Function to clear the search results and show the original chat list.
69115 void clearSearch () {
70- if (chatListStreamController .isClosed) return ;
71- chatListStreamController.sink. add (initialUsersList );
116+ if (_chatListStreamController .isClosed) return ;
117+ _chatListStreamController. add (chatListMap );
72118 }
73119
74120 /// Used to dispose ValueNotifiers and Streams.
75121 void dispose () {
76122 scrollController.dispose ();
77- chatListStreamController .close ();
123+ _chatListStreamController .close ();
78124 }
79125}
0 commit comments