Refactoring. Fixes for screen rotation.

This commit is contained in:
Nico Schmitz 2019-03-20 23:09:46 +01:00
parent c074ec36c4
commit 87cf62e8f6
10 changed files with 169 additions and 47 deletions

View file

@ -136,8 +136,10 @@ public class AutomationFragment extends SubscriberFragment {
final Action action = mActionList.get(position); final Action action = mActionList.get(position);
holder.actionTitle.setText(action.friendlyName()); holder.actionTitle.setText(action.friendlyName());
holder.itemRoot.setOnClickListener(v -> { holder.itemRoot.setOnClickListener(v -> {
EditActionDialog dialog = EditActionDialog.newInstance(action); if (action.hasDialog()) {
dialog.show(mFragmentManager, "EditActionDialog"); EditActionDialog dialog = EditActionDialog.newInstance(action);
dialog.show(mFragmentManager, "EditActionDialog");
}
}); });
} }
@ -203,11 +205,11 @@ public class AutomationFragment extends SubscriberFragment {
buttonAdd.setText("Add New"); buttonAdd.setText("Add New");
buttonAdd.setOnClickListener(v -> { buttonAdd.setOnClickListener(v -> {
ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance(); ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance();
dialog.show(mFragmentManager, "ChooseTriggerDialog");
dialog.setOnClickListener(newTriggerObject -> { dialog.setOnClickListener(newTriggerObject -> {
mRootConnector.add(newTriggerObject); mRootConnector.add(newTriggerObject);
rebuild(); rebuild();
}); });
dialog.show(mFragmentManager, "ChooseTriggerDialog");
}); });
mRootLayout.addView(buttonAdd); mRootLayout.addView(buttonAdd);
} }

View file

