From 32ce8af144884ba8e8daa195be9be5762b52527a Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 22 Dec 2019 22:27:43 +0100 Subject: [PATCH] fix loading profiles --- .../androidaps/db/DatabaseHelper.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java index b1fb199fc1..74f7ed234e 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -1602,15 +1602,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { queryBuilder.limit(100L); Where where = queryBuilder.where(); where.ge("date", from); - queryBuilder.setCountOf(true); PreparedQuery preparedQuery = queryBuilder.prepare(); - long count = daoProfileSwitch.countOf(preparedQuery); - // now do query of count + 1 - queryBuilder = daoProfileSwitch.queryBuilder(); - queryBuilder.orderBy("date", ascending); - queryBuilder.limit(count + 1); - preparedQuery = queryBuilder.prepare(); profileSwitches = daoProfileSwitch.query(preparedQuery); + //add last one without duration + ProfileSwitch last = getLastProfileSwitchWithoutDuration(); + if (last != null) { + if (!profileSwitches.contains(last)) + profileSwitches.add(last); + } return profileSwitches; } catch (SQLException e) { log.error("Unhandled exception", e); @@ -1618,6 +1617,28 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return new ArrayList<>(); } + @Nullable + private ProfileSwitch getLastProfileSwitchWithoutDuration() { + try { + Dao daoProfileSwitch = getDaoProfileSwitch(); + List profileSwitches; + QueryBuilder queryBuilder = daoProfileSwitch.queryBuilder(); + queryBuilder.orderBy("date", false); + queryBuilder.limit(1L); + Where where = queryBuilder.where(); + where.eq("durationInMinutes", 0); + PreparedQuery preparedQuery = queryBuilder.prepare(); + profileSwitches = daoProfileSwitch.query(preparedQuery); + if (profileSwitches.size() > 0) + return profileSwitches.get(0); + else + return null; + } catch (SQLException e) { + log.error("Unhandled exception", e); + } + return null; + } + public List getProfileSwitchEventsFromTime(long mills, boolean ascending) { try { Dao daoProfileSwitch = getDaoProfileSwitch();