App icon indicating copy to clipboard operation
App copied to clipboard

[HOLD for payment 2021-12-27] Add support for Frequently/Recently Used Emojis

Open mananjadhav opened this issue 5 years ago • 64 comments

If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!


With respect to comment #4456 (comment), adding this issue.

Action Performed:

  1. Click on Chat
  2. Open Emoji Picker

Expected Result:

There should be an option to see Recently used or Frequently used Emojis.

Actual Result:

No option to use recently used/frequently used Emojis. image

Workaround:

User has to find the Emoji and enter again

Platform:

Where is this issue occurring?

  • Web
  • iOS
  • Android
  • Desktop App
  • Mobile Web

Version Number: Logs: https://stackoverflow.com/c/expensify/questions/4856 Notes/Photos/Videos: Any additional supporting documentation Expensify/Expensify Issue URL:

View all open jobs on Upwork

Upwork job: https://www.upwork.com/jobs/~010528886766f948f5

mananjadhav avatar Aug 11 '21 16:08 mananjadhav

Triggered auto assignment to @sonialiap (AutoAssignerTriage), see https://stackoverflow.com/c/expensify/questions/4749 for more details.

MelvinBot avatar Aug 11 '21 16:08 MelvinBot

Proposal

We should have additional category Frequently Used and/or Recently Used emojis.

It could have a set of default emojis in the list by default if we choose to keep Frequently used. Based on the user behavior we can sync this with Onyx and ensure that it available for the user to use. I don't think it is necessary to sync it across devices, but can be discussed.

Changes:

An additional FlatList in EmojiPicker with a recentEmojis

<FlatList
	ref={el => this.recentEmojiList = el}
	data={this.state.recentEmojis}
	renderItem={this.renderItem}
	keyExtractor={item => `emoji_picker_${item.code}`}
	numColumns={this.numColumns}
	style={styles.emojiPickerList}
	extraData={[this.state.filteredEmojis, this.state.highlightedRecentIndex]}
	stickyHeaderIndices={this.state.headerIndices}
	onScroll={e => this.currentScrollOffset = e.nativeEvent.contentOffset.y}
/>	

Extend onEmojiSelected/addEmojiToTextBox to add recently used emojis. We can restrict the maximum number of emojis for this category.

addEmojiToTextBox(emoji) {
	
	if(recentEmojis.length >= maxAllowedRecent) {
		recentEmojis.splice(-1)
	}
	recentEmojis.unshift(emoji);

	Onyx.set(ONYXKEYS.RECENT_EMOJIS, recentEmojis);

	// existing code

}

For frequently used, the logic would differ. We would have to somehow manage a counter { 1faa: 10, 1f78: 45, ...}. The logic of the map would also change as we would then add based on the counter.

Apple adds to the list by using both - Recently used anf Frequently used. Your frequently used appear first and then your recently used. A combined list could also work for us.

mananjadhav avatar Aug 11 '21 16:08 mananjadhav

@sonialiap Huh... This is 4 days overdue. Who can take care of this?

MelvinBot avatar Aug 16 '21 19:08 MelvinBot

@sonialiap 6 days overdue. This is scarier than being forced to listen to Vogon poetry!

MelvinBot avatar Aug 18 '21 19:08 MelvinBot

Triggered auto assignment to @timszot (Engineering), see https://stackoverflow.com/c/expensify/questions/4319 for more details.

MelvinBot avatar Aug 18 '21 22:08 MelvinBot

Sorry for not triaging this earlier, I was OOO. Looks good

sonialiap avatar Aug 18 '21 22:08 sonialiap

Triggered auto assignment to @michaelhaxhiu (External), see https://stackoverflow.com/c/expensify/questions/8582 for more details.

MelvinBot avatar Aug 19 '21 16:08 MelvinBot

@mananjadhav thank you for being patient on this :). The past 2 weeks have been abnormal for us, and we are just getting back to normal operations (especially true for me).

michaelhaxhiu avatar Aug 31 '21 17:08 michaelhaxhiu

Job posted here. https://www.upwork.com/jobs/~010528886766f948f5

michaelhaxhiu avatar Aug 31 '21 17:08 michaelhaxhiu

Triggered auto assignment to @NikkiWines (Exported), see https://stackoverflow.com/c/expensify/questions/7972 for more details.

