From 47d757066d2b4d252f2effe08984e3e40601e1cc Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 29 Sep 2021 08:27:29 +0200 Subject: [PATCH] Filter contacts on background --- .../eu/faircode/email/AdapterContact.java | 99 +++++++++++-------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterContact.java b/app/src/main/java/eu/faircode/email/AdapterContact.java index 96bc0865ed..c2fa5c0250 100644 --- a/app/src/main/java/eu/faircode/email/AdapterContact.java +++ b/app/src/main/java/eu/faircode/email/AdapterContact.java @@ -65,6 +65,7 @@ import java.io.InputStream; import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutorService; public class AdapterContact extends RecyclerView.Adapter { private Fragment parentFragment; @@ -84,6 +85,9 @@ public class AdapterContact extends RecyclerView.Adapter filtered; - if (types.size() == 0) - filtered = all; - else { - filtered = new ArrayList<>(); - for (TupleContactEx contact : all) - if (types.contains(contact.type)) - filtered.add(contact); - } - - List items; - if (TextUtils.isEmpty(search)) - items = filtered; - else { - items = new ArrayList<>(); - String query = search.toLowerCase().trim(); - for (TupleContactEx contact : filtered) - if (contact.email.toLowerCase().contains(query) || - (contact.name != null && contact.name.toLowerCase().contains(query))) - items.add(contact); - } - - DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false); - - selected = items; - - diff.dispatchUpdatesTo(new ListUpdateCallback() { + new SimpleTask>() { @Override - public void onInserted(int position, int count) { - Log.d("Inserted @" + position + " #" + count); + protected List onExecute(Context context, Bundle args) throws Throwable { + List filtered; + if (types.size() == 0) + filtered = contacts; + else { + filtered = new ArrayList<>(); + for (TupleContactEx contact : contacts) + if (types.contains(contact.type)) + filtered.add(contact); + } + + List items; + if (TextUtils.isEmpty(search)) + items = filtered; + else { + items = new ArrayList<>(); + String query = search.toLowerCase().trim(); + for (TupleContactEx contact : filtered) + if (contact.email.toLowerCase().contains(query) || + (contact.name != null && contact.name.toLowerCase().contains(query))) + items.add(contact); + } + + return items; } @Override - public void onRemoved(int position, int count) { - Log.d("Removed @" + position + " #" + count); + protected void onExecuted(Bundle args, List items) { + DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false); + + selected = items; + + diff.dispatchUpdatesTo(new ListUpdateCallback() { + @Override + public void onInserted(int position, int count) { + Log.d("Inserted @" + position + " #" + count); + } + + @Override + public void onRemoved(int position, int count) { + Log.d("Removed @" + position + " #" + count); + } + + @Override + public void onMoved(int fromPosition, int toPosition) { + Log.d("Moved " + fromPosition + ">" + toPosition); + } + + @Override + public void onChanged(int position, int count, Object payload) { + Log.d("Changed @" + position + " #" + count); + } + }); + diff.dispatchUpdatesTo(AdapterContact.this); } @Override - public void onMoved(int fromPosition, int toPosition) { - Log.d("Moved " + fromPosition + ">" + toPosition); + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(parentFragment.getParentFragmentManager(), ex); } - - @Override - public void onChanged(int position, int count, Object payload) { - Log.d("Changed @" + position + " #" + count); - } - }); - diff.dispatchUpdatesTo(this); + }.setExecutor(executor).execute(context, owner, new Bundle(), "contacts:filter"); } public void search(String query) {