@ -18,7 +18,7 @@ public abstract class Action {
public void generateDialog(LinearLayout root) { } public void generateDialog(LinearLayout root) { }
public void saveFromDialog() { } public boolean hasDialog() { return false; }
public String toJSON() { public String toJSON() {
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
@ -30,11 +30,18 @@ public abstract class Action {
return o.toString(); return o.toString();
} }
public void copy(Action action) { }
/*package*/ Action fromJSON(String data) {
return this;
}
public static Action instantiate(JSONObject object) { public static Action instantiate(JSONObject object) {
try { try {
String type = object.getString("type"); String type = object.getString("type");
JSONObject data = object.getJSONObject("data");
Class clazz = Class.forName(type); Class clazz = Class.forName(type);
return (Action) clazz.newInstance(); return ((Action) clazz.newInstance()).fromJSON(data.toString());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) { } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -3,6 +3,9 @@ package info.nightscout.androidaps.plugins.general.automation.actions;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import org.json.JSONException;
import org.json.JSONObject;
import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
@ -16,22 +19,20 @@ import info.nightscout.androidaps.plugins.general.automation.elements.InputDurat
import info.nightscout.androidaps.plugins.general.automation.elements.Label; import info.nightscout.androidaps.plugins.general.automation.elements.Label;
import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.Callback;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.JsonHelper;
public class ActionStartTempTarget extends Action { public class ActionStartTempTarget extends Action {
private String reason; private String reason;
private double valueInMgdl;
private String units;
private int durationInMinutes;
private InputBg inputBg; private InputBg value;
private InputDuration inputDuration; private InputDuration duration = new InputDuration(0, InputDuration.TimeUnit.MINUTES);
public ActionStartTempTarget() { public ActionStartTempTarget() {
units = Constants.MGDL; value = new InputBg(Constants.MGDL);
} }
public ActionStartTempTarget(String units) { public ActionStartTempTarget(String units) {
this.units = Constants.MGDL; value = new InputBg(units);
} }
@Override @Override
@ -41,7 +42,7 @@ public class ActionStartTempTarget extends Action {
@Override @Override
void doAction(Callback callback) { void doAction(Callback callback) {
TempTarget tempTarget = new TempTarget().date(DateUtil.now()).duration(durationInMinutes).reason(reason).source(Source.USER).low(valueInMgdl).high(valueInMgdl); TempTarget tempTarget = new TempTarget().date(DateUtil.now()).duration((int)duration.getMinutes()).reason(reason).source(Source.USER).low(value.getMgdl()).high(value.getMgdl());
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget); TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
if (callback != null) if (callback != null)
callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run(); callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run();
@ -49,21 +50,56 @@ public class ActionStartTempTarget extends Action {
@Override @Override
public void generateDialog(LinearLayout root) { public void generateDialog(LinearLayout root) {
inputBg = new InputBg(units); int unitResId = value.getUnits().equals(Constants.MGDL) ? R.string.mgdl : R.string.mmol;
if (valueInMgdl != 0d) inputBg.setMgdl(valueInMgdl); Label labelBg = new Label(MainApp.gs(R.string.careportal_newnstreatment_percentage_label), MainApp.gs(unitResId), value);
inputDuration = new InputDuration(durationInMinutes, InputDuration.TimeUnit.MINUTES);
int unitResId = units.equals(Constants.MGDL) ? R.string.mgdl : R.string.mmol;
Label labelBg = new Label(MainApp.gs(R.string.careportal_newnstreatment_percentage_label), MainApp.gs(unitResId), inputBg);
labelBg.generateDialog(root); labelBg.generateDialog(root);
Label labelDuration = new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "min", duration);
Label labelDuration = new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "min", inputDuration);
labelDuration.generateDialog(root); labelDuration.generateDialog(root);
} }
@Override @Override
public void saveFromDialog() { public boolean hasDialog() {
valueInMgdl = inputBg.getMgdl(); return true;
durationInMinutes = inputDuration.getMinutes(); }
@Override
public String toJSON() {
JSONObject o = new JSONObject();
try {
o.put("type", ActionStartTempTarget.class.getName());
JSONObject data = new JSONObject();
data.put("reason", reason);
data.put("valueInMg", value.getMgdl());
data.put("units", value.getUnits());
data.put("durationInMinutes", duration.getMinutes());
o.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
return o.toString();
}
@Override
Action fromJSON(String data) {
try {
JSONObject d = new JSONObject(data);
reason = JsonHelper.safeGetString(d, "reason");
value.setUnits(JsonHelper.safeGetString(d, "units"));
value.setMgdl(JsonHelper.safeGetInt(d, "valueInMg"));
duration.setMinutes(JsonHelper.safeGetDouble(d, "durationInMinutes"));
} catch (JSONException e) {
e.printStackTrace();
}
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;
}
} }
} }

View file

@ -30,6 +30,8 @@ public class ChooseActionDialog extends DialogFragment {
void onClick(Action newActionObject); void onClick(Action newActionObject);
} }
private static OnClickListener mClickListener = null;
private static final List<Action> actionDummyObjects = new ArrayList<Action>() {{ private static final List<Action> actionDummyObjects = new ArrayList<Action>() {{
add(new ActionLoopDisable()); add(new ActionLoopDisable());
add(new ActionLoopEnable()); add(new ActionLoopEnable());
@ -39,7 +41,6 @@ public class ChooseActionDialog extends DialogFragment {
}}; }};
private Unbinder mUnbinder; private Unbinder mUnbinder;
private OnClickListener mClickListener = null;
@BindView(R.id.radioGroup) @BindView(R.id.radioGroup)
RadioGroup mRadioGroup; RadioGroup mRadioGroup;
@ -65,11 +66,25 @@ public class ChooseActionDialog extends DialogFragment {
mRadioGroup.addView(radioButton); mRadioGroup.addView(radioButton);
} }
((RadioButton)mRadioGroup.getChildAt(0)).setChecked(true); // restore checked radio button
int checkedIndex = 0;
if (savedInstanceState != null) {
checkedIndex = savedInstanceState.getInt("checkedIndex");
}
((RadioButton)mRadioGroup.getChildAt(checkedIndex)).setChecked(true);
return view; return view;
} }
private int getCheckedIndex() {
for(int i = 0; i < mRadioGroup.getChildCount(); ++i) {
if (((RadioButton)mRadioGroup.getChildAt(i)).isChecked())
return i;
}
return -1;
}
private Class getActionClass() { private Class getActionClass() {
int radioButtonID = mRadioGroup.getCheckedRadioButtonId(); int radioButtonID = mRadioGroup.getCheckedRadioButtonId();
RadioButton radioButton = mRadioGroup.findViewById(radioButtonID); RadioButton radioButton = mRadioGroup.findViewById(radioButtonID);
@ -94,7 +109,7 @@ public class ChooseActionDialog extends DialogFragment {
} }
public void setOnClickListener(OnClickListener clickListener) { public static void setOnClickListener(OnClickListener clickListener) {
mClickListener = clickListener; mClickListener = clickListener;
} }
@ -117,4 +132,8 @@ public class ChooseActionDialog extends DialogFragment {
dismiss(); dismiss();
} }
@Override
public void onSaveInstanceState(Bundle bundle) {
bundle.putInt("checkedIndex", getCheckedIndex());
}
} }

View file

@ -59,11 +59,25 @@ public class ChooseTriggerDialog extends DialogFragment {
mRadioGroup.addView(radioButton); mRadioGroup.addView(radioButton);
} }
((RadioButton)mRadioGroup.getChildAt(0)).setChecked(true); // restore checked radio button
int checkedIndex = 0;
if (savedInstanceState != null) {
checkedIndex = savedInstanceState.getInt("checkedIndex");
}
((RadioButton)mRadioGroup.getChildAt(checkedIndex)).setChecked(true);
return view; return view;
} }
private int getCheckedIndex() {
for(int i = 0; i < mRadioGroup.getChildCount(); ++i) {
if (((RadioButton)mRadioGroup.getChildAt(i)).isChecked())
return i;
}
return -1;
}
private Class getTriggerClass() { private Class getTriggerClass() {
int radioButtonID = mRadioGroup.getCheckedRadioButtonId(); int radioButtonID = mRadioGroup.getCheckedRadioButtonId();
RadioButton radioButton = mRadioGroup.findViewById(radioButtonID); RadioButton radioButton = mRadioGroup.findViewById(radioButtonID);
@ -111,4 +125,8 @@ public class ChooseTriggerDialog extends DialogFragment {
dismiss(); dismiss();
} }
@Override
public void onSaveInstanceState(Bundle bundle) {
bundle.putInt("checkedIndex", getCheckedIndex());
}
} }

