From 6b27050e5c326e5bdbe1b9992182d97bcda483a0 Mon Sep 17 00:00:00 2001 From: amoohashem Date: Sat, 24 Apr 2021 03:00:00 +0430 Subject: [PATCH] Replace Hashmap with SparseArray in PeekView.java and HistoryPosts.java --- .../redditslide/Adapters/HistoryPosts.java | 26 ++++++++----------- .../redditslide/ForceTouch/PeekView.java | 25 +++++++++--------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/me/ccrama/redditslide/Adapters/HistoryPosts.java b/app/src/main/java/me/ccrama/redditslide/Adapters/HistoryPosts.java index c7ef65c803..9a82c07e1b 100644 --- a/app/src/main/java/me/ccrama/redditslide/Adapters/HistoryPosts.java +++ b/app/src/main/java/me/ccrama/redditslide/Adapters/HistoryPosts.java @@ -1,6 +1,7 @@ package me.ccrama.redditslide.Adapters; import android.os.AsyncTask; +import android.util.SparseArray; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; @@ -12,10 +13,7 @@ import net.dean.jraw.paginators.FullnamesPaginator; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; -import java.util.TreeMap; import me.ccrama.redditslide.Authentication; import me.ccrama.redditslide.PostMatch; @@ -24,9 +22,9 @@ * Created by ccrama on 9/17/2015. */ public class HistoryPosts extends GeneralPosts { - private SwipeRefreshLayout refreshLayout; + private SwipeRefreshLayout refreshLayout; private ContributionAdapter adapter; - public boolean loading; + public boolean loading; String prefix = ""; public HistoryPosts() { @@ -112,7 +110,7 @@ protected ArrayList doInBackground(String... subredditPaginators) try { if (reset || paginator == null) { ArrayList ids = new ArrayList<>(); - HashMap idsSorted = new HashMap<>(); + SparseArray idsSorted = new SparseArray<>(); Map values; if (prefix.isEmpty()) { values = KVStore.getInstance().getByContains(""); @@ -134,29 +132,27 @@ protected ArrayList doInBackground(String... subredditPaginators) ids.add("t3_" + entry.getKey()); } else if (done instanceof Long) { if (entry.getKey().contains("_")) { - idsSorted.put((Long) done, entry.getKey()); + idsSorted.put((int) done, entry.getKey()); } else { - idsSorted.put((Long) done, "t3_" + entry.getKey()); + idsSorted.put((int) done, "t3_" + entry.getKey()); } } } } else { String key = entry.getKey(); - if(!key.contains("_")){ + if (!key.contains("_")) { key = "t3_" + key; } - idsSorted.put((Long) done, key.replace(prefix, "")); + idsSorted.put((int) done, key.replace(prefix, "")); } } - if (!idsSorted.isEmpty()) { - TreeMap result2 = new TreeMap<>(Collections.reverseOrder()); - result2.putAll(idsSorted); - ids.addAll(0, result2.values()); + for (int i = 0; i < idsSorted.size(); i++) { + ids.add(0, idsSorted.valueAt(i)); } paginator = new FullnamesPaginator(Authentication.reddit, - ids.toArray(new String[ids.size()-1])); + ids.toArray(new String[ids.size() - 1])); } diff --git a/app/src/main/java/me/ccrama/redditslide/ForceTouch/PeekView.java b/app/src/main/java/me/ccrama/redditslide/ForceTouch/PeekView.java index abfe12d438..930999ae0f 100644 --- a/app/src/main/java/me/ccrama/redditslide/ForceTouch/PeekView.java +++ b/app/src/main/java/me/ccrama/redditslide/ForceTouch/PeekView.java @@ -8,6 +8,7 @@ import android.graphics.Color; import android.graphics.Point; import android.graphics.Rect; +import android.util.SparseArray; import android.view.Display; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -24,9 +25,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import java.util.HashMap; -import java.util.Map; - import jp.wasabeef.blurry.Blurry; import me.ccrama.redditslide.ForceTouch.builder.PeekViewOptions; import me.ccrama.redditslide.ForceTouch.callback.OnButtonUp; @@ -96,12 +94,13 @@ public void onAnimationUpdate(ValueAnimator valueAnimator){ return; } } - for (Integer i : buttons.keySet()) { - final View v = content.findViewById(i); + for (int i = 0; i < buttons.size(); i++){ + int key = buttons.keyAt(i); + final View v = content.findViewById(key); Rect outRect = new Rect(); v.getGlobalVisibleRect(outRect); - if(outRect.contains((int) event.getX(), (int) event.getY()) && i != currentHighlight){ - currentHighlight = i; + if(outRect.contains((int) event.getX(), (int) event.getY()) && key != currentHighlight){ + currentHighlight = key; ValueAnimator animator = ValueAnimator.ofInt(eight * 2, eight); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override @@ -135,15 +134,17 @@ public PeekView(Activity context, PeekViewOptions options, @NonNull View content init(context, options, content, callbacks); } - HashMap buttons = new HashMap<>(); + SparseArray buttons = new SparseArray<>(); public void checkButtons(MotionEvent event) { - for (Map.Entry entry : buttons.entrySet()) { - View v = content.findViewById(entry.getKey()); + for (int i = 0; i < buttons.size(); i++) { + int key = buttons.keyAt(i); + OnButtonUp value = buttons.get(key); + View v = content.findViewById(key); Rect outRect = new Rect(); v.getGlobalVisibleRect(outRect); - if(outRect.contains((int) event.getX(), (int) event.getY())){ - entry.getValue().onButtonUp(); + if (outRect.contains((int) event.getX(), (int) event.getY())) { + value.onButtonUp(); } } }