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.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);
new SimpleTask<List<TupleContactEx>>() {
@Override
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);
}
selected = items;
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);
}
diff.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
Log.d("Inserted @" + position + " #" + count);
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);
@Override
public void onMoved(int fromPosition, int toPosition) {
Log.d("Moved " + fromPosition + ">" + toPosition);
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 onChanged(int position, int count, Object payload) {
Log.d("Changed @" + position + " #" + count);
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
});
diff.dispatchUpdatesTo(this);
}.setExecutor(executor).execute(context, owner, new Bundle(), "contacts:filter");
}
public void search(String query) {

Loading…
Cancel
Save