Improved rule search

pull/207/head
M66B 3 years ago
parent f2cbbeb83a
commit a4f0198b36

@ -551,24 +551,9 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
else {
items = new ArrayList<>();
String query = search.toLowerCase().trim();
for (TupleRuleEx rule : rules) {
if (rule.name.toLowerCase().contains(query))
for (TupleRuleEx rule : rules)
if (rule.matches(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);

@ -52,6 +52,7 @@ import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
@ -1121,6 +1122,41 @@ public class EntityRule {
return false;
}
boolean matches(String query) {
if (this.name.toLowerCase().contains(query))
return true;
try {
JSONObject jcondition = new JSONObject(this.condition);
JSONObject jaction = new JSONObject(this.action);
JSONObject jmerged = new JSONObject();
jmerged.put("condition", jcondition);
jmerged.put("action", jaction);
return contains(jmerged, query);
} catch (JSONException ex) {
Log.e(ex);
}
return false;
}
private boolean contains(JSONObject jobject, String query) throws JSONException {
Iterator<String> keys = jobject.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = jobject.get(key);
if (value instanceof JSONObject) {
if (contains((JSONObject) value, query))
return true;
} else {
if (value.toString().toLowerCase().contains(query))
return true;
}
}
return false;
}
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("id", id);

Loading…
Cancel
Save