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.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 String title;
@ -70,28 +71,4 @@ public class AutomationEvent implements Cloneable {
}
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;
public abstract class Action implements Cloneable {
public abstract class Action {
public abstract int friendlyName();
@ -31,14 +31,7 @@ public abstract class Action implements Cloneable {
public abstract Optional<Integer> icon();
public void copy(Action action) { }
@Override
public Action clone() throws CloneNotSupportedException {
return (Action) super.clone();
}
/*package*/ Action fromJSON(String data) {
public Action fromJSON(String data) {
return this;
}
@ -53,4 +46,17 @@ public abstract class Action implements Cloneable {
}
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
Action fromJSON(String data) {
public Action fromJSON(String data) {
try {
JSONObject d = new JSONObject(data);
reason = JsonHelper.safeGetString(d, "reason");
@ -94,25 +94,6 @@ public class ActionStartTempTarget extends Action {
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
public Optional<Integer> icon() {
return Optional.of(R.drawable.icon_cp_cgm_target);

View file

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

View file

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