View file

@ -9,14 +9,20 @@ import android.view.ViewGroup;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
import butterknife.Unbinder; import butterknife.Unbinder;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
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.TriggerConnector;
public class EditActionDialog extends DialogFragment { public class EditActionDialog extends DialogFragment {
private static Action resultAction;
private Unbinder mUnbinder; private Unbinder mUnbinder;
private Action mAction; private Action mAction;
@ -30,7 +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);
fragment.mAction = action; resultAction = action;
return fragment; return fragment;
} }
@ -40,6 +46,19 @@ 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);
if (savedInstanceState != null) {
String actionData = savedInstanceState.getString("action");
if (actionData != null) {
try {
mAction = Action.instantiate(new JSONObject(actionData));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
if (mAction == null)
mAction = resultAction;
mViewActionTitle.setText(mAction.friendlyName()); mViewActionTitle.setText(mAction.friendlyName());
mRootLayout.removeAllViews(); mRootLayout.removeAllViews();
mAction.generateDialog(mRootLayout); mAction.generateDialog(mRootLayout);
@ -55,7 +74,7 @@ public class EditActionDialog extends DialogFragment {
@OnClick(R.id.ok) @OnClick(R.id.ok)
public void onButtonOk(View view) { public void onButtonOk(View view) {
mAction.saveFromDialog(); resultAction.copy(mAction);
dismiss(); dismiss();
} }
@ -63,4 +82,9 @@ public class EditActionDialog extends DialogFragment {
public void onButtonCancel(View view) { public void onButtonCancel(View view) {
dismiss(); dismiss();
} }
@Override
public void onSaveInstanceState(Bundle bundle) {
bundle.putString("action", mAction.toJSON());
}
} }

View file

@ -69,13 +69,13 @@ public class EditEventDialog extends DialogFragment {
mTriggerDescription.setText(mEvent.getTrigger().friendlyDescription()); mTriggerDescription.setText(mEvent.getTrigger().friendlyDescription());
// setup trigger click event listener // setup trigger click event listener
EditTriggerDialog.setOnClickListener(trigger -> {
mEvent.setTrigger(trigger);
mTriggerDescription.setText(mEvent.getTrigger().friendlyDescription());
});
mEditTrigger.setOnClickListener(v -> { mEditTrigger.setOnClickListener(v -> {
EditTriggerDialog dialog = EditTriggerDialog.newInstance(mEvent.getTrigger()); EditTriggerDialog dialog = EditTriggerDialog.newInstance(mEvent.getTrigger());
dialog.show(getFragmentManager(), "EditTriggerDialog"); dialog.show(getFragmentManager(), "EditTriggerDialog");
dialog.setOnClickListener(trigger -> {
mEvent.setTrigger(trigger);
mTriggerDescription.setText(mEvent.getTrigger().friendlyDescription());
});
}); });
// setup action list view // setup action list view
@ -84,13 +84,13 @@ public class EditEventDialog extends DialogFragment {
mActionListView.setAdapter(mActionListAdapter); mActionListView.setAdapter(mActionListAdapter);
// setup action click event listener // setup action click event listener
ChooseActionDialog.setOnClickListener(newActionObject -> {
mEvent.addAction(newActionObject);
mActionListAdapter.notifyDataSetChanged();
});
mEditAction.setOnClickListener(v -> { mEditAction.setOnClickListener(v -> {
ChooseActionDialog dialog = ChooseActionDialog.newInstance(); ChooseActionDialog dialog = ChooseActionDialog.newInstance();
dialog.show(getFragmentManager(), "ChooseActionDialog"); dialog.show(getFragmentManager(), "ChooseActionDialog");
dialog.setOnClickListener(newActionObject -> {
mEvent.addAction(newActionObject);
mActionListAdapter.notifyDataSetChanged();
});
}); });

View file

@ -21,12 +21,13 @@ public class EditTriggerDialog extends DialogFragment {
void onClick(Trigger newTriggerObject); void onClick(Trigger newTriggerObject);
} }
private static OnClickListener mClickListener = null;
@BindView(R.id.layoutTrigger) @BindView(R.id.layoutTrigger)
LinearLayout mLayoutTrigger; LinearLayout mLayoutTrigger;
private Trigger mTrigger; private Trigger mTrigger;
private Unbinder mUnbinder; private Unbinder mUnbinder;
private OnClickListener mClickListener = null;
public static EditTriggerDialog newInstance(Trigger trigger) { public static EditTriggerDialog newInstance(Trigger trigger) {
Bundle args = new Bundle(); Bundle args = new Bundle();
@ -54,7 +55,7 @@ public class EditTriggerDialog extends DialogFragment {
return view; return view;
} }
public void setOnClickListener(OnClickListener clickListener) { public static void setOnClickListener(OnClickListener clickListener) {
mClickListener = clickListener; mClickListener = clickListener;
} }

View file

@ -67,15 +67,23 @@ public class InputBg extends Element {
return units; return units;
} }
public void setUnits(String units) {
if (!this.units.equals(units)) {
String previousUnits = this.units;
this.units = units;
value = Profile.toUnits(Profile.toMgdl(value, previousUnits), Profile.toMmol(value, previousUnits), units);
}
}
public double getValue() { public double getValue() {
return value; return value;
} }
public double getMgdl() { public int getMgdl() {
return Profile.toMgdl(value, units); return (int)Profile.toMgdl(value, units);
} }
public void setMgdl(double value) { public void setMgdl(int value) {
this.value = Profile.fromMgdlToUnits(value, units); this.value = Profile.fromMgdlToUnits(value, units);
} }
} }

View file

@ -22,7 +22,6 @@ public class InputDuration extends Element {
this.value = value; this.value = value;
} }
@Override @Override
public void generateDialog(LinearLayout root) { public void generateDialog(LinearLayout root) {
NumberPicker numberPicker = new NumberPicker(root.getContext(), null); NumberPicker numberPicker = new NumberPicker(root.getContext(), null);
@ -46,11 +45,19 @@ public class InputDuration extends Element {
return value; return value;
} }
public int getMinutes() { public void setMinutes(double value) {
if (unit.equals(TimeUnit.MINUTES)) { if (unit.equals(TimeUnit.MINUTES)) {
return (int)value; this.value = value;
} else { } else {
return (int)(value * 60d); this.value = value / 60d;
}
}
public double getMinutes() {
if (unit.equals(TimeUnit.MINUTES)) {
return value;
} else {
return value * 60d;
} }
} }
} }