Improved rule search

This commit is contained in:
M66B
2022-04-01 08:49:40 +02:00
parent f2cbbeb83a
commit a4f0198b36
2 changed files with 38 additions and 17 deletions

View File

@@ -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);