Cache key/position pairs

This commit is contained in:
M66B 2019-08-11 16:08:53 +02:00
parent 8b216aa79a
commit 2b00f53c46
1 changed files with 29 additions and 21 deletions

View File

@ -128,7 +128,9 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import javax.mail.Address; import javax.mail.Address;
@ -191,6 +193,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private int answers = -1; private int answers = -1;
private boolean gotoTop = false; private boolean gotoTop = false;
private AsyncPagedListDiffer<TupleMessageEx> differ; private AsyncPagedListDiffer<TupleMessageEx> differ;
private Map<Long, Integer> keyPosition = new HashMap<>();
private SelectionTracker<Long> selectionTracker = null; private SelectionTracker<Long> selectionTracker = null;
enum ViewType {UNIFIED, FOLDER, THREAD, SEARCH} enum ViewType {UNIFIED, FOLDER, THREAD, SEARCH}
@ -3015,6 +3018,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
void submitList(PagedList<TupleMessageEx> list) { void submitList(PagedList<TupleMessageEx> list) {
keyPosition.clear();
differ.submitList(list); differ.submitList(list);
} }
@ -3375,23 +3379,32 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
int getPositionForKey(long key) { int getPositionForKey(long key) {
if (keyPosition.isEmpty()) {
PagedList<TupleMessageEx> messages = getCurrentList(); PagedList<TupleMessageEx> messages = getCurrentList();
if (messages != null) if (messages != null) {
for (int i = 0; i < messages.size(); i++) { for (int i = 0; i < messages.size(); i++) {
TupleMessageEx message = messages.get(i); TupleMessageEx message = messages.get(i);
if (message != null && message.id.equals(key)) { if (message != null)
Log.d("Position=" + i + " @Key=" + key); keyPosition.put(message.id, i);
return 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); Log.i("Position=" + RecyclerView.NO_POSITION + " @Key=" + key);
return RecyclerView.NO_POSITION; return RecyclerView.NO_POSITION;
} }
TupleMessageEx getItemAtPosition(int pos) { TupleMessageEx getItemAtPosition(int pos) {
PagedList<TupleMessageEx> list = getCurrentList(); PagedList<TupleMessageEx> messages = getCurrentList();
if (list != null && pos >= 0 && pos < list.size()) { if (messages != null && pos >= 0 && pos < messages.size()) {
TupleMessageEx message = list.get(pos); TupleMessageEx message = messages.get(pos);
Long key = (message == null ? null : message.id); Long key = (message == null ? null : message.id);
Log.d("Item=" + key + " @Position=" + pos); Log.d("Item=" + key + " @Position=" + pos);
return message; return message;
@ -3402,17 +3415,12 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} }
TupleMessageEx getItemForKey(long key) { TupleMessageEx getItemForKey(long key) {
PagedList<TupleMessageEx> messages = getCurrentList(); int pos = getPositionForKey(key);
if (messages != null) if (pos == RecyclerView.NO_POSITION) {
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); Log.d("Item=" + null + " @Key=" + key);
return null; return null;
} else
return getItemAtPosition(pos);
} }
Long getKeyAtPosition(int pos) { Long getKeyAtPosition(int pos) {