fix NPE in TempBasal treatments tab

This commit is contained in:
Milos Kozak 2018-04-09 19:52:01 +02:00
parent 132400bb9e
commit e9ac979a1b
14 changed files with 150 additions and 94 deletions

View file

@ -24,9 +24,6 @@ public class IobTotal {
// oref1 // oref1
public long lastBolusTime; public long lastBolusTime;
public long lastTempDate;
public int lastTempDuration;
public double lastTempRate;
public IobTotal iobWithZeroTemp; public IobTotal iobWithZeroTemp;
public double netInsulin = 0d; // for calculations from temp basals only public double netInsulin = 0d; // for calculations from temp basals only
@ -76,9 +73,6 @@ public class IobTotal {
result.netInsulin = basalIob.netInsulin + bolusIOB.netInsulin; result.netInsulin = basalIob.netInsulin + bolusIOB.netInsulin;
result.extendedBolusInsulin = basalIob.extendedBolusInsulin + bolusIOB.extendedBolusInsulin; result.extendedBolusInsulin = basalIob.extendedBolusInsulin + bolusIOB.extendedBolusInsulin;
result.lastBolusTime = bolusIOB.lastBolusTime; result.lastBolusTime = bolusIOB.lastBolusTime;
result.lastTempDate = basalIob.lastTempDate;
result.lastTempRate = basalIob.lastTempRate;
result.lastTempDuration = basalIob.lastTempDuration;
result.iobWithZeroTemp = basalIob.iobWithZeroTemp; result.iobWithZeroTemp = basalIob.iobWithZeroTemp;
return result; return result;
} }

View file

