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();
Runnable sRefreshLoop = null;
final Object updateSync = new Object();
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledUpdate = null;
@ -901,9 +903,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
}
@SuppressLint("SetTextI18n")
public void updateGUI(String from) {
public void updateGUI(final String from) {
log.debug("updateGUI entered from: " + from);
Date updateGUIStart = new Date();
final Date updateGUIStart = new Date();
if (getActivity() == null)
return;
@ -925,7 +927,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
BgReading actualBG = DatabaseHelper.actualBg();
BgReading lastBG = DatabaseHelper.lastBg();
PumpInterface pump = ConfigBuilderPlugin.getActivePump();
final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
Profile profile = MainApp.getConfigBuilder().getProfile();
String units = profile.getUnits();
@ -937,8 +939,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
return;
}
double lowLine = SP.getDouble("low_mark", 0d);
double highLine = SP.getDouble("high_mark", 0d);
final double lowLine = SP.getDouble("low_mark", Profile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units));
final double highLine = SP.getDouble("high_mark", Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units));
//Start with updating the BG as it is unaffected by loop.
// **** 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 ****
if (lastBG == null) { //left this here as it seems you want to exit at this point if it is null...
return;
}
Integer flag = bgView.getPaintFlags();
@ -1204,7 +1198,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
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)) {
showPredictionView.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 *******
new Thread(new Runnable() {
@Override
public void run() {
// allign to hours
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
@ -1257,9 +1254,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
calendar.add(Calendar.HOUR, 1);
int hoursToFetch;
long toTime;
long fromTime;
long endTime;
final long toTime;
final long fromTime;
final long endTime;
if (showPrediction) {
int predHours = (int) (Math.ceil(((DetermineBasalResultAMA) finalLastRun.constraintsProcessed).getLatestPredictionsTime() - System.currentTimeMillis()) / (60 * 60 * 1000));
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
// remove old data
iobGraph.getSeries().clear();
// ------------------ 1st graph
Profiler.log(log, from + " - 1st graph - START", updateGUIStart);
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 useCobForScale = false;
@ -1300,53 +1324,36 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
}
if (showIobView.isChecked())
secondGraphData.addIob(iobGraph, fromTime, now, useIobForScale, 1d);
secondGraphData.addIob(fromTime, now, useIobForScale, 1d);
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())
secondGraphData.addDeviations(iobGraph, fromTime, now, useDevForScale, 1d);
secondGraphData.addDeviations(fromTime, now, useDevForScale, 1d);
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()) {
iobGraph.setVisibility(View.VISIBLE);
} else {
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
bgGraph.onDataChanged(false, false);
iobGraph.onDataChanged(false, false);
graphData.performUpdate();
secondGraphData.performUpdate();
Profiler.log(log, from + " - onDataChanged", updateGUIStart);
}
});
}
}).start();
Profiler.log(log, from, updateGUIStart);
}
@ -1432,6 +1439,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
}
}
}
}
void updateNotifications() {

View file

@ -45,15 +45,18 @@ import info.nightscout.utils.Round;
public class GraphData {
public GraphData() {
units = MainApp.getConfigBuilder().getProfileUnits();
}
private GraphView graph;
public double maxY = 0;
private List<BgReading> bgReadingsArray;
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;
bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true);
List<DataPointWithLabelInterface> bgListArray = new ArrayList<>();
@ -81,20 +84,18 @@ public class GraphData {
DataPointWithLabelInterface[] bg = new DataPointWithLabelInterface[bgListArray.size()];
bg = bgListArray.toArray(bg);
if (bg.length > 0) {
addSeriesWithoutInvalidate(bgGraph, new PointsWithLabelGraphSeries<>(bg));
}
maxY = maxBgValue;
// set manual y bounds to have nice steps
bgGraph.getViewport().setMaxY(maxY);
bgGraph.getViewport().setMinY(0);
bgGraph.getViewport().setYAxisBoundsManual(true);
bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfVertLines);
graph.getViewport().setMaxY(maxY);
graph.getViewport().setMinY(0);
graph.getViewport().setYAxisBoundsManual(true);
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;
DoubleDataPoint[] inRangeAreaDataPoints = new DoubleDataPoint[]{
@ -102,14 +103,15 @@ public class GraphData {
new DoubleDataPoint(toTime, lowLine, highLine)
};
inRangeAreaSeries = new AreaGraphSeries<>(inRangeAreaDataPoints);
addSeriesWithoutInvalidate(bgGraph, inRangeAreaSeries);
inRangeAreaSeries.setColor(0);
inRangeAreaSeries.setDrawBackground(true);
inRangeAreaSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.inrangebackground));
addSeries(inRangeAreaSeries);
}
// 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> absoluteBasalsLineSeries;
LineGraphSeries<ScaledDataPoint> baseBasalsSeries;
@ -194,7 +196,7 @@ public class GraphData {
basalsLineSeries = new LineGraphSeries<>(basalLine);
Paint paint = new Paint();
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.setColor(MainApp.sResources.getColor(R.color.basal));
basalsLineSeries.setCustomPaint(paint);
@ -204,19 +206,19 @@ public class GraphData {
absoluteBasalsLineSeries = new LineGraphSeries<>(absoluteBasalLine);
Paint absolutePaint = new Paint();
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));
absoluteBasalsLineSeries.setCustomPaint(absolutePaint);
basalScale.setMultiplier(maxY * scale / maxBasalValueFound);
addSeriesWithoutInvalidate(bgGraph, baseBasalsSeries);
addSeriesWithoutInvalidate(bgGraph, tempBasalsSeries);
addSeriesWithoutInvalidate(bgGraph, basalsLineSeries);
addSeriesWithoutInvalidate(bgGraph, absoluteBasalsLineSeries);
addSeries(baseBasalsSeries);
addSeries(tempBasalsSeries);
addSeries(basalsLineSeries);
addSeries(absoluteBasalsLineSeries);
}
public void addTreatments(GraphView bgGraph, long fromTime, long endTime) {
public void addTreatments(long fromTime, long endTime) {
List<DataPointWithLabelInterface> filteredTreatments = new ArrayList<>();
List<Treatment> treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory();
@ -262,9 +264,7 @@ public class GraphData {
DataPointWithLabelInterface[] treatmentsArray = new DataPointWithLabelInterface[filteredTreatments.size()];
treatmentsArray = filteredTreatments.toArray(treatmentsArray);
if (treatmentsArray.length > 0) {
addSeriesWithoutInvalidate(bgGraph, new PointsWithLabelGraphSeries<>(treatmentsArray));
}
addSeries(new PointsWithLabelGraphSeries<>(treatmentsArray));
}
double getNearestBg(long date) {
@ -279,7 +279,7 @@ public class GraphData {
}
// 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;
List<ScaledDataPoint> iobArray = new ArrayList<>();
Double maxIobValueFound = 0d;
@ -310,11 +310,11 @@ public class GraphData {
iobScale.setMultiplier(maxY * scale / maxIobValueFound);
addSeriesWithoutInvalidate(graph, iobSeries);
addSeries(iobSeries);
}
// 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;
List<ScaledDataPoint> cobArray = new ArrayList<>();
Double maxCobValueFound = 0d;
@ -349,11 +349,11 @@ public class GraphData {
cobScale.setMultiplier(maxY * scale / maxCobValueFound);
addSeriesWithoutInvalidate(graph, cobSeries);
addSeries(cobSeries);
}
// 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 {
public int color;
@ -396,11 +396,11 @@ public class GraphData {
devScale.setMultiplier(maxY * scale / maxDevValueFound);
addSeriesWithoutInvalidate(graph, devSeries);
addSeries(devSeries);
}
// 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;
List<DataPoint> ratioArray = new ArrayList<>();
Double maxRatioValueFound = 0d;
@ -426,11 +426,11 @@ public class GraphData {
ratioScale.setMultiplier(maxY * scale / maxRatioValueFound);
addSeriesWithoutInvalidate(graph, ratioSeries);
addSeries(ratioSeries);
}
// scale in % of vertical size (like 0.3)
public void addNowLine(GraphView graph, long now) {
public void addNowLine(long now) {
LineGraphSeries<DataPoint> seriesNow;
DataPoint[] nowPoints = new DataPoint[]{
new DataPoint(now, 0),
@ -447,10 +447,10 @@ public class GraphData {
paint.setColor(Color.WHITE);
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().setMinX(fromTime);
graph.getViewport().setXAxisBoundsManual(true);
@ -458,11 +458,23 @@ public class GraphData {
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()) {
s.onGraphViewAttached(bgGraph);
bgGraph.getSeries().add(s);
s.onGraphViewAttached(graph);
graph.getSeries().add(s);
}
}
// draw it
graph.onDataChanged(false, false);
}
}