NewNSTreatment dialog cleanup
This commit is contained in:
parent
0a377eb98e
commit
54048b29c4
|
@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.Careportal.Dialogs;
|
||||||
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
|
@ -15,7 +14,6 @@ import android.view.ViewGroup;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.CompoundButton;
|
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.RadioButton;
|
import android.widget.RadioButton;
|
||||||
|
@ -109,14 +107,14 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
|
|
||||||
public void setOptions(OptionsToShow options, int event) {
|
public void setOptions(OptionsToShow options, int event) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.event = MainApp.sResources.getString(event);
|
this.event = MainApp.gs(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
public NewNSTreatmentDialog() {
|
public NewNSTreatmentDialog() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
if (seconds == null) {
|
if (seconds == null) {
|
||||||
seconds = new Double(Math.random() * 59).intValue();
|
seconds = Double.valueOf(Math.random() * 59).intValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +134,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
Bundle savedInstanceState) {
|
Bundle savedInstanceState) {
|
||||||
if (options == null) return null;
|
if (options == null) return null;
|
||||||
getDialog().setTitle(getString(options.eventName));
|
getDialog().setTitle(MainApp.gs(options.eventName));
|
||||||
setStyle(DialogFragment.STYLE_NORMAL, getTheme());
|
setStyle(DialogFragment.STYLE_NORMAL, getTheme());
|
||||||
View view = inflater.inflate(R.layout.careportal_newnstreatment_dialog, container, false);
|
View view = inflater.inflate(R.layout.careportal_newnstreatment_dialog, container, false);
|
||||||
|
|
||||||
|
@ -193,32 +191,29 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
final Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, units);
|
final Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, units);
|
||||||
|
|
||||||
// temp target
|
// temp target
|
||||||
final ArrayList<CharSequence> reasonList = new ArrayList<CharSequence>();
|
final ArrayList<CharSequence> reasonList = new ArrayList<>();
|
||||||
reasonList.add(MainApp.sResources.getString(R.string.manual));
|
reasonList.add(MainApp.gs(R.string.manual));
|
||||||
reasonList.add(MainApp.sResources.getString(R.string.eatingsoon));
|
reasonList.add(MainApp.gs(R.string.eatingsoon));
|
||||||
reasonList.add(MainApp.sResources.getString(R.string.activity));
|
reasonList.add(MainApp.gs(R.string.activity));
|
||||||
ArrayAdapter<CharSequence> adapterReason = new ArrayAdapter<CharSequence>(getContext(),
|
ArrayAdapter<CharSequence> adapterReason = new ArrayAdapter<>(getContext(),
|
||||||
R.layout.spinner_centered, reasonList);
|
R.layout.spinner_centered, reasonList);
|
||||||
reasonSpinner.setAdapter(adapterReason);
|
reasonSpinner.setAdapter(adapterReason);
|
||||||
reasonSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
reasonSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||||
double defaultDuration = 0;
|
double defaultDuration;
|
||||||
double defaultTarget = 0;
|
double defaultTarget = 0;
|
||||||
if (profile != null) {
|
if (profile != null) {
|
||||||
defaultTarget = bg.doubleValue();
|
defaultTarget = bg;
|
||||||
}
|
}
|
||||||
boolean erase = false;
|
boolean erase = false;
|
||||||
|
|
||||||
if (MainApp.sResources.getString(R.string.eatingsoon).equals(reasonList.get(position))) {
|
if (MainApp.gs(R.string.eatingsoon).equals(reasonList.get(position))) {
|
||||||
defaultDuration = SP.getDouble(R.string.key_eatingsoon_duration, 0d);
|
defaultDuration = SP.getDouble(R.string.key_eatingsoon_duration, 0d);
|
||||||
defaultTarget = SP.getDouble(R.string.key_eatingsoon_target, 0d);
|
defaultTarget = SP.getDouble(R.string.key_eatingsoon_target, 0d);
|
||||||
;
|
} else if (MainApp.gs(R.string.activity).equals(reasonList.get(position))) {
|
||||||
} else if (MainApp.sResources.getString(R.string.activity).equals(reasonList.get(position))) {
|
|
||||||
defaultDuration = SP.getDouble(R.string.key_activity_duration, 0d);
|
defaultDuration = SP.getDouble(R.string.key_activity_duration, 0d);
|
||||||
;
|
|
||||||
defaultTarget = SP.getDouble(R.string.key_activity_target, 0d);
|
defaultTarget = SP.getDouble(R.string.key_activity_target, 0d);
|
||||||
;
|
|
||||||
} else {
|
} else {
|
||||||
defaultDuration = 0;
|
defaultDuration = 0;
|
||||||
erase = true;
|
erase = true;
|
||||||
|
@ -267,12 +262,9 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false, bgTextWatcher);
|
editBg.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false, bgTextWatcher);
|
||||||
editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
|
editTemptarget.setParams(bg, 0d, 500d, 1d, new DecimalFormat("0"), false);
|
||||||
}
|
}
|
||||||
sensorRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
sensorRadioButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||||
@Override
|
Double bg1 = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile.getUnits());
|
||||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
editBg.setValue(bg1);
|
||||||
Double bg = Profile.fromMgdlToUnits(GlucoseStatus.getGlucoseStatusData() != null ? GlucoseStatus.getGlucoseStatusData().glucose : 0d, profile.getUnits());
|
|
||||||
editBg.setValue(bg);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Integer maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value();
|
Integer maxCarbs = MainApp.getConstraintChecker().getMaxCarbsAllowed().value();
|
||||||
|
@ -408,7 +400,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
tpd.show(context.getFragmentManager(), "Timepickerdialog");
|
tpd.show(context.getFragmentManager(), "Timepickerdialog");
|
||||||
break;
|
break;
|
||||||
case R.id.ok:
|
case R.id.ok:
|
||||||
createNSTreatment();
|
confirmNSTreatmentCreation();
|
||||||
dismiss();
|
dismiss();
|
||||||
break;
|
break;
|
||||||
case R.id.cancel:
|
case R.id.cancel:
|
||||||
|
@ -572,156 +564,151 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick
|
||||||
|
|
||||||
String buildConfirmText(JSONObject data) {
|
String buildConfirmText(JSONObject data) {
|
||||||
String ret = "";
|
String ret = "";
|
||||||
try {
|
if (data.has("eventType")) {
|
||||||
if (data.has("eventType")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_eventtype);
|
||||||
ret += getString(R.string.careportal_newnstreatment_eventtype);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += Translator.translate(JsonHelper.safeGetString(data, "eventType", ""));
|
||||||
ret += Translator.translate(data.getString("eventType"));
|
ret += "\n";
|
||||||
ret += "\n";
|
}
|
||||||
}
|
if (data.has("glucose")) {
|
||||||
if (data.has("glucose")) {
|
ret += MainApp.gs(R.string.treatments_wizard_bg_label);
|
||||||
ret += getString(R.string.treatments_wizard_bg_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "glucose", "");
|
||||||
ret += data.get("glucose");
|
ret += " " + units + "\n";
|
||||||
ret += " " + units + "\n";
|
}
|
||||||
}
|
if (data.has("glucoseType")) {
|
||||||
if (data.has("glucoseType")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_glucosetype);
|
||||||
ret += getString(R.string.careportal_newnstreatment_glucosetype);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += Translator.translate(JsonHelper.safeGetString(data, "glucoseType", ""));
|
||||||
ret += Translator.translate(data.getString("glucoseType"));
|
ret += "\n";
|
||||||
ret += "\n";
|
}
|
||||||
}
|
if (data.has("carbs")) {
|
||||||
if (data.has("carbs")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_carbs_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_carbs_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "carbs", "");
|
||||||
ret += data.get("carbs");
|
ret += " g\n";
|
||||||
ret += " g\n";
|
}
|
||||||
}
|
if (data.has("insulin")) {
|
||||||
if (data.has("insulin")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_insulin_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_insulin_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "insulin", "");
|
||||||
ret += data.get("insulin");
|
ret += " U\n";
|
||||||
ret += " U\n";
|
}
|
||||||
}
|
if (data.has("duration")) {
|
||||||
if (data.has("duration")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_duration_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_duration_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "duration", "");
|
||||||
ret += data.get("duration");
|
ret += " min\n";
|
||||||
ret += " min\n";
|
}
|
||||||
}
|
if (data.has("percent")) {
|
||||||
if (data.has("percent")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_percent_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_percent_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "percent", "");
|
||||||
ret += data.get("percent");
|
ret += " %\n";
|
||||||
ret += " %\n";
|
}
|
||||||
}
|
if (data.has("absolute")) {
|
||||||
if (data.has("absolute")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_absolute_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_absolute_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "absolute", "");
|
||||||
ret += data.get("absolute");
|
ret += " U/h\n";
|
||||||
ret += " U/h\n";
|
}
|
||||||
}
|
if (data.has("preBolus")) {
|
||||||
if (data.has("preBolus")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_carbtime_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_carbtime_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "preBolus", "");
|
||||||
ret += data.get("preBolus");
|
ret += " min\n";
|
||||||
ret += " min\n";
|
}
|
||||||
}
|
if (data.has("notes")) {
|
||||||
if (data.has("notes")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_notes_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_notes_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "notes", "");
|
||||||
ret += data.get("notes");
|
ret += "\n";
|
||||||
ret += "\n";
|
}
|
||||||
}
|
if (data.has("profile")) {
|
||||||
if (data.has("profile")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_profile_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_profile_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "profile", "");
|
||||||
ret += data.get("profile");
|
ret += "\n";
|
||||||
ret += "\n";
|
}
|
||||||
}
|
if (data.has("percentage")) {
|
||||||
if (data.has("percentage")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_percentage_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_percentage_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "percentage", "");
|
||||||
ret += data.get("percentage");
|
ret += " %\n";
|
||||||
ret += " %\n";
|
}
|
||||||
}
|
if (data.has("timeshift")) {
|
||||||
if (data.has("timeshift")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_timeshift_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_timeshift_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "timeshift", "");
|
||||||
ret += data.get("timeshift");
|
ret += " h\n";
|
||||||
ret += " h\n";
|
}
|
||||||
}
|
if (data.has("targetBottom") && data.has("targetTop")) {
|
||||||
if (data.has("targetBottom") && data.has("targetTop")) {
|
ret += MainApp.gs(R.string.target_range);
|
||||||
ret += getString(R.string.target_range);
|
ret += " ";
|
||||||
ret += " ";
|
ret += JsonHelper.safeGetObject(data, "targetBottom", "");
|
||||||
ret += data.get("targetBottom");
|
ret += " - ";
|
||||||
ret += " - ";
|
ret += JsonHelper.safeGetObject(data, "targetTop", "");
|
||||||
ret += data.get("targetTop");
|
ret += "\n";
|
||||||
ret += "\n";
|
}
|
||||||
}
|
if (data.has("created_at")) {
|
||||||
if (data.has("created_at")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_eventtime_label);
|
||||||
ret += getString(R.string.careportal_newnstreatment_eventtime_label);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += eventTime.toLocaleString();
|
||||||
ret += eventTime.toLocaleString();
|
ret += "\n";
|
||||||
ret += "\n";
|
}
|
||||||
}
|
if (data.has("enteredBy")) {
|
||||||
if (data.has("enteredBy")) {
|
ret += MainApp.gs(R.string.careportal_newnstreatment_enteredby_title);
|
||||||
ret += getString(R.string.careportal_newnstreatment_enteredby_title);
|
ret += ": ";
|
||||||
ret += ": ";
|
ret += JsonHelper.safeGetObject(data, "enteredBy", "");
|
||||||
ret += data.get("enteredBy");
|
ret += "\n";
|
||||||
ret += "\n";
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
log.error("Unhandled exception", e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void createNSTreatment() {
|
void confirmNSTreatmentCreation() {
|
||||||
final JSONObject data = gatherData();
|
if (context != null) {
|
||||||
String confirmText = buildConfirmText(data);
|
final JSONObject data = gatherData();
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
final String confirmText = buildConfirmText(data);
|
||||||
builder.setTitle(getContext().getString(R.string.confirmation));
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||||||
builder.setMessage(confirmText);
|
builder.setTitle(MainApp.gs(R.string.confirmation));
|
||||||
builder.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() {
|
builder.setMessage(confirmText);
|
||||||
public void onClick(DialogInterface dialog, int id) {
|
builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> createNSTreatment(data));
|
||||||
if (options.executeProfileSwitch) {
|
builder.setNegativeButton(MainApp.gs(R.string.cancel), null);
|
||||||
if (data.has("profile")) {
|
builder.show();
|
||||||
try {
|
}
|
||||||
doProfileSwitch(profileStore, data.getString("profile"), data.getInt("duration"), data.getInt("percentage"), data.getInt("timeshift"));
|
}
|
||||||
} catch (JSONException e) {
|
|
||||||
log.error("Unhandled exception", e);
|
|
||||||
}
|
void createNSTreatment(JSONObject data) {
|
||||||
}
|
if (options.executeProfileSwitch) {
|
||||||
} else if (options.executeTempTarget) {
|
if (data.has("profile")) {
|
||||||
final int duration = JsonHelper.safeGetInt(data, "duration");
|
doProfileSwitch(profileStore, JsonHelper.safeGetString(data, "profile"), JsonHelper.safeGetInt(data, "duration"), JsonHelper.safeGetInt(data, "percentage"), JsonHelper.safeGetInt(data, "timeshift"));
|
||||||
final double targetBottom = JsonHelper.safeGetDouble(data, "targetBottom");
|
|
||||||
final double targetTop = JsonHelper.safeGetDouble(data, "targetTop");
|
|
||||||
final String reason = JsonHelper.safeGetString(data, "reason", "");
|
|
||||||
if ((targetBottom != 0d && targetTop != 0d) || duration == 0) {
|
|
||||||
TempTarget tempTarget = new TempTarget()
|
|
||||||
.date(eventTime.getTime())
|
|
||||||
.duration(duration)
|
|
||||||
.reason(reason)
|
|
||||||
.source(Source.USER);
|
|
||||||
if (tempTarget.durationInMinutes != 0) {
|
|
||||||
tempTarget.low(Profile.toMgdl(targetBottom, profile.getUnits()))
|
|
||||||
.high(Profile.toMgdl(targetTop, profile.getUnits()));
|
|
||||||
} else {
|
|
||||||
tempTarget.low(0).high(0);
|
|
||||||
}
|
|
||||||
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
|
||||||
FabricPrivacy.getInstance().logCustom(new CustomEvent("TempTarget"));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
NSUpload.uploadCareportalEntryToNS(data);
|
|
||||||
FabricPrivacy.getInstance().logCustom(new CustomEvent("NSTreatment"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
} else if (options.executeTempTarget) {
|
||||||
builder.setNegativeButton(getContext().getString(R.string.cancel), null);
|
final int duration = JsonHelper.safeGetInt(data, "duration");
|
||||||
builder.show();
|
final double targetBottom = JsonHelper.safeGetDouble(data, "targetBottom");
|
||||||
|
final double targetTop = JsonHelper.safeGetDouble(data, "targetTop");
|
||||||
|
final String reason = JsonHelper.safeGetString(data, "reason", "");
|
||||||
|
if ((targetBottom != 0d && targetTop != 0d) || duration == 0) {
|
||||||
|
TempTarget tempTarget = new TempTarget()
|
||||||
|
.date(eventTime.getTime())
|
||||||
|
.duration(duration)
|
||||||
|
.reason(reason)
|
||||||
|
.source(Source.USER);
|
||||||
|
if (tempTarget.durationInMinutes != 0) {
|
||||||
|
tempTarget.low(Profile.toMgdl(targetBottom, profile.getUnits()))
|
||||||
|
.high(Profile.toMgdl(targetTop, profile.getUnits()));
|
||||||
|
} else {
|
||||||
|
tempTarget.low(0).high(0);
|
||||||
|
}
|
||||||
|
TreatmentsPlugin.getPlugin().addToHistoryTempTarget(tempTarget);
|
||||||
|
FabricPrivacy.getInstance().logCustom(new CustomEvent("TempTarget"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
NSUpload.uploadCareportalEntryToNS(data);
|
||||||
|
FabricPrivacy.getInstance().logCustom(new CustomEvent("NSTreatment"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift) {
|
public static void doProfileSwitch(final ProfileStore profileStore, final String profileName, final int duration, final int percentage, final int timeshift) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package info.nightscout.utils;
|
package info.nightscout.utils;
|
||||||
|
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -17,6 +19,20 @@ public class JsonHelper {
|
||||||
|
|
||||||
private JsonHelper() {};
|
private JsonHelper() {};
|
||||||
|
|
||||||
|
public static Object safeGetObject(JSONObject json, String fieldName, Object defaultValue) {
|
||||||
|
Object result = defaultValue;
|
||||||
|
|
||||||
|
if (json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.get(fieldName);
|
||||||
|
} catch (JSONException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
public static String safeGetString(JSONObject json, String fieldName) {
|
public static String safeGetString(JSONObject json, String fieldName) {
|
||||||
String result = null;
|
String result = null;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
package info;
|
package info;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
import com.squareup.otto.Bus;
|
import com.squareup.otto.Bus;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.mockito.ArgumentMatchers;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
import org.powermock.api.mockito.PowerMockito;
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -34,6 +40,8 @@ public class AAPSMocker {
|
||||||
private static String validProfile = "{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"30\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"100\"},{\"time\":\"2:00\",\"value\":\"110\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"4\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"5\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}";
|
private static String validProfile = "{\"dia\":\"3\",\"carbratio\":[{\"time\":\"00:00\",\"value\":\"30\"}],\"carbs_hr\":\"20\",\"delay\":\"20\",\"sens\":[{\"time\":\"00:00\",\"value\":\"100\"},{\"time\":\"2:00\",\"value\":\"110\"}],\"timezone\":\"UTC\",\"basal\":[{\"time\":\"00:00\",\"value\":\"1\"}],\"target_low\":[{\"time\":\"00:00\",\"value\":\"4\"}],\"target_high\":[{\"time\":\"00:00\",\"value\":\"5\"}],\"startDate\":\"1970-01-01T00:00:00.000Z\",\"units\":\"mmol\"}";
|
||||||
private static Profile profile;
|
private static Profile profile;
|
||||||
|
|
||||||
|
public static Intent intentSent = null;
|
||||||
|
|
||||||
public static void mockStrings() {
|
public static void mockStrings() {
|
||||||
Locale.setDefault(new Locale("en", "US"));
|
Locale.setDefault(new Locale("en", "US"));
|
||||||
|
|
||||||
|
@ -81,6 +89,7 @@ public class AAPSMocker {
|
||||||
when(MainApp.gs(R.string.basalprofilenotaligned)).thenReturn("Basal values not aligned to hours: %s");
|
when(MainApp.gs(R.string.basalprofilenotaligned)).thenReturn("Basal values not aligned to hours: %s");
|
||||||
when(MainApp.gs(R.string.minago)).thenReturn("%d min ago");
|
when(MainApp.gs(R.string.minago)).thenReturn("%d min ago");
|
||||||
when(MainApp.gs(R.string.hoursago)).thenReturn("%.1fh ago");
|
when(MainApp.gs(R.string.hoursago)).thenReturn("%.1fh ago");
|
||||||
|
when(MainApp.gs(R.string.careportal_profileswitch)).thenReturn("Profile Switch");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MainApp mockMainApp() {
|
public static MainApp mockMainApp() {
|
||||||
|
@ -116,6 +125,28 @@ public class AAPSMocker {
|
||||||
public static void mockApplicationContext() {
|
public static void mockApplicationContext() {
|
||||||
Context context = mock(Context.class);
|
Context context = mock(Context.class);
|
||||||
when(MainApp.instance().getApplicationContext()).thenReturn(context);
|
when(MainApp.instance().getApplicationContext()).thenReturn(context);
|
||||||
|
try {
|
||||||
|
PowerMockito.when(context, "sendBroadcast", ArgumentMatchers.any()).then(invocation -> {
|
||||||
|
Intent i = invocation.getArgument(0);
|
||||||
|
intentSent = i;
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
Assert.fail("Unable to mock the construction of the Context object: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void mockBundle() {
|
||||||
|
try {
|
||||||
|
PowerMockito.whenNew(Bundle.class).withNoArguments().thenAnswer(new Answer<Bundle>() {
|
||||||
|
@Override
|
||||||
|
public Bundle answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return BundleMock.mock();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
Assert.fail("Unable to mock the construction of the Bundle object: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void mockDatabaseHelper() {
|
public static void mockDatabaseHelper() {
|
||||||
|
|
223
app/src/test/java/info/BundleMock.java
Normal file
223
app/src/test/java/info/BundleMock.java
Normal file
|
@ -0,0 +1,223 @@
|
||||||
|
package info;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Parcelable;
|
||||||
|
import android.util.SparseArray;
|
||||||
|
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.anyBoolean;
|
||||||
|
import static org.mockito.Matchers.anyByte;
|
||||||
|
import static org.mockito.Matchers.anyChar;
|
||||||
|
import static org.mockito.Matchers.anyDouble;
|
||||||
|
import static org.mockito.Matchers.anyFloat;
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Matchers.anyLong;
|
||||||
|
import static org.mockito.Matchers.anyShort;
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public final class BundleMock {
|
||||||
|
|
||||||
|
public static Bundle mock() {
|
||||||
|
return mock(new HashMap<String, Object>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Bundle mock(final HashMap<String, Object> map) {
|
||||||
|
|
||||||
|
Answer unsupported = new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Answer put = new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
map.put((String)invocation.getArguments()[0], invocation.getArguments()[1]);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Answer<Object> get = new Answer<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return map.get(invocation.getArguments()[0]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Answer<Object> getOrDefault = new Answer<Object>() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
Object key = invocation.getArguments()[0];
|
||||||
|
return map.containsKey(key) ? map.get(key) : invocation.getArguments()[1];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Bundle bundle = Mockito.mock(Bundle.class);
|
||||||
|
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return map.size();
|
||||||
|
}
|
||||||
|
}).when(bundle).size();
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return map.isEmpty();
|
||||||
|
}
|
||||||
|
}).when(bundle).isEmpty();
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
map.clear();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}).when(bundle).clear();
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return map.containsKey(invocation.getArguments()[0]);
|
||||||
|
}
|
||||||
|
}).when(bundle).containsKey(anyString());
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return map.get(invocation.getArguments()[0]);
|
||||||
|
}
|
||||||
|
}).when(bundle).get(anyString());
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
map.remove(invocation.getArguments()[0]);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}).when(bundle).remove(anyString());
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return map.keySet();
|
||||||
|
}
|
||||||
|
}).when(bundle).keySet();
|
||||||
|
doAnswer(new Answer() {
|
||||||
|
@Override
|
||||||
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return BundleMock.class.getSimpleName() + "{map=" + map.toString() + "}";
|
||||||
|
}
|
||||||
|
}).when(bundle).toString();
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putBoolean(anyString(), anyBoolean());
|
||||||
|
when(bundle.getBoolean(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getBoolean(anyString(), anyBoolean())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putByte(anyString(), anyByte());
|
||||||
|
when(bundle.getByte(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getByte(anyString(), anyByte())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putChar(anyString(), anyChar());
|
||||||
|
when(bundle.getChar(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getChar(anyString(), anyChar())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putInt(anyString(), anyShort());
|
||||||
|
when(bundle.getShort(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getShort(anyString(), anyShort())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putLong(anyString(), anyLong());
|
||||||
|
when(bundle.getLong(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getLong(anyString(), anyLong())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putFloat(anyString(), anyFloat());
|
||||||
|
when(bundle.getFloat(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getFloat(anyString(), anyFloat())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putDouble(anyString(), anyDouble());
|
||||||
|
when(bundle.getDouble(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getDouble(anyString(), anyDouble())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putString(anyString(), anyString());
|
||||||
|
when(bundle.getString(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getString(anyString(), anyString())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putBooleanArray(anyString(), any(boolean[].class));
|
||||||
|
when(bundle.getBooleanArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putLongArray(anyString(), any(long[].class));
|
||||||
|
when(bundle.getLongArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putDoubleArray(anyString(), any(double[].class));
|
||||||
|
when(bundle.getDoubleArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putIntArray(anyString(), any(int[].class));
|
||||||
|
when(bundle.getIntArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putInt(anyString(), anyInt());
|
||||||
|
when(bundle.getInt(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getInt(anyString(), anyInt())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(unsupported).when(bundle).putAll(any(Bundle.class));
|
||||||
|
when(bundle.hasFileDescriptors()).thenAnswer(unsupported);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putShort(anyString(), anyShort());
|
||||||
|
when(bundle.getShort(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getShort(anyString(), anyShort())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putFloat(anyString(), anyFloat());
|
||||||
|
when(bundle.getFloat(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getFloat(anyString(), anyFloat())).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putCharSequence(anyString(), any(CharSequence.class));
|
||||||
|
when(bundle.getCharSequence(anyString())).thenAnswer(get);
|
||||||
|
when(bundle.getCharSequence(anyString(), any(CharSequence.class))).thenAnswer(getOrDefault);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putBundle(anyString(), any(Bundle.class));
|
||||||
|
when(bundle.getBundle(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putParcelable(anyString(), any(Parcelable.class));
|
||||||
|
when(bundle.getParcelable(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putParcelableArray(anyString(), any(Parcelable[].class));
|
||||||
|
when(bundle.getParcelableArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putParcelableArrayList(anyString(), any(ArrayList.class));
|
||||||
|
when(bundle.getParcelableArrayList(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putSparseParcelableArray(anyString(), any(SparseArray.class));
|
||||||
|
when(bundle.getSparseParcelableArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putSerializable(anyString(), any(Serializable.class));
|
||||||
|
when(bundle.getSerializable(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putIntegerArrayList(anyString(), any(ArrayList.class));
|
||||||
|
when(bundle.getIntegerArrayList(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putStringArrayList(anyString(), any(ArrayList.class));
|
||||||
|
when(bundle.getStringArrayList(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putCharSequenceArrayList(anyString(), any(ArrayList.class));
|
||||||
|
when(bundle.getCharSequenceArrayList(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putCharArray(anyString(), any(char[].class));
|
||||||
|
when(bundle.getCharArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putByteArray(anyString(), any(byte[].class));
|
||||||
|
when(bundle.getByteArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putShortArray(anyString(), any(short[].class));
|
||||||
|
when(bundle.getShortArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putFloatArray(anyString(), any(float[].class));
|
||||||
|
when(bundle.getFloatArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
doAnswer(put).when(bundle).putCharSequenceArray(anyString(), any(CharSequence[].class));
|
||||||
|
when(bundle.getCharSequenceArray(anyString())).thenAnswer(get);
|
||||||
|
|
||||||
|
return bundle;
|
||||||
|
}
|
||||||
|
}
|
|
@ -36,8 +36,7 @@ public class SPMocker {
|
||||||
return value;
|
return value;
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Assert.fail("Unable to mock the construction of "
|
Assert.fail("Unable to mock the construction of the SP object");
|
||||||
+ "the SP object");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package info.nightscout.androidaps.plugins.Careportal.Dialogs;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
|
|
||||||
|
import info.AAPSMocker;
|
||||||
|
import info.BundleMock;
|
||||||
|
import info.nightscout.androidaps.MainApp;
|
||||||
|
import info.nightscout.androidaps.R;
|
||||||
|
import info.nightscout.androidaps.plugins.Careportal.CareportalFragment;
|
||||||
|
import info.nightscout.utils.SP;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@RunWith(PowerMockRunner.class)
|
||||||
|
@PrepareForTest({MainApp.class, SP.class, Context.class})
|
||||||
|
public class NewNSTreatmentDialogTest {
|
||||||
|
|
||||||
|
NewNSTreatmentDialog dialog;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void createNSTreatmentTest() throws JSONException {
|
||||||
|
// Test profile creation
|
||||||
|
dialog.setOptions(CareportalFragment.PROFILESWITCH, R.string.careportal_profileswitch);
|
||||||
|
JSONObject data = new JSONObject();
|
||||||
|
data.put("profile", AAPSMocker.getValidProfile().getData());
|
||||||
|
data.put("duration", 0);
|
||||||
|
data.put("percentage", 110);
|
||||||
|
data.put("timeshift", 0);
|
||||||
|
dialog.createNSTreatment(data);
|
||||||
|
|
||||||
|
Bundle bundles = AAPSMocker.intentSent.getExtras();
|
||||||
|
Assert.assertTrue(bundles.getString("profile").contains("00:00"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void doProfileSwitch() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void doProfileSwitch1() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void prepareMock() {
|
||||||
|
AAPSMocker.mockMainApp();
|
||||||
|
AAPSMocker.mockApplicationContext();
|
||||||
|
AAPSMocker.mockSP();
|
||||||
|
AAPSMocker.mockStrings();
|
||||||
|
AAPSMocker.mockBundle();
|
||||||
|
|
||||||
|
dialog = new NewNSTreatmentDialog();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue