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);
holder.actionTitle.setText(action.friendlyName());
holder.itemRoot.setOnClickListener(v -> {
EditActionDialog dialog = EditActionDialog.newInstance(action);
dialog.show(mFragmentManager, "EditActionDialog");
if (action.hasDialog()) {
EditActionDialog dialog = EditActionDialog.newInstance(action);
dialog.show(mFragmentManager, "EditActionDialog");
}
});
}
@ -203,11 +205,11 @@ public class AutomationFragment extends SubscriberFragment {
buttonAdd.setText("Add New");
buttonAdd.setOnClickListener(v -> {
ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance();
dialog.show(mFragmentManager, "ChooseTriggerDialog");
dialog.setOnClickListener(newTriggerObject -> {
mRootConnector.add(newTriggerObject);
rebuild();
});
dialog.show(mFragmentManager, "ChooseTriggerDialog");
});
mRootLayout.addView(buttonAdd);
}

View file

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

View file

@ -3,6 +3,9 @@ package info.nightscout.androidaps.plugins.general.automation.actions;
import android.support.annotation.StringRes;
import android.widget.LinearLayout;
import org.json.JSONException;
import org.json.JSONObject;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
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.queue.Callback;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.JsonHelper;
public class ActionStartTempTarget extends Action {
private String reason;
private double valueInMgdl;
private String units;
private int durationInMinutes;
private InputBg inputBg;
private InputDuration inputDuration;
private InputBg value;
private InputDuration duration = new InputDuration(0, InputDuration.TimeUnit.MINUTES);
public ActionStartTempTarget() {
units = Constants.MGDL;
value = new InputBg(Constants.MGDL);
}
public ActionStartTempTarget(String units) {
this.units = Constants.MGDL;
value = new InputBg(units);
}
@Override
@ -41,7 +42,7 @@ public class ActionStartTempTarget extends Action {
@Override
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);
if (callback != null)
callback.result(new PumpEnactResult().success(true).comment(R.string.ok)).run();
@ -49,21 +50,56 @@ public class ActionStartTempTarget extends Action {
@Override
public void generateDialog(LinearLayout root) {
inputBg = new InputBg(units);
if (valueInMgdl != 0d) inputBg.setMgdl(valueInMgdl);
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);
int unitResId = value.getUnits().equals(Constants.MGDL) ? R.string.mgdl : R.string.mmol;
Label labelBg = new Label(MainApp.gs(R.string.careportal_newnstreatment_percentage_label), MainApp.gs(unitResId), value);
labelBg.generateDialog(root);
Label labelDuration = new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "min", inputDuration);
Label labelDuration = new Label(MainApp.gs(R.string.careportal_newnstreatment_duration_min_label), "min", duration);
labelDuration.generateDialog(root);
}
@Override
public void saveFromDialog() {
valueInMgdl = inputBg.getMgdl();
durationInMinutes = inputDuration.getMinutes();
public boolean hasDialog() {
return true;
}
@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);
}
private static OnClickListener mClickListener = null;
private static final List<Action> actionDummyObjects = new ArrayList<Action>() {{
add(new ActionLoopDisable());
add(new ActionLoopEnable());
@ -39,7 +41,6 @@ public class ChooseActionDialog extends DialogFragment {
}};
private Unbinder mUnbinder;
private OnClickListener mClickListener = null;
@BindView(R.id.radioGroup)
RadioGroup mRadioGroup;
@ -65,11 +66,25 @@ public class ChooseActionDialog extends DialogFragment {
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;
}
private int getCheckedIndex() {
for(int i = 0; i < mRadioGroup.getChildCount(); ++i) {
if (((RadioButton)mRadioGroup.getChildAt(i)).isChecked())
return i;
}
return -1;
}
private Class getActionClass() {
int radioButtonID = mRadioGroup.getCheckedRadioButtonId();
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;
}
@ -117,4 +132,8 @@ public class ChooseActionDialog extends DialogFragment {
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);
}
((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;
}
private int getCheckedIndex() {
for(int i = 0; i < mRadioGroup.getChildCount(); ++i) {
if (((RadioButton)mRadioGroup.getChildAt(i)).isChecked())
return i;
}
return -1;
}
private Class getTriggerClass() {
int radioButtonID = mRadioGroup.getCheckedRadioButtonId();
RadioButton radioButton = mRadioGroup.findViewById(radioButtonID);
@ -111,4 +125,8 @@ public class ChooseTriggerDialog extends DialogFragment {
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.TextView;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.general.automation.actions.Action;
import info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector;
public class EditActionDialog extends DialogFragment {
private static Action resultAction;
private Unbinder mUnbinder;
private Action mAction;
@ -30,7 +36,7 @@ public class EditActionDialog extends DialogFragment {
Bundle args = new Bundle();
EditActionDialog fragment = new EditActionDialog();
fragment.setArguments(args);
fragment.mAction = action;
resultAction = action;
return fragment;
}
@ -40,6 +46,19 @@ public class EditActionDialog extends DialogFragment {
View view = inflater.inflate(R.layout.automation_dialog_action, container, false);
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());
mRootLayout.removeAllViews();
mAction.generateDialog(mRootLayout);
@ -55,7 +74,7 @@ public class EditActionDialog extends DialogFragment {
@OnClick(R.id.ok)
public void onButtonOk(View view) {
mAction.saveFromDialog();
resultAction.copy(mAction);
dismiss();
}
@ -63,4 +82,9 @@ public class EditActionDialog extends DialogFragment {
public void onButtonCancel(View view) {
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());
// setup trigger click event listener
EditTriggerDialog.setOnClickListener(trigger -> {
mEvent.setTrigger(trigger);
mTriggerDescription.setText(mEvent.getTrigger().friendlyDescription());
});
mEditTrigger.setOnClickListener(v -> {
EditTriggerDialog dialog = EditTriggerDialog.newInstance(mEvent.getTrigger());
dialog.show(getFragmentManager(), "EditTriggerDialog");
dialog.setOnClickListener(trigger -> {
mEvent.setTrigger(trigger);
mTriggerDescription.setText(mEvent.getTrigger().friendlyDescription());
});
});
// setup action list view
@ -84,13 +84,13 @@ public class EditEventDialog extends DialogFragment {
mActionListView.setAdapter(mActionListAdapter);
// setup action click event listener
ChooseActionDialog.setOnClickListener(newActionObject -> {
mEvent.addAction(newActionObject);
mActionListAdapter.notifyDataSetChanged();
});
mEditAction.setOnClickListener(v -> {
ChooseActionDialog dialog = ChooseActionDialog.newInstance();
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);
}
private static OnClickListener mClickListener = null;
@BindView(R.id.layoutTrigger)
LinearLayout mLayoutTrigger;
private Trigger mTrigger;
private Unbinder mUnbinder;
private OnClickListener mClickListener = null;
public static EditTriggerDialog newInstance(Trigger trigger) {
Bundle args = new Bundle();
@ -54,7 +55,7 @@ public class EditTriggerDialog extends DialogFragment {
return view;
}
public void setOnClickListener(OnClickListener clickListener) {
public static void setOnClickListener(OnClickListener clickListener) {
mClickListener = clickListener;
}

View file

@ -67,15 +67,23 @@ public class InputBg extends Element {
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() {
return value;
}
public double getMgdl() {
return Profile.toMgdl(value, units);
public int getMgdl() {
return (int)Profile.toMgdl(value, units);
}
public void setMgdl(double value) {
public void setMgdl(int value) {
this.value = Profile.fromMgdlToUnits(value, units);
}
}

View file

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