@ -217,7 +217,7 @@ public class TemporaryBasal implements Interval {
// -------- Interval interface end --------- // -------- Interval interface end ---------
public IobTotal iobCalc(long time) { public IobTotal iobCalc(long time, Profile profile) {
if(isFakeExtended){ if(isFakeExtended){
log.error("iobCalc should only be called on Extended boluses separately"); log.error("iobCalc should only be called on Extended boluses separately");
@ -225,7 +225,6 @@ public class TemporaryBasal implements Interval {
} }
IobTotal result = new IobTotal(time); IobTotal result = new IobTotal(time);
Profile profile = MainApp.getConfigBuilder().getProfile(time);
InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin();
int realDuration = getDurationToTime(time); int realDuration = getDurationToTime(time);

View file

@ -5,6 +5,7 @@ import java.util.List;
import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.MealData;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.ProfileSwitch;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
@ -24,7 +25,7 @@ public interface TreatmentsInterface {
IobTotal getLastCalculationTreatments(); IobTotal getLastCalculationTreatments();
IobTotal getCalculationToTimeTreatments(long time); IobTotal getCalculationToTimeTreatments(long time);
IobTotal getLastCalculationTempBasals(); IobTotal getLastCalculationTempBasals();
IobTotal getCalculationToTimeTempBasals(long time); IobTotal getCalculationToTimeTempBasals(long time, Profile profile);
MealData getMealData(); MealData getMealData();

View file

@ -293,13 +293,13 @@ public class IobCobCalculatorPlugin extends PluginBase {
return getBGDataFrom; return getBGDataFrom;
} }
public IobTotal calculateFromTreatmentsAndTempsSynchronized(long time) { public IobTotal calculateFromTreatmentsAndTempsSynchronized(long time, Profile profile) {
synchronized (dataLock) { synchronized (dataLock) {
return calculateFromTreatmentsAndTemps(time); return calculateFromTreatmentsAndTemps(time, profile);
} }
} }
public IobTotal calculateFromTreatmentsAndTemps(long time) { public IobTotal calculateFromTreatmentsAndTemps(long time, Profile profile) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
time = roundUpTime(time); time = roundUpTime(time);
if (time < now && iobTable.get(time) != null) { if (time < now && iobTable.get(time) != null) {
@ -309,16 +309,16 @@ public class IobCobCalculatorPlugin extends PluginBase {
//log.debug(">>> calculateFromTreatmentsAndTemps Cache miss " + new Date(time).toLocaleString()); //log.debug(">>> calculateFromTreatmentsAndTemps Cache miss " + new Date(time).toLocaleString());
} }
IobTotal bolusIob = TreatmentsPlugin.getPlugin().getCalculationToTimeTreatments(time).round(); IobTotal bolusIob = TreatmentsPlugin.getPlugin().getCalculationToTimeTreatments(time).round();
IobTotal basalIob = TreatmentsPlugin.getPlugin().getCalculationToTimeTempBasals(time).round(); IobTotal basalIob = TreatmentsPlugin.getPlugin().getCalculationToTimeTempBasals(time, profile).round();
if (OpenAPSSMBPlugin.getPlugin().isEnabled(PluginType.APS)) { if (OpenAPSSMBPlugin.getPlugin().isEnabled(PluginType.APS)) {
// Add expected zere temp basal for next 240 mins // Add expected zero temp basal for next 240 mins
IobTotal basalIobWithZeroTemp = basalIob.copy(); IobTotal basalIobWithZeroTemp = basalIob.copy();
TemporaryBasal t = new TemporaryBasal() TemporaryBasal t = new TemporaryBasal()
.date(now + 60 * 1000L) .date(now + 60 * 1000L)
.duration(240) .duration(240)
.absolute(0); .absolute(0);
if (t.date < time) { if (t.date < time) {
IobTotal calc = t.iobCalc(time); IobTotal calc = t.iobCalc(time, profile);
basalIobWithZeroTemp.plus(calc); basalIobWithZeroTemp.plus(calc);
} }
@ -426,8 +426,7 @@ public class IobCobCalculatorPlugin extends PluginBase {
} }
} }
public IobTotal[] calculateIobArrayInDia() { public IobTotal[] calculateIobArrayInDia(Profile profile) {
Profile profile = MainApp.getConfigBuilder().getProfile();
// predict IOB out to DIA plus 30m // predict IOB out to DIA plus 30m
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
time = roundUpTime(time); time = roundUpTime(time);
@ -436,15 +435,14 @@ public class IobCobCalculatorPlugin extends PluginBase {
int pos = 0; int pos = 0;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
long t = time + i * 5 * 60000; long t = time + i * 5 * 60000;
IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t); IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t, profile);
array[pos] = iob; array[pos] = iob;
pos++; pos++;
} }
return array; return array;
} }
public IobTotal[] calculateIobArrayForSMB() { public IobTotal[] calculateIobArrayForSMB(Profile profile) {
Profile profile = MainApp.getConfigBuilder().getProfile();
// predict IOB out to DIA plus 30m // predict IOB out to DIA plus 30m
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
time = roundUpTime(time); time = roundUpTime(time);
@ -453,7 +451,7 @@ public class IobCobCalculatorPlugin extends PluginBase {
int pos = 0; int pos = 0;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
long t = time + i * 5 * 60000; long t = time + i * 5 * 60000;
IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t); IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t, profile);
array[pos] = iob; array[pos] = iob;
pos++; pos++;
} }
@ -479,6 +477,7 @@ public class IobCobCalculatorPlugin extends PluginBase {
} }
@Subscribe @Subscribe
@SuppressWarnings("unused")
public void onEventAppInitialized(EventAppInitialized ev) { public void onEventAppInitialized(EventAppInitialized ev) {
if (this != getPlugin()) { if (this != getPlugin()) {
log.debug("Ignoring event for non default instance"); log.debug("Ignoring event for non default instance");
@ -488,6 +487,7 @@ public class IobCobCalculatorPlugin extends PluginBase {
} }
@Subscribe @Subscribe
@SuppressWarnings("unused")
public void onEventNewBG(EventNewBG ev) { public void onEventNewBG(EventNewBG ev) {
if (this != getPlugin()) { if (this != getPlugin()) {
log.debug("Ignoring event for non default instance"); log.debug("Ignoring event for non default instance");

View file

@ -138,7 +138,7 @@ public class IobCobThread extends Thread {
delta = (bg - bucketed_data.get(i + 1).value); delta = (bg - bucketed_data.get(i + 1).value);
avgDelta = (bg - bucketed_data.get(i + 3).value) / 3; avgDelta = (bg - bucketed_data.get(i + 3).value) / 3;
IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime); IobTotal iob = iobCobCalculatorPlugin.calculateFromTreatmentsAndTemps(bgTime, profile);
double bgi = -iob.activity * sens * 5; double bgi = -iob.activity * sens * 5;
double deviation = delta - bgi; double deviation = delta - bgi;

View file

@ -136,7 +136,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface {
Date start = new Date(); Date start = new Date();
Date startPart = new Date(); Date startPart = new Date();
IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(); IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(profile);
Profiler.log(log, "calculateIobArrayInDia()", startPart); Profiler.log(log, "calculateIobArrayInDia()", startPart);
startPart = new Date(); startPart = new Date();

View file

@ -143,7 +143,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface {
Date start = new Date(); Date start = new Date();
Date startPart = new Date(); Date startPart = new Date();
IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(); IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(profile);
Profiler.log(log, "calculateIobArrayInDia()", startPart); Profiler.log(log, "calculateIobArrayInDia()", startPart);
startPart = new Date(); startPart = new Date();

View file

@ -23,7 +23,6 @@ import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ExtendedBolus;
import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.ProfileSwitch;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.plugins.Treatments.Treatment;
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.IobCobCalculator.BasalData; import info.nightscout.androidaps.plugins.IobCobCalculator.BasalData;
@ -38,6 +37,7 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLab
import info.nightscout.androidaps.plugins.Overview.graphExtensions.Scale; import info.nightscout.androidaps.plugins.Overview.graphExtensions.Scale;
import info.nightscout.androidaps.plugins.Overview.graphExtensions.ScaledDataPoint; import info.nightscout.androidaps.plugins.Overview.graphExtensions.ScaledDataPoint;
import info.nightscout.androidaps.plugins.Overview.graphExtensions.TimeAsXAxisLabelFormatter; import info.nightscout.androidaps.plugins.Overview.graphExtensions.TimeAsXAxisLabelFormatter;
import info.nightscout.androidaps.plugins.Treatments.Treatment;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.Round; import info.nightscout.utils.Round;
@ -334,7 +334,10 @@ public class GraphData {
Scale iobScale = new Scale(); Scale iobScale = new Scale();
for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) { for (long time = fromTime; time <= toTime; time += 5 * 60 * 1000L) {
double iob = IobCobCalculatorPlugin.getPlugin().calculateFromTreatmentsAndTempsSynchronized(time).iob; Profile profile = MainApp.getConfigBuilder().getProfile(time);
double iob = 0d;
if (profile != null)
iob = IobCobCalculatorPlugin.getPlugin().calculateFromTreatmentsAndTempsSynchronized(time, profile).iob;
if (Math.abs(lastIob - iob) > 0.02) { if (Math.abs(lastIob - iob) > 0.02) {
if (Math.abs(lastIob - iob) > 0.2) if (Math.abs(lastIob - iob) > 0.2)
iobArray.add(new ScaledDataPoint(time, lastIob, iobScale)); iobArray.add(new ScaledDataPoint(time, lastIob, iobScale));

View file

@ -40,6 +40,7 @@ import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData;
import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.utils.DateUtil;
import info.nightscout.utils.NSUpload; import info.nightscout.utils.NSUpload;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
import info.nightscout.utils.T; import info.nightscout.utils.T;
@ -320,22 +321,15 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
} }
@Override @Override
public IobTotal getCalculationToTimeTempBasals(long time) { public IobTotal getCalculationToTimeTempBasals(long time, Profile profile) {
IobTotal total = new IobTotal(time); IobTotal total = new IobTotal(time);
synchronized (tempBasals) { synchronized (tempBasals) {
for (Integer pos = 0; pos < tempBasals.size(); pos++) { for (Integer pos = 0; pos < tempBasals.size(); pos++) {
TemporaryBasal t = tempBasals.get(pos); TemporaryBasal t = tempBasals.get(pos);
if (t.date > time) continue; if (t.date > time) continue;
IobTotal calc = t.iobCalc(time); IobTotal calc = t.iobCalc(time, profile);
//log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob); //log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob);
total.plus(calc); total.plus(calc);
if (!t.isEndingEvent()) {
total.lastTempDate = t.date;
total.lastTempDuration = t.durationInMinutes;
Profile profile = MainApp.getConfigBuilder().getProfile(t.date);
total.lastTempRate = t.tempBasalConvertedToAbsolute(t.date, profile);
}
} }
} }
if (ConfigBuilderPlugin.getActivePump().isFakingTempsByExtendedBoluses()) { if (ConfigBuilderPlugin.getActivePump().isFakingTempsByExtendedBoluses()) {
@ -346,13 +340,6 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
if (e.date > time) continue; if (e.date > time) continue;
IobTotal calc = e.iobCalc(time); IobTotal calc = e.iobCalc(time);
totalExt.plus(calc); totalExt.plus(calc);
TemporaryBasal t = new TemporaryBasal(e);
if (!t.isEndingEvent() && t.date > total.lastTempDate) {
total.lastTempDate = t.date;
total.lastTempDuration = t.durationInMinutes;
Profile profile = MainApp.getConfigBuilder().getProfile(t.date);
total.lastTempRate = t.tempBasalConvertedToAbsolute(t.date, profile);
}
} }
} }
// Convert to basal iob // Convert to basal iob
@ -367,7 +354,9 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface
@Override @Override
public void updateTotalIOBTempBasals() { public void updateTotalIOBTempBasals() {
lastTempBasalsCalculation = getCalculationToTimeTempBasals(System.currentTimeMillis()); Profile profile = MainApp.getConfigBuilder().getProfile();
if (profile != null)
lastTempBasalsCalculation = getCalculationToTimeTempBasals(DateUtil.now(), profile);
} }
@Nullable @Nullable

View file

@ -2,9 +2,9 @@ package info.nightscout.androidaps.plugins.Treatments.fragments;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint; import android.graphics.Paint;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
@ -18,9 +18,6 @@ import android.widget.TextView;
import com.crashlytics.android.answers.CustomEvent; import com.crashlytics.android.answers.CustomEvent;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.Intervals; import info.nightscout.androidaps.data.Intervals;
@ -40,8 +37,6 @@ import info.nightscout.utils.NSUpload;
public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
private static Logger log = LoggerFactory.getLogger(TreatmentsTemporaryBasalsFragment.class);
RecyclerView recyclerView; RecyclerView recyclerView;
LinearLayoutManager llm; LinearLayoutManager llm;
@ -70,7 +65,7 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
holder.ns.setVisibility(NSUpload.isIdValid(tempBasal._id) ? View.VISIBLE : View.GONE); holder.ns.setVisibility(NSUpload.isIdValid(tempBasal._id) ? View.VISIBLE : View.GONE);
if (tempBasal.isEndingEvent()) { if (tempBasal.isEndingEvent()) {
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date)); holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date));
holder.duration.setText(MainApp.sResources.getString(R.string.cancel)); holder.duration.setText(MainApp.gs(R.string.cancel));
holder.absolute.setText(""); holder.absolute.setText("");
holder.percent.setText(""); holder.percent.setText("");
holder.realDuration.setText(""); holder.realDuration.setText("");
@ -82,14 +77,16 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
} else { } else {
if (tempBasal.isInProgress()) { if (tempBasal.isInProgress()) {
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date)); holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date));
holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
} else { } else {
holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date) + " - " + DateUtil.timeString(tempBasal.end())); holder.date.setText(DateUtil.dateAndTimeRangeString(tempBasal.date, tempBasal.end()));
holder.date.setTextColor(holder.netRatio.getCurrentTextColor());
} }
holder.duration.setText(DecimalFormatter.to0Decimal(tempBasal.durationInMinutes) + " min"); holder.duration.setText(DecimalFormatter.to0Decimal(tempBasal.durationInMinutes, " min"));
if (tempBasal.isAbsolute) { if (tempBasal.isAbsolute) {
Profile profile = MainApp.getConfigBuilder().getProfile(tempBasal.date); Profile profile = MainApp.getConfigBuilder().getProfile(tempBasal.date);
if (profile != null) { if (profile != null) {
holder.absolute.setText(DecimalFormatter.to0Decimal(tempBasal.tempBasalConvertedToAbsolute(tempBasal.date, profile)) + " U/h"); holder.absolute.setText(DecimalFormatter.to0Decimal(tempBasal.tempBasalConvertedToAbsolute(tempBasal.date, profile), " U/h"));
holder.percent.setText(""); holder.percent.setText("");
} else { } else {
holder.absolute.setText(MainApp.gs(R.string.noprofile)); holder.absolute.setText(MainApp.gs(R.string.noprofile));
@ -97,24 +94,19 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
} }
} else { } else {
holder.absolute.setText(""); holder.absolute.setText("");
holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percentRate) + "%"); holder.percent.setText(DecimalFormatter.to0Decimal(tempBasal.percentRate, "%"));
} }
holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration()) + " min"); holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration(), " min"));
IobTotal iob = new IobTotal(System.currentTimeMillis()); long now = DateUtil.now();
try { // in case app loaded and still no profile selected IobTotal iob = new IobTotal(now);
iob = tempBasal.iobCalc(System.currentTimeMillis()); Profile profile = MainApp.getConfigBuilder().getProfile(now);
} catch (Exception e) { if (profile != null)
} iob = tempBasal.iobCalc(now, profile);
holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob) + " U"); holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob, " U"));
holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin) + " U"); holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin, " U"));
holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio) + " U/h"); holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio, " U/h"));
//holder.extendedFlag.setVisibility(tempBasal.isExtended ? View.VISIBLE : View.GONE);
holder.extendedFlag.setVisibility(View.GONE); holder.extendedFlag.setVisibility(View.GONE);
if (tempBasal.isInProgress()) if (iob.basaliob != 0)
holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
else
holder.date.setTextColor(holder.netRatio.getCurrentTextColor());
if (tempBasal.iobCalc(System.currentTimeMillis()).basaliob != 0)
holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive));
else else
holder.iob.setTextColor(holder.netRatio.getCurrentTextColor()); holder.iob.setTextColor(holder.netRatio.getCurrentTextColor());
@ -172,10 +164,9 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
switch (v.getId()) { switch (v.getId()) {
case R.id.tempbasals_remove: case R.id.tempbasals_remove:
AlertDialog.Builder builder = new AlertDialog.Builder(context); AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); builder.setTitle(MainApp.gs(R.string.confirmation));
builder.setMessage(MainApp.sResources.getString(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(tempBasal.date)); builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(tempBasal.date));
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> {
public void onClick(DialogInterface dialog, int id) {
final String _id = tempBasal._id; final String _id = tempBasal._id;
if (NSUpload.isIdValid(_id)) { if (NSUpload.isIdValid(_id)) {
NSUpload.removeCareportalEntryFromNS(_id); NSUpload.removeCareportalEntryFromNS(_id);
@ -184,9 +175,8 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
} }
MainApp.getDbHelper().delete(tempBasal); MainApp.getDbHelper().delete(tempBasal);
FabricPrivacy.getInstance().logCustom(new CustomEvent("RemoveTempBasal")); FabricPrivacy.getInstance().logCustom(new CustomEvent("RemoveTempBasal"));
}
}); });
builder.setNegativeButton(MainApp.sResources.getString(R.string.cancel), null); builder.setNegativeButton(MainApp.gs(R.string.cancel), null);
builder.show(); builder.show();
break; break;
} }
@ -195,7 +185,7 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.treatments_tempbasals_fragment, container, false); View view = inflater.inflate(R.layout.treatments_tempbasals_fragment, container, false);
@ -216,12 +206,12 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventTempBasalChange ev) { public void onStatusEvent(final EventTempBasalChange ignored) {
updateGUI(); updateGUI();
} }
@Subscribe @Subscribe
public void onStatusEvent(final EventNewBG ev) { public void onStatusEvent(final EventNewBG ignored) {
updateGUI(); updateGUI();
} }
@ -229,15 +219,11 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment {
protected void updateGUI() { protected void updateGUI() {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(() -> {
@Override
public void run() {
recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false);
if (TreatmentsPlugin.getPlugin().getLastCalculationTempBasals() != null) { IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals();
String totalText = DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTempBasals().basaliob) + " U"; if (tempBasalsCalculation != null)
tempBasalTotalView.setText(totalText); tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U"));
}
}
}); });
} }

