diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java index d0acb2a3b4..20229af0f0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Careportal/Dialogs/NewNSTreatmentDialog.java @@ -370,6 +370,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick String enteredBy = SP.getString("careportal_enteredby", ""); JSONObject data = new JSONObject(); try { + boolean allowZeroDuration = false; data.put("created_at", DateUtil.toISOString(eventTime)); switch (options.eventType) { case R.id.careportal_bgcheck: @@ -431,6 +432,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick break; case R.id.careportal_temptarget: data.put("eventType", "Temporary Target"); + allowZeroDuration = true; break; } if (SafeParse.stringToDouble(bgInputEdit.getText().toString()) != 0d) { @@ -443,7 +445,7 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick data.put("carbs", SafeParse.stringToDouble(carbsEdit.getText().toString())); if (SafeParse.stringToDouble(insulinEdit.getText().toString()) != 0d) data.put("insulin", SafeParse.stringToDouble(insulinEdit.getText().toString())); - if (SafeParse.stringToDouble(durationeEdit.getText().toString()) != 0d) + if (allowZeroDuration || SafeParse.stringToDouble(durationeEdit.getText().toString()) != 0d) data.put("duration", SafeParse.stringToDouble(durationeEdit.getText().toString())); if (layoutPercent.getVisibility() != View.GONE) data.put("percent", SafeParse.stringToDouble(percentEdit.getText().toString())); @@ -607,30 +609,38 @@ public class NewNSTreatmentDialog extends DialogFragment implements View.OnClick ConfigBuilderPlugin.uploadCareportalEntryToNS(data); } if (options.executeTempTarget) { - if (data.has("targetBottom") && data.has("targetTop")) { - sHandler.post(new Runnable() { - @Override - public void run() { - try { - TempTarget tempTarget = new TempTarget(); - tempTarget.timeStart = eventTime; - tempTarget.duration = data.getInt("duration"); - tempTarget.reason = data.getString("reason"); - tempTarget.low = NSProfile.toMgdl(data.getDouble("targetBottom"), MainApp.getConfigBuilder().getActiveProfile().getProfile().getUnits()); - tempTarget.high = NSProfile.toMgdl(data.getDouble("targetTop"), MainApp.getConfigBuilder().getActiveProfile().getProfile().getUnits()); - tempTarget.setTimeIndex(tempTarget.getTimeIndex()); - Dao dao = MainApp.getDbHelper().getDaoTempTargets(); - log.debug("Creating new TempTarget db record: " + tempTarget.log()); - dao.createIfNotExists(tempTarget); - MainApp.bus().post(new EventTempTargetRangeChange()); - ConfigBuilderPlugin.uploadCareportalEntryToNS(data); - } catch (JSONException e) { - e.printStackTrace(); - } catch (SQLException e) { - e.printStackTrace(); + + + try { + if ((data.has("targetBottom") && data.has("targetTop")) || (data.has("duration")&& data.getInt("duration") == 0)) { + sHandler.post(new Runnable() { + @Override + public void run() { + try { + TempTarget tempTarget = new TempTarget(); + tempTarget.timeStart = eventTime; + tempTarget.duration = data.getInt("duration"); + tempTarget.reason = data.getString("reason"); + if(tempTarget.duration != 0) { + tempTarget.low = NSProfile.toMgdl(data.getDouble("targetBottom"), MainApp.getConfigBuilder().getActiveProfile().getProfile().getUnits()); + tempTarget.high = NSProfile.toMgdl(data.getDouble("targetTop"), MainApp.getConfigBuilder().getActiveProfile().getProfile().getUnits()); + } + tempTarget.setTimeIndex(tempTarget.getTimeIndex()); + Dao dao = MainApp.getDbHelper().getDaoTempTargets(); + log.debug("Creating new TempTarget db record: " + tempTarget.log()); + dao.createIfNotExists(tempTarget); + MainApp.bus().post(new EventTempTargetRangeChange()); + ConfigBuilderPlugin.uploadCareportalEntryToNS(data); + } catch (JSONException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } } - } - }); + }); + } + } catch (JSONException e) { + e.printStackTrace(); } } else { ConfigBuilderPlugin.uploadCareportalEntryToNS(data); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java index 332da1d8bd..e5d16aa909 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangeFragment.java @@ -83,6 +83,9 @@ public class TempTargetRangeFragment extends Fragment implements View.OnClickLis holder.reason.setText(tempTarget.reason); if (tempTarget.isInProgress()) holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.colorInProgress)); + else if (tempTarget.duration == 0){ + holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.notificationUrgent)); + } else holder.dateLinearLayout.setBackgroundColor(MainApp.instance().getResources().getColor(R.color.cardColorBackground)); holder.remove.setTag(tempTarget); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangePlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangePlugin.java index 8b7068d94f..bad15d0c55 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangePlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempTargetRange/TempTargetRangePlugin.java @@ -91,7 +91,11 @@ public class TempTargetRangePlugin implements PluginBase { @Nullable public TempTarget getTempTargetInProgress(long time) { - for (int i = tempTargets.size() - 1; i >= 0; i--) { + // for (int i = tempTargets.size() - 1; i >= 0; i--) { + for (int i = 0; i < tempTargets.size(); i++) { + // a zero-duration temp target will cancel all prior targets + if (tempTargets.get(i).duration == 0) return null; + if (tempTargets.get(i).timeStart.getTime() > time) continue; if (tempTargets.get(i).getPlannedTimeEnd().getTime() >= time) return tempTargets.get(i); }