Eros: prevent DB access from UI thread

This commit is contained in:
Milos Kozak 2021-10-19 21:19:25 +02:00
parent 490caf22d0
commit 391cbb575c
4 changed files with 45 additions and 36 deletions

View file

@ -73,7 +73,7 @@ class ProfileTest : TestBase() {
// Test valid profile
var p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(okProfile), dateUtil)!!)
Assert.assertEquals(true, p.isValid("Test", testPumpPlugin, config, resourceHelper, rxBus, hardLimits).isValid)
Assert.assertEquals(true, p.isValid("Test", testPumpPlugin, config, resourceHelper, rxBus, hardLimits, false).isValid)
// Assert.assertEquals(true, p.log().contains("NS units: mmol"))
// JSONAssert.assertEquals(JSONObject(okProfile), p.toPureNsJson(dateUtil), false)
Assert.assertEquals(5.0, p.dia, 0.01)
@ -125,7 +125,7 @@ class ProfileTest : TestBase() {
//Test basal profile below limit
p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(belowLimitValidProfile), dateUtil)!!)
p.isValid("Test", testPumpPlugin, config, resourceHelper, rxBus, hardLimits)
p.isValid("Test", testPumpPlugin, config, resourceHelper, rxBus, hardLimits, false)
// Test profile w/o units
Assert.assertNull(pureProfileFromJson(JSONObject(noUnitsValidProfile), dateUtil))
@ -158,6 +158,6 @@ class ProfileTest : TestBase() {
// Test hour alignment
testPumpPlugin.pumpDescription.is30minBasalRatesCapable = false
p = ProfileSealed.Pure(pureProfileFromJson(JSONObject(notAlignedBasalValidProfile), dateUtil)!!)
p.isValid("Test", testPumpPlugin, config, resourceHelper, rxBus, hardLimits)
p.isValid("Test", testPumpPlugin, config, resourceHelper, rxBus, hardLimits, false)
}
}

View file

@ -1,31 +0,0 @@
package info.nightscout.androidaps.plugins.pump.omnipod.eros.history;
import java.util.List;
import info.nightscout.androidaps.plugins.pump.omnipod.eros.history.database.ErosHistoryRecordDao;
import info.nightscout.androidaps.plugins.pump.omnipod.eros.history.database.ErosHistoryRecordEntity;
import io.reactivex.Single;
public class ErosHistory
{
private final ErosHistoryRecordDao dao;
public ErosHistory(ErosHistoryRecordDao dao) {
this.dao = dao;
}
public List<ErosHistoryRecordEntity> getAllErosHistoryRecordsFromTimestamp(long timeInMillis) {
return dao.allSinceAsc(timeInMillis).blockingGet();
}
public ErosHistoryRecordEntity findErosHistoryRecordByPumpId(long pumpId) {
Single<ErosHistoryRecordEntity> entity = dao.byId(pumpId);
return (entity == null) ? null: entity.blockingGet();
}
public void create(ErosHistoryRecordEntity historyRecord){
// no need for rowId, but lose warnings in IDE and make sure transaction is completed.
long rowId = Single.just(dao.insert(historyRecord)).blockingGet();
}
}

View file

@ -0,0 +1,40 @@
package info.nightscout.androidaps.plugins.pump.omnipod.eros.history
import info.nightscout.androidaps.plugins.pump.omnipod.eros.history.database.ErosHistoryRecordDao
import info.nightscout.androidaps.plugins.pump.omnipod.eros.history.database.ErosHistoryRecordEntity
import io.reactivex.Maybe
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
class ErosHistory(private val dao: ErosHistoryRecordDao) {
fun getAllErosHistoryRecordsFromTimestamp(timeInMillis: Long): List<ErosHistoryRecordEntity> {
return dao.allSinceAsc(timeInMillis)
.subscribeOn(Schedulers.io())
.blockingGet()
}
fun findErosHistoryRecordByPumpId(pumpId: Long): ErosHistoryRecordEntity? {
val record = dao.byId(pumpId)
.subscribeOn(Schedulers.io())
.toWrappedSingle()
.blockingGet()
return if (record is ValueWrapper.Existing) record.value else null
}
fun create(historyRecord: ErosHistoryRecordEntity?) {
// no need for rowId, but lose warnings in IDE and make sure transaction is completed.
val rowId = Single.just(dao.insert(historyRecord!!)).blockingGet()
}
}
@Suppress("USELESS_CAST")
inline fun <reified T : Any> Maybe<T>.toWrappedSingle(): Single<ValueWrapper<T>> =
this.map { ValueWrapper.Existing(it) as ValueWrapper<T> }
.switchIfEmpty(Maybe.just(ValueWrapper.Absent()))
.toSingle()
sealed class ValueWrapper<T> {
data class Existing<T>(val value: T) : ValueWrapper<T>()
class Absent<T> : ValueWrapper<T>()
}

View file

@ -4,7 +4,7 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import io.reactivex.Maybe
import io.reactivex.Single
@Dao
@ -14,7 +14,7 @@ interface ErosHistoryRecordDao {
fun allSinceAsc(since: Long): Single<List<ErosHistoryRecordEntity>>
@Query("SELECT * FROM historyrecords WHERE pumpId = :id LIMIT 1")
fun byId(id: Long): Single<ErosHistoryRecordEntity>?
fun byId(id: Long): Maybe<ErosHistoryRecordEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(ErosHistoryRecordEntity: ErosHistoryRecordEntity): Long