@@ -115,13 +115,7 @@ mixin EmojiStore {
115115 ///
116116 /// See description in the web code:
117117 /// https://github.com/zulip/zulip/blob/83a121c7e/web/shared/src/typeahead.ts#L3-L21
118- // Someday this list may start varying rather than being hard-coded,
119- // and then this will become a non-static member on EmojiStore.
120- // For now, though, the fact it's constant is convenient when writing
121- // tests of the logic that uses this data; so we guarantee it in the API.
122- static Iterable <EmojiCandidate > get popularEmojiCandidates {
123- return EmojiStoreImpl ._popularCandidates;
124- }
118+ Iterable <EmojiCandidate > popularEmojiCandidates ();
125119
126120 Iterable <EmojiCandidate > allEmojiCandidates ();
127121
@@ -218,36 +212,54 @@ class EmojiStoreImpl extends PerAccountStoreBase with EmojiStore {
218212 /// retrieving the data.
219213 Map <String , List <String >>? _serverEmojiData;
220214
221- static final _popularCandidates = _generatePopularCandidates () ;
215+ List < EmojiCandidate > ? _popularCandidates;
222216
223- static List <EmojiCandidate > _generatePopularCandidates () {
224- EmojiCandidate candidate (String emojiCode, String emojiUnicode,
225- List < String > names) {
226- final emojiName = names. removeAt ( 0 );
227- assert (emojiUnicode == tryParseEmojiCodeToUnicode (emojiCode) );
217+ List <EmojiCandidate > _generatePopularCandidates () {
218+ EmojiCandidate candidate (String emojiCode, List < String > names) {
219+ final [emojiName, ...aliases] = names;
220+ final emojiUnicode = tryParseEmojiCodeToUnicode (emojiCode );
221+ assert (emojiUnicode != null );
228222 return EmojiCandidate (emojiType: ReactionType .unicodeEmoji,
229- emojiCode: emojiCode, emojiName: emojiName, aliases: names ,
223+ emojiCode: emojiCode, emojiName: emojiName, aliases: aliases ,
230224 emojiDisplay: UnicodeEmojiDisplay (
231- emojiName: emojiName, emojiUnicode: emojiUnicode));
225+ emojiName: emojiName, emojiUnicode: emojiUnicode! ));
232226 }
233- return [
234- // This list should match web:
235- // https://github.com/zulip/zulip/blob/83a121c7e/web/shared/src/typeahead.ts#L22-L29
236- candidate ('1f44d' , '👍' , ['+1' , 'thumbs_up' , 'like' ]),
237- candidate ('1f389' , '🎉' , ['tada' ]),
238- candidate ('1f642' , '🙂' , ['smile' ]),
239- candidate ( '2764' , '❤' , ['heart' , 'love' , 'love_you' ]),
240- candidate ('1f6e0' , '🛠' , ['working_on_it' , 'hammer_and_wrench' , 'tools' ]),
241- candidate ('1f419' , '🐙' , ['octopus' ]),
242- ];
227+ if (_serverEmojiData == null ) return [];
228+
229+ final result = < EmojiCandidate > [];
230+ for (final emojiCode in _popularEmojiCodesList) {
231+ final names = _serverEmojiData! [emojiCode];
232+ if (names == null ) continue ; // TODO(log)
233+ result.add (candidate (emojiCode, names));
234+ }
235+ return result;
243236 }
244237
245- static final _popularEmojiCodes = (() {
246- assert (_popularCandidates.every ((c) =>
247- c.emojiType == ReactionType .unicodeEmoji));
248- return Set .of (_popularCandidates.map ((c) => c.emojiCode));
238+ @override
239+ Iterable <EmojiCandidate > popularEmojiCandidates () {
240+ return _popularCandidates ?? = _generatePopularCandidates ();
241+ }
242+
243+ /// Codes for the popular emoji, in order; all are Unicode emoji.
244+ // This list should match web:
245+ // https://github.com/zulip/zulip/blob/9feba0f16/web/shared/src/typeahead.ts#L22-L29
246+ static final List <String > _popularEmojiCodesList = (() {
247+ String check (String emojiCode, String emojiUnicode) {
248+ assert (emojiUnicode == tryParseEmojiCodeToUnicode (emojiCode));
249+ return emojiCode;
250+ }
251+ return [
252+ check ('1f44d' , '👍' ),
253+ check ('1f389' , '🎉' ),
254+ check ('1f642' , '🙂' ),
255+ check ('2764' , '❤' ),
256+ check ('1f6e0' , '🛠' ),
257+ check ('1f419' , '🐙' ),
258+ ];
249259 })();
250260
261+ static final Set <String > _popularEmojiCodes = Set .of (_popularEmojiCodesList);
262+
251263 static bool _isPopularEmoji (EmojiCandidate candidate) {
252264 return candidate.emojiType == ReactionType .unicodeEmoji
253265 && _popularEmojiCodes.contains (candidate.emojiCode);
@@ -307,7 +319,7 @@ class EmojiStoreImpl extends PerAccountStoreBase with EmojiStore {
307319
308320 // Include the "popular" emoji, in their canonical order
309321 // relative to each other.
310- results.addAll (_popularCandidates );
322+ results.addAll (popularEmojiCandidates () );
311323
312324 final namesOverridden = {
313325 for (final emoji in activeRealmEmoji) emoji.name,
@@ -366,6 +378,7 @@ class EmojiStoreImpl extends PerAccountStoreBase with EmojiStore {
366378 @override
367379 void setServerEmojiData (ServerEmojiData data) {
368380 _serverEmojiData = data.codeToNames;
381+ _popularCandidates = null ;
369382 _allEmojiCandidates = null ;
370383 }
371384
0 commit comments