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 a0632068cd..c3003ac9fd 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -143,16 +143,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return null; } - public List getHoursOfBg(int hoursToFetch) { + public List getDataFromTime(long mills) { try { Dao daoBgreadings = getDaoBgReadings(); List bgReadings; QueryBuilder queryBuilder = daoBgreadings.queryBuilder(); queryBuilder.orderBy("timeIndex", false); Where where = queryBuilder.where(); - long now = new Date().getTime(); - long dayAgo = now - hoursToFetch * 60 * 60 * 1000l; - where.ge("timeIndex", (long) Math.ceil(dayAgo / 60000d)); + where.ge("timeIndex", (long) Math.ceil(mills / 60000d)); PreparedQuery preparedQuery = queryBuilder.prepare(); bgReadings = daoBgreadings.query(preparedQuery); return bgReadings; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index f9599d3292..77422dfedd 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -12,6 +12,8 @@ import android.widget.TextView; import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter; +import com.jjoe64.graphview.series.DataPoint; +import com.jjoe64.graphview.series.LineGraphSeries; import com.jjoe64.graphview.series.PointsGraphSeries; import com.squareup.otto.Subscribe; @@ -23,6 +25,7 @@ import java.util.Calendar; import java.util.Date; import java.util.List; +import info.nightscout.androidaps.Constants; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; @@ -86,15 +89,36 @@ public class OverviewFragment extends Fragment implements PluginBase { if (profile != null && bgReading != null && bg != null) { bg.setText(bgReading.valueToUnitsToString(profile.getUnits())); BgReading.units = profile.getUnits(); - } + } else + return; // Skip if not initialized yet if (bgGraph == null) return; - int hoursToFetch = 6; + // allign to hours + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(new Date().getTime()); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.add(Calendar.HOUR, 1); - List bgReadingsArray = MainApp.getDbHelper().getHoursOfBg(hoursToFetch); + int hoursToFetch = 6; + long toTime = calendar.getTimeInMillis(); + long fromTime = toTime - hoursToFetch * 60 * 60 * 1000l; + + Double lowLine = 80d; // TODO: make this customisable + Double highLine = 180d; + Double maxY = 400d; // TODO: add some scale support + + String units = profile.getUnits(); + if (units.equals(Constants.MMOL)) { + lowLine = 4d; + highLine = 10d; + maxY = 20d; + } + + List bgReadingsArray = MainApp.getDbHelper().getDataFromTime(fromTime); BgReading[] bgReadings = new BgReading[bgReadingsArray.size()]; bgReadings = bgReadingsArray.toArray(bgReadings); @@ -107,24 +131,33 @@ public class OverviewFragment extends Fragment implements PluginBase { series.setSize(5); series.setColor(Color.GREEN); - // allign to hours - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(new Date().getTime()); - calendar.set(Calendar.SECOND, 0); - calendar.set(Calendar.MINUTE, 0); - calendar.add(Calendar.HOUR, 1); + // targets + LineGraphSeries seriesLow = new LineGraphSeries(new DataPoint[]{ + new DataPoint(fromTime, lowLine), + new DataPoint(toTime, lowLine) + }); + seriesLow.setColor(Color.RED); + bgGraph.addSeries(seriesLow); + + LineGraphSeries seriesHigh = new LineGraphSeries(new DataPoint[]{ + new DataPoint(fromTime, highLine), + new DataPoint(toTime, highLine) + }); + seriesHigh.setColor(Color.RED); + bgGraph.addSeries(seriesHigh); + + // set manual x bounds to have nice steps - bgGraph.getViewport().setMaxX(calendar.getTimeInMillis()); - bgGraph.getViewport().setMinX(calendar.getTimeInMillis() - hoursToFetch * 60 * 60 * 1000l); + bgGraph.getViewport().setMaxX(toTime); + bgGraph.getViewport().setMinX(fromTime); bgGraph.getViewport().setXAxisBoundsManual(true); - bgGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(),"HH")); + bgGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH")); bgGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space String test = new SimpleDateFormat("HH").format(calendar.getTimeInMillis()); // set manual y bounds to have nice steps - // TODO: MGDL support, some scale support - bgGraph.getViewport().setMaxY(20); + bgGraph.getViewport().setMaxY(maxY); bgGraph.getViewport().setMinY(0); bgGraph.getViewport().setYAxisBoundsManual(true); bgGraph.getGridLabelRenderer().setNumVerticalLabels(11);