Skip to content

Commit

Permalink
Store recent reactions separately from keyboard emoji.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed May 29, 2020
1 parent d70c33d commit 70c88b6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
Expand Up @@ -29,6 +29,8 @@ public class EmojiKeyboardProvider implements MediaKeyboardProvider,
{
private static final KeyEvent DELETE_KEY_EVENT = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);

private static final String RECENT_STORAGE_KEY = "pref_recent_emoji2";

private final Context context;
private final List<EmojiPageModel> models;
private final RecentEmojiPageModel recentModel;
Expand All @@ -41,7 +43,7 @@ public EmojiKeyboardProvider(@NonNull Context context, @Nullable EmojiEventListe
this.context = context;
this.emojiEventListener = emojiEventListener;
this.models = new LinkedList<>();
this.recentModel = new RecentEmojiPageModel(context);
this.recentModel = new RecentEmojiPageModel(context, RECENT_STORAGE_KEY);
this.emojiPagerAdapter = new EmojiPagerAdapter(context, models, new EmojiEventListener() {
@Override
public void onEmojiSelected(String emoji) {
Expand Down
Expand Up @@ -22,20 +22,21 @@
import java.util.List;

public class RecentEmojiPageModel implements EmojiPageModel {
private static final String TAG = RecentEmojiPageModel.class.getSimpleName();
private static final String EMOJI_LRU_PREFERENCE = "pref_recent_emoji2";
private static final int EMOJI_LRU_SIZE = 50;
private static final String TAG = RecentEmojiPageModel.class.getSimpleName();
private static final int EMOJI_LRU_SIZE = 50;

private final SharedPreferences prefs;
private final String preferenceName;
private final LinkedHashSet<String> recentlyUsed;

public RecentEmojiPageModel(Context context) {
this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.recentlyUsed = getPersistedCache();
public RecentEmojiPageModel(Context context, @NonNull String preferenceName) {
this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.preferenceName = preferenceName;
this.recentlyUsed = getPersistedCache();
}

private LinkedHashSet<String> getPersistedCache() {
String serialized = prefs.getString(EMOJI_LRU_PREFERENCE, "[]");
String serialized = prefs.getString(preferenceName, "[]");
try {
CollectionType collectionType = TypeFactory.defaultInstance()
.constructCollectionType(LinkedHashSet.class, String.class);
Expand Down Expand Up @@ -90,7 +91,7 @@ protected Void doInBackground(Void... params) {
try {
String serialized = JsonUtils.toJson(latestRecentlyUsed);
prefs.edit()
.putString(EMOJI_LRU_PREFERENCE, serialized)
.putString(preferenceName, serialized)
.apply();
} catch (IOException e) {
Log.w(TAG, e);
Expand Down
@@ -1,12 +1,9 @@
package org.thoughtcrime.securesms.reactions.any;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.util.Consumer;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import org.thoughtcrime.securesms.components.emoji.EmojiKeyboardProvider;
import org.thoughtcrime.securesms.components.emoji.EmojiPageModel;
Expand Down
Expand Up @@ -17,13 +17,15 @@

final class ReactWithAnyEmojiRepository {

private static final String RECENT_STORAGE_KEY = "reactions_recent_emoji";

private final Context context;
private final RecentEmojiPageModel recentEmojiPageModel;
private final List<EmojiPageModel> emojiPageModels;

ReactWithAnyEmojiRepository(@NonNull Context context) {
this.context = context;
this.recentEmojiPageModel = new RecentEmojiPageModel(context);
this.recentEmojiPageModel = new RecentEmojiPageModel(context, RECENT_STORAGE_KEY);
this.emojiPageModels = new LinkedList<>();

emojiPageModels.add(recentEmojiPageModel);
Expand Down

0 comments on commit 70c88b6

Please sign in to comment.