Filter contacts on background

This commit is contained in:
M66B 2021-09-29 08:27:29 +02:00
parent b00399dc04
commit 47d757066d
1 changed files with 59 additions and 40 deletions

View File

@ -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<AdapterContact.ViewHolder> {
private Fragment parentFragment;
@ -84,6 +85,9 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
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 {
private View view;
private ImageView ivType;
@ -377,54 +381,69 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
all = contacts;
List<TupleContactEx> filtered;
if (types.size() == 0)
filtered = all;
else {
filtered = new ArrayList<>();
for (TupleContactEx contact : all)
if (types.contains(contact.type))
filtered.add(contact);
}
List<TupleContactEx> 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<List<TupleContactEx>>() {
@Override
public void onInserted(int position, int count) {
Log.d("Inserted @" + position + " #" + count);
protected List<TupleContactEx> onExecute(Context context, Bundle args) throws Throwable {
List<TupleContactEx> filtered;
if (types.size() == 0)
filtered = contacts;
else {
filtered = new ArrayList<>();
for (TupleContactEx contact : contacts)
if (types.contains(contact.type))
filtered.add(contact);
}
List<TupleContactEx> 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<TupleContactEx> 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) {