Skip to content

Commit b5523e5

Browse files
committed
unreads: Add helpers to provide unread counts for narrows
These helpers do not account for muted topics and streams, those are left for zulip#346.
1 parent e1c581b commit b5523e5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

lib/model/unreads.dart

+44
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,50 @@ class Unreads extends ChangeNotifier {
125125

126126
int countInDmNarrow(DmNarrow narrow) => dms[narrow]?.length ?? 0;
127127

128+
// TODO(#346): account for muted topics and streams
129+
int countInAllMessagesNarrow() {
130+
int c = 0;
131+
for (final messageIds in dms.values) {
132+
c = c + messageIds.length;
133+
}
134+
for (final topics in streams.values) {
135+
for (final messageIds in topics.values) {
136+
c = c + messageIds.length;
137+
}
138+
}
139+
return c;
140+
}
141+
142+
// TODO(#346): account for muted topics and streams
143+
int countInStreamNarrow(StreamNarrow narrow) {
144+
final topics = streams[narrow.streamId];
145+
if (topics == null) return 0;
146+
int c = 0;
147+
for (final messageIds in topics.values) {
148+
c = c + messageIds.length;
149+
}
150+
return c;
151+
}
152+
153+
// TODO(#346): account for muted topics and streams
154+
int countInTopicNarrow(TopicNarrow narrow) {
155+
final topics = streams[narrow.streamId];
156+
return topics?[narrow.topic]?.length ?? 0;
157+
}
158+
159+
int countInNarrow(Narrow narrow) {
160+
switch (narrow) {
161+
case DmNarrow():
162+
return countInDmNarrow(narrow);
163+
case AllMessagesNarrow():
164+
return countInAllMessagesNarrow();
165+
case StreamNarrow():
166+
return countInStreamNarrow(narrow);
167+
case TopicNarrow():
168+
return countInTopicNarrow(narrow);
169+
}
170+
}
171+
128172
void handleMessageEvent(MessageEvent event) {
129173
final message = event.message;
130174
if (message.flags.contains(MessageFlag.read)) {

0 commit comments

Comments
 (0)