1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-26 17:57:16 +00:00

Added template search

This commit is contained in:
M66B 2021-02-26 15:18:08 +01:00
parent f572954419
commit 2d1def0c19
3 changed files with 90 additions and 8 deletions

View file

@ -57,7 +57,9 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
private LifecycleOwner owner;
private LayoutInflater inflater;
private List<EntityAnswer> items = new ArrayList<>();
private String search = null;
private List<EntityAnswer> all = new ArrayList<>();
private List<EntityAnswer> selected = new ArrayList<>();
private boolean composable = false;
@ -105,7 +107,7 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
if (pos == RecyclerView.NO_POSITION)
return;
EntityAnswer answer = items.get(pos);
EntityAnswer answer = selected.get(pos);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
@ -119,7 +121,7 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
if (pos == RecyclerView.NO_POSITION)
return false;
final EntityAnswer answer = items.get(pos);
final EntityAnswer answer = selected.get(pos);
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
@ -235,9 +237,25 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
public void set(@NonNull List<EntityAnswer> answers) {
Log.i("Set answers=" + answers.size());
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, answers), false);
all = answers;
items = answers;
List<EntityAnswer> 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<AdapterAnswer.ViewHolder
diff.dispatchUpdatesTo(this);
}
public void search(String query) {
search = query;
set(all);
}
private static class DiffCallback extends DiffUtil.Callback {
private List<EntityAnswer> prev = new ArrayList<>();
private List<EntityAnswer> next = new ArrayList<>();
@ -299,12 +322,12 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
@Override
public long getItemId(int position) {
return items.get(position).id;
return selected.get(position).id;
}
@Override
public int getItemCount() {
return items.size();
return selected.size();
}
@Override
@ -315,7 +338,7 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
EntityAnswer answer = items.get(position);
EntityAnswer answer = selected.get(position);
holder.powner.recreate(answer == null ? null : answer.id);
holder.unwire();

View file

@ -21,12 +21,17 @@ package eu.faircode.email;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;
@ -50,6 +55,7 @@ public class FragmentAnswers extends FragmentBase {
private Group grpReady;
private FloatingActionButton fab;
private String searching = null;
private AdapterAnswer adapter;
@Override
@ -112,10 +118,21 @@ public class FragmentAnswers extends FragmentBase {
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("fair:searching", searching);
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
searching = savedInstanceState.getString("fair:searching");
adapter.search(searching);
}
DB db = DB.getInstance(getContext());
db.answer().liveAnswers().observe(getViewLifecycleOwner(), new Observer<List<EntityAnswer>>() {
@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);
}
}

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_search"
android:icon="@drawable/twotone_search_24"
android:title="@string/title_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="collapseActionView|always" />
</menu>