From 04574d759fda6314ffa558f14178f2f644f6141d Mon Sep 17 00:00:00 2001 From: Nico Schmitz Date: Mon, 29 Oct 2018 21:24:30 +0100 Subject: [PATCH] Improve dialogs. Add JSON serialization for actions and events. --- .../general/automation/AutomationEvent.java | 41 +++++++++++++++++++ .../general/automation/actions/Action.java | 25 +++++++++++ .../automation/dialogs/EditEventDialog.java | 18 +++++--- .../general/automation/triggers/Trigger.java | 15 +++++-- .../automation/triggers/TriggerBg.java | 2 +- .../automation/triggers/TriggerConnector.java | 2 +- .../automation/triggers/TriggerTime.java | 2 +- .../res/layout/automation_dialog_event.xml | 25 +++++++++++ .../res/layout/automation_dialog_trigger.xml | 8 +++- 9 files changed, 125 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java index f7cd5c6510..a38952d0a5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationEvent.java @@ -1,5 +1,9 @@ package info.nightscout.androidaps.plugins.general.automation; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; import java.util.List; @@ -27,4 +31,41 @@ public class AutomationEvent { public String getTitle() { return title; } + + public String toJSON() { + JSONObject o = new JSONObject(); + try { + // title + o.put("title", title); + // trigger + o.put("trigger", trigger.toJSON()); + // actions + JSONArray array = new JSONArray(); + for (Action a : actions) { + array.put(a.toJSON()); + } + o.put("actions", array); + } catch (JSONException e) { + e.printStackTrace(); + } + return o.toString(); + } + + public AutomationEvent fromJSON(String data) { + try { + JSONObject d = new JSONObject(data); + // title + title = d.getString("title"); + // trigger + trigger = Trigger.instantiate(d.getString("trigger")); + // actions + JSONArray array = d.getJSONArray("actions"); + for (int i = 0; i < array.length(); i++) { + actions.add(Action.instantiate(array.getJSONObject(i))); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return this; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java index b74243326e..ab06ef268e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Action.java @@ -1,9 +1,34 @@ package info.nightscout.androidaps.plugins.general.automation.actions; +import org.json.JSONException; +import org.json.JSONObject; + import info.nightscout.androidaps.queue.Callback; public abstract class Action { abstract int friendlyName(); + abstract void doAction(Callback callback); + + public String toJSON() { + JSONObject o = new JSONObject(); + try { + o.put("type", this.getClass().getName()); + } catch (JSONException e) { + e.printStackTrace(); + } + return o.toString(); + } + + public static Action instantiate(JSONObject object) { + try { + String type = object.getString("type"); + Class clazz = Class.forName(type); + return (Action) clazz.newInstance(); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) { + e.printStackTrace(); + } + return null; + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java index a2d5304ce0..4ff65a0a45 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.java @@ -15,7 +15,6 @@ import butterknife.OnClick; import butterknife.Unbinder; import info.nightscout.androidaps.R; import info.nightscout.androidaps.plugins.general.automation.AutomationEvent; -import info.nightscout.androidaps.plugins.general.automation.AutomationFragment; import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector; @@ -30,7 +29,6 @@ public class EditEventDialog extends DialogFragment { LinearLayout mLayoutTrigger; private Unbinder mUnbinder; - private AutomationFragment.TriggerListAdapter mTriggerListAdapter; public static EditEventDialog newInstance(AutomationEvent event) { mEvent = event; @@ -47,12 +45,16 @@ public class EditEventDialog extends DialogFragment { View view = inflater.inflate(R.layout.automation_dialog_event, container, false); mUnbinder = ButterKnife.bind(this, view); - // initialization - TriggerConnector rootTrigger = new TriggerConnector(TriggerConnector.Type.OR); - mEvent.setTrigger(rootTrigger); + // load data from bundle + if (savedInstanceState != null) { + String eventData = savedInstanceState.getString("event"); + if (eventData != null) mEvent.fromJSON(eventData); + } else { + mEvent.setTrigger(new TriggerConnector(TriggerConnector.Type.OR)); + } // display root trigger - mLayoutTrigger.addView(rootTrigger.createView(getContext(), getFragmentManager())); + mLayoutTrigger.addView(mEvent.getTrigger().createView(getContext(), getFragmentManager())); return view; } @@ -82,5 +84,9 @@ public class EditEventDialog extends DialogFragment { dismiss(); } + @Override + public void onSaveInstanceState(Bundle bundle) { + bundle.putString("event", mEvent.toJSON()); + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java index cfc3774d21..efe8a66e71 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.java @@ -87,9 +87,9 @@ public abstract class Trigger { public abstract boolean shouldRun(); - abstract String toJSON(); + public abstract String toJSON(); - abstract Trigger fromJSON(String data); + /*package*/ abstract Trigger fromJSON(String data); public abstract int friendlyName(); @@ -100,7 +100,16 @@ public abstract class Trigger { public abstract Trigger duplicate(); - static Trigger instantiate(JSONObject object) { + public static Trigger instantiate(String json) { + try { + return instantiate(new JSONObject(json)); + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + public static Trigger instantiate(JSONObject object) { try { String type = object.getString("type"); JSONObject data = object.getJSONObject("data"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java index 44697c1b19..5b0ef66e6f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBg.java @@ -71,7 +71,7 @@ public class TriggerBg extends Trigger { } @Override - synchronized String toJSON() { + public synchronized String toJSON() { JSONObject o = new JSONObject(); try { o.put("type", TriggerBg.class.getName()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java index b4268c868e..70cfc2752b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.java @@ -120,7 +120,7 @@ public class TriggerConnector extends Trigger { } @Override - synchronized String toJSON() { + public synchronized String toJSON() { JSONObject o = new JSONObject(); try { o.put("type", TriggerConnector.class.getName()); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java index 6fc34d489a..2dee94eadb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTime.java @@ -162,7 +162,7 @@ public class TriggerTime extends Trigger { } @Override - String toJSON() { + public String toJSON() { JSONObject object = new JSONObject(); JSONObject data = new JSONObject(); try { diff --git a/app/src/main/res/layout/automation_dialog_event.xml b/app/src/main/res/layout/automation_dialog_event.xml index 80f849ca3e..7cf23e0658 100644 --- a/app/src/main/res/layout/automation_dialog_event.xml +++ b/app/src/main/res/layout/automation_dialog_event.xml @@ -22,12 +22,37 @@ android:layout_height="wrap_content" android:hint="Event Name" /> + + + + + + + + diff --git a/app/src/main/res/layout/automation_dialog_trigger.xml b/app/src/main/res/layout/automation_dialog_trigger.xml index 62bda01d61..37e456702e 100644 --- a/app/src/main/res/layout/automation_dialog_trigger.xml +++ b/app/src/main/res/layout/automation_dialog_trigger.xml @@ -16,10 +16,16 @@ android:orientation="vertical" android:padding="10dp"> + + + android:layout_height="match_parent" + android:layout_marginVertical="5dp"/>