@@ -9,28 +9,34 @@ import 'package:commet/client/matrix/matrix_client.dart';
99import 'package:commet/client/stale_info.dart' ;
1010import 'package:commet/client/tasks/client_connection_status_task.dart' ;
1111import 'package:commet/main.dart' ;
12- import 'package:commet/utils/notifying_list.dart' ;
12+ import 'package:commet/utils/notifying_map.dart' ;
13+ import 'package:commet/utils/notifying_sub_map.dart' ;
1314
1415class ClientManager {
15- final Map <String , Client > _clients = {} ;
16+ final NotifyingMap <String , BaseRoom > _all_rooms = NotifyingMap () ;
1617
17- final NotifyingList < Room > _rooms = NotifyingList . empty (growable : true );
18+ final NotifyingMap < String , Client > _clients = NotifyingMap ( );
1819
19- final NotifyingList <Space > _spaces = NotifyingList .empty (growable: true );
20+ late final NotifyingMap <String , Room > _rooms = NotifyingSubMap (_all_rooms, null );
21+
22+ late final NotifyingMap <String , Space > _spaces = NotifyingSubMap (_all_rooms, null );
23+
24+ NotifyingMap <String , Room > get motifyingMapRoom => _rooms;
25+ NotifyingMap <String , Space > get motifyingMapSpace => _spaces;
2026
2127 final AlertManager alertManager = AlertManager ();
2228 late CallManager callManager;
2329
2430 late final DirectMessagesAggregator directMessages;
2531
26- static ClientManager instance = ClientManager ._();
32+ static final ClientManager instance = ClientManager ._();
2733
2834 ClientManager ._() {
2935 directMessages = DirectMessagesAggregator (this );
3036 callManager = CallManager (this );
3137 }
3238
33- List <Room > get rooms => _rooms;
39+ Iterable <Room > get rooms => _rooms.values ;
3440
3541 List <Room > singleRooms ({Client ? filterClient}) {
3642 var result = List <Room >.empty (growable: true );
@@ -59,7 +65,7 @@ class ClientManager {
5965 return result;
6066 }
6167
62- List <Space > get spaces => _spaces;
68+ Iterable <Space > get spaces => _spaces.values ;
6369
6470 final List <Client > _clientsList = List .empty (growable: true );
6571 final Map <Client , List <StreamSubscription >> _clientSubscriptions = {};
@@ -68,18 +74,17 @@ class ClientManager {
6874
6975 late StreamController <void > onSync = StreamController .broadcast ();
7076
71- Stream <Room > get onRoomAdded => _rooms.onAdd;
77+ Stream <Room > get onRoomAdded => _rooms.onAdd. map ((e) => e.value) ;
7278
73- Stream <Room > get onRoomRemoved => _rooms.onRemove;
79+ Stream <Room > get onRoomRemoved => _rooms.onRemove. map ((e) => e.value) ;
7480
75- Stream <Space > get onSpaceAdded => _spaces.onAdd;
81+ Stream <Space > get onSpaceAdded => _spaces.onAdd. map ((e) => e.value) ;
7682
77- Stream <Space > get onSpaceRemoved => _spaces.onRemove;
83+ Stream <Space > get onSpaceRemoved => _spaces.onRemove. map ((e) => e.value) ;
7884
79- late StreamController < int > onClientAdded = StreamController . broadcast ( );
85+ Stream < Client > get onClientAdded => _clients.onAdd. map ((e) => e.value );
8086
81- late StreamController <StalePeerInfo > onClientRemoved =
82- StreamController .broadcast ();
87+ Stream <Client > get onClientRemoved => _clients.onRemove.map ((e) => e.value);
8388
8489 late StreamController <Space > onSpaceUpdated = StreamController .broadcast ();
8590 late StreamController <Space > onSpaceChildUpdated =
@@ -94,8 +99,6 @@ class ClientManager {
9499 // previousValue + element.displayNotificationCount);
95100
96101 static Future <ClientManager > init ({bool isBackgroundService = false }) async {
97- instance = ClientManager ._();
98-
99102 await Future .wait ([
100103 MatrixClient .loadFromDB (instance,
101104 isBackgroundService: isBackgroundService),
@@ -110,27 +113,16 @@ class ClientManager {
110113
111114 _clientsList.add (client);
112115
113- for (final e in client.rooms) {
114- _onClientAddedRoom (client, e);
115- }
116-
117116 for (final e in client.spaces) {
118117 _addSpace (client, e);
119118 }
120119
121120 _clientSubscriptions[client] = [
122121 client.onSync.listen ((_) => _synced ()),
123- client.onRoomAdded.listen ((room) => _onClientAddedRoom (client, room)),
124- client.onRoomRemoved
125- .listen ((room) => _onClientRemovedRoom (client, room)),
126122 client.onSpaceAdded.listen ((space) => _addSpace (client, space)),
127- client.onSpaceRemoved
128- .listen ((space) => _onClientRemovedSpace (client, space)),
129123 client.connectionStatusChanged.stream
130124 .listen ((event) => _onClientConnectionStatusChanged (client, event)),
131125 ];
132-
133- onClientAdded.add (_clients.length - 1 );
134126 } catch (error) {}
135127 }
136128
@@ -148,22 +140,9 @@ class ClientManager {
148140 }
149141 }
150142
151- void _onClientAddedRoom (Client client, Room room) {
152- rooms.add (room);
153- }
154-
155- void _onClientRemovedRoom (Client client, Room room) {
156- _rooms.remove (room);
157- }
158-
159- void _onClientRemovedSpace (Client client, Space space) {
160- _spaces.remove (space);
161- }
162-
163143 void _addSpace (Client client, Space space) {
164144 space.onUpdate.listen ((_) => spaceUpdated (space));
165145 space.onChildRoomUpdated.listen ((_) => spaceChildUpdated (space));
166- spaces.add (space);
167146 }
168147
169148 void spaceUpdated (Space space) {
@@ -195,20 +174,19 @@ class ClientManager {
195174 identifier: client.self! .identifier,
196175 avatar: client.self! .avatar);
197176
198- for (int i = rooms.length - 1 ; i >= 0 ; i -- ) {
199- if (rooms[i] .client == client) {
200- rooms. removeAt (i );
177+ for (final room in rooms) {
178+ if (room .client == client) {
179+ _rooms. remove (room.localId );
201180 }
202181 }
203182
204- for (int i = spaces.length - 1 ; i >= 0 ; i -- ) {
205- if (spaces[i] .client == client) {
206- spaces. removeAt (i );
183+ for (final space in spaces) {
184+ if (space .client == client) {
185+ _spaces. remove (space.localId );
207186 }
208187 }
209188
210189 await client.logout ();
211- onClientRemoved.add (clientInfo);
212190 _clients.remove (client.identifier);
213191 _clientsList.removeAt (clientIndex);
214192 }
0 commit comments