Fix: prevent eros history db access for main thread
This commit is contained in:
parent
23fdfd61a3
commit
33be14c592
5 changed files with 16 additions and 24 deletions
|
@ -34,7 +34,7 @@ class ErosHistoryTest {
|
|||
|
||||
@Test
|
||||
fun testInsertionAndRetrieval() {
|
||||
var history = erosHistory.getAllErosHistoryRecordsFromTimestamp(0L, true);
|
||||
var history = erosHistory.getAllErosHistoryRecordsFromTimestamp(0L);
|
||||
assert(history.isEmpty())
|
||||
|
||||
val type = PodHistoryEntryType.SET_BOLUS.code.toLong()
|
||||
|
@ -42,14 +42,10 @@ class ErosHistoryTest {
|
|||
erosHistory.create(entity)
|
||||
erosHistory.create(ErosHistoryRecordEntity(3000L, PodHistoryEntryType.CANCEL_BOLUS.code.toLong()))
|
||||
|
||||
history = erosHistory.getAllErosHistoryRecordsFromTimestamp(0L, true);
|
||||
history = erosHistory.getAllErosHistoryRecordsFromTimestamp(0L);
|
||||
assert(history.size == 2)
|
||||
assert(type.equals(history.first().podEntryTypeCode))
|
||||
|
||||
history = erosHistory.getAllErosHistoryRecordsFromTimestamp(0L, false);
|
||||
assert(history.size == 2)
|
||||
assert(type.equals(history.last().podEntryTypeCode))
|
||||
|
||||
val returnedEntity = erosHistory.findErosHistoryRecordByPumpId(entity.pumpId)
|
||||
assertNotNull(returnedEntity)
|
||||
assert(type.equals(returnedEntity.podEntryTypeCode))
|
||||
|
|
|
@ -4,29 +4,28 @@ 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 ErosHistoryRecordDao dao;
|
||||
private final ErosHistoryRecordDao dao;
|
||||
|
||||
public ErosHistory(ErosHistoryRecordDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public List<ErosHistoryRecordEntity> getAllErosHistoryRecordsFromTimestamp(long timeInMillis, boolean ascending) {
|
||||
if (ascending){
|
||||
return dao.allSinceAsc(timeInMillis);
|
||||
}
|
||||
else {
|
||||
return dao.allSinceDesc(timeInMillis);
|
||||
}
|
||||
public List<ErosHistoryRecordEntity> getAllErosHistoryRecordsFromTimestamp(long timeInMillis) {
|
||||
return dao.allSinceAsc(timeInMillis).blockingGet();
|
||||
|
||||
}
|
||||
|
||||
public ErosHistoryRecordEntity findErosHistoryRecordByPumpId(long pumpId) {
|
||||
return dao.byId(pumpId);
|
||||
Single<ErosHistoryRecordEntity> entity = dao.byId(pumpId);
|
||||
return (entity == null) ? null: entity.blockingGet();
|
||||
}
|
||||
|
||||
public void create(ErosHistoryRecordEntity historyRecord){
|
||||
dao.insert(historyRecord);
|
||||
// no need for rowId, but lose warnings in IDE and make sure transaction is completed.
|
||||
long rowId = Single.just(dao.insert(historyRecord)).blockingGet();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ abstract class ErosHistoryDatabase : RoomDatabase() {
|
|||
ErosHistoryDatabase::class.java,
|
||||
"omnipod_eros_history_database.db"
|
||||
)
|
||||
.allowMainThreadQueries()
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
}
|
||||
|
|
|
@ -5,20 +5,18 @@ import androidx.room.Insert
|
|||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import io.reactivex.Single
|
||||
|
||||
@Dao
|
||||
interface ErosHistoryRecordDao {
|
||||
|
||||
@Query("SELECT * from historyrecords WHERE date >= :since order by date asc")
|
||||
fun allSinceAsc(since: Long): List<ErosHistoryRecordEntity>
|
||||
|
||||
@Query("SELECT * from historyrecords WHERE date >= :since order by date desc")
|
||||
fun allSinceDesc(since: Long): List<ErosHistoryRecordEntity>
|
||||
fun allSinceAsc(since: Long): Single<List<ErosHistoryRecordEntity>>
|
||||
|
||||
@Query("SELECT * FROM historyrecords WHERE pumpId = :id LIMIT 1")
|
||||
fun byId(id: Long): ErosHistoryRecordEntity?
|
||||
fun byId(id: Long): Single<ErosHistoryRecordEntity>?
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(ErosHistoryRecordEntity: ErosHistoryRecordEntity)
|
||||
fun insert(ErosHistoryRecordEntity: ErosHistoryRecordEntity): Long
|
||||
|
||||
}
|
||||
|
|
|
@ -68,7 +68,7 @@ public class ErosPodHistoryActivity extends NoSplashAppCompatActivity {
|
|||
GregorianCalendar gc = new GregorianCalendar();
|
||||
gc.add(Calendar.HOUR_OF_DAY, -24);
|
||||
|
||||
fullHistoryList.addAll(erosHistory.getAllErosHistoryRecordsFromTimestamp(gc.getTimeInMillis(), true));
|
||||
fullHistoryList.addAll(erosHistory.getAllErosHistoryRecordsFromTimestamp(gc.getTimeInMillis()));
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue