Skip to content

Commit 7e90350

Browse files
committed
autocomplete [nfc]: Factor out compareRecentMessageIds
1 parent dbb2dcf commit 7e90350

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/model/autocomplete.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,21 @@ class MentionAutocompleteView extends ChangeNotifier {
204204
final aLatestMessageId = recentDms.latestMessagesByRecipient[userA.userId];
205205
final bLatestMessageId = recentDms.latestMessagesByRecipient[userB.userId];
206206

207-
return switch((aLatestMessageId, bLatestMessageId)) {
208-
(int a, int b) => -a.compareTo(b),
209-
(int(), _) => -1,
210-
(_, int()) => 1,
207+
return -compareRecentMessageIds(aLatestMessageId, bLatestMessageId);
208+
}
209+
210+
/// Compares [a] to [b], with null less than all integers.
211+
///
212+
/// The values should represent the most recent message ID in each of two
213+
/// sets of messages, with null meaning the set is empty.
214+
///
215+
/// Return values are as with [Comparable.compareTo].
216+
@visibleForTesting
217+
static int compareRecentMessageIds(int? a, int? b) {
218+
return switch ((a, b)) {
219+
(int a, int b) => a.compareTo(b),
220+
(int(), _) => 1,
221+
(_, int()) => -1,
211222
_ => 0,
212223
};
213224
}

test/model/autocomplete_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,23 @@ void main() {
365365
await store.addUsers(users);
366366
}
367367

368+
group('MentionAutocompleteView.compareRecentMessageIds', () {
369+
test('both a and b are non-null', () async {
370+
check(MentionAutocompleteView.compareRecentMessageIds(2, 5)).isLessThan(0);
371+
check(MentionAutocompleteView.compareRecentMessageIds(5, 2)).isGreaterThan(0);
372+
check(MentionAutocompleteView.compareRecentMessageIds(5, 5)).equals(0);
373+
});
374+
375+
test('one of a and b is null', () async {
376+
check(MentionAutocompleteView.compareRecentMessageIds(null, 5)).isLessThan(0);
377+
check(MentionAutocompleteView.compareRecentMessageIds(5, null)).isGreaterThan(0);
378+
});
379+
380+
test('both of a and b are null', () async {
381+
check(MentionAutocompleteView.compareRecentMessageIds(null, null)).equals(0);
382+
});
383+
});
384+
368385
group('MentionAutocompleteView.compareByDms', () {
369386
const idA = 1;
370387
const idB = 2;

0 commit comments

Comments
 (0)