diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/insulin/ActivityGraph.kt b/app/src/main/java/info/nightscout/androidaps/plugins/insulin/ActivityGraph.kt index 93932d5633..c7f66ef4eb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/insulin/ActivityGraph.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/insulin/ActivityGraph.kt @@ -18,10 +18,11 @@ class ActivityGraph : GraphView { constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - fun show(insulin: Insulin) { + fun show(insulin: Insulin, diaSample: Double? = null) { removeAllSeries() + val dia = diaSample ?: insulin.dia mSecondScale = null - val hours = floor(insulin.dia + 1).toLong() + val hours = floor(dia + 1).toLong() val bolus = Bolus( timestamp = 0, amount = 1.0, @@ -31,7 +32,7 @@ class ActivityGraph : GraphView { val iobArray: MutableList = ArrayList() var time: Long = 0 while (time <= T.hours(hours).msecs()) { - val iob = insulin.iobCalcForTreatment(bolus, time, insulin.dia) + val iob = insulin.iobCalcForTreatment(bolus, time, dia) activityArray.add(DataPoint(T.msecs(time).mins().toDouble(), iob.activityContrib)) iobArray.add(DataPoint(T.msecs(time).mins().toDouble(), iob.iobContrib)) time += T.mins(5).msecs() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index 87c6875084..8892c397a9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -3,6 +3,7 @@ package info.nightscout.androidaps.plugins.profile.local import android.os.Bundle import android.text.Editable import android.text.TextWatcher +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -20,7 +21,6 @@ import info.nightscout.androidaps.dialogs.ProfileSwitchDialog import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.GlucoseUnit import info.nightscout.androidaps.interfaces.Profile -import info.nightscout.shared.logging.AAPSLogger import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.profile.local.events.EventLocalProfileChanged @@ -31,8 +31,10 @@ import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.ui.SpinnerHelper import info.nightscout.androidaps.utils.ui.TimeListEdit import info.nightscout.shared.SafeParse +import info.nightscout.shared.logging.AAPSLogger import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.plusAssign +import java.math.RoundingMode import java.text.DecimalFormat import javax.inject.Inject @@ -61,6 +63,8 @@ class LocalProfileFragment : DaggerFragment() { binding.basalGraph.show(ProfileSealed.Pure(it)) binding.icGraph.show(ProfileSealed.Pure(it)) binding.isfGraph.show(ProfileSealed.Pure(it)) + binding.targetGraph.show(ProfileSealed.Pure(it)) + binding.insulinGraph.show(activePlugin.activeInsulin, SafeParse.stringToDouble(binding.dia.text)) } } @@ -130,14 +134,22 @@ class LocalProfileFragment : DaggerFragment() { binding.name.addTextChangedListener(textWatch) binding.dia.setParams(currentProfile.dia, hardLimits.minDia(), hardLimits.maxDia(), 0.1, DecimalFormat("0.0"), false, null, textWatch) binding.dia.tag = "LP_DIA" - TimeListEdit(context, aapsLogger, dateUtil, view, R.id.ic_holder, "IC", rh.gs(R.string.ic_label), currentProfile.ic, null, hardLimits.minIC(), hardLimits.maxIC(), 0.1, DecimalFormat("0.0"), save) - basalView = TimeListEdit(context, aapsLogger, dateUtil, view, R.id.basal_holder, "BASAL", rh.gs(R.string.basal_label) + ": " + sumLabel(), currentProfile.basal, null, pumpDescription.basalMinimumRate, pumpDescription.basalMaximumRate, 0.01, DecimalFormat("0.00"), save) + TimeListEdit(context, aapsLogger, dateUtil, view, R.id.ic_holder, "IC", rh.gs(R.string.ic_long_label), currentProfile.ic, null, doubleArrayOf(hardLimits.minIC(), hardLimits.maxIC()), null, 0.1, DecimalFormat ("0.0"), save) + basalView = TimeListEdit(context, aapsLogger, dateUtil, view, R.id.basal_holder, "BASAL", rh.gs(R.string.basal_long_label) + ": " + sumLabel(), currentProfile.basal, null, doubleArrayOf(pumpDescription.basalMinimumRate, pumpDescription.basalMaximumRate), null, 0.01, DecimalFormat("0.00"), save) if (units == Constants.MGDL) { - TimeListEdit(context, aapsLogger, dateUtil, view, R.id.isf_holder, "ISF", rh.gs(R.string.isf_label), currentProfile.isf, null, HardLimits.MIN_ISF, HardLimits.MAX_ISF, 1.0, DecimalFormat("0"), save) - TimeListEdit(context, aapsLogger, dateUtil, view, R.id.target, "TARGET", rh.gs(R.string.target_label), currentProfile.targetLow, currentProfile.targetHigh, HardLimits.VERY_HARD_LIMIT_TARGET_BG[0], HardLimits.VERY_HARD_LIMIT_TARGET_BG[1], 1.0, DecimalFormat("0"), save) + val isfRange = doubleArrayOf(HardLimits.MIN_ISF, HardLimits.MAX_ISF) + TimeListEdit(context, aapsLogger, dateUtil, view, R.id.isf_holder, "ISF", rh.gs(R.string.isf_long_label), currentProfile.isf, null, isfRange , null, 1.0, DecimalFormat("0"), save) + TimeListEdit(context, aapsLogger, dateUtil, view, R.id.target_holder, "TARGET", rh.gs(R.string.target_long_label), currentProfile.targetLow, currentProfile.targetHigh, HardLimits.VERY_HARD_LIMIT_MIN_BG, HardLimits.VERY_HARD_LIMIT_TARGET_BG, 1.0, DecimalFormat("0"), save) } else { - TimeListEdit(context, aapsLogger, dateUtil, view, R.id.isf_holder, "ISF", rh.gs(R.string.isf_label), currentProfile.isf, null, Profile.fromMgdlToUnits(HardLimits.MIN_ISF, GlucoseUnit.MMOL), Profile.fromMgdlToUnits(HardLimits.MAX_ISF, GlucoseUnit.MMOL), 0.1, DecimalFormat("0.0"), save) - TimeListEdit(context, aapsLogger, dateUtil, view, R.id.target, "TARGET", rh.gs(R.string.target_label), currentProfile.targetLow, currentProfile.targetHigh, Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[0], GlucoseUnit.MMOL), Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_TARGET_BG[1], GlucoseUnit.MMOL), 0.1, DecimalFormat("0.0"), save) + val isfRange = doubleArrayOf(roundUp(Profile.fromMgdlToUnits(HardLimits.MIN_ISF, GlucoseUnit.MMOL)), + roundDown(Profile.fromMgdlToUnits(HardLimits.MAX_ISF, GlucoseUnit.MMOL))) + TimeListEdit(context, aapsLogger, dateUtil, view, R.id.isf_holder, "ISF", rh.gs(R.string.isf_long_label), currentProfile.isf, null,isfRange , null, 0.1, DecimalFormat("0.0"), save) + val range1 = doubleArrayOf(roundUp(Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_MIN_BG[0], GlucoseUnit.MMOL)), + roundDown(Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_MIN_BG[1], GlucoseUnit.MMOL))) + val range2 = doubleArrayOf(roundUp(Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_MAX_BG[0], GlucoseUnit.MMOL)), + roundDown(Profile.fromMgdlToUnits(HardLimits.VERY_HARD_LIMIT_MAX_BG[1], GlucoseUnit.MMOL))) + Log.i("TimeListEdit", "build: range1" + range1[0] + " " + range1[1] + " range2" + range2[0] + " " + range2[1]) + TimeListEdit(context, aapsLogger, dateUtil, view, R.id.target_holder, "TARGET", rh.gs(R.string.target_long_label), currentProfile.targetLow, currentProfile.targetHigh, range1 , range2, 0.1, DecimalFormat("0.0"), save) } // Spinner @@ -175,6 +187,8 @@ class LocalProfileFragment : DaggerFragment() { binding.basalGraph.show(ProfileSealed.Pure(it)) binding.icGraph.show(ProfileSealed.Pure(it)) binding.isfGraph.show(ProfileSealed.Pure(it)) + binding.targetGraph.show(ProfileSealed.Pure(it)) + binding.insulinGraph.show(activePlugin.activeInsulin, SafeParse.stringToDouble(binding.dia.text)) } binding.profileAdd.setOnClickListener { @@ -265,6 +279,14 @@ class LocalProfileFragment : DaggerFragment() { updateGUI() } + private fun roundUp(number: Double): Double { + return number.toBigDecimal().setScale(1, RoundingMode.UP).toDouble() + } + + private fun roundDown(number: Double): Double { + return number.toBigDecimal().setScale(1, RoundingMode.DOWN).toDouble() + } + private fun updateGUI() { if (_binding == null) return val isValid = localProfilePlugin.isValidEditState(activity) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/ui/TimeListEdit.java b/app/src/main/java/info/nightscout/androidaps/utils/ui/TimeListEdit.java index 5f435b91d3..246792e17b 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/ui/TimeListEdit.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/ui/TimeListEdit.java @@ -57,6 +57,8 @@ public class TimeListEdit { private final double step; private final double min; private final double max; + private final double min2; + private final double max2; private final NumberFormat formatter; private final Runnable save; private LinearLayout layout; @@ -68,7 +70,7 @@ public class TimeListEdit { Context context, AAPSLogger aapsLogger, DateUtil dateUtil, - View view, int resLayoutId, String tagPrefix, String label, JSONArray data1, JSONArray data2, double min, double max, double step, NumberFormat formatter, Runnable save) { + View view, int resLayoutId, String tagPrefix, String label, JSONArray data1, JSONArray data2, double[] range1, double[] range2, double step, NumberFormat formatter, Runnable save) { this.context = context; this.aapsLogger = aapsLogger; this.dateUtil = dateUtil; @@ -79,8 +81,10 @@ public class TimeListEdit { this.data1 = data1; this.data2 = data2; this.step = step; - this.min = min; - this.max = max; + this.min = range1[0]; + this.max = range1[1]; + this.min2 = range2 != null ? range2[0] : 0; + this.max2 = range2 != null ? range2[1] : 0; this.formatter = formatter; this.save = save; buildView(); @@ -177,7 +181,13 @@ public class TimeListEdit { numberPickers1[position].setTextWatcher(new TextWatcher() { @Override public void afterTextChanged(Editable s) { - editItem(position, secondFromMidnight(position), SafeParse.stringToDouble(numberPickers1[position].getText()), value2(position)); + Double value1 = SafeParse.stringToDouble(numberPickers1[position].getText()); + Double value2 = value2(position); + if (data2 != null && value1 > value2) { + value2 = value1; + numberPickers2[position].setValue(value2); + } + editItem(position, secondFromMidnight(position), value1, value2); callSave(); log(); } @@ -197,7 +207,13 @@ public class TimeListEdit { numberPickers2[position].setTextWatcher(new TextWatcher() { @Override public void afterTextChanged(Editable s) { - editItem(position, secondFromMidnight(position), value1(position), SafeParse.stringToDouble(numberPickers2[position].getText())); + Double value1 = value1(position); + Double value2 = SafeParse.stringToDouble(numberPickers2[position].getText()); + if (data2 != null && value2 < value1) { + value1 = value2; + numberPickers1[position].setValue(value1); + } + editItem(position, secondFromMidnight(position), value1, value2); callSave(); log(); } @@ -246,7 +262,7 @@ public class TimeListEdit { fillSpinner(timeSpinner, secondFromMidnight(i), previous, next); editText1.setParams(value1(i), min, max, step, formatter, false, null); - editText2.setParams(value2(i), min, max, step, formatter, false, null); + editText2.setParams(value2(i), min2, max2, step, formatter, false, null); if (data2 == null) { editText2.setVisibility(View.GONE); diff --git a/app/src/main/res/layout/localprofile_fragment.xml b/app/src/main/res/layout/localprofile_fragment.xml index 628584707a..dccf3238ba 100644 --- a/app/src/main/res/layout/localprofile_fragment.xml +++ b/app/src/main/res/layout/localprofile_fragment.xml @@ -35,6 +35,40 @@ + + + + + + + + + + - - - - - - - + android:gravity="center_horizontal" + android:text="@string/dia_label" + android:textColor="@android:color/white" + android:textSize="20sp" /> + + + + + +