EmojiSearch.addEmoji fixes CustomEmoji shortcodes

Phillip Davis created

- now `EmojiSearch.emoji` is truly immutable, in the sense that its
members are not mutated after being inserted.
- attempt to fix: https://todo.sr.ht/~singpolyma/soprani.ca/473

Change summary

src/cheogram/java/com/cheogram/android/EmojiSearch.java | 21 ++++------
1 file changed, 9 insertions(+), 12 deletions(-)

Detailed changes

src/cheogram/java/com/cheogram/android/EmojiSearch.java 🔗

@@ -62,6 +62,15 @@ public class EmojiSearch {
 	}
 
 	public synchronized void addEmoji(final Emoji one) {
+		if (one instanceof CustomEmoji) {
+			for (Emoji e : emoji) {
+				if (one.shortcodeMatch(e.uniquePart())) {
+					one.shortcodes.clear();
+					one.shortcodes.addAll(e.shortcodes);
+					break;
+				}
+			}
+		}
 		emoji.add(one);
 	}
 
@@ -76,18 +85,6 @@ public class EmojiSearch {
 			pq.addTopK(e, Math.max(shortcodeScore, tagScore), 10);
 		}
 
-		for (BoundExtractedResult<Emoji> r : new ArrayList<>(pq)) {
-			for (Emoji e : emoji) {
-				if (e.shortcodeMatch(r.getReferent().uniquePart())) {
-					// hack see https://stackoverflow.com/questions/76880072/imagespan-with-emojicompat
-					e.shortcodes.clear();
-					e.shortcodes.addAll(r.getReferent().shortcodes);
-
-					pq.addTopK(e, r.getScore() - 1, 10);
-				}
-			}
-		}
-
 		List<Emoji> result = new ArrayList<>();
 		for (int i = 0; i < 10; i++) {
 			BoundExtractedResult<Emoji> e = pq.poll();