From b5123ff8650c8d95a2438b9454656f13517c3e50 Mon Sep 17 00:00:00 2001 From: LucasGGamerM Date: Sun, 3 Sep 2023 15:28:29 -0300 Subject: [PATCH] feat: readd recent emoji support --- .../settings/SettingsAboutAppFragment.java | 18 +++++++- .../android/ui/CustomEmojiPopupKeyboard.java | 43 ++++++++++++++++++- mastodon/src/main/res/values/strings_mo.xml | 1 + 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java index 2b726dac0..0bfd2b582 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsAboutAppFragment.java @@ -12,9 +12,11 @@ import org.joinmastodon.android.R; import org.joinmastodon.android.api.MastodonAPIController; import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; +import org.joinmastodon.android.fragments.HasAccountID; import org.joinmastodon.android.model.viewmodel.ListItem; import org.joinmastodon.android.ui.utils.UiUtils; +import java.util.HashMap; import java.util.List; import java.util.Objects; @@ -24,9 +26,11 @@ import me.grishka.appkit.utils.MergeRecyclerAdapter; import me.grishka.appkit.utils.SingleViewRecyclerAdapter; import me.grishka.appkit.utils.V; -public class SettingsAboutAppFragment extends BaseSettingsFragment{ +public class SettingsAboutAppFragment extends BaseSettingsFragment implements HasAccountID{ private ListItem mediaCacheItem; + // MOSHIDON + private ListItem clearRecentEmojisItem; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); @@ -37,6 +41,7 @@ public class SettingsAboutAppFragment extends BaseSettingsFragment{ new ListItem<>(R.string.mo_settings_contribute, 0, R.drawable.ic_fluent_open_24_regular, ()->UiUtils.launchWebBrowser(getActivity(), getString(R.string.mo_repo_url))), new ListItem<>(R.string.settings_tos, 0, R.drawable.ic_fluent_open_24_regular, ()->UiUtils.launchWebBrowser(getActivity(), "https://"+s.domain+"/terms")), new ListItem<>(R.string.settings_privacy_policy, 0, R.drawable.ic_fluent_open_24_regular, ()->UiUtils.launchWebBrowser(getActivity(), getString(R.string.privacy_policy_url)), 0, true), + clearRecentEmojisItem=new ListItem<>(R.string.mo_clear_recent_emoji, 0, this::onClearRecentEmojisClick), mediaCacheItem=new ListItem<>(R.string.settings_clear_cache, 0, this::onClearMediaCacheClick) )); @@ -73,10 +78,21 @@ public class SettingsAboutAppFragment extends BaseSettingsFragment{ }); } + private void onClearRecentEmojisClick(){ + getLocalPrefs().recentEmojis=new HashMap<>(); + getLocalPrefs().save(); + Toast.makeText(getContext(), R.string.mo_recent_emoji_cleared, Toast.LENGTH_SHORT).show(); + } + private void updateMediaCacheItem(){ long size=ImageCache.getInstance(getActivity()).getDiskCache().size(); mediaCacheItem.subtitle=UiUtils.formatFileSize(getActivity(), size, false); mediaCacheItem.isEnabled=size>0; rebindItem(mediaCacheItem); } + + @Override + public String getAccountID(){ + return accountID; + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java b/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java index d5aaa72e6..ccf983a29 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java @@ -30,11 +30,15 @@ import org.joinmastodon.android.GlobalUserPreferences; import org.joinmastodon.android.R; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.events.EmojiUpdatedEvent; +import org.joinmastodon.android.fragments.HasAccountID; import org.joinmastodon.android.model.Emoji; import org.joinmastodon.android.model.EmojiCategory; import org.joinmastodon.android.ui.utils.UiUtils; +import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -52,7 +56,9 @@ import me.grishka.appkit.utils.MergeRecyclerAdapter; import me.grishka.appkit.utils.V; import me.grishka.appkit.views.UsableRecyclerView; -public class CustomEmojiPopupKeyboard extends PopupKeyboard{ +public class CustomEmojiPopupKeyboard extends PopupKeyboard implements HasAccountID{ + //determines how many emoji need to be clicked, before it disappears from the recent emojis + private static final int NEW_RECENT_VALUE=15; private List emojis; private UsableRecyclerView list; private ListImageLoaderWrapper imgLoader; @@ -104,6 +110,17 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ list.setPadding(V.dp(16), 0, V.dp(16), 0); imgLoader=new ListImageLoaderWrapper(activity, list, new RecyclerViewDelegate(list), null); + // inject category with last used emojis + if (!getLocalPrefs().recentEmojis.isEmpty()) { + List allAvailableEmojis = emojis.stream().flatMap(category -> category.emojis.stream()).collect(Collectors.toList()); + List recentEmojiList = new ArrayList<>(); + for (String emojiCode : getLocalPrefs().recentEmojis.keySet().stream().sorted(Comparator.comparingInt(getLocalPrefs().recentEmojis::get).reversed()).collect(Collectors.toList())) { + Optional element = allAvailableEmojis.stream().filter(e -> e.shortcode.equals(emojiCode)).findFirst(); + element.ifPresent(recentEmojiList::add); + } + emojis.add(0, new EmojiCategory(activity.getString(R.string.mo_emoji_recent), recentEmojiList)); + } + for(EmojiCategory category:emojis) adapter.addAdapter(new SingleCategoryAdapter(category)); list.setAdapter(adapter); @@ -193,6 +210,11 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ bottomPanel.addView(backspace, new FrameLayout.LayoutParams(V.dp(48), V.dp(48), Gravity.END | Gravity.CENTER_VERTICAL)); } + //remove recently used afterwards, it would get duplicated otherwise + if (!getLocalPrefs().recentEmojis.isEmpty()) { + emojis.remove(0); + } + return ll; } @@ -209,6 +231,11 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ } } + @Override + public String getAccountID(){ + return accountID; + } + private class SingleCategoryAdapter extends UsableRecyclerView.Adapter implements ImageLoaderRecyclerAdapter{ private final EmojiCategory category; private final List requests; @@ -278,6 +305,19 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ } } + private void increaseEmojiCount(Emoji emoji) { + Integer usageCount = getLocalPrefs().recentEmojis.get(emoji.shortcode); + if (usageCount != null) { + getLocalPrefs().recentEmojis.put(emoji.shortcode, usageCount + 1); + } else { + getLocalPrefs().recentEmojis.put(emoji.shortcode, NEW_RECENT_VALUE); + } + + getLocalPrefs().recentEmojis.entrySet().removeIf(e -> e.getValue() <= 0); + getLocalPrefs().recentEmojis.replaceAll((k, v) -> v - 1); + getLocalPrefs().save(); + } + private class EmojiViewHolder extends BindableViewHolder implements ImageLoaderViewHolder, UsableRecyclerView.Clickable{ public int positionWithinCategory; public EmojiViewHolder(){ @@ -309,6 +349,7 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ @Override public void onClick(){ + increaseEmojiCount(item); listener.onEmojiSelected(item); } } diff --git a/mastodon/src/main/res/values/strings_mo.xml b/mastodon/src/main/res/values/strings_mo.xml index 680f20bb4..0b633b015 100644 --- a/mastodon/src/main/res/values/strings_mo.xml +++ b/mastodon/src/main/res/values/strings_mo.xml @@ -95,4 +95,5 @@ Automatically mention account who reblogged the post in replies Unfollow Account Confirm to unfollow %s + Recent emoji cleared \ No newline at end of file