revert asynctask

This commit is contained in:
Milos Kozak 2017-06-06 14:49:17 +02:00
parent f93090b012
commit b8b00dbc81

View file

@ -7,7 +7,6 @@ import android.content.DialogInterface;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.DashPathEffect; import android.graphics.DashPathEffect;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
@ -54,6 +53,8 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
@ -189,8 +190,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor();
private static ScheduledFuture<?> scheduledUpdate = null; private static ScheduledFuture<?> scheduledUpdate = null;
private static UpdateGUIAsyncTask updateGUIAsyncTask;
public OverviewFragment() { public OverviewFragment() {
super(); super();
if (sHandlerThread == null) { if (sHandlerThread == null) {
@ -324,6 +323,14 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
}); });
Timer timeTimer = new Timer();
timeTimer.schedule(new TimerTask() {
@Override
public void run() {
timeUpdate();
}
}, 0, 30000);
return view; return view;
} }
@ -359,15 +366,15 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
switch (buttonView.getId()) { switch (buttonView.getId()) {
case R.id.overview_showprediction: case R.id.overview_showprediction:
SP.putBoolean("showprediction", showPredictionView.isChecked()); SP.putBoolean("showprediction", showPredictionView.isChecked());
updateGUI("onPredictionCheckedChanged"); scheduleUpdateGUI("onPredictionCheckedChanged");
break; break;
case R.id.overview_showbasals: case R.id.overview_showbasals:
SP.putBoolean("showbasals", showBasalsView.isChecked()); SP.putBoolean("showbasals", showBasalsView.isChecked());
updateGUI("onBasalsCheckedChanged"); scheduleUpdateGUI("onBasalsCheckedChanged");
break; break;
case R.id.overview_showiob: case R.id.overview_showiob:
SP.putBoolean("showiob", showIobView.isChecked()); SP.putBoolean("showiob", showIobView.isChecked());
updateGUI("onIobCheckedChanged"); scheduleUpdateGUI("onIobCheckedChanged");
break; break;
case R.id.overview_showcob: case R.id.overview_showcob:
showDeviationsView.setOnCheckedChangeListener(null); showDeviationsView.setOnCheckedChangeListener(null);
@ -375,7 +382,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
showDeviationsView.setOnCheckedChangeListener(this); showDeviationsView.setOnCheckedChangeListener(this);
SP.putBoolean("showcob", showCobView.isChecked()); SP.putBoolean("showcob", showCobView.isChecked());
SP.putBoolean("showdeviations", showDeviationsView.isChecked()); SP.putBoolean("showdeviations", showDeviationsView.isChecked());
updateGUI("onCobCheckedChanged"); scheduleUpdateGUI("onCobCheckedChanged");
break; break;
case R.id.overview_showdeviations: case R.id.overview_showdeviations:
showCobView.setOnCheckedChangeListener(null); showCobView.setOnCheckedChangeListener(null);
@ -383,7 +390,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
showCobView.setOnCheckedChangeListener(this); showCobView.setOnCheckedChangeListener(this);
SP.putBoolean("showcob", showCobView.isChecked()); SP.putBoolean("showcob", showCobView.isChecked());
SP.putBoolean("showdeviations", showDeviationsView.isChecked()); SP.putBoolean("showdeviations", showDeviationsView.isChecked());
updateGUI("onDeviationsCheckedChanged"); scheduleUpdateGUI("onDeviationsCheckedChanged");
break; break;
} }
} }
@ -458,7 +465,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
return true; return true;
} else if (item.getTitle().equals(MainApp.sResources.getString(R.string.suspendloopfor3h))) { } else if (item.getTitle().equals(MainApp.sResources.getString(R.string.suspendloopfor3h))) {
activeloop.suspendTo(new Date().getTime() + 3 * 60L * 60 * 1000); activeloop.suspendTo(new Date().getTime() + 3 * 60L * 60 * 1000);
scheduleUpdateGUI("suspendmenu"); updateGUI("suspendmenu");
sHandler.post(new Runnable() { sHandler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -770,6 +777,11 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
scheduleUpdateGUI("EventTreatmentChange"); scheduleUpdateGUI("EventTreatmentChange");
} }
@Subscribe
public void onStatusEvent(final EventCareportalEventChange ev) {
scheduleUpdateGUI("EventCareportalEventChange");
}
@Subscribe @Subscribe
public void onStatusEvent(final EventTempBasalChange ev) { public void onStatusEvent(final EventTempBasalChange ev) {
scheduleUpdateGUI("EventTempBasalChange"); scheduleUpdateGUI("EventTempBasalChange");
@ -780,11 +792,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
scheduleUpdateGUI("EventExtendedBolusChange"); scheduleUpdateGUI("EventExtendedBolusChange");
} }
@Subscribe
public void onStatusEvent(final EventCareportalEventChange ev) {
scheduleUpdateGUI("EventCareportalEventChange");
}
@Subscribe @Subscribe
public void onStatusEvent(final EventNewBG ev) { public void onStatusEvent(final EventNewBG ev) {
scheduleUpdateGUI("EventNewBG"); scheduleUpdateGUI("EventNewBG");
@ -849,6 +856,20 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
} }
private void timeUpdate() {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (timeView != null) { //must not exists
timeView.setText(DateUtil.timeString(new Date()));
}
log.debug("Time updated");
}
});
}
public void scheduleUpdateGUI(final String from) { public void scheduleUpdateGUI(final String from) {
class UpdateRunnable implements Runnable { class UpdateRunnable implements Runnable {
public void run() { public void run() {
@ -868,92 +889,45 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
if (scheduledUpdate != null) if (scheduledUpdate != null)
scheduledUpdate.cancel(false); scheduledUpdate.cancel(false);
Runnable task = new UpdateRunnable(); Runnable task = new UpdateRunnable();
final int msec = 1000; final int msec = 400;
scheduledUpdate = worker.schedule(task, msec, TimeUnit.MILLISECONDS); scheduledUpdate = worker.schedule(task, msec, TimeUnit.MILLISECONDS);
} }
private class UpdateGUIAsyncTask extends AsyncTask<String, Void, String> { @SuppressLint("SetTextI18n")
private String from; public void updateGUI(String from) {
log.debug("updateGUI entered from: " + from);
BgReading actualBG = DatabaseHelper.actualBg();
BgReading lastBG = DatabaseHelper.lastBg();
Profile profile = MainApp.getConfigBuilder().getProfile();
String units = profile.getUnits();
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
PumpInterface pump = MainApp.getConfigBuilder();
long now = new Date().getTime();
IobTotal bolusIob;
IobTotal basalIob;
Double lowLine;
Double highLine;
int hoursToFetch;
long toTime;
long fromTime;
long endTime;
boolean showPrediction;
boolean showBasals;
boolean showIob;
boolean showCob;
boolean showDeviations;
LineGraphSeries<DataPoint> basalsLineSeries;
LineGraphSeries<DataPoint> absoluteBasalsLineSeries;
LineGraphSeries<DataPoint> baseBasalsSeries;
LineGraphSeries<DataPoint> tempBasalsSeries;
AreaGraphSeries<DoubleDataPoint> areaSeries;
LineGraphSeries<DataPoint> seriesNow, seriesNow2;
PointsWithLabelGraphSeries<DataPointWithLabelInterface> bgSeries = null;
PointsWithLabelGraphSeries<DataPointWithLabelInterface> treatmentSeries = null;
class DeviationDataPoint extends DataPoint {
public int color;
public DeviationDataPoint(double x, double y, int color) {
super(x, y);
this.color = color;
}
}
FixedLineGraphSeries<DataPoint> iobSeries;
FixedLineGraphSeries<DataPoint> cobSeries;
BarGraphSeries<DeviationDataPoint> devSeries;
DataPoint[] cobData;
DeviationDataPoint[] devData;
List<BgReading> bgReadingsArray;
Double maxBgValue = 0d;
Double maxIobValueFound = 0d;
Double maxCobValueFound = 0d;
Double maxDevValueFound = 0d;
Double maxBasalValueFound = 0d;
Integer numOfHorizLines;
public UpdateGUIAsyncTask(String from) {
super();
this.from = from;
}
@Override
protected void onPreExecute() {
log.debug("UpdateGUIAsyncTask onPreExecute from: " + from);
if (getActivity() == null) if (getActivity() == null)
return; return;
if (updating != null)
updating.setVisibility(View.VISIBLE);
if (timeView != null) { //must not exists if (timeView != null) { //must not exists
timeView.setText(DateUtil.timeString(new Date())); timeView.setText(DateUtil.timeString(new Date()));
} }
if (updating != null)
updating.setVisibility(View.VISIBLE);
if (MainApp.getConfigBuilder().getProfile() == null) {// app not initialized yet
pumpStatusView.setText(R.string.noprofileset);
pumpStatusLayout.setVisibility(View.VISIBLE);
loopStatusLayout.setVisibility(View.GONE);
if (updating != null)
updating.setVisibility(View.GONE);
return;
}
pumpStatusLayout.setVisibility(View.GONE);
loopStatusLayout.setVisibility(View.VISIBLE);
updateNotifications(); updateNotifications();
CareportalFragment.updateAge(getActivity(), sage, iage, cage, pbage); CareportalFragment.updateAge(getActivity(), sage, iage, cage, pbage);
BgReading actualBG = DatabaseHelper.actualBg();
BgReading lastBG = DatabaseHelper.lastBg();
PumpInterface pump = MainApp.getConfigBuilder();
Profile profile = MainApp.getConfigBuilder().getProfile();
// open loop mode // open loop mode
final LoopPlugin.LastRun finalLastRun = LoopPlugin.lastRun;
if (Config.APS && MainApp.getConfigBuilder().getPumpDescription().isTempBasalCapable) { if (Config.APS && MainApp.getConfigBuilder().getPumpDescription().isTempBasalCapable) {
apsModeView.setVisibility(View.VISIBLE); apsModeView.setVisibility(View.VISIBLE);
apsModeView.setBackgroundColor(MainApp.sResources.getColor(R.color.loopenabled)); apsModeView.setBackgroundColor(MainApp.sResources.getColor(R.color.loopenabled));
@ -1117,8 +1091,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
String units = profile.getUnits(); String units = profile.getUnits();
lowLine = SP.getDouble("low_mark", 0d); Double lowLine = SP.getDouble("low_mark", 0d);
highLine = SP.getDouble("high_mark", 0d); Double highLine = SP.getDouble("high_mark", 0d);
if (lowLine < 1) { if (lowLine < 1) {
lowLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units); lowLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetLow, units);
} }
@ -1126,6 +1100,8 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units); highLine = Profile.fromMgdlToUnits(OverviewPlugin.bgTargetHigh, units);
} }
timeUpdate();
// **** BG value **** // **** BG value ****
if (lastBG != null) { if (lastBG != null) {
int color = MainApp.sResources.getColor(R.color.inrange); int color = MainApp.sResources.getColor(R.color.inrange);
@ -1148,9 +1124,11 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
BgReading.units = profile.getUnits(); BgReading.units = profile.getUnits();
} else } else {
if (updating != null)
updating.setVisibility(View.GONE);
return; return;
}
Integer flag = bgView.getPaintFlags(); Integer flag = bgView.getPaintFlags();
if (actualBG == null) { if (actualBG == null) {
flag |= Paint.STRIKE_THRU_TEXT_FLAG; flag |= Paint.STRIKE_THRU_TEXT_FLAG;
@ -1162,6 +1140,17 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
int agoMin = (int) (agoMsec / 60d / 1000d); int agoMin = (int) (agoMsec / 60d / 1000d);
timeAgoView.setText(String.format(MainApp.sResources.getString(R.string.minago), agoMin)); timeAgoView.setText(String.format(MainApp.sResources.getString(R.string.minago), agoMin));
// iob
MainApp.getConfigBuilder().updateTotalIOBTreatments();
MainApp.getConfigBuilder().updateTotalIOBTempBasals();
IobTotal bolusIob = MainApp.getConfigBuilder().getLastCalculationTreatments().round();
IobTotal basalIob = MainApp.getConfigBuilder().getLastCalculationTempBasals().round();
String iobtext = getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U ("
+ getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U "
+ getString(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)";
iobView.setText(iobtext);
// cob // cob
if (cobView != null) { // view must not exists if (cobView != null) { // view must not exists
String cobText = ""; String cobText = "";
@ -1171,6 +1160,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);
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);
@ -1189,12 +1179,10 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR, 1); calendar.add(Calendar.HOUR, 1);
showPrediction = showPredictionView.isChecked() && finalLastRun != null && finalLastRun.constraintsProcessed.getClass().equals(DetermineBasalResultAMA.class); int hoursToFetch;
showBasals = showBasalsView.isChecked(); long toTime;
showIob = showIobView.isChecked(); long fromTime;
showCob = showCobView.isChecked(); long endTime;
showDeviations = showDeviationsView.isChecked();
if (showPrediction) { if (showPrediction) {
int predHours = (int) (Math.ceil(((DetermineBasalResultAMA) finalLastRun.constraintsProcessed).getLatestPredictionsTime() - new Date().getTime()) / (60 * 60 * 1000)); int predHours = (int) (Math.ceil(((DetermineBasalResultAMA) finalLastRun.constraintsProcessed).getLatestPredictionsTime() - new Date().getTime()) / (60 * 60 * 1000));
predHours = Math.min(2, predHours); predHours = Math.min(2, predHours);
@ -1210,47 +1198,18 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
endTime = toTime; endTime = toTime;
} }
// **** Area **** LineGraphSeries<DataPoint> basalsLineSeries = null;
DoubleDataPoint[] areaDataPoints = new DoubleDataPoint[]{ LineGraphSeries<DataPoint> absoluteBasalsLineSeries = null;
new DoubleDataPoint(fromTime, lowLine, highLine), LineGraphSeries<DataPoint> baseBasalsSeries = null;
new DoubleDataPoint(endTime, lowLine, highLine) LineGraphSeries<DataPoint> tempBasalsSeries = null;
}; AreaGraphSeries<DoubleDataPoint> areaSeries;
areaSeries = new AreaGraphSeries<>(areaDataPoints); LineGraphSeries<DataPoint> seriesNow, seriesNow2;
areaSeries.setColor(0);
areaSeries.setDrawBackground(true);
areaSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.inrangebackground));
// set manual x bounds to have nice steps
bgGraph.getViewport().setMaxX(endTime);
bgGraph.getViewport().setMinX(fromTime);
bgGraph.getViewport().setXAxisBoundsManual(true);
bgGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH"));
bgGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space
iobGraph.getViewport().setMaxX(endTime);
iobGraph.getViewport().setMinX(fromTime);
iobGraph.getViewport().setXAxisBoundsManual(true);
iobGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH"));
iobGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space
bgGraph.onDataChanged(true, true);
iobGraph.onDataChanged(true, true);
}
@Override
protected String doInBackground(String... params) {
log.debug("UpdateGUIAsyncTask doInBackground from: " + from);
// IOB
MainApp.getConfigBuilder().updateTotalIOBTreatments();
MainApp.getConfigBuilder().updateTotalIOBTempBasals();
bolusIob = MainApp.getConfigBuilder().getLastCalculationTreatments().round();
basalIob = MainApp.getConfigBuilder().getLastCalculationTempBasals().round();
// ****** GRAPH *******
// **** TEMP BASALS graph **** // **** TEMP BASALS graph ****
Double maxBasalValueFound = 0d;
if (pump.getPumpDescription().isTempBasalCapable && showBasals) { long now = new Date().getTime();
if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) {
List<DataPoint> baseBasalArray = new ArrayList<>(); List<DataPoint> baseBasalArray = new ArrayList<>();
List<DataPoint> tempBasalArray = new ArrayList<>(); List<DataPoint> tempBasalArray = new ArrayList<>();
List<DataPoint> basalLineArray = new ArrayList<>(); List<DataPoint> basalLineArray = new ArrayList<>();
@ -1343,25 +1302,39 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
} }
// **** IOB COB DEV graph **** // **** IOB COB DEV graph ****
class DeviationDataPoint extends DataPoint {
public int color;
if (showIob || showCob || showDeviations) { public DeviationDataPoint(double x, double y, int color) {
super(x, y);
this.color = color;
}
}
FixedLineGraphSeries<DataPoint> iobSeries;
FixedLineGraphSeries<DataPoint> cobSeries;
BarGraphSeries<DeviationDataPoint> devSeries;
Double maxIobValueFound = 0d;
Double maxCobValueFound = 0d;
Double maxDevValueFound = 0d;
if (showIobView.isChecked() || showCobView.isChecked() || showDeviationsView.isChecked()) {
//Date start = new Date(); //Date start = new Date();
List<DataPoint> iobArray = new ArrayList<>(); List<DataPoint> iobArray = new ArrayList<>();
List<DataPoint> cobArray = new ArrayList<>(); List<DataPoint> cobArray = new ArrayList<>();
List<DeviationDataPoint> devArray = new ArrayList<>(); List<DeviationDataPoint> devArray = new ArrayList<>();
for (long time = fromTime; time <= now; time += 5 * 60 * 1000L) { for (long time = fromTime; time <= now; time += 5 * 60 * 1000L) {
if (showIob) { if (showIobView.isChecked()) {
IobTotal iob = IobCobCalculatorPlugin.calulateFromTreatmentsAndTemps(time); IobTotal iob = IobCobCalculatorPlugin.calulateFromTreatmentsAndTemps(time);
iobArray.add(new DataPoint(time, iob.iob)); iobArray.add(new DataPoint(time, iob.iob));
maxIobValueFound = Math.max(maxIobValueFound, Math.abs(iob.iob)); maxIobValueFound = Math.max(maxIobValueFound, Math.abs(iob.iob));
} }
if (showCob || showDeviations) { if (showCobView.isChecked() || showDeviationsView.isChecked()) {
AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time); AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time);
if (autosensData != null && showCob) { if (autosensData != null && showCobView.isChecked()) {
cobArray.add(new DataPoint(time, autosensData.cob)); cobArray.add(new DataPoint(time, autosensData.cob));
maxCobValueFound = Math.max(maxCobValueFound, autosensData.cob); maxCobValueFound = Math.max(maxCobValueFound, autosensData.cob);
} }
if (autosensData != null && showDeviations) { if (autosensData != null && showDeviationsView.isChecked()) {
int color = Color.BLACK; // "=" int color = Color.BLACK; // "="
if (autosensData.pastSensitivity.equals("C")) color = Color.GRAY; if (autosensData.pastSensitivity.equals("C")) color = Color.GRAY;
if (autosensData.pastSensitivity.equals("+")) color = Color.GREEN; if (autosensData.pastSensitivity.equals("+")) color = Color.GREEN;
@ -1381,7 +1354,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
iobSeries.setThickness(3); iobSeries.setThickness(3);
if (showIob && (showCob || showDeviations)) { if (showIobView.isChecked() && (showCobView.isChecked() || showDeviationsView.isChecked())) {
List<DataPoint> cobArrayRescaled = new ArrayList<>(); List<DataPoint> cobArrayRescaled = new ArrayList<>();
List<DeviationDataPoint> devArrayRescaled = new ArrayList<>(); List<DeviationDataPoint> devArrayRescaled = new ArrayList<>();
for (int ci = 0; ci < cobArray.size(); ci++) { for (int ci = 0; ci < cobArray.size(); ci++) {
@ -1394,7 +1367,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
devArray = devArrayRescaled; devArray = devArrayRescaled;
} }
// COB // COB
cobData = new DataPoint[cobArray.size()]; DataPoint[] cobData = new DataPoint[cobArray.size()];
cobData = cobArray.toArray(cobData); cobData = cobArray.toArray(cobData);
cobSeries = new FixedLineGraphSeries<>(cobData); cobSeries = new FixedLineGraphSeries<>(cobData);
cobSeries.setDrawBackground(true); cobSeries.setDrawBackground(true);
@ -1403,7 +1376,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
cobSeries.setThickness(3); cobSeries.setThickness(3);
// DEVIATIONS // DEVIATIONS
devData = new DeviationDataPoint[devArray.size()]; DeviationDataPoint[] devData = new DeviationDataPoint[devArray.size()];
devData = devArray.toArray(devData); devData = devArray.toArray(devData);
devSeries = new BarGraphSeries<>(devData); devSeries = new BarGraphSeries<>(devData);
devSeries.setValueDependentColor(new ValueDependentColor<DeviationDataPoint>() { devSeries.setValueDependentColor(new ValueDependentColor<DeviationDataPoint>() {
@ -1412,13 +1385,64 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
return data.color; return data.color;
} }
}); });
//devSeries.setBackgroundColor(0xB0FFFFFF & MainApp.sResources.getColor(R.color.cob)); //50%
//devSeries.setColor(MainApp.sResources.getColor(R.color.cob));
//devSeries.setThickness(3);
iobGraph.removeAllSeries();
if (showIobView.isChecked()) {
iobGraph.addSeries(iobSeries);
}
if (showCobView.isChecked() && cobData.length > 0) {
iobGraph.addSeries(cobSeries);
}
if (showDeviationsView.isChecked() && devData.length > 0) {
iobGraph.addSeries(devSeries);
}
iobGraph.setVisibility(View.VISIBLE);
} else {
iobGraph.setVisibility(View.GONE);
} }
// remove old data from graph
bgGraph.getSecondScale().getSeries().clear();
bgGraph.removeAllSeries();
// **** Area ****
DoubleDataPoint[] areaDataPoints = new DoubleDataPoint[]{
new DoubleDataPoint(fromTime, lowLine, highLine),
new DoubleDataPoint(endTime, lowLine, highLine)
};
bgGraph.addSeries(areaSeries = new AreaGraphSeries<>(areaDataPoints));
areaSeries.setColor(0);
areaSeries.setDrawBackground(true);
areaSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.inrangebackground));
// set manual x bounds to have nice steps
bgGraph.getViewport().setMaxX(endTime);
bgGraph.getViewport().setMinX(fromTime);
bgGraph.getViewport().setXAxisBoundsManual(true);
bgGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH"));
bgGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space
iobGraph.getViewport().setMaxX(endTime);
iobGraph.getViewport().setMinX(fromTime);
iobGraph.getViewport().setXAxisBoundsManual(true);
iobGraph.getGridLabelRenderer().setLabelFormatter(new TimeAsXAxisLabelFormatter(getActivity(), "HH"));
iobGraph.getGridLabelRenderer().setNumHorizontalLabels(7); // only 7 because of the space
// **** BG graph **** // **** BG graph ****
bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true); List<BgReading> bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true);
List<DataPointWithLabelInterface> bgListArray = new ArrayList<>(); List<DataPointWithLabelInterface> bgListArray = new ArrayList<>();
if (bgReadingsArray.size() == 0) {
if (updating != null)
updating.setVisibility(View.GONE);
return;
}
Iterator<BgReading> it = bgReadingsArray.iterator(); Iterator<BgReading> it = bgReadingsArray.iterator();
Double maxBgValue = 0d;
while (it.hasNext()) { while (it.hasNext()) {
BgReading bg = it.next(); BgReading bg = it.next();
if (bg.value > maxBgValue) maxBgValue = bg.value; if (bg.value > maxBgValue) maxBgValue = bg.value;
@ -1437,14 +1461,40 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
maxBgValue = Profile.fromMgdlToUnits(maxBgValue, units); maxBgValue = Profile.fromMgdlToUnits(maxBgValue, units);
maxBgValue = units.equals(Constants.MGDL) ? Round.roundTo(maxBgValue, 40d) + 80 : Round.roundTo(maxBgValue, 2d) + 4; maxBgValue = units.equals(Constants.MGDL) ? Round.roundTo(maxBgValue, 40d) + 80 : Round.roundTo(maxBgValue, 2d) + 4;
if (highLine > maxBgValue) maxBgValue = highLine; if (highLine > maxBgValue) maxBgValue = highLine;
numOfHorizLines = units.equals(Constants.MGDL) ? (int) (maxBgValue / 40 + 1) : (int) (maxBgValue / 2 + 1); Integer numOfHorizLines = units.equals(Constants.MGDL) ? (int) (maxBgValue / 40 + 1) : (int) (maxBgValue / 2 + 1);
DataPointWithLabelInterface[] bg = new DataPointWithLabelInterface[bgListArray.size()]; DataPointWithLabelInterface[] bg = new DataPointWithLabelInterface[bgListArray.size()];
bg = bgListArray.toArray(bg); bg = bgListArray.toArray(bg);
if (bg.length > 0)
bgSeries = new PointsWithLabelGraphSeries<>(bg);
// **** treatments graph **** if (bg.length > 0) {
bgGraph.addSeries(new PointsWithLabelGraphSeries<>(bg));
}
// **** NOW line ****
DataPoint[] nowPoints = new DataPoint[]{
new DataPoint(now, 0),
new DataPoint(now, maxBgValue)
};
bgGraph.addSeries(seriesNow = new LineGraphSeries<>(nowPoints));
seriesNow.setDrawDataPoints(false);
DataPoint[] nowPoints2 = new DataPoint[]{
new DataPoint(now, 0),
new DataPoint(now, maxIobValueFound)
};
iobGraph.addSeries(seriesNow2 = new LineGraphSeries<>(nowPoints2));
seriesNow2.setDrawDataPoints(false);
//seriesNow.setThickness(1);
// custom paint to make a dotted line
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setPathEffect(new DashPathEffect(new float[]{10, 20}, 0));
paint.setColor(Color.WHITE);
seriesNow.setCustomPaint(paint);
seriesNow2.setCustomPaint(paint);
// Treatments
List<DataPointWithLabelInterface> filteredTreatments = new ArrayList<>(); List<DataPointWithLabelInterface> filteredTreatments = new ArrayList<>();
List<Treatment> treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory(); List<Treatment> treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory();
@ -1491,86 +1541,9 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
DataPointWithLabelInterface[] treatmentsArray = new DataPointWithLabelInterface[filteredTreatments.size()]; DataPointWithLabelInterface[] treatmentsArray = new DataPointWithLabelInterface[filteredTreatments.size()];
treatmentsArray = filteredTreatments.toArray(treatmentsArray); treatmentsArray = filteredTreatments.toArray(treatmentsArray);
if (treatmentsArray.length > 0) { if (treatmentsArray.length > 0) {
treatmentSeries = new PointsWithLabelGraphSeries<>(treatmentsArray); bgGraph.addSeries(new PointsWithLabelGraphSeries<>(treatmentsArray));
} }
return params[0];
}
@Override
protected void onPostExecute(String result) {
log.debug("UpdateGUIAsyncTask onPostExecute from: " + from);
// IOB
if (getActivity() == null)
return;
String iobtext = getString(R.string.treatments_iob_label_string) + " " + DecimalFormatter.to2Decimal(bolusIob.iob + basalIob.basaliob) + "U ("
+ getString(R.string.bolus) + ": " + DecimalFormatter.to2Decimal(bolusIob.iob) + "U "
+ getString(R.string.basal) + ": " + DecimalFormatter.to2Decimal(basalIob.basaliob) + "U)";
iobView.setText(iobtext);
// ****** GRAPH *******
// **** IOB COB DEV graph ****
if (showIob || showCob || showDeviations) {
iobGraph.removeAllSeries();
if (showIob) {
iobGraph.addSeries(iobSeries);
}
if (showCob && cobData.length > 0) {
iobGraph.addSeries(cobSeries);
}
if (showDeviations && devData.length > 0) {
iobGraph.addSeries(devSeries);
}
iobGraph.setVisibility(View.VISIBLE);
} else {
iobGraph.setVisibility(View.GONE);
}
// remove old data from graph
bgGraph.getSecondScale().getSeries().clear();
bgGraph.removeAllSeries();
// **** Area ***
if (areaSeries != null)
bgGraph.addSeries(areaSeries);
// **** BG graph ****
if (bgSeries != null) {
bgGraph.addSeries(bgSeries);
}
// **** treatments graph ****
if (treatmentSeries != null) {
bgGraph.addSeries(treatmentSeries);
}
// **** NOW line ****
DataPoint[] nowPoints = new DataPoint[]{
new DataPoint(now, 0),
new DataPoint(now, maxBgValue)
};
bgGraph.addSeries(seriesNow = new LineGraphSeries<>(nowPoints));
seriesNow.setDrawDataPoints(false);
DataPoint[] nowPoints2 = new DataPoint[]{
new DataPoint(now, 0),
new DataPoint(now, maxIobValueFound)
};
iobGraph.addSeries(seriesNow2 = new LineGraphSeries<>(nowPoints2));
seriesNow2.setDrawDataPoints(false);
//seriesNow.setThickness(1);
// custom paint to make a dotted line
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setPathEffect(new DashPathEffect(new float[]{10, 20}, 0));
paint.setColor(Color.WHITE);
seriesNow.setCustomPaint(paint);
seriesNow2.setCustomPaint(paint);
// set manual y bounds to have nice steps // set manual y bounds to have nice steps
bgGraph.getViewport().setMaxY(maxBgValue); bgGraph.getViewport().setMaxY(maxBgValue);
bgGraph.getViewport().setMinY(0); bgGraph.getViewport().setMinY(0);
@ -1578,7 +1551,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfHorizLines); bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfHorizLines);
// set second scale // set second scale
if (pump.getPumpDescription().isTempBasalCapable && showBasals) { if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) {
bgGraph.getSecondScale().addSeries(baseBasalsSeries); bgGraph.getSecondScale().addSeries(baseBasalsSeries);
bgGraph.getSecondScale().addSeries(tempBasalsSeries); bgGraph.getSecondScale().addSeries(tempBasalsSeries);
bgGraph.getSecondScale().addSeries(basalsLineSeries); bgGraph.getSecondScale().addSeries(basalsLineSeries);
@ -1599,29 +1572,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
}); });
if (updating != null) if (updating != null)
updating.setVisibility(View.GONE); updating.setVisibility(View.GONE);
// release for next run
log.debug("UpdateGUIAsyncTask finishing onPostExecute from: " + from);
updateGUIAsyncTask = null;
}
}
public void updateGUI(String from) {
if (MainApp.getConfigBuilder().getProfile() == null) {// app not initialized yet
pumpStatusView.setText(R.string.noprofileset);
pumpStatusLayout.setVisibility(View.VISIBLE);
loopStatusLayout.setVisibility(View.GONE);
return;
}
pumpStatusLayout.setVisibility(View.GONE);
loopStatusLayout.setVisibility(View.VISIBLE);
if (updateGUIAsyncTask != null) {
log.debug("Update already running. From: " + from);
return;
}
log.debug("updateGUI entered from: " + from);
updateGUIAsyncTask = new UpdateGUIAsyncTask(from);
updateGUIAsyncTask.execute(from);
} }
public double getNearestBg(long date, List<BgReading> bgReadingsArray) { public double getNearestBg(long date, List<BgReading> bgReadingsArray) {