Filter contacts on background

pull/206/head
M66B 3 years ago
parent b00399dc04
commit 47d757066d

@ -65,6 +65,7 @@ import java.io.InputStream;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService;
public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHolder> { public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHolder> {
private Fragment parentFragment; private Fragment parentFragment;
@ -84,6 +85,9 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
private NumberFormat NF = NumberFormat.getNumberInstance(); private NumberFormat NF = NumberFormat.getNumberInstance();
private static final ExecutorService executor =
Helper.getBackgroundExecutor(1, "contacts");
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private View view; private View view;
private ImageView ivType; private ImageView ivType;
@ -377,12 +381,15 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
all = contacts; all = contacts;
new SimpleTask<List<TupleContactEx>>() {
@Override
protected List<TupleContactEx> onExecute(Context context, Bundle args) throws Throwable {
List<TupleContactEx> filtered; List<TupleContactEx> filtered;
if (types.size() == 0) if (types.size() == 0)
filtered = all; filtered = contacts;
else { else {
filtered = new ArrayList<>(); filtered = new ArrayList<>();
for (TupleContactEx contact : all) for (TupleContactEx contact : contacts)
if (types.contains(contact.type)) if (types.contains(contact.type))
filtered.add(contact); filtered.add(contact);
} }
@ -399,6 +406,11 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
items.add(contact); items.add(contact);
} }
return items;
}
@Override
protected void onExecuted(Bundle args, List<TupleContactEx> items) {
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false); DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false);
selected = items; selected = items;
@ -424,7 +436,14 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
Log.d("Changed @" + position + " #" + count); Log.d("Changed @" + position + " #" + count);
} }
}); });
diff.dispatchUpdatesTo(this); diff.dispatchUpdatesTo(AdapterContact.this);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.setExecutor(executor).execute(context, owner, new Bundle(), "contacts:filter");
} }
public void search(String query) { public void search(String query) {

Loading…
Cancel
Save