From 33c0f92fe619b93bfec9f80b90b24ad7878cac53 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Thu, 26 Oct 2023 15:21:07 +0100 Subject: [PATCH] unreads: Add proposal for countInNarrow --- lib/model/unreads.dart | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/model/unreads.dart b/lib/model/unreads.dart index b10a7587bd2..cdfee170925 100644 --- a/lib/model/unreads.dart +++ b/lib/model/unreads.dart @@ -125,6 +125,37 @@ class Unreads extends ChangeNotifier { int countInDmNarrow(DmNarrow narrow) => dms[narrow]?.length ?? 0; + int countInNarrow(Narrow narrow) { + switch (narrow) { + case DmNarrow(): + return dms[narrow]?.length ?? 0; + case AllMessagesNarrow(): + int c = 0; + for (final model in dms.values) { + c = c + model.length; + } + for (final streamData in streams.values) { + for (final model in streamData.values) { + c = c + model.length; + } + } + return c; + case StreamNarrow(): + final streamData = streams[narrow.streamId]; + if (streamData == null) return 0; + int c = 0; + for (final model in streamData.values) { + c = c + model.length; + } + return c; + case TopicNarrow(): + final streamData = streams[narrow.streamId]; + if (streamData == null) return 0; + final model = streamData[narrow.topic]; + return model?.length ?? 0; + } + } + void handleMessageEvent(MessageEvent event) { final message = event.message; if (message.flags.contains(MessageFlag.read)) {