AndroidAPS/app/src/main/java/info/nightscout/androidaps/db/CompatDBHelper.kt

89 lines
4.4 KiB
Kotlin
Raw Normal View History

2021-02-06 00:30:27 +01:00
package info.nightscout.androidaps.db
import info.nightscout.androidaps.database.AppRepository
2021-03-25 17:48:07 +01:00
import info.nightscout.androidaps.database.entities.*
2021-04-01 23:46:21 +02:00
import info.nightscout.androidaps.events.*
2021-12-10 15:19:19 +01:00
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
2021-10-15 14:56:22 +02:00
import info.nightscout.androidaps.plugins.bus.RxBus
2021-02-06 00:30:27 +01:00
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventNewHistoryData
2022-02-10 19:11:58 +01:00
import io.reactivex.rxjava3.disposables.Disposable
2021-02-06 00:30:27 +01:00
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class CompatDBHelper @Inject constructor(
val aapsLogger: AAPSLogger,
val repository: AppRepository,
2021-10-15 14:56:22 +02:00
val rxBus: RxBus
2021-02-06 00:30:27 +01:00
) {
fun dbChangeDisposable(): Disposable = repository
.changeObservable()
.doOnSubscribe {
rxBus.send(EventNewBG(null))
}
.subscribe {
2021-04-12 14:31:15 +02:00
/**
* GlucoseValues can come in batch
* oldest one should be used for invalidation, newest one for for triggering Loop.
* Thus we need to collect both
*
*/
2021-04-29 20:42:45 +02:00
var newestGlucoseValue: GlucoseValue? = null
2021-04-12 14:31:15 +02:00
it.filterIsInstance<GlucoseValue>().lastOrNull()?.let { gv ->
2021-11-16 17:09:47 +01:00
aapsLogger.debug(LTag.DATABASE, "Firing EventNewBg $gv")
2021-04-12 14:31:15 +02:00
rxBus.send(EventNewBG(gv))
newestGlucoseValue = gv
}
it.filterIsInstance<GlucoseValue>().map { gv -> gv.timestamp }.minOrNull()?.let { timestamp ->
2021-11-16 17:09:47 +01:00
aapsLogger.debug(LTag.DATABASE, "Firing EventNewHistoryData $newestGlucoseValue")
2021-04-12 14:31:15 +02:00
rxBus.send(EventNewHistoryData(timestamp, true, newestGlucoseValue))
2021-02-06 00:30:27 +01:00
}
2021-04-12 14:31:15 +02:00
it.filterIsInstance<Carbs>().map { t -> t.timestamp }.minOrNull()?.let { timestamp ->
2021-11-16 17:09:47 +01:00
aapsLogger.debug(LTag.DATABASE, "Firing EventTreatmentChange $timestamp")
2021-04-12 14:31:15 +02:00
rxBus.send(EventTreatmentChange())
rxBus.send(EventNewHistoryData(timestamp, false))
}
it.filterIsInstance<Bolus>().map { t -> t.timestamp }.minOrNull()?.let { timestamp ->
2021-11-16 17:09:47 +01:00
aapsLogger.debug(LTag.DATABASE, "Firing EventTreatmentChange $timestamp")
2021-04-12 14:31:15 +02:00
rxBus.send(EventTreatmentChange())
rxBus.send(EventNewHistoryData(timestamp, false))
}
it.filterIsInstance<TemporaryBasal>().map { t -> t.timestamp }.minOrNull()?.let { timestamp ->
2021-11-16 17:09:47 +01:00
aapsLogger.debug(LTag.DATABASE, "Firing EventTempBasalChange $timestamp")
2021-04-12 14:31:15 +02:00
rxBus.send(EventTempBasalChange())
rxBus.send(EventNewHistoryData(timestamp, false))
}
it.filterIsInstance<ExtendedBolus>().map { t -> t.timestamp }.minOrNull()?.let { timestamp ->
2021-11-16 17:09:47 +01:00
aapsLogger.debug(LTag.DATABASE, "Firing EventExtendedBolusChange $timestamp")
2021-04-12 14:31:15 +02:00
rxBus.send(EventExtendedBolusChange())
rxBus.send(EventNewHistoryData(timestamp, false))
2021-02-06 00:30:27 +01:00
}
2022-04-04 22:00:03 +02:00
it.filterIsInstance<EffectiveProfileSwitch>().firstOrNull()?.let { eps ->
aapsLogger.debug(LTag.DATABASE, "Firing EventEffectiveProfileSwitchChanged $eps")
rxBus.send(EventEffectiveProfileSwitchChanged(eps))
rxBus.send(EventNewHistoryData(eps.timestamp, false))
}
2021-11-16 17:09:47 +01:00
it.filterIsInstance<TemporaryTarget>().firstOrNull()?.let { tt ->
aapsLogger.debug(LTag.DATABASE, "Firing EventTempTargetChange $tt")
2021-02-06 00:30:27 +01:00
rxBus.send(EventTempTargetChange())
}
2021-11-16 17:09:47 +01:00
it.filterIsInstance<TherapyEvent>().firstOrNull()?.let { te ->
aapsLogger.debug(LTag.DATABASE, "Firing EventTherapyEventChange $te")
2021-03-08 20:10:02 +01:00
rxBus.send(EventTherapyEventChange())
}
2021-11-16 17:09:47 +01:00
it.filterIsInstance<Food>().firstOrNull()?.let { food ->
aapsLogger.debug(LTag.DATABASE, "Firing EventFoodDatabaseChanged $food")
2021-03-19 18:49:34 +01:00
rxBus.send(EventFoodDatabaseChanged())
}
2021-11-16 17:09:47 +01:00
it.filterIsInstance<ProfileSwitch>().firstOrNull()?.let { ps ->
aapsLogger.debug(LTag.DATABASE, "Firing EventProfileSwitchChanged $ps")
2021-04-29 20:42:45 +02:00
rxBus.send(EventProfileSwitchChanged())
}
2021-11-16 17:09:47 +01:00
it.filterIsInstance<OfflineEvent>().firstOrNull()?.let { oe ->
aapsLogger.debug(LTag.DATABASE, "Firing EventOfflineChange $oe")
2021-05-28 16:06:44 +02:00
rxBus.send(EventOfflineChange())
}
2021-02-06 00:30:27 +01:00
}
}