diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt index 917ff84630..2fd42278d5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/StatusLightHandler.kt @@ -74,7 +74,7 @@ class StatusLightHandler @Inject constructor( private fun handleAge(view: TextView?, type: TherapyEvent.Type, @StringRes warnSettings: Int, defaultWarnThreshold: Double, @StringRes urgentSettings: Int, defaultUrgentThreshold: Double) { val warn = sp.getDouble(warnSettings, defaultWarnThreshold) val urgent = sp.getDouble(urgentSettings, defaultUrgentThreshold) - val therapyEvent = repository.getLastTherapyRecord(type).blockingGet() + val therapyEvent = repository.getLastTherapyRecordUpToNow(type).blockingGet() if (therapyEvent is ValueWrapper.Existing) { warnColors.setColorByAge(view, therapyEvent.value, warn, urgent) view?.text = therapyEvent.value.age(resourceHelper.shortTextMode(), resourceHelper, dateUtil) diff --git a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt index bdfaf474b1..458eff722e 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -342,8 +342,8 @@ open class AppRepository @Inject internal constructor( fun deleteAllTherapyEventsEntries() = database.therapyEventDao.deleteAllEntries() - fun getLastTherapyRecord(type: TherapyEvent.Type): Single> = - database.therapyEventDao.getLastTherapyRecord(type).toWrappedSingle() + fun getLastTherapyRecordUpToNow(type: TherapyEvent.Type): Single> = + database.therapyEventDao.getLastTherapyRecord(type, System.currentTimeMillis()).toWrappedSingle() .subscribeOn(Schedulers.io()) fun getTherapyEventByTimestamp(type: TherapyEvent.Type, timestamp: Long): TherapyEvent? = diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt index cc42b5b922..5e541b6800 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/TherapyEventDao.kt @@ -39,8 +39,8 @@ internal interface TherapyEventDao : TraceableDao { @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY timestamp ASC") fun getTherapyEventDataIncludingInvalidFromTime(timestamp: Long): Single> - @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND isValid = 1 ORDER BY id DESC LIMIT 1") - fun getLastTherapyRecord(type: TherapyEvent.Type): Maybe + @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE type = :type AND isValid = 1 AND timestamp <= :now ORDER BY id DESC LIMIT 1") + fun getLastTherapyRecord(type: TherapyEvent.Type, now: Long): Maybe @Query("SELECT * FROM $TABLE_THERAPY_EVENTS WHERE timestamp >= :timestamp AND isValid = 1 AND referenceId IS NULL ORDER BY timestamp ASC") fun compatGetTherapyEventDataFromTime(timestamp: Long): Single>