diff --git a/app/src/main/java/info/nightscout/androidaps/Constants.java b/app/src/main/java/info/nightscout/androidaps/Constants.java index 805f469851..959e4fd6ee 100644 --- a/app/src/main/java/info/nightscout/androidaps/Constants.java +++ b/app/src/main/java/info/nightscout/androidaps/Constants.java @@ -43,6 +43,14 @@ public class Constants { public static final int CPP_MIN_PERCENTAGE = 50; public static final int CPP_MAX_PERCENTAGE = 200; + + public static final String MAX_BG_DEFAULT_MGDL = "180"; + public static final String MAX_BG_DEFAULT_MMOL = "10"; + public static final String MIN_BG_DEFAULT_MGDL = "100"; + public static final String MIN_BG_DEFAULT_MMOL = "5"; + public static final String TARGET_BG_DEFAULT_MGDL = "150"; + public static final String TARGET_BG_DEFAULT_MMOL = "7"; + // Very Hard Limits Ranges // First value is the Lowest and second value is the Highest a Limit can define public static final int[] VERY_HARD_LIMIT_MIN_BG = {72,180}; 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..00de620ba1 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,41 @@ 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()); + } else { + tempTarget.low = 0; + tempTarget.high = 0; + } + 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/OpenAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java index 44dac68ec4..9ba9dd48f8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java @@ -147,13 +147,13 @@ public class OpenAPSAMAPlugin implements PluginBase, APSInterface { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); String units = profile.getUnits(); - String maxBgDefault = "180"; - String minBgDefault = "100"; - String targetBgDefault = "150"; + String maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; + String minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; + String targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; if (!units.equals(Constants.MGDL)) { - maxBgDefault = "10"; - minBgDefault = "5"; - targetBgDefault = "7"; + maxBgDefault = Constants.MAX_BG_DEFAULT_MMOL; + minBgDefault = Constants.MIN_BG_DEFAULT_MMOL; + targetBgDefault = Constants.TARGET_BG_DEFAULT_MMOL; } Date now = new Date(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java index 1a11c256e8..f6c7be26e2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSMA/OpenAPSMAPlugin.java @@ -148,13 +148,13 @@ public class OpenAPSMAPlugin implements PluginBase, APSInterface { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext()); String units = profile.getUnits(); - String maxBgDefault = "180"; - String minBgDefault = "100"; - String targetBgDefault = "150"; + String maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; + String minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; + String targetBgDefault = Constants.TARGET_BG_DEFAULT_MGDL; if (!units.equals(Constants.MGDL)) { - maxBgDefault = "10"; - minBgDefault = "5"; - targetBgDefault = "7"; + maxBgDefault = Constants.MAX_BG_DEFAULT_MMOL; + minBgDefault = Constants.MIN_BG_DEFAULT_MMOL; + targetBgDefault = Constants.TARGET_BG_DEFAULT_MMOL; } Date now = new Date(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 2fae08892a..763c6acc80 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -81,7 +81,9 @@ import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotificati import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries; import info.nightscout.androidaps.plugins.Overview.graphExtensions.TimeAsXAxisLabelFormatter; +import info.nightscout.androidaps.plugins.TempBasals.TempBasalsPlugin; import info.nightscout.androidaps.plugins.TempTargetRange.TempTargetRangePlugin; +import info.nightscout.androidaps.plugins.TempTargetRange.events.EventTempTargetRangeChange; import info.nightscout.client.data.NSProfile; import info.nightscout.utils.BolusWizard; import info.nightscout.utils.DateUtil; @@ -430,6 +432,9 @@ public class OverviewFragment extends Fragment { @Subscribe public void onStatusEvent(final EventNewBasalProfile ev) { updateGUIIfVisible(); } + @Subscribe + public void onStatusEvent(final EventTempTargetRangeChange ev) {updateGUIIfVisible();} + @Subscribe public void onStatusEvent(final EventNewNotification n) { updateNotifications(); } @@ -531,11 +536,25 @@ public class OverviewFragment extends Fragment { if (tempTargetRangePlugin != null && tempTargetRangePlugin.isEnabled(PluginBase.GENERAL)) { TempTarget tempTarget = tempTargetRangePlugin.getTempTargetInProgress(new Date().getTime()); if (tempTarget != null) { + tempTargetView.setTextColor(Color.BLACK); + tempTargetView.setBackgroundResource(R.drawable.temptargetborder); tempTargetView.setVisibility(View.VISIBLE); tempTargetView.setText(NSProfile.toUnitsString(tempTarget.low, NSProfile.fromMgdlToUnits(tempTarget.low, profile.getUnits()), profile.getUnits()) + " - " + NSProfile.toUnitsString(tempTarget.high, NSProfile.fromMgdlToUnits(tempTarget.high, profile.getUnits()), profile.getUnits())); } else { - tempTargetView.setVisibility(View.GONE); + + String maxBgDefault = Constants.MAX_BG_DEFAULT_MGDL; + String minBgDefault = Constants.MIN_BG_DEFAULT_MGDL; + if (!profile.getUnits().equals(Constants.MGDL)) { + maxBgDefault = Constants.MAX_BG_DEFAULT_MMOL; + minBgDefault = Constants.MIN_BG_DEFAULT_MMOL; + } + tempTargetView.setTextColor(Color.WHITE); + tempTargetView.setBackgroundResource(R.drawable.temptargetborderdisabled); + tempTargetView.setText(prefs.getString("openapsma_min_bg", minBgDefault) + " - " + prefs.getString("openapsma_max_bg", maxBgDefault)); + tempTargetView.setVisibility(View.VISIBLE); } + } else { + tempTargetView.setVisibility(View.GONE); } // **** Temp button **** @@ -588,6 +607,21 @@ public class OverviewFragment extends Fragment { }); activeProfileView.setLongClickable(true); + + tempTargetView.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View view) { + view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); + NewNSTreatmentDialog newTTDialog = new NewNSTreatmentDialog(); + final OptionsToShow temptarget = new OptionsToShow(R.id.careportal_temptarget, R.string.careportal_temptarget, false, false, false, false, true, false, false, false, false, true); + temptarget.executeTempTarget = true; + newTTDialog.setOptions(temptarget); + newTTDialog.show(getFragmentManager(), "NewNSTreatmentDialog"); + return true; + } + }); + tempTargetView.setLongClickable(true); + // QuickWizard button QuickWizard.QuickWizardEntry quickWizardEntry = getPlugin().quickWizard.getActive(); if (quickWizardEntry != null && lastBG != null && pump.isInitialized()) { 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 384c99836e..6693bbd717 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,12 @@ 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.low.setText(""); + holder.high.setText(""); + holder.duration.setText(R.string.cancel); + 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..177ed8d9fe 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,10 @@ public class TempTargetRangePlugin implements PluginBase { @Nullable public TempTarget getTempTargetInProgress(long time) { - 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); } diff --git a/app/src/main/res/drawable-mdpi-v11/temptargetborderdisabled.xml b/app/src/main/res/drawable-mdpi-v11/temptargetborderdisabled.xml new file mode 100644 index 0000000000..60a46f667e --- /dev/null +++ b/app/src/main/res/drawable-mdpi-v11/temptargetborderdisabled.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/actions_fragment.xml b/app/src/main/res/layout/actions_fragment.xml index 78b6fa00ae..7eb751ac1d 100644 --- a/app/src/main/res/layout/actions_fragment.xml +++ b/app/src/main/res/layout/actions_fragment.xml @@ -32,8 +32,8 @@ android:layout_marginRight="10dp" android:layout_marginTop="3dp" android:layout_weight="0.5" - android:text="@string/careportal_temptarget" - android:textColor="@color/colorTempTargetButton" /> + android:text="@string/careportal_temporarytarget" + android:textColor="@color/colorWizardButton" />