diff --git a/app/build.gradle b/app/build.gradle index 9a7c70ac19..797ac55410 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,4 +31,5 @@ dependencies { compile 'com.j256.ormlite:ormlite-android:4.46' compile 'com.github.tony19:logback-android-classic:1.1.1-4' compile 'org.slf4j:slf4j-api:1.7.12' + compile 'com.jjoe64:graphview:4.0.1' } diff --git a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java index 697bac70a3..eeac3cf264 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/BgReading.java +++ b/app/src/main/java/info/nightscout/androidaps/db/BgReading.java @@ -2,6 +2,8 @@ package info.nightscout.androidaps.db; import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.table.DatabaseTable; +import com.jjoe64.graphview.series.DataPoint; +import com.jjoe64.graphview.series.DataPointInterface; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,7 +16,7 @@ import info.nightscout.client.data.NSCal; import info.nightscout.client.data.NSSgv; @DatabaseTable(tableName = "BgReadings") -public class BgReading { +public class BgReading implements DataPointInterface { private static Logger log = LoggerFactory.getLogger(BgReading.class); public static final DecimalFormat mmolFormat = new DecimalFormat("0.0"); public static final DecimalFormat mgdlFormat = new DecimalFormat("0"); @@ -45,6 +47,8 @@ public class BgReading { @DatabaseField public int battery_level; + public static String units = Constants.MGDL; + public BgReading() {} public BgReading(NSSgv sgv) { @@ -77,4 +81,14 @@ public class BgReading { ", battery_level=" + battery_level + '}'; } + + @Override + public double getX() { + return timestamp; + } + + @Override + public double getY() { + return valueToUnits(units); + } } 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 ddb9614abd..8c594c9bb3 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.db; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -11,6 +12,7 @@ import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.stmt.QueryBuilder; +import com.j256.ormlite.stmt.Where; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; @@ -140,4 +142,24 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { return null; } + + public List get12HoursOfBg() { + 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 - 12 * 60 * 60 * 1000l; + where.ge("timeIndex", (long) Math.ceil(dayAgo / 60000d)); + PreparedQuery preparedQuery = queryBuilder.prepare(); + bgReadings = daoBgreadings.query(preparedQuery); + return bgReadings; + } catch (SQLException e) { + e.printStackTrace(); + } + return new ArrayList(); + } + } 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 40e19d93c2..e95d8b5164 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 @@ -1,22 +1,24 @@ package info.nightscout.androidaps.plugins.Overview; import android.app.Activity; -import android.content.Context; -import android.net.Uri; +import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import com.jjoe64.graphview.GraphView; +import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter; +import com.jjoe64.graphview.series.PointsGraphSeries; import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.BgReading; @@ -29,6 +31,7 @@ public class OverviewFragment extends Fragment { private static Logger log = LoggerFactory.getLogger(OverviewFragment.class); TextView bg; + GraphView bgGraph; public static OverviewFragment newInstance() { OverviewFragment fragment = new OverviewFragment(); @@ -46,6 +49,9 @@ public class OverviewFragment extends Fragment { Bundle savedInstanceState) { View view = inflater.inflate(R.layout.overview_fragment, container, false); bg = (TextView) view.findViewById(R.id.overview_bg); + bgGraph = (GraphView) view.findViewById(R.id.overview_bggraph); + + updateData(); return view; } @@ -61,8 +67,39 @@ public class OverviewFragment extends Fragment { private void updateData() { BgReading bgReading = MainApp.getDbHelper().lastBg(); NSProfile profile = MainApp.getNSProfile(); - if (profile != null && bgReading != null && bg != null) + if (profile != null && bgReading != null && bg != null) { bg.setText(bgReading.valueToUnitsToString(profile.getUnits())); + BgReading.units = profile.getUnits(); + } + + // Skip if not initialized yet + if (bgGraph == null) + return; + + List bgReadingsArray = MainApp.getDbHelper().get12HoursOfBg(); + BgReading[] bgReadings = new BgReading[bgReadingsArray.size()]; + bgReadings = bgReadingsArray.toArray(bgReadings); + + PointsGraphSeries series = new PointsGraphSeries(bgReadings); + bgGraph.addSeries(series); + series.setShape(PointsGraphSeries.Shape.POINT); + series.setSize(5); + series.setColor(Color.GREEN); + + // set date label formatter + bgGraph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity(), android.text.format.DateFormat.getTimeFormat(getActivity()))); + bgGraph.getGridLabelRenderer().setNumVerticalLabels(5); // only 4 because of the space + //bgGraph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space + + // set manual x bounds to have nice steps + bgGraph.getViewport().setMaxX(bgReadings[0].timestamp); + bgGraph.getViewport().setMinX(bgReadings[bgReadings.length-1].timestamp); + bgGraph.getViewport().setXAxisBoundsManual(true); + + // set manual y bounds to have nice steps + bgGraph.getViewport().setMaxY(20); + bgGraph.getViewport().setMinY(0); + bgGraph.getViewport().setYAxisBoundsManual(true); } @Subscribe diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java index deb8b2e6fb..9851fca2b2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/TempBasals/TempBasalsFragment.java @@ -259,7 +259,7 @@ public class TempBasalsFragment extends Fragment { @Subscribe public void onStatusEvent(final EventTempBasalChange ev) { Activity activity = getActivity(); - if (activity != null) + if (activity != null && recyclerView != null) activity.runOnUiThread(new Runnable() { @Override public void run() { @@ -274,7 +274,7 @@ public class TempBasalsFragment extends Fragment { @Subscribe public void onStatusEvent(final EventNewBG ev) { Activity activity = getActivity(); - if (activity != null) + if (activity != null && recyclerView != null) activity.runOnUiThread(new Runnable() { @Override public void run() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java index a24256e7de..b09070a0f3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsFragment.java @@ -251,7 +251,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener @Subscribe public void onStatusEvent(final EventTreatmentChange ev) { Activity activity = getActivity(); - if (activity != null) + if (activity != null && recyclerView != null) activity.runOnUiThread(new Runnable() { @Override public void run() { @@ -265,7 +265,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener @Subscribe public void onStatusEvent(final EventNewBG ev) { Activity activity = getActivity(); - if (activity != null) + if (activity != null && recyclerView != null) activity.runOnUiThread(new Runnable() { @Override public void run() { diff --git a/app/src/main/res/layout/overview_fragment.xml b/app/src/main/res/layout/overview_fragment.xml index 9a3b811023..28f5cc81fe 100644 --- a/app/src/main/res/layout/overview_fragment.xml +++ b/app/src/main/res/layout/overview_fragment.xml @@ -5,10 +5,22 @@ tools:context="info.nightscout.androidaps.plugins.Overview.OverviewFragment"> - + + + + + + +