From b310310511f943e4ffd1b30c877185b9a483afd7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 12 Jun 2016 13:30:34 +0200 Subject: [PATCH] GUI improvements --- .../androidaps/db/DatabaseHelper.java | 9 +- .../androidaps/plugins/APSResult.java | 6 +- .../LowSuspend/LowSuspendFragment.java | 3 +- .../plugins/Overview/OverviewFragment.java | 99 +++++++++++++------ .../nightscout/client/data/NSProfile.java | 17 ++++ app/src/main/res/layout/overview_fragment.xml | 41 ++++++-- app/src/main/res/values/strings.xml | 4 + 7 files changed, 137 insertions(+), 42 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 a5a122b5ca..0f7fb1ef48 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java +++ b/app/src/main/java/info/nightscout/androidaps/db/DatabaseHelper.java @@ -21,6 +21,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; public class DatabaseHelper extends OrmLiteSqliteOpenHelper { private static Logger log = LoggerFactory.getLogger(DatabaseHelper.class); @@ -171,13 +172,13 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper { @Override public String toString() { + Context context = MainApp.instance().getApplicationContext(); DecimalFormat formatNumber0decimalplaces = new DecimalFormat("0"); DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); - return "{\"glucose\"=" + formatNumber0decimalplaces.format(glucose) + - ",\"delta\"=" + formatNumber0decimalplaces.format(delta) + - ",\"avgdelta\"=" + formatNumber2decimalplaces.format(avgdelta) + - "}"; + return context.getString(R.string.glucose) + " " + formatNumber0decimalplaces.format(glucose) + "\n" + + context.getString(R.string.delta) + " " + formatNumber0decimalplaces.format(delta) + "\n" + + context.getString(R.string.avgdelta) + " " + formatNumber2decimalplaces.format(avgdelta); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java b/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java index ad9402393d..27b4650f76 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/APSResult.java @@ -25,9 +25,9 @@ public class APSResult { DecimalFormat formatNumber2decimalplaces = new DecimalFormat("0.00"); if (changeRequested) - return context.getString(R.string.rate) + " " + formatNumber2decimalplaces.format(rate) + " U/h\n" + - context.getString(R.string.duration) + " " + formatNumber0decimalplaces.format(duration) + " min\n" + - context.getString(R.string.reason) + " " + reason; + return context.getString(R.string.rate) + " " + formatNumber2decimalplaces.format(rate) + " U/h\n" + + context.getString(R.string.duration) + " " + formatNumber0decimalplaces.format(duration) + " min\n" + + context.getString(R.string.reason) + " " + reason; else return MainApp.instance().getApplicationContext().getString(R.string.nochangerequested); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java index ef72636f0c..c2d863c6d0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/LowSuspend/LowSuspendFragment.java @@ -102,6 +102,7 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener } MainApp.bus().register(this); } + @Override public void onClick(View view) { switch (view.getId()) { @@ -187,7 +188,7 @@ public class LowSuspendFragment extends Fragment implements View.OnClickListener request.changeRequested = false; request.reason = getString(R.string.nochangerequested); } - } else if (tempBasalRate == 0d){ + } else if (tempBasalRate == 0d) { request.changeRequested = true; request.rate = baseBasalRate; request.duration = 30; 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 7d79cc72fa..baf09c344a 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 @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.Overview; import android.app.Activity; import android.graphics.Color; +import android.graphics.Paint; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; @@ -18,15 +19,17 @@ import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.Iterator; 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; +import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.events.EventNewBG; import info.nightscout.androidaps.events.EventTempBasalChange; import info.nightscout.androidaps.plugins.PluginBase; @@ -36,7 +39,9 @@ import info.nightscout.client.data.NSProfile; public class OverviewFragment extends Fragment implements PluginBase { private static Logger log = LoggerFactory.getLogger(OverviewFragment.class); - TextView bg; + TextView bgView; + TextView timeAgoView; + TextView deltaView; GraphView bgGraph; @@ -65,7 +70,9 @@ public class OverviewFragment extends Fragment implements PluginBase { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.overview_fragment, container, false); - bg = (TextView) view.findViewById(R.id.overview_bg); + bgView = (TextView) view.findViewById(R.id.overview_bg); + timeAgoView = (TextView) view.findViewById(R.id.overview_timeago); + deltaView = (TextView) view.findViewById(R.id.overview_delta); bgGraph = (GraphView) view.findViewById(R.id.overview_bggraph); updateData(); @@ -82,18 +89,39 @@ public class OverviewFragment extends Fragment implements PluginBase { } private void updateData() { - BgReading bgReading = MainApp.getDbHelper().lastBg(); + BgReading actualBG = MainApp.getDbHelper().actualBg(); + BgReading lastBG = MainApp.getDbHelper().lastBg(); NSProfile profile = MainApp.getNSProfile(); - if (profile != null && bgReading != null && bg != null) { - bg.setText(bgReading.valueToUnitsToString(profile.getUnits())); - BgReading.units = profile.getUnits(); - } else + if (profile == null) return; + String units = profile.getUnits(); + // Skip if not initialized yet if (bgGraph == null) return; + if (profile != null && lastBG != null && bgView != null) { + bgView.setText(lastBG.valueToUnitsToString(profile.getUnits())); + DatabaseHelper.GlucoseStatus glucoseStatus = MainApp.getDbHelper().getGlucoseStatusData(); + deltaView.setText(NSProfile.toUnitsString(glucoseStatus.delta, glucoseStatus.delta * Constants.MGDL_TO_MMOLL, units) + " " + units); + BgReading.units = profile.getUnits(); + } else + return; + + // **** BG value **** + Integer flag = bgView.getPaintFlags(); + if (actualBG == null) { + flag |= Paint.STRIKE_THRU_TEXT_FLAG; + } else + flag &= ~Paint.STRIKE_THRU_TEXT_FLAG; + bgView.setPaintFlags(flag); + + Long agoMsec = new Date().getTime() - lastBG.timestamp; + int agoMin = (int) (agoMsec / 60d / 60d / 1000d); + timeAgoView.setText(agoMin + " " + getString(R.string.minago)); + + // **** BG graph **** // allign to hours Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(new Date().getTime()); @@ -105,29 +133,30 @@ public class OverviewFragment extends Fragment implements PluginBase { 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; - } + Double lowLine = NSProfile.toUnits(80d, 4d, units); // TODO: make this customisable + Double highLine = NSProfile.toUnits(180d, 10d, units); + Double maxY = NSProfile.toUnits(400d , 20d, units); // TODO: add some scale support List bgReadingsArray = MainApp.getDbHelper().getDataFromTime(fromTime); - BgReading[] bgReadings = new BgReading[bgReadingsArray.size()]; - bgReadings = bgReadingsArray.toArray(bgReadings); + List inRangeArray = new ArrayList(); + List outOfRangeArray = new ArrayList(); - if (bgReadings.length == 0) + if (bgReadingsArray.size() == 0) return; - PointsGraphSeries series = new PointsGraphSeries(bgReadings); - bgGraph.addSeries(series); - series.setShape(PointsGraphSeries.Shape.POINT); - series.setSize(5); - series.setColor(Color.GREEN); + Iterator it = bgReadingsArray.iterator(); + while (it.hasNext()) { + BgReading bg = it.next(); + if (bg.valueToUnits(units) < lowLine || bg.valueToUnits(units) > highLine) + outOfRangeArray.add(bg); + else + inRangeArray.add(bg); + } + BgReading[] inRange = new BgReading[inRangeArray.size()]; + BgReading[] outOfRange = new BgReading[outOfRangeArray.size()]; + inRange = inRangeArray.toArray(inRange); + outOfRange = outOfRangeArray.toArray(outOfRange); + // targets LineGraphSeries seriesLow = new LineGraphSeries(new DataPoint[]{ @@ -145,6 +174,22 @@ public class OverviewFragment extends Fragment implements PluginBase { bgGraph.addSeries(seriesHigh); + if (inRange.length > 0) { + PointsGraphSeries seriesInRage = new PointsGraphSeries(inRange); + bgGraph.addSeries(seriesInRage); + seriesInRage.setShape(PointsGraphSeries.Shape.POINT); + seriesInRage.setSize(5); + seriesInRage.setColor(Color.GREEN); + } + + if (outOfRange.length > 0) { + PointsGraphSeries seriesOutOfRange = new PointsGraphSeries(outOfRange); + bgGraph.addSeries(seriesOutOfRange); + seriesOutOfRange.setShape(PointsGraphSeries.Shape.POINT); + seriesOutOfRange.setSize(5); + seriesOutOfRange.setColor(Color.RED); + } + // set manual x bounds to have nice steps bgGraph.getViewport().setMaxX(toTime); bgGraph.getViewport().setMinX(fromTime); @@ -152,8 +197,6 @@ public class OverviewFragment extends Fragment implements PluginBase { 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 bgGraph.getViewport().setMaxY(maxY); bgGraph.getViewport().setMinY(0); diff --git a/app/src/main/java/info/nightscout/client/data/NSProfile.java b/app/src/main/java/info/nightscout/client/data/NSProfile.java index c85545abf3..8f3e6776cb 100644 --- a/app/src/main/java/info/nightscout/client/data/NSProfile.java +++ b/app/src/main/java/info/nightscout/client/data/NSProfile.java @@ -299,4 +299,21 @@ public class NSProfile { if (units.equals(Constants.MGDL)) return value; else return value * Constants.MMOLL_TO_MGDL; } + + public static Double fromMgdlToUnits(Double value, String units) { + if (units.equals(Constants.MGDL)) return value; + else return value * Constants.MGDL_TO_MMOLL; + } + + public static Double toUnits(Double valueInMgdl, Double valueInMmol, String units) { + if (units.equals(Constants.MGDL)) return valueInMgdl; + else return valueInMmol; + } + + public static String toUnitsString(Double valueInMgdl, Double valueInMmol, String units) { + DecimalFormat formatNumber0decimalplaces = new DecimalFormat("0"); + DecimalFormat formatNumber1decimalplaces = new DecimalFormat("0.0"); + if (units.equals(Constants.MGDL)) return formatNumber0decimalplaces.format(valueInMgdl); + else return formatNumber1decimalplaces.format(valueInMmol); + } } diff --git a/app/src/main/res/layout/overview_fragment.xml b/app/src/main/res/layout/overview_fragment.xml index 28f5cc81fe..da40789554 100644 --- a/app/src/main/res/layout/overview_fragment.xml +++ b/app/src/main/res/layout/overview_fragment.xml @@ -10,12 +10,41 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + + + + + + + + + + + + Rate: Duration: Reason: + Glucose: + Delta: + Avg. delta: + min ago