MelvinBot avatar Aug 31 '21 17:08 MelvinBot

@mananjadhav I think with your proposed solution of an additional flatlist you might get some strange UI interactions like so:

https://user-images.githubusercontent.com/16747822/131591837-c45dfa49-a24a-42cf-8337-e6834af69ec2.mov

I'm not sure what the plan is for emojis and if we planned to support Recently Used or Frequently Used functionality. cc: @stitesExpensify since you implemented the emoji picker for New Expensify

NikkiWines avatar Sep 01 '21 00:09 NikkiWines

@NikkiWines Might be because we're using the same highlightIndex and filteredEmojis. We could even have an additional category in Emoji rather than a separate FlatList. I'll let you and @stitesExpensify decide which is the way we would want to go with.

mananjadhav avatar Sep 01 '21 04:09 mananjadhav

We could even have an additional category in Emoji rather than a separate FlatList

Yeah, I was thinking adding an additional section to the existing emoji list would make sense. We could store the user's most recently used emojis in Onyx and populate them as necessary.

One question @mananjadhav, is your proposal for recently used or frequently used functionality - you mention both in your original proposal, but also mention they'd require slightly different implementations.

NikkiWines avatar Sep 01 '21 19:09 NikkiWines

One question @mananjadhav, is your proposal for recently used or frequently used functionality - you mention both in your original proposal, but also mention they'd require slightly different implementations.

Well, I wasn't sure what would be more relevant for Expensify. If you look like Apple it uses both the functionalities. If you add an emoji, it gets added to the frequently used emojis. If you keep adding the same emoji, the sort order is defined by frequently added.

Now for frequently used I've added a suggestion here,

For frequently used, the logic would differ. We would have to somehow manage a counter { 1faa: 10, 1f78: 45, ...}. The logic of the map would also change as we would then add based on the counter.

If I were to implement this in the code with and without recent addition:

function addToFrequentEmojis(emoji) {
   let frequentEmojis = Onyx.get(ONYXKEYS.FREQUENT_EMOJIS);
   frequentEmojis[emoji.unicode] =    frequentEmojis[emoji.unicode] + 1;
   frequentEmojis = lodash(frequentEmojis).toPairs().orderBy([1], ['desc']).take(maxEmojiAllowedLength).fromPairs().value();
   Onyx.set(ONYXKEYS.FREQUENT_EMOJIS, frequentEmojis);
}



function addToFrequentAndRecent(emoji) {
   let frequentEmojis = Onyx.get(ONYXKEYS.COLLECTION.FREQUENT_EMOJIS);
   let emojiCount = frequentEmojis[emoji.unicode] + 1;
   frequentEmojis = lodash(frequentEmojis).toPairs().orderBy([1], ['desc']).take(maxEmojiAllowedLength - 1).fromPairs().value();
   frequentEmojis[emoji.unicode] = emojiCount;
   Onyx.set(ONYXKEYS.COLLECTION.FREQUENT_EMOJIS, frequentEmojis);
}

and in the EmojiPicker we used it this way:

   frequentEmojis = this.props.frequentEmojis;


   Onyx.connect({

      frequentEmojis: () => lodash(`${ONYXKEYS.COLLECTION.FREQUENT_EMOJIS}`).toPairs().orderBy([1], ['desc']).map(em => em[0]);
      
   });

   

mananjadhav avatar Sep 01 '21 20:09 mananjadhav

That makes sense, thanks for explaining @mananjadhav - I'm not sure which we'd prefer either and will defer to @stitesExpensify for this one!

NikkiWines avatar Sep 02 '21 21:09 NikkiWines

Proposal

  1. First we need to remove the spacer items from emojis.js so they can be dynamically added in EmojiPickerMenu.
