fire immediatelly TreatmentChange if not from NS

This commit is contained in:
Milos Kozak 2020-03-06 20:27:39 +01:00
parent 4cca305e1b
commit 1e02773b71

View file

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