Skip to content

Commit 1eebcb6

Browse files
sirpengignprice
authored andcommitted
unreads: Add helpers to provide unread counts for narrows
These helpers do not account for muted topics and streams, those are left for #346.
1 parent 2b4019d commit 1eebcb6

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Diff for: lib/model/unreads.dart

+46
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,54 @@ class Unreads extends ChangeNotifier {
123123

124124
final int selfUserId;
125125

126+
// TODO(#346): account for muted topics and streams
127+
// TODO(#370): maintain this count incrementally, rather than recomputing from scratch
128+
int countInAllMessagesNarrow() {
129+
int c = 0;
130+
for (final messageIds in dms.values) {
131+
c = c + messageIds.length;
132+
}
133+
for (final topics in streams.values) {
134+
for (final messageIds in topics.values) {
135+
c = c + messageIds.length;
136+
}
137+
}
138+
return c;
139+
}
140+
141+
// TODO(#346): account for muted topics and streams
142+
// TODO(#370): maintain this count incrementally, rather than recomputing from scratch
143+
int countInStreamNarrow(int streamId) {
144+
final topics = streams[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(int streamId, String topic) {
155+
final topics = streams[streamId];
156+
return topics?[topic]?.length ?? 0;
157+
}
158+
126159
int countInDmNarrow(DmNarrow narrow) => dms[narrow]?.length ?? 0;
127160

161+
int countInNarrow(Narrow narrow) {
162+
switch (narrow) {
163+
case AllMessagesNarrow():
164+
return countInAllMessagesNarrow();
165+
case StreamNarrow():
166+
return countInStreamNarrow(narrow.streamId);
167+
case TopicNarrow():
168+
return countInTopicNarrow(narrow.streamId, narrow.topic);
169+
case DmNarrow():
170+
return countInDmNarrow(narrow);
171+
}
172+
}
173+
128174
void handleMessageEvent(MessageEvent event) {
129175
final message = event.message;
130176
if (message.flags.contains(MessageFlag.read)) {

Diff for: test/model/unreads_test.dart

+47
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,53 @@ void main() {
146146
});
147147
});
148148

149+
group('count helpers', () {
150+
test('countInAllMessagesNarrow', () {
151+
final stream1 = eg.stream();
152+
final stream2 = eg.stream();
153+
prepare();
154+
fillWithMessages([
155+
eg.streamMessage(stream: stream1, topic: 'a', flags: []),
156+
eg.streamMessage(stream: stream1, topic: 'b', flags: []),
157+
eg.streamMessage(stream: stream1, topic: 'b', flags: []),
158+
eg.streamMessage(stream: stream2, topic: 'c', flags: []),
159+
eg.dmMessage(from: eg.otherUser, to: [eg.selfUser], flags: []),
160+
eg.dmMessage(from: eg.thirdUser, to: [eg.selfUser], flags: []),
161+
]);
162+
check(model.countInAllMessagesNarrow()).equals(6);
163+
});
164+
165+
test('countInStreamNarrow', () {
166+
final stream = eg.stream();
167+
prepare();
168+
fillWithMessages([
169+
eg.streamMessage(stream: stream, topic: 'a', flags: []),
170+
eg.streamMessage(stream: stream, topic: 'a', flags: []),
171+
eg.streamMessage(stream: stream, topic: 'b', flags: []),
172+
eg.streamMessage(stream: stream, topic: 'b', flags: []),
173+
eg.streamMessage(stream: stream, topic: 'b', flags: []),
174+
]);
175+
check(model.countInStreamNarrow(stream.streamId)).equals(5);
176+
});
177+
178+
test('countInTopicNarrow', () {
179+
final stream = eg.stream();
180+
prepare();
181+
fillWithMessages(List.generate(7, (i) => eg.streamMessage(
182+
stream: stream, topic: 'a', flags: [])));
183+
check(model.countInTopicNarrow(stream.streamId, 'a')).equals(7);
184+
});
185+
186+
test('countInDmNarrow', () {
187+
prepare();
188+
fillWithMessages(List.generate(5, (i) => eg.dmMessage(
189+
from: eg.otherUser, to: [eg.selfUser], flags: [])));
190+
final narrow = DmNarrow.withUser(
191+
eg.otherUser.userId, selfUserId: eg.selfUser.userId);
192+
check(model.countInDmNarrow(narrow)).equals(5);
193+
});
194+
});
195+
149196
group('handleMessageEvent', () {
150197
for (final (isUnread, isStream, isDirectMentioned, isWildcardMentioned) in [
151198
(true, true, true, true ),

0 commit comments

Comments
 (0)