mirror of https://github.com/M66B/FairEmail.git
Added rule search
This commit is contained in:
parent
34575454f3
commit
23f3d29aa3
|
@ -56,6 +56,7 @@ import org.json.JSONObject;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
|
@ -67,7 +68,9 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
||||||
private LayoutInflater inflater;
|
private LayoutInflater inflater;
|
||||||
|
|
||||||
private int protocol = -1;
|
private int protocol = -1;
|
||||||
private List<TupleRuleEx> items = new ArrayList<>();
|
private String search = null;
|
||||||
|
private List<TupleRuleEx> all = new ArrayList<>();
|
||||||
|
private List<TupleRuleEx> selected = new ArrayList<>();
|
||||||
|
|
||||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
||||||
private View view;
|
private View view;
|
||||||
|
@ -189,7 +192,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
||||||
if (pos == RecyclerView.NO_POSITION)
|
if (pos == RecyclerView.NO_POSITION)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
TupleRuleEx rule = items.get(pos);
|
TupleRuleEx rule = selected.get(pos);
|
||||||
|
|
||||||
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
||||||
lbm.sendBroadcast(
|
lbm.sendBroadcast(
|
||||||
|
@ -206,7 +209,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
||||||
if (pos == RecyclerView.NO_POSITION)
|
if (pos == RecyclerView.NO_POSITION)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
final TupleRuleEx rule = items.get(pos);
|
final TupleRuleEx rule = selected.get(pos);
|
||||||
|
|
||||||
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
|
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
|
||||||
|
|
||||||
|
@ -417,9 +420,37 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
||||||
this.protocol = protocol;
|
this.protocol = protocol;
|
||||||
Log.i("Set protocol=" + protocol + " rules=" + rules.size());
|
Log.i("Set protocol=" + protocol + " rules=" + rules.size());
|
||||||
|
|
||||||
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, rules), false);
|
all = rules;
|
||||||
|
|
||||||
items = rules;
|
List<TupleRuleEx> items;
|
||||||
|
if (TextUtils.isEmpty(search))
|
||||||
|
items = all;
|
||||||
|
else {
|
||||||
|
items = new ArrayList<>();
|
||||||
|
String query = search.toLowerCase().trim();
|
||||||
|
for (TupleRuleEx rule : rules) {
|
||||||
|
if (rule.name.toLowerCase().contains(query))
|
||||||
|
items.add(rule);
|
||||||
|
else
|
||||||
|
try {
|
||||||
|
JSONObject jcondition = new JSONObject(rule.condition);
|
||||||
|
Iterator<String> keys = jcondition.keys();
|
||||||
|
while (keys.hasNext()) {
|
||||||
|
String key = keys.next();
|
||||||
|
if (jcondition.get(key).toString().toLowerCase().contains(query)) {
|
||||||
|
items.add(rule);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (JSONException ex) {
|
||||||
|
Log.e(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false);
|
||||||
|
|
||||||
|
selected = items;
|
||||||
|
|
||||||
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -445,6 +476,11 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
||||||
diff.dispatchUpdatesTo(this);
|
diff.dispatchUpdatesTo(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void search(String query) {
|
||||||
|
search = query;
|
||||||
|
set(protocol, all);
|
||||||
|
}
|
||||||
|
|
||||||
private class DiffCallback extends DiffUtil.Callback {
|
private class DiffCallback extends DiffUtil.Callback {
|
||||||
private List<TupleRuleEx> prev = new ArrayList<>();
|
private List<TupleRuleEx> prev = new ArrayList<>();
|
||||||
private List<TupleRuleEx> next = new ArrayList<>();
|
private List<TupleRuleEx> next = new ArrayList<>();
|
||||||
|
@ -481,12 +517,12 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getItemId(int position) {
|
public long getItemId(int position) {
|
||||||
return items.get(position).id;
|
return selected.get(position).id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getItemCount() {
|
public int getItemCount() {
|
||||||
return items.size();
|
return selected.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -498,7 +534,7 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
|
||||||
@Override
|
@Override
|
||||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||||
holder.unwire();
|
holder.unwire();
|
||||||
TupleRuleEx rule = items.get(position);
|
TupleRuleEx rule = selected.get(position);
|
||||||
holder.bindTo(rule);
|
holder.bindTo(rule);
|
||||||
holder.wire();
|
holder.wire();
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
|
@ -32,6 +33,7 @@ import android.view.ViewGroup;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.appcompat.widget.SearchView;
|
||||||
import androidx.constraintlayout.widget.Group;
|
import androidx.constraintlayout.widget.Group;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.fragment.app.FragmentTransaction;
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
|
@ -64,6 +66,7 @@ public class FragmentRules extends FragmentBase {
|
||||||
private Group grpReady;
|
private Group grpReady;
|
||||||
private FloatingActionButton fab;
|
private FloatingActionButton fab;
|
||||||
|
|
||||||
|
private String searching = null;
|
||||||
private AdapterRule adapter;
|
private AdapterRule adapter;
|
||||||
|
|
||||||
static final int REQUEST_MOVE = 1;
|
static final int REQUEST_MOVE = 1;
|
||||||
|
@ -141,10 +144,21 @@ public class FragmentRules extends FragmentBase {
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
|
outState.putString("fair:searching", searching);
|
||||||
|
super.onSaveInstanceState(outState);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
|
||||||
|
if (savedInstanceState != null) {
|
||||||
|
searching = savedInstanceState.getString("fair:searching");
|
||||||
|
adapter.search(searching);
|
||||||
|
}
|
||||||
|
|
||||||
DB db = DB.getInstance(getContext());
|
DB db = DB.getInstance(getContext());
|
||||||
db.rule().liveRules(folder).observe(getViewLifecycleOwner(), new Observer<List<TupleRuleEx>>() {
|
db.rule().liveRules(folder).observe(getViewLifecycleOwner(), new Observer<List<TupleRuleEx>>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -183,6 +197,32 @@ public class FragmentRules extends FragmentBase {
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
inflater.inflate(R.menu.menu_rules, menu);
|
inflater.inflate(R.menu.menu_rules, menu);
|
||||||
|
|
||||||
|
MenuItem menuSearch = menu.findItem(R.id.menu_search);
|
||||||
|
SearchView searchView = (SearchView) menuSearch.getActionView();
|
||||||
|
searchView.setQueryHint(getString(R.string.title_search));
|
||||||
|
|
||||||
|
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);
|
super.onCreateOptionsMenu(menu, inflater);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
<item
|
||||||
|
android:id="@+id/menu_search"
|
||||||
|
android:icon="@drawable/baseline_search_24"
|
||||||
|
android:title="@string/title_search"
|
||||||
|
app:actionViewClass="androidx.appcompat.widget.SearchView"
|
||||||
|
app:showAsAction="collapseActionView|always" />
|
||||||
|
|
||||||
<item
|
<item
|
||||||
android:id="@+id/menu_clear"
|
android:id="@+id/menu_clear"
|
||||||
android:title="@string/title_rules_clear"
|
android:title="@string/title_rules_clear"
|
||||||
|
|
Loading…
Reference in New Issue