const emojis = [
    {
        code: 'Smileys & People',
        header: true,
    },
    {
        code: '😀',
        keywords: [
            'face',
            'grin',
        ],
    },
  1. Then, in EmojiPickerMenu:
  • Connect to Onyx
export default compose(
    withWindowDimensions,
    withLocalize,
    withOnyx({
        frequentlyUsedEmojis: {
            key: ONYXKEYS.FREQUENTLY_USED_EMOJIS
        },
    }),

and create a new Onyx key for FREQUENTLY_USED_EMOJIS in ONYXKEYS.js. This will store an object with the form of { 😎 : 3, 🤑 : 2 } which will be added to the beginning of the emojis list in the constructor.

  • Set this.emojis and this.unfilteredHeaderIndices in the constructor like so:
        function includeFrequentlyUsedEmojis(frequentlyUsedEmojis = {}, allEmojis) {
            // sort the list by frequency of use, adding the most used emojis to the beginning
            const ordered = Object.entries(frequentlyUsedEmojis).sort((a, b) => {
                if (a[1] < b[1]) {
                    return 1
                } else if (a[1] > b[1]) {
                    return -1
                } 
                return 0
            }).map((item) => ({
                code: item[0]
            }))
            return ordered.length > 0 ? [
                {
                    code: 'Frequently Used Emojis',
                    header: true
                },
                ...ordered.slice(0, 16), // show only two rows of emojis
                ...allEmojis
            ] : allEmojis
        }

        // Add the spacer items so the headers always render in their own row
        function spaceEmojis(list) {
            let updatedList = [];
            list.forEach((emoji, i) => {
                if (emoji.header) {
                    const spacersToAdd = (i === 0) ? 0 : (8 - (updatedList.length % 8));
                    updatedList = [
                        ...updatedList,
                        ...Array(spacersToAdd).fill({
                            code: CONST.EMOJI_SPACER,
                        }),
                        emoji,
                        ...Array(7).fill({
                            code: CONST.EMOJI_SPACER,
                        }),
                    ];
                } else {
                    updatedList = [
                        ...updatedList,
                        emoji,
                    ];
                }
            });
            return updatedList;
        }
        
        // get the index of the headers so FlatList knows which row to sticky
        function getHeaderIndices(list) {
            return list.map((e, i) => {
                if (i === 0) {
                    return i;
                } else if (e.header) {
                    return (i / 8);
                } else {
                    return null
                }
            }).filter(e => e !== null);
        }

        const emojisWithFrequentlyUsed = includeFrequentlyUsedEmojis(props.frequentlyUsedEmojis, emojis)
        const emojisWithSpacing = spaceEmojis(emojisWithFrequentlyUsed);
        this.unfilteredHeaderIndices = getHeaderIndices(emojisWithSpacing);

        // If we're on Windows, don't display the flag emojis (the last category),
        // since Windows doesn't support them (and only displays country codes instead)
        this.emojis = getOperatingSystem() === CONST.OS.WINDOWS
            ? emojisWithSpacing.slice(0, this.unfilteredHeaderIndices.pop() * this.numColumns)
            : emojisWithSpacing;
  • Add a new addEmojiToFrequentlyUsed method to <EmojiPickerMenuItem>
            <EmojiPickerMenuItem
                onPress={(emoji) => {
                    this.props.onEmojiSelected(emoji)
                    this.addEmojiToFrequentlyUsed(emoji)
                }}
    addEmojiToFrequentlyUsed(emoji) {
        const existingFrequentlyUsedEmojis = this.props.frequentlyUsedEmojis || {}
        Onyx.merge(ONYXKEYS.FREQUENTLY_USED_EMOJIS, {
            [emoji]: (existingFrequentlyUsedEmojis[emoji] || 0) + 1
        })
    }

https://user-images.githubusercontent.com/39444813/132372487-36ceddea-c92f-403c-a8c3-55a4c6f4f6fd.mov

This will also make it possible to easily add new emojis to the list in the future or fetch the list from a server.

elemdos avatar Sep 03 '21 00:09 elemdos

@michaelhaxhiu, @NikkiWines Whoops! This issue is 2 days overdue. Let's get this updated quick!

MelvinBot avatar Sep 06 '21 19:09 MelvinBot

@NikkiWines should we assign @stitesExpensify so it's clear he's going to review this? Or were you planning to stay assigned and have Brandon give a buddy check?

michaelhaxhiu avatar Sep 07 '21 19:09 michaelhaxhiu

Assigned myself since I will def be involved on this. Up to @NikkiWines if she wants to stay assigned :)

stitesExpensify avatar Sep 07 '21 19:09 stitesExpensify

I <3 nikki but she's been helping with NL delivery today, so let's keep assigned to you Brandon.

michaelhaxhiu avatar Sep 07 '21 20:09 michaelhaxhiu