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; - items = 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); + + 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 @@ + + + +