From 2b00f53c46fa1bedf8c1bd8f3e92c7b6c7debcfd Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 11 Aug 2019 16:08:53 +0200 Subject: [PATCH] Cache key/position pairs --- .../eu/faircode/email/AdapterMessage.java | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index bb58623587..810d48640f 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -128,7 +128,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import javax.mail.Address; @@ -191,6 +193,7 @@ public class AdapterMessage extends RecyclerView.Adapter differ; + private Map keyPosition = new HashMap<>(); private SelectionTracker selectionTracker = null; enum ViewType {UNIFIED, FOLDER, THREAD, SEARCH} @@ -3015,6 +3018,7 @@ public class AdapterMessage extends RecyclerView.Adapter list) { + keyPosition.clear(); differ.submitList(list); } @@ -3375,23 +3379,32 @@ public class AdapterMessage extends RecyclerView.Adapter messages = getCurrentList(); - if (messages != null) - for (int i = 0; i < messages.size(); i++) { - TupleMessageEx message = messages.get(i); - if (message != null && message.id.equals(key)) { - Log.d("Position=" + i + " @Key=" + key); - return i; + if (keyPosition.isEmpty()) { + PagedList messages = getCurrentList(); + if (messages != null) { + for (int i = 0; i < messages.size(); i++) { + TupleMessageEx message = messages.get(i); + if (message != null) + keyPosition.put(message.id, i); } + Log.i("Mapped keys=" + keyPosition.size()); } + } + + if (keyPosition.containsKey(key)) { + int pos = keyPosition.get(key); + Log.d("Position=" + pos + " @Key=" + key); + return pos; + } + Log.i("Position=" + RecyclerView.NO_POSITION + " @Key=" + key); return RecyclerView.NO_POSITION; } TupleMessageEx getItemAtPosition(int pos) { - PagedList list = getCurrentList(); - if (list != null && pos >= 0 && pos < list.size()) { - TupleMessageEx message = list.get(pos); + PagedList messages = getCurrentList(); + if (messages != null && pos >= 0 && pos < messages.size()) { + TupleMessageEx message = messages.get(pos); Long key = (message == null ? null : message.id); Log.d("Item=" + key + " @Position=" + pos); return message; @@ -3402,17 +3415,12 @@ public class AdapterMessage extends RecyclerView.Adapter messages = getCurrentList(); - if (messages != null) - for (int i = 0; i < messages.size(); i++) { - TupleMessageEx message = messages.get(i); - if (message != null && message.id.equals(key)) { - Log.d("Item=" + message.id + " @Key=" + key); - return message; - } - } - Log.d("Item=" + null + " @Key=" + key); - return null; + int pos = getPositionForKey(key); + if (pos == RecyclerView.NO_POSITION) { + Log.d("Item=" + null + " @Key=" + key); + return null; + } else + return getItemAtPosition(pos); } Long getKeyAtPosition(int pos) {