Skip to content

Commit

Permalink
Use the emoji picker's search in getSuggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSunCat authored and The one with the braid (she/her) | Dфҿ mit dem Zopf (sie/ihr) committed Jul 21, 2023
1 parent bc73a5d commit 94972f9
Showing 1 changed file with 6 additions and 26 deletions.
32 changes: 6 additions & 26 deletions lib/pages/chat/input_bar.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:emoji_picker_flutter/emoji_picker_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:emojis/emoji.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:matrix/matrix.dart';
Expand Down Expand Up @@ -47,7 +47,7 @@ class InputBar extends StatelessWidget {
Key? key,
}) : super(key: key);

List<Map<String, String?>> getSuggestions(String text) {
Future<List<Map<String, String?>>> getSuggestions(String text) async {
if (controller!.selection.baseOffset !=
controller!.selection.extentOffset ||
controller!.selection.baseOffset < 0) {
Expand Down Expand Up @@ -119,34 +119,14 @@ class InputBar extends StatelessWidget {
}
}
// aside of emote packs, also propose normal (tm) unicode emojis
final matchingUnicodeEmojis = Emoji.all()
.where(
(element) => [element.name, ...element.keywords]
.any((element) => element.toLowerCase().contains(emoteSearch)),
)
.toList();
// sort by the index of the search term in the name in order to have
// best matches first
// (thanks for the hint by github.com/nextcloud/circles devs)
matchingUnicodeEmojis.sort((a, b) {
final indexA = a.name.indexOf(emoteSearch);
final indexB = b.name.indexOf(emoteSearch);
if (indexA == -1 || indexB == -1) {
if (indexA == indexB) return 0;
if (indexA == -1) {
return 1;
} else {
return 0;
}
}
return indexA.compareTo(indexB);
});
final matchingUnicodeEmojis = await EmojiPickerUtils().searchEmoji(emoteSearch, defaultEmojiSet);

for (final emoji in matchingUnicodeEmojis) {
ret.add({
'type': 'emoji',
'emoji': emoji.char,
'emoji': emoji.emoji,
// don't include sub-group names, splitting at `:` hence
'label': '${emoji.char} - ${emoji.name.split(':').first}',
'label': '${emoji.emoji} - ${emoji.name.split(':').first}',
'current_word': ':$emoteSearch',
});
if (ret.length > maxResults) {
Expand Down

0 comments on commit 94972f9

Please sign in to comment.