Skip to content

Commit ca26716

Browse files
committed
narrow: add DmNarrow.withUsers
Refactored other DmNarrow factories to use this more generic interface
1 parent 221a8be commit ca26716

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/model/narrow.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,22 @@ class DmNarrow extends Narrow implements SendableNarrow {
155155

156156
/// A [DmNarrow] from an item in [InitialSnapshot.recentPrivateConversations].
157157
factory DmNarrow.ofRecentDmConversation(RecentDmConversation conversation, {required int selfUserId}) {
158-
return DmNarrow(
159-
allRecipientIds: [...conversation.userIds, selfUserId]..sort(),
158+
return DmNarrow.withUsers(
159+
conversation.userIds,
160160
selfUserId: selfUserId,
161161
);
162162
}
163163

164164
factory DmNarrow.withUser(int userId, {required int selfUserId}) {
165+
return DmNarrow.withUsers(
166+
[userId],
167+
selfUserId: selfUserId,
168+
);
169+
}
170+
171+
factory DmNarrow.withUsers(List<int> userIds, {required int selfUserId}) {
165172
return DmNarrow(
166-
allRecipientIds: {userId, selfUserId}.toList()..sort(),
173+
allRecipientIds: {...userIds, selfUserId}.toList()..sort(),
167174
selfUserId: selfUserId,
168175
);
169176
}

test/model/narrow_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ void main() {
9090
selfUserId: 5));
9191
});
9292

93+
test('withUsers: without selfUserId', () {
94+
final actual = DmNarrow.withUsers([1, 2], selfUserId: 3);
95+
check(actual).equals(DmNarrow(
96+
allRecipientIds: [1, 2, 3],
97+
selfUserId: 3));
98+
});
99+
100+
test('withUsers: with selfUserId', () {
101+
final actual = DmNarrow.withUsers([1, 2, 3], selfUserId: 3);
102+
check(actual).equals(DmNarrow(
103+
allRecipientIds: [1, 2, 3],
104+
selfUserId: 3));
105+
});
106+
93107
test('otherRecipientIds', () {
94108
check(DmNarrow(allRecipientIds: [1, 2, 3], selfUserId: 2))
95109
.otherRecipientIds.deepEquals([1, 3]);

0 commit comments

Comments
 (0)