Remove clone() for AutomationEvent and Action

This commit is contained in:
Nico Schmitz 2019-03-26 22:36:24 +01:00
parent e1523ee82d
commit 7b5c26bbb9
5 changed files with 38 additions and 76 deletions

View file

@ -9,10 +9,11 @@ import java.util.List;
import info.nightscout.androidaps.plugins.general.automation.actions.Action; import info.nightscout.androidaps.plugins.general.automation.actions.Action;
import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger; import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector;
public class AutomationEvent implements Cloneable { public class AutomationEvent {
private Trigger trigger; private Trigger trigger = new TriggerConnector();
private List<Action> actions = new ArrayList<>(); private List<Action> actions = new ArrayList<>();
private String title; private String title;
@ -70,28 +71,4 @@ public class AutomationEvent implements Cloneable {
} }
return this; return this;
} }
public void apply(AutomationEvent event) {
trigger = event.trigger;
actions = event.actions;
title = event.title;
}
@Override
public AutomationEvent clone() throws CloneNotSupportedException {
AutomationEvent e = (AutomationEvent) super.clone();
e.title = title;
// clone actions
e.actions = new ArrayList<>();
for(Action a : actions) {
e.actions.add(a.clone());
}
// clone triggers
if (trigger != null) {
e.trigger = trigger.clone();
}
return e;
}
} }

View file

@ -9,7 +9,7 @@ import org.json.JSONObject;
import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.Callback;
public abstract class Action implements Cloneable { public abstract class Action {
public abstract int friendlyName(); public abstract int friendlyName();
@ -31,14 +31,7 @@ public abstract class Action implements Cloneable {
public abstract Optional<Integer> icon(); public abstract Optional<Integer> icon();
public void copy(Action action) { } public Action fromJSON(String data) {
@Override
public Action clone() throws CloneNotSupportedException {
return (Action) super.clone();
}
/*package*/ Action fromJSON(String data) {
return this; return this;
} }
@ -53,4 +46,17 @@ public abstract class Action implements Cloneable {
} }
return null; return null;
} }
public void apply(Action a) {
try {
JSONObject object = new JSONObject(a.toJSON());
String type = object.getString("type");
JSONObject data = object.getJSONObject("data");
if (type.equals(getClass().getName())) {
fromJSON(data.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} }

View file

@ -81,7 +81,7 @@ public class ActionStartTempTarget extends Action {
} }
@Override @Override
Action fromJSON(String data) { public Action fromJSON(String data) {
try { try {
JSONObject d = new JSONObject(data); JSONObject d = new JSONObject(data);
reason = JsonHelper.safeGetString(d, "reason"); reason = JsonHelper.safeGetString(d, "reason");
@ -94,25 +94,6 @@ public class ActionStartTempTarget extends Action {
return this; return this;
} }
@Override
public void copy(Action action) {
if (action instanceof ActionStartTempTarget) {
ActionStartTempTarget src = (ActionStartTempTarget)action;
this.duration = src.duration;
this.value = src.value;
this.reason = src.reason;
}
}
@Override
public ActionStartTempTarget clone() throws CloneNotSupportedException {
ActionStartTempTarget a = (ActionStartTempTarget) super.clone();
a.reason = reason;
a.value = value;
a.duration = duration;
return a;
}
@Override @Override
public Optional<Integer> icon() { public Optional<Integer> icon() {
return Optional.of(R.drawable.icon_cp_cgm_target); return Optional.of(R.drawable.icon_cp_cgm_target);

View file

@ -36,13 +36,7 @@ public class EditActionDialog extends DialogFragment {
Bundle args = new Bundle(); Bundle args = new Bundle();
EditActionDialog fragment = new EditActionDialog(); EditActionDialog fragment = new EditActionDialog();
fragment.setArguments(args); fragment.setArguments(args);
resultAction = action;
// clone action to static object
try {
resultAction = action.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return fragment; return fragment;
} }
@ -52,18 +46,22 @@ public class EditActionDialog extends DialogFragment {
View view = inflater.inflate(R.layout.automation_dialog_action, container, false); View view = inflater.inflate(R.layout.automation_dialog_action, container, false);
mUnbinder = ButterKnife.bind(this, view); mUnbinder = ButterKnife.bind(this, view);
// get json data for action
String actionData = null;
if (savedInstanceState != null) { if (savedInstanceState != null) {
String actionData = savedInstanceState.getString("action"); actionData = savedInstanceState.getString("action");
if (actionData != null) {
try {
mAction = Action.instantiate(new JSONObject(actionData));
} catch (JSONException e) {
e.printStackTrace();
}
}
} }
if (mAction == null) if (actionData == null) {
mAction = resultAction; actionData = resultAction.toJSON();
}
// create action from json
try {
mAction = Action.instantiate(new JSONObject(actionData));
} catch (JSONException e) {
e.printStackTrace();
}
mViewActionTitle.setText(mAction.friendlyName()); mViewActionTitle.setText(mAction.friendlyName());
mRootLayout.removeAllViews(); mRootLayout.removeAllViews();
@ -80,7 +78,7 @@ public class EditActionDialog extends DialogFragment {
@OnClick(R.id.ok) @OnClick(R.id.ok)
public void onButtonOk(View view) { public void onButtonOk(View view) {
resultAction.copy(mAction); resultAction.apply(mAction);
dismiss(); dismiss();
} }

View file

@ -62,8 +62,8 @@ public class EditEventDialog extends DialogFragment {
fragment.setArguments(args); fragment.setArguments(args);
// clone event // clone event
try { try {
fragment.mEvent = event.clone(); fragment.mEvent = new AutomationEvent().fromJSON(event.toJSON());
} catch (CloneNotSupportedException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
fragment.mAddNew = addNew; fragment.mAddNew = addNew;
@ -149,7 +149,7 @@ public class EditEventDialog extends DialogFragment {
} }
// apply changes // apply changes
staticEvent.apply(mEvent); staticEvent.fromJSON(mEvent.toJSON());
// add new // add new
if (mAddNew) { if (mAddNew) {