Skip to content

Commit

Permalink
narrow: add DmNarrow.withUsers
Browse files Browse the repository at this point in the history
Refactored other DmNarrow factories to use
this more generic interface
  • Loading branch information
sirpengi committed Sep 15, 2023
1 parent 221a8be commit ca26716
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/model/narrow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,22 @@ class DmNarrow extends Narrow implements SendableNarrow {

/// A [DmNarrow] from an item in [InitialSnapshot.recentPrivateConversations].
factory DmNarrow.ofRecentDmConversation(RecentDmConversation conversation, {required int selfUserId}) {
return DmNarrow(
allRecipientIds: [...conversation.userIds, selfUserId]..sort(),
return DmNarrow.withUsers(
conversation.userIds,
selfUserId: selfUserId,
);
}

factory DmNarrow.withUser(int userId, {required int selfUserId}) {
return DmNarrow.withUsers(
[userId],
selfUserId: selfUserId,
);
}

factory DmNarrow.withUsers(List<int> userIds, {required int selfUserId}) {
return DmNarrow(
allRecipientIds: {userId, selfUserId}.toList()..sort(),
allRecipientIds: {...userIds, selfUserId}.toList()..sort(),
selfUserId: selfUserId,
);
}
Expand Down
14 changes: 14 additions & 0 deletions test/model/narrow_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ void main() {
selfUserId: 5));
});

test('withUsers: without selfUserId', () {
final actual = DmNarrow.withUsers([1, 2], selfUserId: 3);
check(actual).equals(DmNarrow(
allRecipientIds: [1, 2, 3],
selfUserId: 3));
});

test('withUsers: with selfUserId', () {
final actual = DmNarrow.withUsers([1, 2, 3], selfUserId: 3);
check(actual).equals(DmNarrow(
allRecipientIds: [1, 2, 3],
selfUserId: 3));
});

test('otherRecipientIds', () {
check(DmNarrow(allRecipientIds: [1, 2, 3], selfUserId: 2))
.otherRecipientIds.deepEquals([1, 3]);
Expand Down

0 comments on commit ca26716

Please sign in to comment.