From 2d1def0c1931428c84c4ea75a8510e49d6d3716c Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 26 Feb 2021 15:18:08 +0100 Subject: [PATCH] Added template search --- .../java/eu/faircode/email/AdapterAnswer.java | 39 ++++++++++++--- .../eu/faircode/email/FragmentAnswers.java | 49 +++++++++++++++++++ app/src/main/res/menu/menu_answers.xml | 10 ++++ 3 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/menu/menu_answers.xml diff --git a/app/src/main/java/eu/faircode/email/AdapterAnswer.java b/app/src/main/java/eu/faircode/email/AdapterAnswer.java index c4563130cf..c4096adb8d 100644 --- a/app/src/main/java/eu/faircode/email/AdapterAnswer.java +++ b/app/src/main/java/eu/faircode/email/AdapterAnswer.java @@ -57,7 +57,9 @@ public class AdapterAnswer extends RecyclerView.Adapter items = new ArrayList<>(); + private String search = null; + private List all = new ArrayList<>(); + private List selected = new ArrayList<>(); private boolean composable = false; @@ -105,7 +107,7 @@ public class AdapterAnswer extends RecyclerView.Adapter answers) { Log.i("Set answers=" + answers.size()); - DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, answers), false); + all = answers; + + List items; + if (TextUtils.isEmpty(search)) + items = all; + else { + items = new ArrayList<>(); + String query = search.toLowerCase().trim(); + for (EntityAnswer answer : answers) { + if (answer.name.toLowerCase().contains(query) || + (answer.group != null && answer.group.toLowerCase().contains(query)) || + answer.text.toLowerCase().contains(query)) + items.add(answer); + } + } + + DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false); - items = answers; + selected = items; diff.dispatchUpdatesTo(new ListUpdateCallback() { @Override @@ -263,6 +281,11 @@ public class AdapterAnswer extends RecyclerView.Adapter prev = new ArrayList<>(); private List next = new ArrayList<>(); @@ -299,12 +322,12 @@ public class AdapterAnswer extends RecyclerView.Adapter>() { @Override @@ -130,4 +147,36 @@ public class FragmentAnswers extends FragmentBase { } }); } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.menu_answers, menu); + + MenuItem menuSearch = menu.findItem(R.id.menu_search); + SearchView searchView = (SearchView) menuSearch.getActionView(); + searchView.setQueryHint(getString(R.string.title_rules_search_hint)); + + if (!TextUtils.isEmpty(searching)) { + menuSearch.expandActionView(); + searchView.setQuery(searching, true); + } + + searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextChange(String newText) { + searching = newText; + adapter.search(newText); + return true; + } + + @Override + public boolean onQueryTextSubmit(String query) { + searching = query; + adapter.search(query); + return true; + } + }); + + super.onCreateOptionsMenu(menu, inflater); + } } diff --git a/app/src/main/res/menu/menu_answers.xml b/app/src/main/res/menu/menu_answers.xml new file mode 100644 index 0000000000..6ef892b4f3 --- /dev/null +++ b/app/src/main/res/menu/menu_answers.xml @@ -0,0 +1,10 @@ + + + +