ECarbs: Allow remove carbs in the future
This commit is contained in:
parent
1c6293effb
commit
aad1e08c46
|
@ -17,6 +17,7 @@ import info.nightscout.androidaps.database.entities.Carbs
|
||||||
import info.nightscout.androidaps.database.entities.UserEntry.Action
|
import info.nightscout.androidaps.database.entities.UserEntry.Action
|
||||||
import info.nightscout.androidaps.database.entities.UserEntry.Sources
|
import info.nightscout.androidaps.database.entities.UserEntry.Sources
|
||||||
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
import info.nightscout.androidaps.database.entities.ValueWithUnit
|
||||||
|
import info.nightscout.androidaps.database.transactions.CutCarbsTransaction
|
||||||
import info.nightscout.androidaps.database.transactions.InvalidateBolusCalculatorResultTransaction
|
import info.nightscout.androidaps.database.transactions.InvalidateBolusCalculatorResultTransaction
|
||||||
import info.nightscout.androidaps.database.transactions.InvalidateBolusTransaction
|
import info.nightscout.androidaps.database.transactions.InvalidateBolusTransaction
|
||||||
import info.nightscout.androidaps.database.transactions.InvalidateCarbsTransaction
|
import info.nightscout.androidaps.database.transactions.InvalidateCarbsTransaction
|
||||||
|
@ -30,8 +31,6 @@ import info.nightscout.androidaps.extensions.iobCalc
|
||||||
import info.nightscout.androidaps.extensions.toVisibility
|
import info.nightscout.androidaps.extensions.toVisibility
|
||||||
import info.nightscout.androidaps.interfaces.ActivePlugin
|
import info.nightscout.androidaps.interfaces.ActivePlugin
|
||||||
import info.nightscout.androidaps.interfaces.ProfileFunction
|
import info.nightscout.androidaps.interfaces.ProfileFunction
|
||||||
import info.nightscout.shared.logging.AAPSLogger
|
|
||||||
import info.nightscout.shared.logging.LTag
|
|
||||||
import info.nightscout.androidaps.logging.UserEntryLogger
|
import info.nightscout.androidaps.logging.UserEntryLogger
|
||||||
import info.nightscout.androidaps.plugins.bus.RxBus
|
import info.nightscout.androidaps.plugins.bus.RxBus
|
||||||
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart
|
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart
|
||||||
|
@ -43,6 +42,8 @@ import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
||||||
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
|
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
|
||||||
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
import info.nightscout.androidaps.utils.resources.ResourceHelper
|
||||||
import info.nightscout.androidaps.utils.rx.AapsSchedulers
|
import info.nightscout.androidaps.utils.rx.AapsSchedulers
|
||||||
|
import info.nightscout.shared.logging.AAPSLogger
|
||||||
|
import info.nightscout.shared.logging.LTag
|
||||||
import info.nightscout.shared.sharedPreferences.SP
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
import io.reactivex.Completable
|
import io.reactivex.Completable
|
||||||
import io.reactivex.disposables.CompositeDisposable
|
import io.reactivex.disposables.CompositeDisposable
|
||||||
|
@ -131,15 +132,26 @@ class TreatmentsBolusCarbsFragment : DaggerFragment() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
repository
|
repository
|
||||||
.getCarbsDataFromTime(dateUtil.now(), false)
|
.getCarbsDataFromTimeNotExpanded(dateUtil.now(), false)
|
||||||
.observeOn(aapsSchedulers.main)
|
.observeOn(aapsSchedulers.main)
|
||||||
.subscribe { list ->
|
.subscribe { list ->
|
||||||
list.forEach { carb ->
|
list.forEach { carb ->
|
||||||
|
if (carb.duration == 0L)
|
||||||
disposable += repository.runTransactionForResult(InvalidateCarbsTransaction(carb.id))
|
disposable += repository.runTransactionForResult(InvalidateCarbsTransaction(carb.id))
|
||||||
.subscribe(
|
.subscribe(
|
||||||
{ result -> result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") } },
|
{ result -> result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") } },
|
||||||
{ aapsLogger.error(LTag.DATABASE, "Error while invalidating carbs", it) }
|
{ aapsLogger.error(LTag.DATABASE, "Error while invalidating carbs", it) }
|
||||||
)
|
)
|
||||||
|
else {
|
||||||
|
disposable += repository.runTransactionForResult(CutCarbsTransaction(carb.id, dateUtil.now()))
|
||||||
|
.subscribe(
|
||||||
|
{ result ->
|
||||||
|
result.invalidated.forEach { aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it") }
|
||||||
|
result.updated.forEach { aapsLogger.debug(LTag.DATABASE, "Updated (cut end) carbs $it") }
|
||||||
|
},
|
||||||
|
{ aapsLogger.error(LTag.DATABASE, "Error while invalidating carbs", it) }
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
repository
|
repository
|
||||||
|
|
|
@ -131,6 +131,13 @@ class NSClientAddUpdateWorker(
|
||||||
)
|
)
|
||||||
aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it")
|
aapsLogger.debug(LTag.DATABASE, "Invalidated carbs $it")
|
||||||
}
|
}
|
||||||
|
result.updated.forEach {
|
||||||
|
uel.log(Action.CARBS, Sources.NSClient,
|
||||||
|
ValueWithUnit.Timestamp(it.timestamp),
|
||||||
|
ValueWithUnit.Gram(it.amount.toInt())
|
||||||
|
)
|
||||||
|
aapsLogger.debug(LTag.DATABASE, "Updated carbs $it")
|
||||||
|
}
|
||||||
result.updatedNsId.forEach {
|
result.updatedNsId.forEach {
|
||||||
aapsLogger.debug(LTag.DATABASE, "Updated nsId carbs $it")
|
aapsLogger.debug(LTag.DATABASE, "Updated nsId carbs $it")
|
||||||
}
|
}
|
||||||
|
|
|
@ -543,6 +543,11 @@ import kotlin.math.roundToInt
|
||||||
.map { if (!ascending) it.reversed() else it }
|
.map { if (!ascending) it.reversed() else it }
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
|
|
||||||
|
fun getCarbsDataFromTimeNotExpanded(timestamp: Long, ascending: Boolean): Single<List<Carbs>> =
|
||||||
|
database.carbsDao.getCarbsFromTimeExpandable(timestamp)
|
||||||
|
.map { if (!ascending) it.reversed() else it }
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
|
||||||
fun getCarbsDataFromTimeToTime(from: Long, to: Long, ascending: Boolean): Single<List<Carbs>> =
|
fun getCarbsDataFromTimeToTime(from: Long, to: Long, ascending: Boolean): Single<List<Carbs>> =
|
||||||
database.carbsDao.getCarbsFromTimeToTime(from, to)
|
database.carbsDao.getCarbsFromTimeToTime(from, to)
|
||||||
.map { if (!ascending) it.reversed() else it }
|
.map { if (!ascending) it.reversed() else it }
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package info.nightscout.androidaps.database.transactions
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.database.entities.Carbs
|
||||||
|
import info.nightscout.androidaps.database.interfaces.end
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
class CutCarbsTransaction(val id: Long, val end: Long) : Transaction<CutCarbsTransaction.TransactionResult>() {
|
||||||
|
|
||||||
|
override fun run(): TransactionResult {
|
||||||
|
val result = TransactionResult()
|
||||||
|
val carbs = database.carbsDao.findById(id)
|
||||||
|
?: throw IllegalArgumentException("There is no such Carbs with the specified ID.")
|
||||||
|
if (carbs.timestamp == end) {
|
||||||
|
carbs.isValid = false
|
||||||
|
database.carbsDao.updateExistingEntry(carbs)
|
||||||
|
result.invalidated.add(carbs)
|
||||||
|
} else if (end in carbs.timestamp..carbs.end) {
|
||||||
|
val pctRun = (end - carbs.timestamp) / carbs.duration.toDouble()
|
||||||
|
carbs.amount = (carbs.amount * pctRun).roundToInt().toDouble()
|
||||||
|
carbs.end = end
|
||||||
|
database.carbsDao.updateExistingEntry(carbs)
|
||||||
|
result.updated.add(carbs)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
class TransactionResult {
|
||||||
|
|
||||||
|
val invalidated = mutableListOf<Carbs>()
|
||||||
|
val updated = mutableListOf<Carbs>()
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,13 @@ class SyncNsCarbsTransaction(private val carbs: Carbs) : Transaction<SyncNsCarbs
|
||||||
database.carbsDao.updateExistingEntry(current)
|
database.carbsDao.updateExistingEntry(current)
|
||||||
result.invalidated.add(current)
|
result.invalidated.add(current)
|
||||||
}
|
}
|
||||||
|
// and change duration
|
||||||
|
if (current.duration != carbs.duration) {
|
||||||
|
current.amount = carbs.amount
|
||||||
|
current.duration = carbs.duration
|
||||||
|
database.carbsDao.updateExistingEntry(current)
|
||||||
|
result.updated.add(current)
|
||||||
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +50,7 @@ class SyncNsCarbsTransaction(private val carbs: Carbs) : Transaction<SyncNsCarbs
|
||||||
|
|
||||||
class TransactionResult {
|
class TransactionResult {
|
||||||
|
|
||||||
|
val updated = mutableListOf<Carbs>()
|
||||||
val updatedNsId = mutableListOf<Carbs>()
|
val updatedNsId = mutableListOf<Carbs>()
|
||||||
val inserted = mutableListOf<Carbs>()
|
val inserted = mutableListOf<Carbs>()
|
||||||
val invalidated = mutableListOf<Carbs>()
|
val invalidated = mutableListOf<Carbs>()
|
||||||
|
|
Loading…
Reference in a new issue