diff --git a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java index 889d842ccf..a87b77be07 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java +++ b/app/src/main/java/info/nightscout/androidaps/data/IobTotal.java @@ -24,9 +24,6 @@ public class IobTotal { // oref1 public long lastBolusTime; - public long lastTempDate; - public int lastTempDuration; - public double lastTempRate; public IobTotal iobWithZeroTemp; public double netInsulin = 0d; // for calculations from temp basals only @@ -76,9 +73,6 @@ public class IobTotal { result.netInsulin = basalIob.netInsulin + bolusIOB.netInsulin; result.extendedBolusInsulin = basalIob.extendedBolusInsulin + bolusIOB.extendedBolusInsulin; result.lastBolusTime = bolusIOB.lastBolusTime; - result.lastTempDate = basalIob.lastTempDate; - result.lastTempRate = basalIob.lastTempRate; - result.lastTempDuration = basalIob.lastTempDuration; result.iobWithZeroTemp = basalIob.iobWithZeroTemp; return result; } diff --git a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java index 0747669538..733027b188 100644 --- a/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java +++ b/app/src/main/java/info/nightscout/androidaps/db/TemporaryBasal.java @@ -217,7 +217,7 @@ public class TemporaryBasal implements Interval { // -------- Interval interface end --------- - public IobTotal iobCalc(long time) { + public IobTotal iobCalc(long time, Profile profile) { if(isFakeExtended){ 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); - Profile profile = MainApp.getConfigBuilder().getProfile(time); InsulinInterface insulinInterface = ConfigBuilderPlugin.getActiveInsulin(); int realDuration = getDurationToTime(time); diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java index f93dbdf5fe..30610ca2f6 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java @@ -5,6 +5,7 @@ import java.util.List; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.MealData; +import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.TempTarget; @@ -24,7 +25,7 @@ public interface TreatmentsInterface { IobTotal getLastCalculationTreatments(); IobTotal getCalculationToTimeTreatments(long time); IobTotal getLastCalculationTempBasals(); - IobTotal getCalculationToTimeTempBasals(long time); + IobTotal getCalculationToTimeTempBasals(long time, Profile profile); MealData getMealData(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index f56939e504..f51c7f7a94 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -293,13 +293,13 @@ public class IobCobCalculatorPlugin extends PluginBase { return getBGDataFrom; } - public IobTotal calculateFromTreatmentsAndTempsSynchronized(long time) { + public IobTotal calculateFromTreatmentsAndTempsSynchronized(long time, Profile profile) { 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(); time = roundUpTime(time); 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()); } 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)) { - // Add expected zere temp basal for next 240 mins + // Add expected zero temp basal for next 240 mins IobTotal basalIobWithZeroTemp = basalIob.copy(); TemporaryBasal t = new TemporaryBasal() .date(now + 60 * 1000L) .duration(240) .absolute(0); if (t.date < time) { - IobTotal calc = t.iobCalc(time); + IobTotal calc = t.iobCalc(time, profile); basalIobWithZeroTemp.plus(calc); } @@ -426,8 +426,7 @@ public class IobCobCalculatorPlugin extends PluginBase { } } - public IobTotal[] calculateIobArrayInDia() { - Profile profile = MainApp.getConfigBuilder().getProfile(); + public IobTotal[] calculateIobArrayInDia(Profile profile) { // predict IOB out to DIA plus 30m long time = System.currentTimeMillis(); time = roundUpTime(time); @@ -436,15 +435,14 @@ public class IobCobCalculatorPlugin extends PluginBase { int pos = 0; for (int i = 0; i < len; i++) { long t = time + i * 5 * 60000; - IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t); + IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t, profile); array[pos] = iob; pos++; } return array; } - public IobTotal[] calculateIobArrayForSMB() { - Profile profile = MainApp.getConfigBuilder().getProfile(); + public IobTotal[] calculateIobArrayForSMB(Profile profile) { // predict IOB out to DIA plus 30m long time = System.currentTimeMillis(); time = roundUpTime(time); @@ -453,7 +451,7 @@ public class IobCobCalculatorPlugin extends PluginBase { int pos = 0; for (int i = 0; i < len; i++) { long t = time + i * 5 * 60000; - IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t); + IobTotal iob = calculateFromTreatmentsAndTempsSynchronized(t, profile); array[pos] = iob; pos++; } @@ -479,6 +477,7 @@ public class IobCobCalculatorPlugin extends PluginBase { } @Subscribe + @SuppressWarnings("unused") public void onEventAppInitialized(EventAppInitialized ev) { if (this != getPlugin()) { log.debug("Ignoring event for non default instance"); @@ -488,6 +487,7 @@ public class IobCobCalculatorPlugin extends PluginBase { } @Subscribe + @SuppressWarnings("unused") public void onEventNewBG(EventNewBG ev) { if (this != getPlugin()) { log.debug("Ignoring event for non default instance"); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java index 4543139f08..e3f12cbea6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobThread.java @@ -138,7 +138,7 @@ public class IobCobThread extends Thread { delta = (bg - bucketed_data.get(i + 1).value); 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 deviation = delta - bgi; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java index d9ffc67830..2789c75252 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSAMA/OpenAPSAMAPlugin.java @@ -136,7 +136,7 @@ public class OpenAPSAMAPlugin extends PluginBase implements APSInterface { Date start = new Date(); Date startPart = new Date(); - IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(); + IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayInDia(profile); Profiler.log(log, "calculateIobArrayInDia()", startPart); startPart = new Date(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java index f30338d024..60b516fc63 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/OpenAPSSMB/OpenAPSSMBPlugin.java @@ -143,7 +143,7 @@ public class OpenAPSSMBPlugin extends PluginBase implements APSInterface { Date start = new Date(); Date startPart = new Date(); - IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(); + IobTotal[] iobArray = IobCobCalculatorPlugin.getPlugin().calculateIobArrayForSMB(profile); Profiler.log(log, "calculateIobArrayInDia()", startPart); startPart = new Date(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java index a017f31209..1a8816d0b3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java @@ -23,7 +23,6 @@ import info.nightscout.androidaps.db.CareportalEvent; import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ProfileSwitch; 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.IobCobCalculator.AutosensData; 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.ScaledDataPoint; 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.utils.Round; @@ -334,7 +334,10 @@ public class GraphData { Scale iobScale = new Scale(); 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.2) iobArray.add(new ScaledDataPoint(time, lastIob, iobScale)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 722ce0539a..36fac67834 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -40,6 +40,7 @@ import info.nightscout.androidaps.plugins.IobCobCalculator.AutosensData; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; import info.nightscout.androidaps.plugins.Overview.notifications.Notification; +import info.nightscout.utils.DateUtil; import info.nightscout.utils.NSUpload; import info.nightscout.utils.SP; import info.nightscout.utils.T; @@ -320,22 +321,15 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface } @Override - public IobTotal getCalculationToTimeTempBasals(long time) { + public IobTotal getCalculationToTimeTempBasals(long time, Profile profile) { IobTotal total = new IobTotal(time); synchronized (tempBasals) { for (Integer pos = 0; pos < tempBasals.size(); pos++) { TemporaryBasal t = tempBasals.get(pos); if (t.date > time) continue; - IobTotal calc = t.iobCalc(time); + IobTotal calc = t.iobCalc(time, profile); //log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob); 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()) { @@ -346,13 +340,6 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface if (e.date > time) continue; IobTotal calc = e.iobCalc(time); 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 @@ -367,7 +354,9 @@ public class TreatmentsPlugin extends PluginBase implements TreatmentsInterface @Override public void updateTotalIOBTempBasals() { - lastTempBasalsCalculation = getCalculationToTimeTempBasals(System.currentTimeMillis()); + Profile profile = MainApp.getConfigBuilder().getProfile(); + if (profile != null) + lastTempBasalsCalculation = getCalculationToTimeTempBasals(DateUtil.now(), profile); } @Nullable diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java index b9004e8dd5..957c5c02f1 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java @@ -2,9 +2,9 @@ package info.nightscout.androidaps.plugins.Treatments.fragments; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.graphics.Paint; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; @@ -18,9 +18,6 @@ import android.widget.TextView; import com.crashlytics.android.answers.CustomEvent; import com.squareup.otto.Subscribe; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Intervals; @@ -40,8 +37,6 @@ import info.nightscout.utils.NSUpload; public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { - private static Logger log = LoggerFactory.getLogger(TreatmentsTemporaryBasalsFragment.class); - RecyclerView recyclerView; LinearLayoutManager llm; @@ -70,7 +65,7 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { holder.ns.setVisibility(NSUpload.isIdValid(tempBasal._id) ? View.VISIBLE : View.GONE); if (tempBasal.isEndingEvent()) { 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.percent.setText(""); holder.realDuration.setText(""); @@ -82,14 +77,16 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { } else { if (tempBasal.isInProgress()) { holder.date.setText(DateUtil.dateAndTimeString(tempBasal.date)); + holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); } 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) { Profile profile = MainApp.getConfigBuilder().getProfile(tempBasal.date); 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(""); } else { holder.absolute.setText(MainApp.gs(R.string.noprofile)); @@ -97,24 +94,19 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { } } else { 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"); - IobTotal iob = new IobTotal(System.currentTimeMillis()); - try { // in case app loaded and still no profile selected - iob = tempBasal.iobCalc(System.currentTimeMillis()); - } catch (Exception e) { - } - holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob) + " U"); - holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin) + " U"); - holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio) + " U/h"); - //holder.extendedFlag.setVisibility(tempBasal.isExtended ? View.VISIBLE : View.GONE); + holder.realDuration.setText(DecimalFormatter.to0Decimal(tempBasal.getRealDuration(), " min")); + long now = DateUtil.now(); + IobTotal iob = new IobTotal(now); + Profile profile = MainApp.getConfigBuilder().getProfile(now); + if (profile != null) + iob = tempBasal.iobCalc(now, profile); + holder.iob.setText(DecimalFormatter.to2Decimal(iob.basaliob, " U")); + holder.netInsulin.setText(DecimalFormatter.to2Decimal(iob.netInsulin, " U")); + holder.netRatio.setText(DecimalFormatter.to2Decimal(iob.netRatio, " U/h")); holder.extendedFlag.setVisibility(View.GONE); - if (tempBasal.isInProgress()) - holder.date.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); - else - holder.date.setTextColor(holder.netRatio.getCurrentTextColor()); - if (tempBasal.iobCalc(System.currentTimeMillis()).basaliob != 0) + if (iob.basaliob != 0) holder.iob.setTextColor(ContextCompat.getColor(MainApp.instance(), R.color.colorActive)); else holder.iob.setTextColor(holder.netRatio.getCurrentTextColor()); @@ -172,21 +164,19 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { switch (v.getId()) { case R.id.tempbasals_remove: AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(MainApp.sResources.getString(R.string.confirmation)); - builder.setMessage(MainApp.sResources.getString(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(tempBasal.date)); - builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - final String _id = tempBasal._id; - if (NSUpload.isIdValid(_id)) { - NSUpload.removeCareportalEntryFromNS(_id); - } else { - UploadQueue.removeID("dbAdd", _id); - } - MainApp.getDbHelper().delete(tempBasal); - FabricPrivacy.getInstance().logCustom(new CustomEvent("RemoveTempBasal")); + builder.setTitle(MainApp.gs(R.string.confirmation)); + builder.setMessage(MainApp.gs(R.string.removerecord) + "\n" + DateUtil.dateAndTimeString(tempBasal.date)); + builder.setPositiveButton(MainApp.gs(R.string.ok), (dialog, id) -> { + final String _id = tempBasal._id; + if (NSUpload.isIdValid(_id)) { + NSUpload.removeCareportalEntryFromNS(_id); + } else { + UploadQueue.removeID("dbAdd", _id); } + MainApp.getDbHelper().delete(tempBasal); + 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(); break; } @@ -195,7 +185,7 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.treatments_tempbasals_fragment, container, false); @@ -216,12 +206,12 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { } @Subscribe - public void onStatusEvent(final EventTempBasalChange ev) { + public void onStatusEvent(final EventTempBasalChange ignored) { updateGUI(); } @Subscribe - public void onStatusEvent(final EventNewBG ev) { + public void onStatusEvent(final EventNewBG ignored) { updateGUI(); } @@ -229,15 +219,11 @@ public class TreatmentsTemporaryBasalsFragment extends SubscriberFragment { protected void updateGUI() { Activity activity = getActivity(); if (activity != null) - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); - if (TreatmentsPlugin.getPlugin().getLastCalculationTempBasals() != null) { - String totalText = DecimalFormatter.to2Decimal(TreatmentsPlugin.getPlugin().getLastCalculationTempBasals().basaliob) + " U"; - tempBasalTotalView.setText(totalText); - } - } + activity.runOnUiThread(() -> { + recyclerView.swapAdapter(new RecyclerViewAdapter(TreatmentsPlugin.getPlugin().getTemporaryBasalsFromHistory()), false); + IobTotal tempBasalsCalculation = TreatmentsPlugin.getPlugin().getLastCalculationTempBasals(); + if (tempBasalsCalculation != null) + tempBasalTotalView.setText(DecimalFormatter.to2Decimal(tempBasalsCalculation.basaliob, " U")); }); } diff --git a/app/src/main/java/info/nightscout/utils/DateUtil.java b/app/src/main/java/info/nightscout/utils/DateUtil.java index fd88ec2f9a..441449e8ee 100644 --- a/app/src/main/java/info/nightscout/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/utils/DateUtil.java @@ -122,6 +122,10 @@ public class DateUtil { return dateString(date) + " " + timeString(date); } + public static String dateAndTimeRangeString(long start, long end) { + return dateAndTimeString(start) + " - " + timeString(end); + } + public static String dateAndTimeString(long mills) { return dateString(mills) + " " + timeString(mills); } diff --git a/app/src/main/java/info/nightscout/utils/DecimalFormatter.java b/app/src/main/java/info/nightscout/utils/DecimalFormatter.java index d821f5ba37..6709b304d9 100644 --- a/app/src/main/java/info/nightscout/utils/DecimalFormatter.java +++ b/app/src/main/java/info/nightscout/utils/DecimalFormatter.java @@ -17,18 +17,34 @@ public class DecimalFormatter { return format0dec.format(value); } + public static String to0Decimal(double value, String unit) { + return format0dec.format(value) + unit; + } + public static String to1Decimal(double value) { return format1dec.format(value); } + public static String to1Decimal(double value, String unit) { + return format1dec.format(value) + unit; + } + public static String to2Decimal(double value) { return format2dec.format(value); } + public static String to2Decimal(double value, String unit) { + return format2dec.format(value) + unit; + } + public static String to3Decimal(double value) { return format3dec.format(value); } + public static String to3Decimal(double value, String unit) { + return format3dec.format(value) + unit; + } + public static String toPumpSupportedBolus(double value) { return ConfigBuilderPlugin.getActivePump().getPumpDescription().bolusStep <= 0.01 ? to2Decimal(value) diff --git a/app/src/test/java/info/nightscout/utils/DateUtilTest.java b/app/src/test/java/info/nightscout/utils/DateUtilTest.java index 36a87e93c2..5e1de56709 100644 --- a/app/src/test/java/info/nightscout/utils/DateUtilTest.java +++ b/app/src/test/java/info/nightscout/utils/DateUtilTest.java @@ -69,6 +69,14 @@ public class DateUtilTest { 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 public void timeStringFromSecondsTest() { Assert.assertEquals("1:00 AM", DateUtil.timeStringFromSeconds((int) T.hours(1).secs())); diff --git a/app/src/test/java/info/nightscout/utils/DecimalFormatterTest.java b/app/src/test/java/info/nightscout/utils/DecimalFormatterTest.java new file mode 100644 index 0000000000..a17a76909b --- /dev/null +++ b/app/src/test/java/info/nightscout/utils/DecimalFormatterTest.java @@ -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(); + } +} \ No newline at end of file