diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java index 1331c8eedf..1b118da1d3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/treatments/TreatmentService.java @@ -160,7 +160,7 @@ public class TreatmentService extends OrmLiteBaseService { } catch (SQLException e) { log.error("Unhandled exception", e); } - scheduleTreatmentChange(null); + scheduleTreatmentChange(null, true); } @@ -208,18 +208,30 @@ public class TreatmentService extends OrmLiteBaseService { /** * Schedule a foodChange Event. */ - public void scheduleTreatmentChange(@Nullable final Treatment treatment) { - this.scheduleEvent(new EventReloadTreatmentData(new EventTreatmentChange(treatment)), treatmentEventWorker, new ICallback() { - @Override - public void setPost(ScheduledFuture post) { - scheduledTreatmentEventPost = post; + public void scheduleTreatmentChange(@Nullable final Treatment treatment, boolean runImmediately) { + if (runImmediately) { + if (L.isEnabled(L.DATATREATMENTS)) + log.debug("Firing EventReloadTreatmentData"); + RxBus.INSTANCE.send(new EventReloadTreatmentData(new EventTreatmentChange(treatment))); + if (DatabaseHelper.earliestDataChange != null) { + if (L.isEnabled(L.DATATREATMENTS)) + log.debug("Firing EventNewHistoryData"); + RxBus.INSTANCE.send(new EventNewHistoryData(DatabaseHelper.earliestDataChange)); } + DatabaseHelper.earliestDataChange = null; + } else { + this.scheduleEvent(new EventReloadTreatmentData(new EventTreatmentChange(treatment)), treatmentEventWorker, new ICallback() { + @Override + public void setPost(ScheduledFuture post) { + scheduledTreatmentEventPost = post; + } - @Override - public ScheduledFuture getPost() { - return scheduledTreatmentEventPost; - } - }); + @Override + public ScheduledFuture getPost() { + return scheduledTreatmentEventPost; + } + }); + } } public List getTreatmentData() { @@ -294,7 +306,7 @@ public class TreatmentService extends OrmLiteBaseService { getDao().create(existingTreatment); DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); return new UpdateReturn(sameSource, false); //updating a pump treatment with another one from the pump is not counted as clash } return new UpdateReturn(equalRePumpHistory, false); @@ -318,14 +330,14 @@ public class TreatmentService extends OrmLiteBaseService { getDao().create(existingTreatment); DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); return new UpdateReturn(equalRePumpHistory || sameSource, false); } getDao().create(treatment); if (L.isEnabled(L.DATATREATMENTS)) log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, true); } if (treatment.source == Source.NIGHTSCOUT) { @@ -343,7 +355,7 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(old.date); } - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, false); return new UpdateReturn(true, true); } if (L.isEnabled(L.DATATREATMENTS)) @@ -366,7 +378,7 @@ public class TreatmentService extends OrmLiteBaseService { DatabaseHelper.updateEarliestDataChange(oldDate); DatabaseHelper.updateEarliestDataChange(old.date); } - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, false); return new UpdateReturn(true, true); } if (L.isEnabled(L.DATATREATMENTS)) @@ -378,7 +390,7 @@ public class TreatmentService extends OrmLiteBaseService { if (L.isEnabled(L.DATATREATMENTS)) log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, false); return new UpdateReturn(true, true); } if (treatment.source == Source.USER) { @@ -386,7 +398,7 @@ public class TreatmentService extends OrmLiteBaseService { if (L.isEnabled(L.DATATREATMENTS)) log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, true); } } catch (SQLException e) { @@ -414,7 +426,7 @@ public class TreatmentService extends OrmLiteBaseService { if (L.isEnabled(L.DATATREATMENTS)) log.debug("New record from: " + Source.getString(treatment.source) + " " + treatment.toString()); DatabaseHelper.updateEarliestDataChange(treatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, true); } else { @@ -428,7 +440,7 @@ public class TreatmentService extends OrmLiteBaseService { } getDao().update(existingTreatment); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, false); } else { if (MedtronicHistoryData.doubleBolusDebug) @@ -439,7 +451,7 @@ public class TreatmentService extends OrmLiteBaseService { optionalTreatmentCopy(existingTreatment, treatment, fromNightScout); getDao().create(existingTreatment); DatabaseHelper.updateEarliestDataChange(existingTreatment.date); - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); return new UpdateReturn(true, false); //updating a pump treatment with another one from the pump is not counted as clash } } @@ -626,9 +638,13 @@ public class TreatmentService extends OrmLiteBaseService { if (stored != null) { if (L.isEnabled(L.DATATREATMENTS)) log.debug("Removing Treatment record from database: " + stored.toString()); - delete(stored); + try { + getDao().delete(stored); + } catch (SQLException e) { + log.error("Unhandled exception", e); + } DatabaseHelper.updateEarliestDataChange(stored.date); - scheduleTreatmentChange(null); + this.scheduleTreatmentChange(stored, false); } } @@ -643,7 +659,7 @@ public class TreatmentService extends OrmLiteBaseService { try { getDao().delete(treatment); DatabaseHelper.updateEarliestDataChange(treatment.date); - this.scheduleTreatmentChange(treatment); + this.scheduleTreatmentChange(treatment, true); } catch (SQLException e) { log.error("Unhandled exception", e); } @@ -656,7 +672,7 @@ public class TreatmentService extends OrmLiteBaseService { } catch (SQLException e) { log.error("Unhandled exception", e); } - scheduleTreatmentChange(treatment); + scheduleTreatmentChange(treatment, true); } /**