From a5d8bada63153ff9584b1ffb5433cdb062441c68 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sun, 8 Oct 2023 13:33:50 +0200 Subject: [PATCH] Add search in groups --- .../code_guard/MainActivity.java | 39 ++++++++++++++++--- .../code_guard/fragment/GroupFragment.java | 4 ++ .../code_guard/otplist/OTPListAdapter.java | 30 ++++++++++++-- .../main/res/drawable/baseline_search_24.xml | 5 +++ app/src/main/res/menu/menu_otps.xml | 6 +++ 5 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/drawable/baseline_search_24.xml diff --git a/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java b/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java index ccb86fb..d456f36 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java +++ b/app/src/main/java/com/cringe_studios/code_guard/MainActivity.java @@ -13,6 +13,7 @@ import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.widget.SearchView; import androidx.core.util.Consumer; import androidx.fragment.app.Fragment; @@ -255,12 +256,16 @@ public class MainActivity extends BaseActivity { ActionBar bar = getSupportActionBar(); if(bar != null) bar.setTitle(((NamedFragment) fragment).getName()); }else { - List groups = SettingsUtil.getGroups(this); - if(!groups.isEmpty()) { - Bundle bundle = new Bundle(); - bundle.putString(GroupFragment.BUNDLE_GROUP, SettingsUtil.getGroups(this).get(0)); - NavigationUtil.navigate(this, GroupFragment.class, bundle); - } + navigateToMainGroup(); + } + } + + private void navigateToMainGroup() { + List groups = SettingsUtil.getGroups(this); + if(!groups.isEmpty()) { + Bundle bundle = new Bundle(); + bundle.putString(GroupFragment.BUNDLE_GROUP, SettingsUtil.getGroups(this).get(0)); + NavigationUtil.navigate(this, GroupFragment.class, bundle); } } @@ -279,6 +284,23 @@ public class MainActivity extends BaseActivity { menu.removeItem(R.id.action_view_otp); menu.removeItem(R.id.action_edit_otp); } + + if(!frag.isEditing()) { + MenuItem search = menu.findItem(R.id.action_search); + SearchView v = (SearchView) search.getActionView(); + v.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + return true; + } + + @Override + public boolean onQueryTextChange(String newText) { + frag.filter(newText); + return true; + } + }); + } return true; } @@ -328,6 +350,11 @@ public class MainActivity extends BaseActivity { return; } + if(!(fragment instanceof GroupFragment)) { + navigateToMainGroup(); + return; + } + if(System.currentTimeMillis() - backLastPressed < BACK_BUTTON_DELAY) { finishAffinity(); }else { diff --git a/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java b/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java index 897c1b1..b4026c9 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java +++ b/app/src/main/java/com/cringe_studios/code_guard/fragment/GroupFragment.java @@ -186,6 +186,10 @@ public class GroupFragment extends NamedFragment { }, null, null); } + public void filter(String newText) { + otpListAdapter.filter(newText); + } + public boolean isEditing() { return otpListAdapter.isEditing(); } diff --git a/app/src/main/java/com/cringe_studios/code_guard/otplist/OTPListAdapter.java b/app/src/main/java/com/cringe_studios/code_guard/otplist/OTPListAdapter.java index c1891b8..03b6ad9 100644 --- a/app/src/main/java/com/cringe_studios/code_guard/otplist/OTPListAdapter.java +++ b/app/src/main/java/com/cringe_studios/code_guard/otplist/OTPListAdapter.java @@ -36,6 +36,8 @@ public class OTPListAdapter extends RecyclerView.Adapter { private final List items; + private List filteredItems; + private final Handler handler; private final Runnable saveOTPs; @@ -47,6 +49,7 @@ public class OTPListAdapter extends RecyclerView.Adapter { this.recyclerView = recyclerView; this.inflater = LayoutInflater.from(context); this.items = new ArrayList<>(); + this.filteredItems = items; this.handler = new Handler(Looper.getMainLooper()); this.saveOTPs = saveOTPs; @@ -62,7 +65,7 @@ public class OTPListAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(@NonNull OTPListItem holder, int position) { - OTPData data = items.get(position); + OTPData data = filteredItems.get(position); holder.setOTPData(data); holder.setSelected(false); @@ -132,7 +135,7 @@ public class OTPListAdapter extends RecyclerView.Adapter { @Override public int getItemCount() { - return items.size(); + return filteredItems.size(); } public List getItems() { @@ -200,7 +203,7 @@ public class OTPListAdapter extends RecyclerView.Adapter { item.setCodeShown(true); } - private List getCodes() { + public List getCodes() { List is = new ArrayList<>(); for(int i = 0; i < items.size(); i++) { OTPListItem vh = (OTPListItem) recyclerView.findViewHolderForAdapterPosition(i); @@ -210,6 +213,27 @@ public class OTPListAdapter extends RecyclerView.Adapter { return is; } + public void filter(String query) { + if(isEditing()) return; + + if(query == null || query.isEmpty()) { + filteredItems = items; + notifyDataSetChanged(); + return; + } + + query = query.toLowerCase(); + + List filtered = new ArrayList<>(); + for(OTPData d : items) { + if(d.getName().toLowerCase().contains(query) + || d.getIssuer().toLowerCase().contains(query)) filtered.add(d); + } + + filteredItems = filtered; + notifyDataSetChanged(); + } + private void attachTouchHelper(RecyclerView view) { new ItemTouchHelper(new OTPListAdapter.TouchHelperCallback()).attachToRecyclerView(view); } diff --git a/app/src/main/res/drawable/baseline_search_24.xml b/app/src/main/res/drawable/baseline_search_24.xml new file mode 100644 index 0000000..73e716a --- /dev/null +++ b/app/src/main/res/drawable/baseline_search_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/menu/menu_otps.xml b/app/src/main/res/menu/menu_otps.xml index 9148fa0..2570f3a 100644 --- a/app/src/main/res/menu/menu_otps.xml +++ b/app/src/main/res/menu/menu_otps.xml @@ -1,6 +1,12 @@ +