Skip to content

Commit

Permalink
wip and use stable sort
Browse files Browse the repository at this point in the history
  • Loading branch information
gnprice committed Dec 7, 2024
1 parent 4397244 commit 7291b38
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/model/emoji.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../api/model/events.dart';
import '../api/model/initial_snapshot.dart';
import '../api/model/model.dart';
import '../api/route/realm.dart';
import 'algorithms.dart';
import 'autocomplete.dart';
import 'narrow.dart';
import 'store.dart';
Expand Down Expand Up @@ -360,23 +361,28 @@ class EmojiAutocompleteView extends AutocompleteView<EmojiAutocompleteQuery, Emo

@override
Future<List<EmojiAutocompleteResult>?> computeResults() async {
final results = <EmojiAutocompleteResult>[];
final unsorted = <EmojiAutocompleteResult>[];
if (await filterCandidates(filter: _testCandidate,
candidates: store.allEmojiCandidates(), results: results)) {
candidates: store.allEmojiCandidates(), results: unsorted)) {
return null;
}
results.sort(_compareResults);
return results;
return bucketSort(unsorted, _rankResult, numBuckets: _numResultRanks);
}

EmojiAutocompleteResult? _testCandidate(EmojiAutocompleteQuery query, EmojiCandidate candidate) {
final match = query.match(candidate);
return match == null ? null : EmojiAutocompleteResult(match, candidate);
}

static int _compareResults(EmojiAutocompleteResult a, EmojiAutocompleteResult b) {
static const _numResultRanks = 3;

static int _rankResult(EmojiAutocompleteResult result) {
// TODO(#1068): rank emoji results also by popular, realm, other
return EmojiMatchQuality.compare(a.matchQuality, b.matchQuality);
return switch (result.matchQuality) {
EmojiMatchQuality.exact => 0,
EmojiMatchQuality.prefix => 1,
EmojiMatchQuality.internal => 2,
};
}
}

Expand Down

0 comments on commit 7291b38

Please sign in to comment.