Remove rounding CalculatedCorrection

This commit is contained in:
Philoul 2021-12-09 23:50:55 +01:00
parent 88b2a8d0d7
commit b05a9b7683
2 changed files with 15 additions and 9 deletions

View file

@ -84,6 +84,7 @@ class WizardDialog : DaggerDialogFragment() {
} }
private var disposable: CompositeDisposable = CompositeDisposable() private var disposable: CompositeDisposable = CompositeDisposable()
private var bolusStep = 0.0
private var _binding: DialogWizardBinding? = null private var _binding: DialogWizardBinding? = null
@ -123,6 +124,7 @@ class WizardDialog : DaggerDialogFragment() {
val maxCarbs = constraintChecker.getMaxCarbsAllowed().value() val maxCarbs = constraintChecker.getMaxCarbsAllowed().value()
val maxCorrection = constraintChecker.getMaxBolusAllowed().value() val maxCorrection = constraintChecker.getMaxBolusAllowed().value()
bolusStep = activePlugin.activePump.pumpDescription.bolusStep
if (profileFunction.getUnits() == GlucoseUnit.MGDL) if (profileFunction.getUnits() == GlucoseUnit.MGDL)
binding.bgInput.setParams(savedInstanceState?.getDouble("bg_input") binding.bgInput.setParams(savedInstanceState?.getDouble("bg_input")
@ -132,7 +134,6 @@ class WizardDialog : DaggerDialogFragment() {
?: 0.0, 0.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.ok, textWatcher) ?: 0.0, 0.0, 30.0, 0.1, DecimalFormat("0.0"), false, binding.ok, textWatcher)
binding.carbsInput.setParams(savedInstanceState?.getDouble("carbs_input") binding.carbsInput.setParams(savedInstanceState?.getDouble("carbs_input")
?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.ok, textWatcher) ?: 0.0, 0.0, maxCarbs.toDouble(), 1.0, DecimalFormat("0"), false, binding.ok, textWatcher)
val bolusStep = activePlugin.activePump.pumpDescription.bolusStep
if (correctionPercent) { if (correctionPercent) {
calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble() calculatedPercentage = sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble()
@ -192,13 +193,12 @@ class WizardDialog : DaggerDialogFragment() {
binding.correctionUnit.text = if (isChecked) "%" else rh.gs(R.string.insulin_unit_shortname) binding.correctionUnit.text = if (isChecked) "%" else rh.gs(R.string.insulin_unit_shortname)
correctionPercent = binding.correctionPercent.isChecked correctionPercent = binding.correctionPercent.isChecked
if (correctionPercent) if (correctionPercent)
binding.correctionInput.setParams(calculatedPercentage.toDouble(), 10.0, 200.0, 1.0, DecimalFormat("0"), false, binding.ok, textWatcher) binding.correctionInput.setParams(calculatedPercentage, 10.0, 200.0, 1.0, DecimalFormat("0"), false, binding.ok, textWatcher)
else else
binding.correctionInput.setParams(savedInstanceState?.getDouble("correction_input") binding.correctionInput.setParams(savedInstanceState?.getDouble("correction_input")
?: 0.0, -maxCorrection, maxCorrection, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, binding.ok, textWatcher) ?: 0.0, -maxCorrection, maxCorrection, bolusStep, DecimalFormatter.pumpSupportedBolusFormat(activePlugin.activePump), false, binding.ok, textWatcher)
binding.correctionInput.value = if (correctionPercent) calculatedPercentage.toDouble() else calculatedCorrection binding.correctionInput.value = if (correctionPercent) calculatedPercentage else Round.roundTo(calculatedCorrection, bolusStep)
} }
} }
// profile spinner // profile spinner
binding.profile.onItemSelectedListener = object : OnItemSelectedListener { binding.profile.onItemSelectedListener = object : OnItemSelectedListener {
@ -321,13 +321,20 @@ class WizardDialog : DaggerDialogFragment() {
val usePercentage = binding.correctionPercent.isChecked val usePercentage = binding.correctionPercent.isChecked
var bg = SafeParse.stringToDouble(binding.bgInput.text) var bg = SafeParse.stringToDouble(binding.bgInput.text)
val carbs = SafeParse.stringToInt(binding.carbsInput.text) val carbs = SafeParse.stringToInt(binding.carbsInput.text)
val correction = if (usePercentage) 0.0 else SafeParse.stringToDouble(binding.correctionInput.text) val correction = if (!usePercentage) {
if (Round.roundTo(calculatedCorrection, bolusStep) == SafeParse.stringToDouble(binding.correctionInput.text))
calculatedCorrection
else
SafeParse.stringToDouble(binding.correctionInput.text)
} else
0.0
val percentageCorrection = if (usePercentage) { val percentageCorrection = if (usePercentage) {
if (Round.roundTo(calculatedPercentage,1.0) == SafeParse.stringToDouble(binding.correctionInput.text)) if (Round.roundTo(calculatedPercentage,1.0) == SafeParse.stringToDouble(binding.correctionInput.text))
calculatedPercentage calculatedPercentage
else else
SafeParse.stringToDouble(binding.correctionInput.text) SafeParse.stringToDouble(binding.correctionInput.text)
} else sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble() } else
sp.getInt(R.string.key_boluswizard_percentage, 100).toDouble()
val carbsAfterConstraint = constraintChecker.applyCarbsConstraints(Constraint(carbs)).value() val carbsAfterConstraint = constraintChecker.applyCarbsConstraints(Constraint(carbs)).value()
if (abs(carbs - carbsAfterConstraint) > 0.01) { if (abs(carbs - carbsAfterConstraint) > 0.01) {
binding.carbsInput.value = 0.0 binding.carbsInput.value = 0.0

View file

@ -471,8 +471,7 @@ class BolusWizard @Inject constructor(
} }
private fun calcCorrectionWithConstraints() { private fun calcCorrectionWithConstraints() {
val bolusStep = activePlugin.activePump.pumpDescription.bolusStep calculatedCorrection = totalBeforePercentageAdjustment * totalPercentage / percentageCorrection - totalBeforePercentageAdjustment
calculatedCorrection = Round.roundTo(totalBeforePercentageAdjustment * totalPercentage / percentageCorrection - totalBeforePercentageAdjustment, bolusStep)
//Apply constraints //Apply constraints
calculatedCorrection = min(constraintChecker.getMaxBolusAllowed().value(), calculatedCorrection) calculatedCorrection = min(constraintChecker.getMaxBolusAllowed().value(), calculatedCorrection)
calculatedCorrection = max(-constraintChecker.getMaxBolusAllowed().value(), calculatedCorrection) calculatedCorrection = max(-constraintChecker.getMaxBolusAllowed().value(), calculatedCorrection)