@@ -48,9 +48,12 @@ class Unreads extends ChangeNotifier {
48
48
for (final unreadStreamSnapshot in initial.streams) {
49
49
final streamId = unreadStreamSnapshot.streamId;
50
50
final topic = unreadStreamSnapshot.topic;
51
- (streams[streamId] ?? = StreamUnreads .empty ())
52
- .topics[topic] = QueueList .from (unreadStreamSnapshot.unreadMessageIds);
53
- totalCount += unreadStreamSnapshot.unreadMessageIds.length;
51
+ final streamUnreads = streams[streamId] ?? = StreamUnreads .empty ();
52
+ final unreadInTopic = unreadStreamSnapshot.unreadMessageIds.length;
53
+ streamUnreads
54
+ ..topics[topic] = QueueList .from (unreadStreamSnapshot.unreadMessageIds)
55
+ ..count += unreadInTopic;
56
+ totalCount += unreadInTopic;
54
57
}
55
58
56
59
for (final unreadDmSnapshot in initial.dms) {
@@ -337,6 +340,7 @@ class Unreads extends ChangeNotifier {
337
340
void _addLastInStreamTopic (int messageId, int streamId, String topic) {
338
341
final streamUnreads = streams[streamId] ?? = StreamUnreads .empty ();
339
342
(streamUnreads.topics[topic] ?? = QueueList ()).addLast (messageId);
343
+ streamUnreads.count++ ;
340
344
_totalCount += 1 ;
341
345
}
342
346
@@ -358,19 +362,23 @@ class Unreads extends ChangeNotifier {
358
362
return result;
359
363
},
360
364
);
365
+ streamUnreads.count += numAdded;
361
366
_totalCount += numAdded;
362
367
}
363
368
364
369
// TODO use efficient model lookups
365
370
void _slowRemoveAllInStreams (Set <int > idsToRemove) {
366
- int numRemoved = 0 ;
371
+ int totalRemoved = 0 ;
367
372
final newlyEmptyStreams = [];
368
373
for (final MapEntry (key: streamId, value: streamUnreads) in streams.entries) {
374
+ int removedInStream = 0 ;
369
375
final newlyEmptyTopics = [];
370
376
for (final MapEntry (key: topic, value: messageIds) in streamUnreads.topics.entries) {
371
377
final lengthBefore = messageIds.length;
372
378
messageIds.removeWhere ((id) => idsToRemove.contains (id));
373
- numRemoved += lengthBefore - messageIds.length;
379
+ final removedInTopic = lengthBefore - messageIds.length;
380
+ removedInStream += removedInTopic;
381
+ totalRemoved += removedInTopic;
374
382
if (messageIds.isEmpty) {
375
383
newlyEmptyTopics.add (topic);
376
384
}
@@ -381,11 +389,12 @@ class Unreads extends ChangeNotifier {
381
389
if (streamUnreads.topics.isEmpty) {
382
390
newlyEmptyStreams.add (streamId);
383
391
}
392
+ streamUnreads.count -= removedInStream;
384
393
}
385
394
for (final streamId in newlyEmptyStreams) {
386
395
streams.remove (streamId);
387
396
}
388
- _totalCount -= numRemoved ;
397
+ _totalCount -= totalRemoved ;
389
398
}
390
399
391
400
void _removeAllInStreamTopic (Set <int > incomingMessageIds, int streamId, String topic) {
@@ -397,7 +406,9 @@ class Unreads extends ChangeNotifier {
397
406
// ([QueueList] doesn't have a `removeAll`)
398
407
final lengthBefore = messageIds.length;
399
408
messageIds.removeWhere ((id) => incomingMessageIds.contains (id));
400
- _totalCount -= lengthBefore - messageIds.length;
409
+ final numRemoved = lengthBefore - messageIds.length;
410
+ streamUnreads.count -= numRemoved;
411
+ _totalCount -= numRemoved;
401
412
if (messageIds.isEmpty) {
402
413
streamUnreads.topics.remove (topic);
403
414
if (streamUnreads.topics.isEmpty) {
@@ -456,11 +467,18 @@ class Unreads extends ChangeNotifier {
456
467
}
457
468
458
469
class StreamUnreads {
459
- StreamUnreads ({required this .topics});
460
- StreamUnreads .empty () : topics = {};
470
+ StreamUnreads ({required this .count, required this .topics});
471
+ StreamUnreads .empty ()
472
+ : count = 0 ,
473
+ topics = {};
474
+
475
+ /// Total unread messages in this stream.
476
+ ///
477
+ /// Prefer this when possible over traversing [topics] to make a sum.
478
+ int count;
461
479
462
480
Map <String , QueueList <int >> topics;
463
481
464
482
@visibleForTesting
465
- Map <String , dynamic > toJson () => {'topics' : topics};
483
+ Map <String , dynamic > toJson () => {'count' : count, ' topics': topics};
466
484
}
0 commit comments