View file

@ -122,6 +122,10 @@ public class DateUtil {
return dateString(date) + " " + timeString(date); return dateString(date) + " " + timeString(date);
} }
public static String dateAndTimeRangeString(long start, long end) {
return dateAndTimeString(start) + " - " + timeString(end);
}
public static String dateAndTimeString(long mills) { public static String dateAndTimeString(long mills) {
return dateString(mills) + " " + timeString(mills); return dateString(mills) + " " + timeString(mills);
} }

View file

@ -17,18 +17,34 @@ public class DecimalFormatter {
return format0dec.format(value); return format0dec.format(value);
} }
public static String to0Decimal(double value, String unit) {
return format0dec.format(value) + unit;
}
public static String to1Decimal(double value) { public static String to1Decimal(double value) {
return format1dec.format(value); return format1dec.format(value);
} }
public static String to1Decimal(double value, String unit) {
return format1dec.format(value) + unit;
}
public static String to2Decimal(double value) { public static String to2Decimal(double value) {
return format2dec.format(value); return format2dec.format(value);
} }
public static String to2Decimal(double value, String unit) {
return format2dec.format(value) + unit;
}
public static String to3Decimal(double value) { public static String to3Decimal(double value) {
return format3dec.format(value); return format3dec.format(value);
} }
public static String to3Decimal(double value, String unit) {
return format3dec.format(value) + unit;
}
public static String toPumpSupportedBolus(double value) { public static String toPumpSupportedBolus(double value) {
return ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep <= 0.01 return ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep <= 0.01
? to2Decimal(value) ? to2Decimal(value)

View file

@ -69,6 +69,14 @@ public class DateUtilTest {
assertTrue(DateUtil.dateAndTimeString(new Date(1513902750000L)).contains("32")); assertTrue(DateUtil.dateAndTimeString(new Date(1513902750000L)).contains("32"));
} }
@Test
public void dateAndTimeRangeStringTest() {
assertTrue(DateUtil.dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("22"));
assertTrue(DateUtil.dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("32"));
assertTrue(DateUtil.dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("22"));
assertTrue(DateUtil.dateAndTimeRangeString(1513902750000L, 1513902750000L).contains("32"));
}
@Test @Test
public void timeStringFromSecondsTest() { public void timeStringFromSecondsTest() {
Assert.assertEquals("1:00 AM", DateUtil.timeStringFromSeconds((int) T.hours(1).secs())); Assert.assertEquals("1:00 AM", DateUtil.timeStringFromSeconds((int) T.hours(1).secs()));

View file

@ -0,0 +1,56 @@
package info.nightscout.utils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class})
public class DecimalFormatterTest {
@Test
public void to0DecimalTest() {
Assert.assertEquals("1", DecimalFormatter.to0Decimal(1.33d));
Assert.assertEquals("1U", DecimalFormatter.to0Decimal(1.33d, "U"));
}
@Test
public void to1DecimalTest() {
Assert.assertEquals("1.3", DecimalFormatter.to1Decimal(1.33d));
Assert.assertEquals("1.3U", DecimalFormatter.to1Decimal(1.33d, "U"));
}
@Test
public void to2DecimalTest() {
Assert.assertEquals("1.33", DecimalFormatter.to2Decimal(1.3333d));
Assert.assertEquals("1.33U", DecimalFormatter.to2Decimal(1.3333d, "U"));
}
@Test
public void to3DecimalTest() {
Assert.assertEquals("1.333", DecimalFormatter.to3Decimal(1.3333d));
Assert.assertEquals("1.333U", DecimalFormatter.to3Decimal(1.3333d, "U"));
}
@Test
public void toPumpSupportedBolus() {
}
@Test
public void pumpSupportedBolusFormat() {
}
@Before
public void prepareMock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockStrings();
}
}