improve GUI response

This commit is contained in:
Milos Kozak 2018-01-03 19:59:18 +01:00
parent 28a52b3ab7
commit 094737adde
2 changed files with 156 additions and 136 deletions

View file

@ -178,6 +178,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
Handler sLoopHandler = new Handler(); Handler sLoopHandler = new Handler();
Runnable sRefreshLoop = null; Runnable sRefreshLoop = null;
final Object updateSync = new Object();
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledUpdate = null; private static ScheduledFuture<?> scheduledUpdate = null;
@ -901,9 +903,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
public void updateGUI(String from) { public void updateGUI(final String from) {
log.debug("updateGUI entered from: " + from); log.debug("updateGUI entered from: " + from);
Date updateGUIStart = new Date(); final Date updateGUIStart = new Date();
if (getActivity() == null) if (getActivity() == null)
return; return;
@ -925,7 +927,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
BgReading actualBG = DatabaseHelper.actualBg(); BgReading actualBG = DatabaseHelper.actualBg();
BgReading lastBG = DatabaseHelper.lastBg(); BgReading lastBG = DatabaseHelper.lastBg();
PumpInterface pump = ConfigBuilderPlugin.getActivePump(); final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
Profile profile = MainApp.getConfigBuilder().getProfile(); Profile profile = MainApp.getConfigBuilder().getProfile();
String units = profile.getUnits(); String units = profile.getUnits();
@ -937,8 +939,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
return; return;
} }
double lowLine = SP.getDouble("low_mark", 0d); final double lowLine = SP.getDouble("low_mark", Profile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units));
double highLine = SP.getDouble("high_mark", 0d); final double highLine = SP.getDouble("high_mark", Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units));
//Start with updating the BG as it is unaffected by loop. //Start with updating the BG as it is unaffected by loop.
// **** BG value **** // **** BG value ****
@ -1142,16 +1144,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
if (lowLine < 1) {
lowLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units);
}
if (highLine < 1) {
highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units);
}
// **** BG value **** // **** BG value ****
if (lastBG == null) { //left this here as it seems you want to exit at this point if it is null... if (lastBG == null) { //left this here as it seems you want to exit at this point if it is null...
return; return;
} }
Integer flag = bgView.getPaintFlags(); Integer flag = bgView.getPaintFlags();
@ -1204,7 +1198,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
cobView.setText(cobText); cobView.setText(cobText);
} }
boolean showPrediction = showPredictionView.isChecked() && finalLastRun != null && finalLastRun.constraintsProcessed.getClass().equals(DetermineBasalResultAMA.class); final boolean showPrediction = showPredictionView.isChecked() && finalLastRun != null && finalLastRun.constraintsProcessed.getClass().equals(DetermineBasalResultAMA.class);
if (MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class) != null && MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class).isEnabled(PluginBase.APS)) { if (MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class) != null && MainApp.getSpecificPlugin(OpenAPSAMAPlugin.class).isEnabled(PluginBase.APS)) {
showPredictionView.setVisibility(View.VISIBLE); showPredictionView.setVisibility(View.VISIBLE);
getActivity().findViewById(R.id.overview_showprediction_label).setVisibility(View.VISIBLE); getActivity().findViewById(R.id.overview_showprediction_label).setVisibility(View.VISIBLE);
@ -1248,6 +1242,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
// ****** GRAPH ******* // ****** GRAPH *******
new Thread(new Runnable() {
@Override
public void run() {
// allign to hours // allign to hours
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis()); calendar.setTimeInMillis(System.currentTimeMillis());
@ -1257,9 +1254,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
calendar.add(Calendar.HOUR, 1); calendar.add(Calendar.HOUR, 1);
int hoursToFetch; int hoursToFetch;
long toTime; final long toTime;
long fromTime; final long fromTime;
long endTime; final long endTime;
if (showPrediction) { if (showPrediction) {
int predHours = (int) (Math.ceil(((DetermineBasalResultAMA) finalLastRun.constraintsProcessed).getLatestPredictionsTime() - System.currentTimeMillis()) / (60 * 60 * 1000)); int predHours = (int) (Math.ceil(((DetermineBasalResultAMA) finalLastRun.constraintsProcessed).getLatestPredictionsTime() - System.currentTimeMillis()) / (60 * 60 * 1000));
predHours = Math.min(2, predHours); predHours = Math.min(2, predHours);
@ -1276,13 +1273,40 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
// 2nd graph // ------------------ 1st graph
// remove old data Profiler.log(log, from + " - 1st graph - START", updateGUIStart);
iobGraph.getSeries().clear();
GraphData secondGraphData = new GraphData(); final GraphData graphData = new GraphData(bgGraph);
// **** In range Area ****
graphData.addInRangeArea(fromTime, endTime, lowLine, highLine);
// **** BG ****
if (showPrediction)
graphData.addBgReadings(fromTime, toTime, lowLine, highLine, (DetermineBasalResultAMA) finalLastRun.constraintsProcessed);
else
graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null);
// set manual x bounds to have nice steps
graphData.formatAxis(fromTime, endTime);
// Treatments
graphData.addTreatments(fromTime, endTime);
// add basal data
if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) {
graphData.addBasals(fromTime, now, lowLine / graphData.maxY / 1.2d);
}
// **** NOW line ****
graphData.addNowLine(now);
// ------------------ 2nd graph
Profiler.log(log, from + " - 2nd graph - START", updateGUIStart);
final GraphData secondGraphData = new GraphData(iobGraph);
boolean useIobForScale = false; boolean useIobForScale = false;
boolean useCobForScale = false; boolean useCobForScale = false;
@ -1300,53 +1324,36 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
if (showIobView.isChecked()) if (showIobView.isChecked())
secondGraphData.addIob(iobGraph, fromTime, now, useIobForScale, 1d); secondGraphData.addIob(fromTime, now, useIobForScale, 1d);
if (showCobView.isChecked()) if (showCobView.isChecked())
secondGraphData.addCob(iobGraph, fromTime, now, useCobForScale, useCobForScale ? 1d : 0.5d); secondGraphData.addCob(fromTime, now, useCobForScale, useCobForScale ? 1d : 0.5d);
if (showDeviationsView.isChecked()) if (showDeviationsView.isChecked())
secondGraphData.addDeviations(iobGraph, fromTime, now, useDevForScale, 1d); secondGraphData.addDeviations(fromTime, now, useDevForScale, 1d);
if (showRatiosView.isChecked()) if (showRatiosView.isChecked())
secondGraphData.addRatio(iobGraph, fromTime, now, useRatioForScale, 1d); secondGraphData.addRatio(fromTime, now, useRatioForScale, 1d);
// **** NOW line ****
// set manual x bounds to have nice steps
secondGraphData.formatAxis(fromTime, endTime);
secondGraphData.addNowLine(now);
// do GUI update
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (showIobView.isChecked() || showCobView.isChecked() || showDeviationsView.isChecked() || showRatiosView.isChecked()) { if (showIobView.isChecked() || showCobView.isChecked() || showDeviationsView.isChecked() || showRatiosView.isChecked()) {
iobGraph.setVisibility(View.VISIBLE); iobGraph.setVisibility(View.VISIBLE);
} else { } else {
iobGraph.setVisibility(View.GONE); iobGraph.setVisibility(View.GONE);
} }
// remove old data from graph
bgGraph.getSeries().clear();
GraphData graphData = new GraphData();
// **** In range Area ****
graphData.addInRangeArea(bgGraph, fromTime, endTime, lowLine, highLine);
// **** BG ****
if (showPrediction)
graphData.addBgReadings(bgGraph, fromTime, toTime, lowLine, highLine, (DetermineBasalResultAMA) finalLastRun.constraintsProcessed);
else
graphData.addBgReadings(bgGraph, fromTime, toTime, lowLine, highLine, null);
// set manual x bounds to have nice steps
graphData.formatAxis(bgGraph, fromTime, endTime);
secondGraphData.formatAxis(iobGraph, fromTime, endTime);
// Treatments
graphData.addTreatments(bgGraph, fromTime, endTime);
// add basal data
if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) {
graphData.addBasals(bgGraph, fromTime, now, lowLine / graphData.maxY / 1.2d);
}
// **** NOW line ****
graphData.addNowLine(bgGraph, now);
secondGraphData.addNowLine(iobGraph, now);
// finaly enforce drawing of graphs // finaly enforce drawing of graphs
bgGraph.onDataChanged(false, false); graphData.performUpdate();
iobGraph.onDataChanged(false, false); secondGraphData.performUpdate();
Profiler.log(log, from + " - onDataChanged", updateGUIStart);
}
});
}
}).start();
Profiler.log(log, from, updateGUIStart); Profiler.log(log, from, updateGUIStart);
} }
@ -1432,6 +1439,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
} }
} }
} }
void updateNotifications() { void updateNotifications() {

View file

@ -45,15 +45,18 @@ import info.nightscout.utils.Round;
public class GraphData { public class GraphData {
public GraphData() { private GraphView graph;
units = MainApp.getConfigBuilder().getProfileUnits();
}
public double maxY = 0; public double maxY = 0;
private List<BgReading> bgReadingsArray; private List<BgReading> bgReadingsArray;
private String units; private String units;
private List<Series> series = new ArrayList<>();
public void addBgReadings(GraphView bgGraph, long fromTime, long toTime, double lowLine, double highLine, DetermineBasalResultAMA amaResult) { public GraphData(GraphView graph) {
units = MainApp.getConfigBuilder().getProfileUnits();
this.graph = graph;
}
public void addBgReadings(long fromTime, long toTime, double lowLine, double highLine, DetermineBasalResultAMA amaResult) {
double maxBgValue = 0d; double maxBgValue = 0d;
bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true); bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true);
List<DataPointWithLabelInterface> bgListArray = new ArrayList<>(); List<DataPointWithLabelInterface> bgListArray = new ArrayList<>();
@ -81,20 +84,18 @@ public class GraphData {
DataPointWithLabelInterface[] bg = new DataPointWithLabelInterface[bgListArray.size()]; DataPointWithLabelInterface[] bg = new DataPointWithLabelInterface[bgListArray.size()];
bg = bgListArray.toArray(bg); bg = bgListArray.toArray(bg);
if (bg.length > 0) {
addSeriesWithoutInvalidate(bgGraph, new PointsWithLabelGraphSeries<>(bg));
}
maxY = maxBgValue; maxY = maxBgValue;
// set manual y bounds to have nice steps // set manual y bounds to have nice steps
bgGraph.getViewport().setMaxY(maxY); graph.getViewport().setMaxY(maxY);
bgGraph.getViewport().setMinY(0); graph.getViewport().setMinY(0);
bgGraph.getViewport().setYAxisBoundsManual(true); graph.getViewport().setYAxisBoundsManual(true);
bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfVertLines); graph.getGridLabelRenderer().setNumVerticalLabels(numOfVertLines);
addSeries(new PointsWithLabelGraphSeries<>(bg));
} }
public void addInRangeArea(GraphView bgGraph, long fromTime, long toTime, double lowLine, double highLine) { public void addInRangeArea(long fromTime, long toTime, double lowLine, double highLine) {
AreaGraphSeries<DoubleDataPoint> inRangeAreaSeries; AreaGraphSeries<DoubleDataPoint> inRangeAreaSeries;
DoubleDataPoint[] inRangeAreaDataPoints = new DoubleDataPoint[]{ DoubleDataPoint[] inRangeAreaDataPoints = new DoubleDataPoint[]{
@ -102,14 +103,15 @@ public class GraphData {
new DoubleDataPoint(toTime, lowLine, highLine) new DoubleDataPoint(toTime, lowLine, highLine)
}; };
inRangeAreaSeries = new AreaGraphSeries<>(inRangeAreaDataPoints); inRangeAreaSeries = new AreaGraphSeries<>(inRangeAreaDataPoints);
addSeriesWithoutInvalidate(bgGraph, inRangeAreaSeries);
inRangeAreaSeries.setColor(0); inRangeAreaSeries.setColor(0);
inRangeAreaSeries.setDrawBackground(true); inRangeAreaSeries.setDrawBackground(true);
inRangeAreaSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.inrangebackground)); inRangeAreaSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.inrangebackground));
addSeries(inRangeAreaSeries);
} }
// scale in % of vertical size (like 0.3) // scale in % of vertical size (like 0.3)
public void addBasals(GraphView bgGraph, long fromTime, long toTime, double scale) { public void addBasals(long fromTime, long toTime, double scale) {
LineGraphSeries<ScaledDataPoint> basalsLineSeries; LineGraphSeries<ScaledDataPoint> basalsLineSeries;
LineGraphSeries<ScaledDataPoint> absoluteBasalsLineSeries; LineGraphSeries<ScaledDataPoint> absoluteBasalsLineSeries;
LineGraphSeries<ScaledDataPoint> baseBasalsSeries; LineGraphSeries<ScaledDataPoint> baseBasalsSeries;
@ -194,7 +196,7 @@ public class GraphData {
basalsLineSeries = new LineGraphSeries<>(basalLine); basalsLineSeries = new LineGraphSeries<>(basalLine);
Paint paint = new Paint(); Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE); paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(MainApp.instance().getApplicationContext().getResources().getDisplayMetrics().scaledDensity*2); paint.setStrokeWidth(MainApp.instance().getApplicationContext().getResources().getDisplayMetrics().scaledDensity * 2);
paint.setPathEffect(new DashPathEffect(new float[]{2, 4}, 0)); paint.setPathEffect(new DashPathEffect(new float[]{2, 4}, 0));
paint.setColor(MainApp.sResources.getColor(R.color.basal)); paint.setColor(MainApp.sResources.getColor(R.color.basal));
basalsLineSeries.setCustomPaint(paint); basalsLineSeries.setCustomPaint(paint);
@ -204,19 +206,19 @@ public class GraphData {
absoluteBasalsLineSeries = new LineGraphSeries<>(absoluteBasalLine); absoluteBasalsLineSeries = new LineGraphSeries<>(absoluteBasalLine);
Paint absolutePaint = new Paint(); Paint absolutePaint = new Paint();
absolutePaint.setStyle(Paint.Style.STROKE); absolutePaint.setStyle(Paint.Style.STROKE);
absolutePaint.setStrokeWidth(MainApp.instance().getApplicationContext().getResources().getDisplayMetrics().scaledDensity*2); absolutePaint.setStrokeWidth(MainApp.instance().getApplicationContext().getResources().getDisplayMetrics().scaledDensity * 2);
absolutePaint.setColor(MainApp.sResources.getColor(R.color.basal)); absolutePaint.setColor(MainApp.sResources.getColor(R.color.basal));
absoluteBasalsLineSeries.setCustomPaint(absolutePaint); absoluteBasalsLineSeries.setCustomPaint(absolutePaint);
basalScale.setMultiplier(maxY * scale / maxBasalValueFound); basalScale.setMultiplier(maxY * scale / maxBasalValueFound);
addSeriesWithoutInvalidate(bgGraph, baseBasalsSeries); addSeries(baseBasalsSeries);
addSeriesWithoutInvalidate(bgGraph, tempBasalsSeries); addSeries(tempBasalsSeries);
addSeriesWithoutInvalidate(bgGraph, basalsLineSeries); addSeries(basalsLineSeries);
addSeriesWithoutInvalidate(bgGraph, absoluteBasalsLineSeries); addSeries(absoluteBasalsLineSeries);
} }
public void addTreatments(GraphView bgGraph, long fromTime, long endTime) { public void addTreatments(long fromTime, long endTime) {
List<DataPointWithLabelInterface> filteredTreatments = new ArrayList<>(); List<DataPointWithLabelInterface> filteredTreatments = new ArrayList<>();
List<Treatment> treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory(); List<Treatment> treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory();
@ -262,9 +264,7 @@ public class GraphData {
DataPointWithLabelInterface[] treatmentsArray = new DataPointWithLabelInterface[filteredTreatments.size()]; DataPointWithLabelInterface[] treatmentsArray = new DataPointWithLabelInterface[filteredTreatments.size()];
treatmentsArray = filteredTreatments.toArray(treatmentsArray); treatmentsArray = filteredTreatments.toArray(treatmentsArray);
if (treatmentsArray.length > 0) { addSeries(new PointsWithLabelGraphSeries<>(treatmentsArray));
addSeriesWithoutInvalidate(bgGraph, new PointsWithLabelGraphSeries<>(treatmentsArray));
}
} }
double getNearestBg(long date) { double getNearestBg(long date) {
@ -279,7 +279,7 @@ public class GraphData {
} }
// scale in % of vertical size (like 0.3) // scale in % of vertical size (like 0.3)
public void addIob(GraphView graph, long fromTime, long toTime, boolean useForScale, double scale) { public void addIob(long fromTime, long toTime, boolean useForScale, double scale) {
FixedLineGraphSeries<ScaledDataPoint> iobSeries; FixedLineGraphSeries<ScaledDataPoint> iobSeries;
List<ScaledDataPoint> iobArray = new ArrayList<>(); List<ScaledDataPoint> iobArray = new ArrayList<>();
Double maxIobValueFound = 0d; Double maxIobValueFound = 0d;
@ -310,11 +310,11 @@ public class GraphData {
iobScale.setMultiplier(maxY * scale / maxIobValueFound); iobScale.setMultiplier(maxY * scale / maxIobValueFound);
addSeriesWithoutInvalidate(graph, iobSeries); addSeries(iobSeries);
} }
// scale in % of vertical size (like 0.3) // scale in % of vertical size (like 0.3)
public void addCob(GraphView graph, long fromTime, long toTime, boolean useForScale, double scale) { public void addCob(long fromTime, long toTime, boolean useForScale, double scale) {
FixedLineGraphSeries<ScaledDataPoint> cobSeries; FixedLineGraphSeries<ScaledDataPoint> cobSeries;
List<ScaledDataPoint> cobArray = new ArrayList<>(); List<ScaledDataPoint> cobArray = new ArrayList<>();
Double maxCobValueFound = 0d; Double maxCobValueFound = 0d;
@ -349,11 +349,11 @@ public class GraphData {
cobScale.setMultiplier(maxY * scale / maxCobValueFound); cobScale.setMultiplier(maxY * scale / maxCobValueFound);
addSeriesWithoutInvalidate(graph, cobSeries); addSeries(cobSeries);
} }
// scale in % of vertical size (like 0.3) // scale in % of vertical size (like 0.3)
public void addDeviations(GraphView graph, long fromTime, long toTime, boolean useForScale, double scale) { public void addDeviations(long fromTime, long toTime, boolean useForScale, double scale) {
class DeviationDataPoint extends ScaledDataPoint { class DeviationDataPoint extends ScaledDataPoint {
public int color; public int color;
@ -396,11 +396,11 @@ public class GraphData {
devScale.setMultiplier(maxY * scale / maxDevValueFound); devScale.setMultiplier(maxY * scale / maxDevValueFound);
addSeriesWithoutInvalidate(graph, devSeries); addSeries(devSeries);
} }
// scale in % of vertical size (like 0.3) // scale in % of vertical size (like 0.3)
public void addRatio(GraphView graph, long fromTime, long toTime, boolean useForScale, double scale) { public void addRatio(long fromTime, long toTime, boolean useForScale, double scale) {
LineGraphSeries<DataPoint> ratioSeries; LineGraphSeries<DataPoint> ratioSeries;
List<DataPoint> ratioArray = new ArrayList<>(); List<DataPoint> ratioArray = new ArrayList<>();
Double maxRatioValueFound = 0d; Double maxRatioValueFound = 0d;
@ -426,11 +426,11 @@ public class GraphData {
ratioScale.setMultiplier(maxY * scale / maxRatioValueFound); ratioScale.setMultiplier(maxY * scale / maxRatioValueFound);
addSeriesWithoutInvalidate(graph, ratioSeries); addSeries(ratioSeries);
} }
// scale in % of vertical size (like 0.3) // scale in % of vertical size (like 0.3)
public void addNowLine(GraphView graph, long now) { public void addNowLine(long now) {
LineGraphSeries<DataPoint> seriesNow; LineGraphSeries<DataPoint> seriesNow;
DataPoint[] nowPoints = new DataPoint[]{ DataPoint[] nowPoints = new DataPoint[]{
new DataPoint(now, 0), new DataPoint(now, 0),
@ -447,10 +447,10 @@ public class GraphData {
paint.setColor(Color.WHITE); paint.setColor(Color.WHITE);
seriesNow.setCustomPaint(paint); seriesNow.setCustomPaint(paint);
addSeriesWithoutInvalidate(graph, seriesNow); addSeries(seriesNow);
} }
public void formatAxis(GraphView graph, long fromTime, long endTime) { public void formatAxis(long fromTime, long endTime) {
graph.getViewport().setMaxX(endTime); graph.getViewport().setMaxX(endTime);
graph.getViewport().setMinX(fromTime); graph.getViewport().setMinX(fromTime);
graph.getViewport().setXAxisBoundsManual(true); graph.getViewport().setXAxisBoundsManual(true);
@ -458,11 +458,23 @@ public class GraphData {
graph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space graph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space
} }
private void addSeriesWithoutInvalidate(GraphView bgGraph, Series s) { private void addSeries(Series s) {
series.add(s);
}
public void performUpdate() {
// clear old data
graph.getSeries().clear();
// add precalculated series
for (Series s: series) {
if (!s.isEmpty()) { if (!s.isEmpty()) {
s.onGraphViewAttached(bgGraph); s.onGraphViewAttached(graph);
bgGraph.getSeries().add(s); graph.getSeries().add(s);
} }
} }
// draw it
graph.onDataChanged(false, false);
}
} }