From 0b6128bcddab1062598c533856d8216ffa6f35b2 Mon Sep 17 00:00:00 2001 From: FineFindus <63370021+FineFindus@users.noreply.github.com> Date: Mon, 2 Jan 2023 16:44:14 +0100 Subject: [PATCH 1/4] feat(compose/emojis): add category of recent emojis --- .../android/GlobalUserPreferences.java | 7 +++ .../android/ui/CustomEmojiPopupKeyboard.java | 49 +++++++++++++++++-- mastodon/src/main/res/values/strings_sk.xml | 1 + 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java index 04b94eb8e..9dbfb1fe2 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java +++ b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java @@ -9,6 +9,8 @@ import android.os.Build; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; +import org.joinmastodon.android.model.Emoji; + import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; @@ -39,6 +41,9 @@ public class GlobalUserPreferences{ private final static Type recentLanguagesType = new TypeToken>>() {}.getType(); public static Map> recentLanguages; + private final static Type recentEmojisType = new TypeToken>() {}.getType(); + public static Map recentEmojis; + private static SharedPreferences getPrefs(){ return MastodonApp.context.getSharedPreferences("global", Context.MODE_PRIVATE); } @@ -68,6 +73,7 @@ public class GlobalUserPreferences{ enableDeleteNotifications=prefs.getBoolean("enableDeleteNotifications", true); theme=ThemePreference.values()[prefs.getInt("theme", 0)]; recentLanguages=fromJson(prefs.getString("recentLanguages", "{}"), recentLanguagesType, new HashMap<>()); + recentEmojis=fromJson(prefs.getString("recentEmojis", "{}"), recentEmojisType, new HashMap<>()); publishButtonText=prefs.getString("publishButtonText", ""); try { @@ -104,6 +110,7 @@ public class GlobalUserPreferences{ .putInt("theme", theme.ordinal()) .putString("color", color.name()) .putString("recentLanguages", gson.toJson(recentLanguages)) + .putString("recentEmojis", gson.toJson(recentEmojis)) .apply(); } 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 995a1c924..dfee4ecdc 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java @@ -1,19 +1,26 @@ package org.joinmastodon.android.ui; +import static org.joinmastodon.android.GlobalUserPreferences.recentEmojis; + import android.annotation.SuppressLint; import android.app.Activity; -import android.content.res.TypedArray; import android.graphics.Rect; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.text.TextUtils; +import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.GridLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + import com.squareup.otto.Subscribe; +import org.joinmastodon.android.GlobalUserPreferences; import org.joinmastodon.android.R; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.events.EmojiUpdatedEvent; @@ -21,13 +28,14 @@ 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.Map; +import java.util.Optional; import java.util.function.Consumer; import java.util.stream.Collectors; -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.GridLayoutManager; -import androidx.recyclerview.widget.RecyclerView; import me.grishka.appkit.imageloader.ImageLoaderRecyclerAdapter; import me.grishka.appkit.imageloader.ImageLoaderViewHolder; import me.grishka.appkit.imageloader.ListImageLoaderWrapper; @@ -40,6 +48,8 @@ import me.grishka.appkit.utils.V; import me.grishka.appkit.views.UsableRecyclerView; public class CustomEmojiPopupKeyboard extends PopupKeyboard{ + private static final int NEW_RECENT=15; + private List emojis; private UsableRecyclerView list; private ListImageLoaderWrapper imgLoader; @@ -82,6 +92,17 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ list.setLayoutManager(lm); imgLoader=new ListImageLoaderWrapper(activity, list, new RecyclerViewDelegate(list), null); + // inject category with last used emojis + if (!recentEmojis.isEmpty()) { + List allAvailableEmojis = emojis.stream().flatMap(category -> category.emojis.stream()).collect(Collectors.toList()); + List recentEmojiList = new ArrayList<>(); + for (String emojiCode : recentEmojis.keySet().stream().sorted(Comparator.comparingInt(GlobalUserPreferences.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.sk_emoji_recent), recentEmojiList)); + } + for(EmojiCategory category:emojis) adapter.addAdapter(new SingleCategoryAdapter(category)); list.setAdapter(adapter); @@ -100,6 +121,11 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ list.setBackgroundColor(UiUtils.getThemeColor(activity, android.R.attr.colorBackground)); list.setSelector(null); + //remove recently used afterwards, it would get duplicated otherwise + if (!recentEmojis.isEmpty()) { + emojis.remove(0); + } + return list; } @@ -107,6 +133,20 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ this.listener=listener; } + private void increaseEmojiCount(Emoji emoji) { + Integer usageCount = recentEmojis.get(emoji.shortcode); + if (usageCount != null) { + recentEmojis.put(emoji.shortcode, usageCount + 1); + } else { + recentEmojis.put(emoji.shortcode, NEW_RECENT); + } + + Log.e("Emojis", "increaseEmojiCount: " + recentEmojis); + recentEmojis.entrySet().removeIf(e -> e.getValue() <= 0); + recentEmojis.replaceAll((k, v) -> v - 1); + GlobalUserPreferences.save(); + } + @SuppressLint("NotifyDataSetChanged") @Subscribe public void onEmojiUpdated(EmojiUpdatedEvent ev){ @@ -203,6 +243,7 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ @Override public void onClick(){ + increaseEmojiCount(item); listener.accept(item); } } diff --git a/mastodon/src/main/res/values/strings_sk.xml b/mastodon/src/main/res/values/strings_sk.xml index 23fa73371..d10643027 100644 --- a/mastodon/src/main/res/values/strings_sk.xml +++ b/mastodon/src/main/res/values/strings_sk.xml @@ -130,4 +130,5 @@ Don’t schedule Don’t draft Reduce motion in animations + Recently used \ No newline at end of file From 8b12fac766f7e9e9715edd91ae709d238a0a7e14 Mon Sep 17 00:00:00 2001 From: FineFindus <63370021+FineFindus@users.noreply.github.com> Date: Mon, 2 Jan 2023 16:47:41 +0100 Subject: [PATCH 2/4] feat(emoji/recent): add clear recent emoji setting --- .../org/joinmastodon/android/fragments/SettingsFragment.java | 4 ++++ mastodon/src/main/res/values/strings_sk.xml | 1 + 2 files changed, 5 insertions(+) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java index 92dbca76c..d64d291f5 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/SettingsFragment.java @@ -273,6 +273,10 @@ public class SettingsFragment extends MastodonToolbarFragment{ GlobalUserPreferences.recentLanguages.remove(accountID); GlobalUserPreferences.save(); }))); + items.add(new TextItem(R.string.sk_clear_recent_emoji, ()-> { + GlobalUserPreferences.recentEmojis.clear(); + GlobalUserPreferences.save(); + })); // items.add(new TextItem(R.string.log_out, this::confirmLogOut)); items.add(new FooterItem(getString(R.string.sk_settings_app_version, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE))); diff --git a/mastodon/src/main/res/values/strings_sk.xml b/mastodon/src/main/res/values/strings_sk.xml index d10643027..47f7cfccb 100644 --- a/mastodon/src/main/res/values/strings_sk.xml +++ b/mastodon/src/main/res/values/strings_sk.xml @@ -131,4 +131,5 @@ Don’t draft Reduce motion in animations Recently used + Clear recently used emoji \ No newline at end of file From 42fcd6df510ecfbe62c10344cf3cbfd3374355d9 Mon Sep 17 00:00:00 2001 From: FineFindus <63370021+FineFindus@users.noreply.github.com> Date: Mon, 2 Jan 2023 16:58:31 +0100 Subject: [PATCH 3/4] refactor(emoji/recent): rename NEW_RECENT to NEW_RECENT_VALUE --- .../joinmastodon/android/ui/CustomEmojiPopupKeyboard.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 dfee4ecdc..693683399 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java @@ -48,7 +48,8 @@ import me.grishka.appkit.utils.V; import me.grishka.appkit.views.UsableRecyclerView; public class CustomEmojiPopupKeyboard extends PopupKeyboard{ - private static final int NEW_RECENT=15; + //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; @@ -138,10 +139,9 @@ public class CustomEmojiPopupKeyboard extends PopupKeyboard{ if (usageCount != null) { recentEmojis.put(emoji.shortcode, usageCount + 1); } else { - recentEmojis.put(emoji.shortcode, NEW_RECENT); + recentEmojis.put(emoji.shortcode, NEW_RECENT_VALUE); } - Log.e("Emojis", "increaseEmojiCount: " + recentEmojis); recentEmojis.entrySet().removeIf(e -> e.getValue() <= 0); recentEmojis.replaceAll((k, v) -> v - 1); GlobalUserPreferences.save(); From 1b02af382c83712c60442f74bff109b9e9de3e98 Mon Sep 17 00:00:00 2001 From: FineFindus <63370021+FineFindus@users.noreply.github.com> Date: Mon, 2 Jan 2023 19:43:30 +0100 Subject: [PATCH 4/4] fix(emoji/recent): remove unused imports --- .../java/org/joinmastodon/android/GlobalUserPreferences.java | 2 -- .../org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java index 9dbfb1fe2..c72c63861 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java +++ b/mastodon/src/main/java/org/joinmastodon/android/GlobalUserPreferences.java @@ -9,8 +9,6 @@ import android.os.Build; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; -import org.joinmastodon.android.model.Emoji; - import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; 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 693683399..ad469ea87 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/CustomEmojiPopupKeyboard.java @@ -8,7 +8,6 @@ import android.graphics.Rect; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.text.TextUtils; -import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; @@ -31,7 +30,6 @@ import org.joinmastodon.android.ui.utils.UiUtils; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.function.Consumer; import java.util.stream.Collectors;