From 93b1b3702194bc300d5c61e729a598639bb45639 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Tue, 30 Mar 2021 23:24:31 +0200 Subject: [PATCH] expandable carbs query extended --- .../nightscout/androidaps/database/AppRepository.kt | 13 ++++++++++--- .../nightscout/androidaps/database/daos/CarbsDao.kt | 8 +++++++- 2 files changed, 17 insertions(+), 4 deletions(-) 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 e0b8b39b0e..00c5e2c503 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/AppRepository.kt @@ -312,7 +312,6 @@ open class AppRepository @Inject internal constructor( // CARBS - val timeBackForExpand = 8 * 60 * 60 * 1000 private fun expandCarbs(carbs: Carbs): List = if (carbs.duration == 0L) { listOf(carbs) @@ -377,8 +376,9 @@ open class AppRepository @Inject internal constructor( .subscribeOn(Schedulers.io()) fun getCarbsDataFromTimeExpanded(timestamp: Long, ascending: Boolean): Single> = - database.carbsDao.getCarbsFromTime(timestamp - timeBackForExpand) + database.carbsDao.getCarbsFromTimeExpandable(timestamp) .expand() + .from(timestamp) .map { if (!ascending) it.reversed() else it } .subscribeOn(Schedulers.io()) @@ -396,7 +396,14 @@ open class AppRepository @Inject internal constructor( .subscribeOn(Schedulers.io()) fun getCarbsIncludingInvalidFromTime(timestamp: Long, ascending: Boolean): Single> = - database.carbsDao.getCarbsIncludingInvalidFromTime(timestamp - timeBackForExpand) + database.carbsDao.getCarbsIncludingInvalidFromTime(timestamp) + .map { if (!ascending) it.reversed() else it } + .subscribeOn(Schedulers.io()) + + fun getCarbsIncludingInvalidFromTimeExpanded(timestamp: Long, ascending: Boolean): Single> = + database.carbsDao.getCarbsIncludingInvalidFromTimeExpandable(timestamp) + .expand() + .from(timestamp) .map { if (!ascending) it.reversed() else it } .subscribeOn(Schedulers.io()) diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt index f52d0d3a60..025eb04a02 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/CarbsDao.kt @@ -35,6 +35,9 @@ internal interface CarbsDao : TraceableDao { @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") fun getCarbsFromTime(timestamp: Long): Single> + @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp + duration >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + fun getCarbsFromTimeExpandable(timestamp: Long): Single> + @Query("SELECT * FROM $TABLE_CARBS WHERE isValid = 1 AND timestamp >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") fun getCarbsFromTimeToTime(from: Long, to: Long): Single> @@ -44,10 +47,13 @@ internal interface CarbsDao : TraceableDao { @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :timestamp AND referenceId IS NULL ORDER BY id DESC") fun getCarbsIncludingInvalidFromTime(timestamp: Long): Single> + @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp + duration >= :timestamp AND referenceId IS NULL ORDER BY id DESC") + fun getCarbsIncludingInvalidFromTimeExpandable(timestamp: Long): Single> + @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") fun getCarbsIncludingInvalidFromTimeToTime(from: Long, to: Long): Single> - @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") + @Query("SELECT * FROM $TABLE_CARBS WHERE timestamp + duration >= :from AND timestamp <= :to AND referenceId IS NULL ORDER BY id DESC") fun getCarbsIncludingInvalidFromTimeToTimeExpandable(from: Long, to: Long): Single> // This query will be used with v3 to get all changed records