Merge pull request #753 from samspycher/feature/eros-history

Fix for DB access on main thread for eros history module
This commit is contained in:
Milos Kozak 2021-10-19 00:03:29 +02:00 committed by GitHub
commit 5cf325056d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 23 deletions

View file

@ -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))

View file

@ -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();
}
}

View file

@ -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
}

View file

@ -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()));
}