From c2c2d131c8714d906fa16d55c5f39ab130f67780 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 18 Sep 2021 19:08:58 +0200 Subject: [PATCH] Filter on junk contacts --- .../eu/faircode/email/AdapterContact.java | 24 +++++++++++++++--- .../eu/faircode/email/FragmentContacts.java | 25 ++++++++++++++++++- app/src/main/res/menu/menu_contacts.xml | 6 +++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterContact.java b/app/src/main/java/eu/faircode/email/AdapterContact.java index 111c892aa2..cca70df14c 100644 --- a/app/src/main/java/eu/faircode/email/AdapterContact.java +++ b/app/src/main/java/eu/faircode/email/AdapterContact.java @@ -77,6 +77,8 @@ public class AdapterContact extends RecyclerView.Adapter types = new ArrayList<>(); + private List all = new ArrayList<>(); private List selected = new ArrayList<>(); @@ -369,17 +371,28 @@ public class AdapterContact extends RecyclerView.Adapter contacts) { - Log.i("Set contacts=" + contacts.size() + " search=" + search); + Log.i("Set contacts=" + contacts.size() + + " search=" + search + " types=" + types.size()); all = contacts; + List 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 = all; + items = filtered; else { items = new ArrayList<>(); String query = search.toLowerCase().trim(); - for (TupleContactEx contact : contacts) + for (TupleContactEx contact : filtered) if (contact.email.toLowerCase().contains(query) || (contact.name != null && contact.name.toLowerCase().contains(query))) items.add(contact); @@ -419,6 +432,11 @@ public class AdapterContact extends RecyclerView.Adapter types) { + this.types = types; + set(all); + } + private static class DiffCallback extends DiffUtil.Callback { private List prev = new ArrayList<>(); private List next = new ArrayList<>(); diff --git a/app/src/main/java/eu/faircode/email/FragmentContacts.java b/app/src/main/java/eu/faircode/email/FragmentContacts.java index 50628948a8..4e1e8caa3a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentContacts.java +++ b/app/src/main/java/eu/faircode/email/FragmentContacts.java @@ -44,6 +44,7 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class FragmentContacts extends FragmentBase { @@ -51,6 +52,7 @@ public class FragmentContacts extends FragmentBase { private ContentLoadingProgressBar pbWait; private Group grpReady; + private boolean junk = BuildConfig.DEBUG; private String searching = null; private AdapterContact adapter; @@ -85,6 +87,7 @@ public class FragmentContacts extends FragmentBase { @Override public void onSaveInstanceState(Bundle outState) { + outState.putBoolean("fair:junk", junk); outState.putString("fair:searching", searching); super.onSaveInstanceState(outState); } @@ -93,8 +96,11 @@ public class FragmentContacts extends FragmentBase { public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - if (savedInstanceState != null) + if (savedInstanceState != null) { + junk = savedInstanceState.getBoolean("fair:junk"); searching = savedInstanceState.getString("fair:searching"); + } + onMenuJunk(junk); adapter.search(searching); DB db = DB.getInstance(getContext()); @@ -148,6 +154,12 @@ public class FragmentContacts extends FragmentBase { super.onCreateOptionsMenu(menu, inflater); } + @Override + public void onPrepareOptionsMenu(@NonNull Menu menu) { + super.onPrepareOptionsMenu(menu); + menu.findItem(R.id.menu_junk).setChecked(junk); + } + @Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); @@ -157,6 +169,10 @@ public class FragmentContacts extends FragmentBase { } else if (itemId == R.id.menu_delete) { new FragmentDelete().show(getParentFragmentManager(), "contacts:delete"); return true; + } else if (itemId == R.id.menu_junk) { + item.setChecked(!item.isChecked()); + onMenuJunk(item.isChecked()); + return true; } return super.onOptionsItemSelected(item); } @@ -165,6 +181,13 @@ public class FragmentContacts extends FragmentBase { Helper.viewFAQ(getContext(), 84); } + private void onMenuJunk(boolean junk) { + this.junk = junk; + adapter.filter(junk + ? Arrays.asList(EntityContact.TYPE_JUNK, EntityContact.TYPE_NO_JUNK) + : new ArrayList<>()); + } + public static class FragmentDelete extends FragmentDialogBase { @NonNull @Override diff --git a/app/src/main/res/menu/menu_contacts.xml b/app/src/main/res/menu/menu_contacts.xml index 86b49c0296..872e1aafc9 100644 --- a/app/src/main/res/menu/menu_contacts.xml +++ b/app/src/main/res/menu/menu_contacts.xml @@ -19,4 +19,10 @@ android:icon="@drawable/twotone_delete_24" android:title="@string/title_delete" app:showAsAction="ifRoom" /> + + \ No newline at end of file