From 73c2be52486791a942f3fde07af32aa2a7483fb0 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 17 Aug 2017 20:51:24 +0200 Subject: [PATCH] isValid implementation --- .../androidaps/data/DetailedBolusInfo.java | 2 +- .../nightscout/androidaps/db/DatabaseHelper.java | 10 ++++++++++ .../info/nightscout/androidaps/db/Treatment.java | 2 ++ .../plugins/Actions/dialogs/FillDialog.java | 2 +- .../plugins/ConfigBuilder/ConfigBuilderPlugin.java | 4 +--- .../plugins/Overview/OverviewFragment.java | 4 +++- .../PumpDanaRv2/comm/MsgHistoryEvents_v2.java | 12 ++++++------ .../plugins/Treatments/TreatmentsPlugin.java | 7 +++++++ .../fragments/TreatmentsBolusFragment.java | 14 +++++++++++--- .../plugins/Wear/ActionStringHandler.java | 2 +- app/src/main/res/layout/treatments_bolus_item.xml | 9 ++++++++- app/src/main/res/values/strings.xml | 1 + 12 files changed, 52 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java index 00a04df4c4..aa54ac100f 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java +++ b/app/src/main/java/info/nightscout/androidaps/data/DetailedBolusInfo.java @@ -22,12 +22,12 @@ public class DetailedBolusInfo { public double insulin = 0; public double carbs = 0; public int source = Source.NONE; + public boolean isValid = true; public double glucose = 0; // Bg value in current units public String glucoseType = ""; // NS values: Manual, Finger, Sensor public int carbTime = 0; // time shift of carbs in minutes public JSONObject boluscalc = null; // additional bolus wizard info public Context context = null; // context for progress dialog - public boolean addToTreatments = true; public long pumpId = 0; // id of record if comming from pump history (not a newly created treatment) public boolean isSMB = false; // is a Super-MicroBolus } diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index 9734456cc9..2b4cc87f20 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -598,6 +598,16 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { scheduleTreatmentChange(); } + public void update(Treatment treatment) { + try { + getDaoTreatments().update(treatment); + updateEarliestDataChange(treatment.date); + } catch (SQLException e) { + e.printStackTrace(); + } + scheduleTreatmentChange(); + } + public void deleteTreatmentById(String _id) { Treatment stored = findTreatmentById(_id); if (stored != null) { diff --git a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java index 722b534c7d..aebd52ac30 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/Treatment.java +++ b/app/src/main/java/info/nightscout/androidaps/db/Treatment.java @@ -181,6 +181,8 @@ public class Treatment implements DataPointWithLabelInterface { // ----------------- DataPointInterface end -------------------- public Iob iobCalc(long time, double dia) { + if (!isValid) + return new Iob(); InsulinInterface insulinInterface = MainApp.getInsulinIterfaceById(insulinInterfaceID); if (insulinInterface == null) insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java index 834206ca34..8a4ec0be38 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Actions/dialogs/FillDialog.java @@ -164,8 +164,8 @@ public class FillDialog extends DialogFragment implements OnClickListener { DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); detailedBolusInfo.insulin = finalInsulinAfterConstraints; detailedBolusInfo.context = context; - detailedBolusInfo.addToTreatments = false; detailedBolusInfo.source = Source.NONE; + detailedBolusInfo.isValid = false; // do not count it in IOB (for pump history) PumpEnactResult result = pump.deliverTreatment(detailedBolusInfo); if (!result.success) { AlertDialog.Builder builder = new AlertDialog.Builder(context); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index e4e4aac7cc..6bc0e1e807 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -884,10 +884,8 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain @Override // return true if new record is created public boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo) { - if (!detailedBolusInfo.addToTreatments) - return false; boolean newRecordCreated = activeTreatments.addToHistoryTreatment(detailedBolusInfo); - if (newRecordCreated) + if (newRecordCreated && detailedBolusInfo.isValid) NSUpload.uploadBolusWizardRecord(detailedBolusInfo); return newRecordCreated; } 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 c9719427c0..342f6bd20f 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 @@ -1597,7 +1597,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, List treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory(); for (int tx = 0; tx < treatments.size(); tx++) { - DataPointWithLabelInterface t = treatments.get(tx); + Treatment t = treatments.get(tx); + if (!t.isValid) + continue; if (t.getX() < fromTime || t.getX() > endTime) continue; t.setY(getNearestBg((long) t.getX(), bgReadingsArray)); filteredTreatments.add(t); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java index 5c1b20a9ea..416a96c269 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/PumpDanaRv2/comm/MsgHistoryEvents_v2.java @@ -99,14 +99,14 @@ public class MsgHistoryEvents_v2 extends MessageBase { MainApp.getConfigBuilder().addToHistoryExtendedBolus(extendedBolus); break; case DanaRPump.BOLUS: - log.debug("EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); detailedBolusInfo.insulin = param1 / 100d; - MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + boolean newRecord = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + log.debug((newRecord ? "**NEW** " : "") + "EVENT BOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); break; case DanaRPump.DUALBOLUS: - log.debug("EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); detailedBolusInfo.insulin = param1 / 100d; - MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + newRecord = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + log.debug((newRecord ? "**NEW** " : "") + "EVENT DUALBOLUS (" + recordCode + ") " + datetime.toLocaleString() + " Bolus: " + (param1 / 100d) + "U Duration: " + param2 + "min"); break; case DanaRPump.DUALEXTENDEDSTART: log.debug("EVENT DUALEXTENDEDSTART (" + recordCode + ") " + datetime.toLocaleString() + " Amount: " + (param1 / 100d) + "U Duration: " + param2 + "min"); @@ -134,9 +134,9 @@ public class MsgHistoryEvents_v2 extends MessageBase { log.debug("EVENT PROFILECHANGE (" + recordCode + ") " + datetime.toLocaleString() + " No: " + param1 + " CurrentRate: " + (param2 / 100d) + "U/h"); break; case DanaRPump.CARBS: - log.debug("EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g"); detailedBolusInfo.carbs = param1; - MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + newRecord = MainApp.getConfigBuilder().addToHistoryTreatment(detailedBolusInfo); + log.debug((newRecord ? "**NEW** " : "") + "EVENT CARBS (" + recordCode + ") " + datetime.toLocaleString() + " Carbs: " + param1 + "g"); break; default: log.debug("Event: " + recordCode + " " + datetime.toLocaleString() + " Param1: " + param1 + " Param2: " + param2); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 34fca17d27..acaecac190 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -179,6 +179,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { for (Integer pos = 0; pos < treatments.size(); pos++) { Treatment t = treatments.get(pos); + if (!t.isValid) continue; if (t.date > time) continue; Iob tIOB = t.iobCalc(time, dia); total.iob += tIOB.iobContrib; @@ -222,6 +223,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { long dia_ago = now - (new Double(1.5d * profile.getDia() * 60 * 60 * 1000l)).longValue(); for (Treatment treatment : treatments) { + if (!treatment.isValid) + continue; long t = treatment.date; if (t > dia_ago && t <= now) { if (treatment.carbs >= 1) { @@ -250,6 +253,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { List in5minback = new ArrayList<>(); for (Integer pos = 0; pos < treatments.size(); pos++) { Treatment t = treatments.get(pos); + if (!t.isValid) + continue; if (t.date <= time && t.date > time - 5 * 60 * 1000 && t.carbs > 0) in5minback.add(t); } @@ -409,6 +414,8 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { treatment.source = detailedBolusInfo.source; treatment.pumpId = detailedBolusInfo.pumpId; treatment.insulin = detailedBolusInfo.insulin; + treatment.isValid = detailedBolusInfo.isValid; + treatment.isSMB = detailedBolusInfo.isSMB; if (detailedBolusInfo.carbTime == 0) treatment.carbs = detailedBolusInfo.carbs; treatment.source = detailedBolusInfo.source; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java index 0e7d94cd88..b47cd3a6bf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsBolusFragment.java @@ -85,6 +85,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. holder.mealOrCorrection.setText(t.mealBolus ? MainApp.sResources.getString(R.string.mealbolus) : MainApp.sResources.getString(R.string.correctionbous)); holder.ph.setVisibility(t.source == Source.PUMP ? View.VISIBLE : View.GONE); holder.ns.setVisibility(t._id != null ? View.VISIBLE : View.GONE); + holder.invalid.setVisibility(t.isValid ? View.GONE : View.VISIBLE); if (iob.iobContrib != 0) holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); else @@ -113,6 +114,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. TextView remove; TextView ph; TextView ns; + TextView invalid; TreatmentsViewHolder(View itemView) { super(itemView); @@ -125,6 +127,7 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. mealOrCorrection = (TextView) itemView.findViewById(R.id.treatments_mealorcorrection); ph = (TextView) itemView.findViewById(R.id.pump_sign); ns = (TextView) itemView.findViewById(R.id.ns_sign); + invalid = (TextView) itemView.findViewById(R.id.invalid_sign); remove = (TextView) itemView.findViewById(R.id.treatments_remove); remove.setOnClickListener(this); remove.setPaintFlags(remove.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); @@ -141,10 +144,15 @@ public class TreatmentsBolusFragment extends SubscriberFragment implements View. builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { final String _id = treatment._id; - if (_id != null && !_id.equals("")) { - NSUpload.removeCareportalEntryFromNS(_id); + if (treatment.source == Source.PUMP) { + treatment.isValid = false; + MainApp.getDbHelper().update(treatment); + } else { + if (_id != null && !_id.equals("")) { + NSUpload.removeCareportalEntryFromNS(_id); + } + MainApp.getDbHelper().delete(treatment); } - MainApp.getDbHelper().delete(treatment); updateGUI(); Answers.getInstance().logCustom(new CustomEvent("RemoveTreatment")); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java index cbff033fb6..a4bbd34f1c 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Wear/ActionStringHandler.java @@ -615,7 +615,7 @@ public class ActionStringHandler { public void run() { DetailedBolusInfo detailedBolusInfo = new DetailedBolusInfo(); detailedBolusInfo.insulin = amount; - detailedBolusInfo.addToTreatments = false; + detailedBolusInfo.isValid = false; detailedBolusInfo.source = Source.USER; PumpEnactResult result = MainApp.getConfigBuilder().deliverTreatment(detailedBolusInfo); if (!result.success) { diff --git a/app/src/main/res/layout/treatments_bolus_item.xml b/app/src/main/res/layout/treatments_bolus_item.xml index a7189f7dc7..e8a06151e8 100644 --- a/app/src/main/res/layout/treatments_bolus_item.xml +++ b/app/src/main/res/layout/treatments_bolus_item.xml @@ -80,7 +80,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" - android:layout_marginRight="30dp" android:textStyle="bold" /> + + Ultra-Rapid Oref "DIA of %s too short - using %s instead!" ACTIVATE PROFILE + INVALID