DetailedBolusInfoStorage: Save last 2 records in SP
This commit is contained in:
parent
30c1c16cd0
commit
35778593b2
2 changed files with 32 additions and 2 deletions
|
@ -1,11 +1,15 @@
|
||||||
package info.nightscout.implementation.pump
|
package info.nightscout.implementation.pump
|
||||||
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
import info.nightscout.androidaps.annotations.OpenForTesting
|
import info.nightscout.androidaps.annotations.OpenForTesting
|
||||||
|
import info.nightscout.implementation.R
|
||||||
import info.nightscout.interfaces.pump.DetailedBolusInfo
|
import info.nightscout.interfaces.pump.DetailedBolusInfo
|
||||||
import info.nightscout.interfaces.pump.DetailedBolusInfoStorage
|
import info.nightscout.interfaces.pump.DetailedBolusInfoStorage
|
||||||
import info.nightscout.rx.logging.AAPSLogger
|
import info.nightscout.rx.logging.AAPSLogger
|
||||||
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
import info.nightscout.rx.logging.LTag
|
import info.nightscout.rx.logging.LTag
|
||||||
|
import info.nightscout.shared.interfaces.ResourceHelper
|
||||||
import info.nightscout.shared.utils.T
|
import info.nightscout.shared.utils.T
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
@ -14,10 +18,12 @@ import kotlin.math.abs
|
||||||
@OpenForTesting
|
@OpenForTesting
|
||||||
@Singleton
|
@Singleton
|
||||||
class DetailedBolusInfoStorageImpl @Inject constructor(
|
class DetailedBolusInfoStorageImpl @Inject constructor(
|
||||||
val aapsLogger: AAPSLogger
|
val aapsLogger: AAPSLogger,
|
||||||
|
val sp: SP,
|
||||||
|
val rh: ResourceHelper
|
||||||
) : DetailedBolusInfoStorage {
|
) : DetailedBolusInfoStorage {
|
||||||
|
|
||||||
val store = ArrayList<DetailedBolusInfo>()
|
val store = loadStore()
|
||||||
|
|
||||||
fun DetailedBolusInfo.toJsonString(): String = Gson().toJson(this)
|
fun DetailedBolusInfo.toJsonString(): String = Gson().toJson(this)
|
||||||
|
|
||||||
|
@ -25,6 +31,7 @@ class DetailedBolusInfoStorageImpl @Inject constructor(
|
||||||
override fun add(detailedBolusInfo: DetailedBolusInfo) {
|
override fun add(detailedBolusInfo: DetailedBolusInfo) {
|
||||||
aapsLogger.debug("Stored bolus info: ${detailedBolusInfo.toJsonString()}")
|
aapsLogger.debug("Stored bolus info: ${detailedBolusInfo.toJsonString()}")
|
||||||
store.add(detailedBolusInfo)
|
store.add(detailedBolusInfo)
|
||||||
|
saveStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
|
@ -36,6 +43,7 @@ class DetailedBolusInfoStorageImpl @Inject constructor(
|
||||||
if (bolusTime > d.timestamp - T.mins(1).msecs() && bolusTime < d.timestamp + T.mins(1).msecs() && abs(store[i].insulin - bolus) < 0.01) {
|
if (bolusTime > d.timestamp - T.mins(1).msecs() && bolusTime < d.timestamp + T.mins(1).msecs() && abs(store[i].insulin - bolus) < 0.01) {
|
||||||
aapsLogger.debug(LTag.PUMP, "Using & removing bolus info for time $bolusTime: ${store[i]}")
|
aapsLogger.debug(LTag.PUMP, "Using & removing bolus info for time $bolusTime: ${store[i]}")
|
||||||
store.removeAt(i)
|
store.removeAt(i)
|
||||||
|
saveStore()
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +53,7 @@ class DetailedBolusInfoStorageImpl @Inject constructor(
|
||||||
if (bolusTime > d.timestamp - T.mins(1).msecs() && bolusTime < d.timestamp + T.mins(1).msecs() && bolus <= store[i].insulin + 0.01) {
|
if (bolusTime > d.timestamp - T.mins(1).msecs() && bolusTime < d.timestamp + T.mins(1).msecs() && bolus <= store[i].insulin + 0.01) {
|
||||||
aapsLogger.debug(LTag.PUMP, "Using TIME-ONLY & removing bolus info for time $bolusTime: ${store[i]}")
|
aapsLogger.debug(LTag.PUMP, "Using TIME-ONLY & removing bolus info for time $bolusTime: ${store[i]}")
|
||||||
store.removeAt(i)
|
store.removeAt(i)
|
||||||
|
saveStore()
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,4 +70,24 @@ class DetailedBolusInfoStorageImpl @Inject constructor(
|
||||||
aapsLogger.debug(LTag.PUMP, "Bolus info not found for time $bolusTime")
|
aapsLogger.debug(LTag.PUMP, "Bolus info not found for time $bolusTime")
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun saveStore() {
|
||||||
|
var lastTwoEntries = store
|
||||||
|
// Only save last two entries, to avoid too much data in preferences
|
||||||
|
if (store.size > 2) {
|
||||||
|
lastTwoEntries = ArrayList(store.subList(store.size - 2, store.size))
|
||||||
|
}
|
||||||
|
val jsonString = Gson().toJson(lastTwoEntries)
|
||||||
|
sp.putString(rh.gs(R.string.bolus_storage), jsonString)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadStore(): ArrayList<DetailedBolusInfo> {
|
||||||
|
val jsonString = sp.getString(rh.gs(R.string.bolus_storage), "")
|
||||||
|
return if (jsonString.isNotEmpty()) {
|
||||||
|
val type = object : TypeToken<List<DetailedBolusInfo>>() {}.type
|
||||||
|
Gson().fromJson(jsonString, type)
|
||||||
|
} else {
|
||||||
|
ArrayList()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
|
<string name="bolus_storage" translatable="false">bolus_storage</string>
|
||||||
<string name="bg_label">BG</string>
|
<string name="bg_label">BG</string>
|
||||||
|
|
||||||
<string name="executing_right_now">Command is executed right now</string>
|
<string name="executing_right_now">Command is executed right now</string>
|
||||||
|
|
Loading…
Reference in a new issue