-
Notifications
You must be signed in to change notification settings - Fork 308
emoji: Generate popular candidates using names from server data #1506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
bbbaa53
5973fec
ecb4f7d
a60ff3f
e8728f5
13292a9
774542e
f66c812
42f3d21
b8abc6b
d8d9e58
3d341b8
1a40f27
076b689
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,13 +115,7 @@ mixin EmojiStore { | |
/// | ||
/// See description in the web code: | ||
/// https://github.com/zulip/zulip/blob/83a121c7e/web/shared/src/typeahead.ts#L3-L21 | ||
// Someday this list may start varying rather than being hard-coded, | ||
// and then this will become a non-static member on EmojiStore. | ||
// For now, though, the fact it's constant is convenient when writing | ||
// tests of the logic that uses this data; so we guarantee it in the API. | ||
static Iterable<EmojiCandidate> get popularEmojiCandidates { | ||
return EmojiStoreImpl._popularCandidates; | ||
} | ||
Iterable<EmojiCandidate> popularEmojiCandidates(); | ||
|
||
Iterable<EmojiCandidate> allEmojiCandidates(); | ||
|
||
|
@@ -218,36 +212,54 @@ class EmojiStoreImpl extends PerAccountStoreBase with EmojiStore { | |
/// retrieving the data. | ||
Map<String, List<String>>? _serverEmojiData; | ||
|
||
static final _popularCandidates = _generatePopularCandidates(); | ||
List<EmojiCandidate>? _popularCandidates; | ||
|
||
static List<EmojiCandidate> _generatePopularCandidates() { | ||
EmojiCandidate candidate(String emojiCode, String emojiUnicode, | ||
List<String> names) { | ||
final emojiName = names.removeAt(0); | ||
assert(emojiUnicode == tryParseEmojiCodeToUnicode(emojiCode)); | ||
List<EmojiCandidate> _generatePopularCandidates() { | ||
EmojiCandidate candidate(String emojiCode, List<String> names) { | ||
final [emojiName, ...aliases] = names; | ||
final emojiUnicode = tryParseEmojiCodeToUnicode(emojiCode); | ||
assert(emojiUnicode != null); | ||
return EmojiCandidate(emojiType: ReactionType.unicodeEmoji, | ||
emojiCode: emojiCode, emojiName: emojiName, aliases: names, | ||
emojiCode: emojiCode, emojiName: emojiName, aliases: aliases, | ||
emojiDisplay: UnicodeEmojiDisplay( | ||
emojiName: emojiName, emojiUnicode: emojiUnicode)); | ||
emojiName: emojiName, emojiUnicode: emojiUnicode!)); | ||
} | ||
return [ | ||
// This list should match web: | ||
// https://github.com/zulip/zulip/blob/83a121c7e/web/shared/src/typeahead.ts#L22-L29 | ||
candidate('1f44d', '👍', ['+1', 'thumbs_up', 'like']), | ||
candidate('1f389', '🎉', ['tada']), | ||
candidate('1f642', '🙂', ['smile']), | ||
candidate( '2764', '❤', ['heart', 'love', 'love_you']), | ||
candidate('1f6e0', '🛠', ['working_on_it', 'hammer_and_wrench', 'tools']), | ||
candidate('1f419', '🐙', ['octopus']), | ||
]; | ||
if (_serverEmojiData == null) return []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I understand, The popular emojis among them, created from Oh, then I saw the lines in |
||
|
||
final result = <EmojiCandidate>[]; | ||
for (final emojiCode in _popularEmojiCodesList) { | ||
final names = _serverEmojiData![emojiCode]; | ||
if (names == null) continue; // TODO(log) | ||
result.add(candidate(emojiCode, names)); | ||
} | ||
return result; | ||
} | ||
|
||
static final _popularEmojiCodes = (() { | ||
assert(_popularCandidates.every((c) => | ||
c.emojiType == ReactionType.unicodeEmoji)); | ||
return Set.of(_popularCandidates.map((c) => c.emojiCode)); | ||
@override | ||
Iterable<EmojiCandidate> popularEmojiCandidates() { | ||
return _popularCandidates ??= _generatePopularCandidates(); | ||
Comment on lines
+239
to
+240
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: put this next to the field that it maintains |
||
} | ||
|
||
/// Codes for the popular emoji, in order; all are Unicode emoji. | ||
// This list should match web: | ||
// https://github.com/zulip/zulip/blob/9feba0f16/web/shared/src/typeahead.ts#L22-L29 | ||
static final List<String> _popularEmojiCodesList = (() { | ||
String check(String emojiCode, String emojiUnicode) { | ||
assert(emojiUnicode == tryParseEmojiCodeToUnicode(emojiCode)); | ||
return emojiCode; | ||
} | ||
return [ | ||
check('1f44d', '👍'), | ||
check('1f389', '🎉'), | ||
check('1f642', '🙂'), | ||
check('2764', '❤'), | ||
check('1f6e0', '🛠'), | ||
check('1f419', '🐙'), | ||
]; | ||
})(); | ||
|
||
static final Set<String> _popularEmojiCodes = Set.of(_popularEmojiCodesList); | ||
|
||
static bool _isPopularEmoji(EmojiCandidate candidate) { | ||
return candidate.emojiType == ReactionType.unicodeEmoji | ||
&& _popularEmojiCodes.contains(candidate.emojiCode); | ||
|
@@ -307,7 +319,7 @@ class EmojiStoreImpl extends PerAccountStoreBase with EmojiStore { | |
|
||
// Include the "popular" emoji, in their canonical order | ||
// relative to each other. | ||
results.addAll(_popularCandidates); | ||
results.addAll(popularEmojiCandidates()); | ||
|
||
final namesOverridden = { | ||
for (final emoji in activeRealmEmoji) emoji.name, | ||
|
@@ -366,6 +378,7 @@ class EmojiStoreImpl extends PerAccountStoreBase with EmojiStore { | |
@override | ||
void setServerEmojiData(ServerEmojiData data) { | ||
_serverEmojiData = data.codeToNames; | ||
_popularCandidates = null; | ||
_allEmojiCandidates = null; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -554,6 +554,8 @@ void showMessageActionSheet({required BuildContext context, required Message mes | |
final pageContext = PageRoot.contextOf(context); | ||
final store = PerAccountStoreWidget.of(pageContext); | ||
|
||
final popularEmojiLoaded = store.popularEmojiCandidates().isNotEmpty; | ||
|
||
// The UI that's conditioned on this won't live-update during this appearance | ||
// of the action sheet (we avoid calling composeBoxControllerOf in a build | ||
// method; see its doc). | ||
|
@@ -567,7 +569,8 @@ void showMessageActionSheet({required BuildContext context, required Message mes | |
final showMarkAsUnreadButton = markAsUnreadSupported && isMessageRead; | ||
|
||
final optionButtons = [ | ||
ReactionButtons(message: message, pageContext: pageContext), | ||
if (popularEmojiLoaded) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can have a test checking when this should not be offered, like we do with quote-and-reply button. |
||
ReactionButtons(message: message, pageContext: pageContext), | ||
StarButton(message: message, pageContext: pageContext), | ||
if (isComposeBoxOffered) | ||
QuoteAndReplyButton(message: message, pageContext: pageContext), | ||
|
@@ -665,11 +668,18 @@ class ReactionButtons extends StatelessWidget { | |
|
||
@override | ||
Widget build(BuildContext context) { | ||
assert(EmojiStore.popularEmojiCandidates.every( | ||
final store = PerAccountStoreWidget.of(pageContext); | ||
final popularEmojiCandidates = store.popularEmojiCandidates(); | ||
assert(popularEmojiCandidates.every( | ||
(emoji) => emoji.emojiType == ReactionType.unicodeEmoji)); | ||
// (if this is empty, the widget isn't built in the first place) | ||
assert(popularEmojiCandidates.isNotEmpty); | ||
// UI not designed to handle more than 6 popular emoji. | ||
// (We might have fewer if ServerEmojiData is lacking expected data, | ||
// but that looks fine in manual testing, even when there's just one.) | ||
assert(popularEmojiCandidates.length <= 6); | ||
|
||
final zulipLocalizations = ZulipLocalizations.of(context); | ||
final store = PerAccountStoreWidget.of(pageContext); | ||
final designVariables = DesignVariables.of(context); | ||
|
||
bool hasSelfVote(EmojiCandidate emoji) { | ||
|
@@ -685,7 +695,7 @@ class ReactionButtons extends StatelessWidget { | |
color: designVariables.contextMenuItemBg.withFadedAlpha(0.12)), | ||
child: Row(children: [ | ||
Flexible(child: Row(spacing: 1, children: List.unmodifiable( | ||
EmojiStore.popularEmojiCandidates.mapIndexed((index, emoji) => | ||
popularEmojiCandidates.mapIndexed((index, emoji) => | ||
_buildButton( | ||
context: context, | ||
emoji: emoji, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll be depending anyway on this being non-null, so might as well keep it simple.