Просмотр исходного кода

refactor conversion for bolus calc to mmol/L

polscm32 2 лет назад
Родитель
Сommit
34d1580f38

+ 10 - 8
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -180,16 +180,19 @@ extension Bolus {
 
         // CALCULATIONS FOR THE BOLUS CALCULATOR
         func calculateInsulin() -> Decimal {
-            var conversion: Decimal = 1.0
-            if units == .mmolL {
-                conversion = 0.0555
+
+            //ensure that isf is in mg/dL
+            var conversion: Decimal {
+                units == .mmolL ? 0.0555 : 1
             }
+            let isfForCalculation = isf / conversion
+            
             // insulin needed for the current blood glucose
-            targetDifference = (currentBG - target) * conversion
-            targetDifferenceInsulin = targetDifference / isf
+            targetDifference = (currentBG - target)
+            targetDifferenceInsulin = targetDifference / isfForCalculation
 
             // more or less insulin because of bg trend in the last 15 minutes
-            fifteenMinInsulin = (deltaBG * conversion) / isf
+            fifteenMinInsulin = deltaBG / isfForCalculation
 
             // determine whole COB for which we want to dose insulin for and then determine insulin for wholeCOB
             wholeCob = cob + carbs
@@ -224,7 +227,6 @@ extension Bolus {
             } else {
                 insulinCalculated = result
             }
-
             // display no negative insulinCalculated
             insulinCalculated = max(insulinCalculated, 0)
             insulinCalculated = min(insulinCalculated, maxBolus)
@@ -232,7 +234,7 @@ extension Bolus {
             return apsManager
                 .roundBolus(amount: max(insulinCalculated, 0))
         }
-
+        
         func add() async {
             guard amount > 0 else {
                 showModal(for: nil)

+ 6 - 4
FreeAPS/Sources/Modules/Bolus/View/AlternativeBolusCalcRootView.swift

@@ -538,6 +538,7 @@ extension Bolus {
 
                 Text("Glucose:").foregroundColor(.secondary)
 
+                let targetDifference = state.units == .mmolL ? state.targetDifference.asMmolL : state.targetDifference
                 let firstRow = currentBG
                     .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
 
@@ -545,7 +546,7 @@ extension Bolus {
                     target
                     .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
                     + " = " +
-                    state.targetDifference
+                    targetDifference
                     .formatted(.number.grouping(.never).rounded().precision(.fractionLength(fractionDigits)))
 
                 Text(firstRow).frame(minWidth: 0, alignment: .leading).foregroundColor(.secondary)
@@ -571,7 +572,8 @@ extension Bolus {
                         state.units.rawValue
                 )
 
-                let secondRow = state.targetDifference
+                let targetDifference = state.units == .mmolL ? state.targetDifference.asMmolL : state.targetDifference
+                let secondRow = targetDifference
                     .formatted(
                         .number.grouping(.never).rounded()
                             .precision(.fractionLength(fractionDigits))
@@ -579,7 +581,7 @@ extension Bolus {
                     + " / " +
                     state.isf.formatted()
                     + " ≈ " +
-                    self.insulinRounder(state.targetDifferenceInsulin).formatted()
+                self.insulinRounder(state.targetDifferenceInsulin).formatted()
 
                 Text(secondRow).foregroundColor(.secondary).gridColumnAlignment(.leading)
 
@@ -675,7 +677,7 @@ extension Bolus {
                         + " / " +
                         state.isf.formatted()
                         + " ≈ " +
-                        self.insulinRounder(state.fifteenMinInsulin).formatted()
+                    self.insulinRounder(state.fifteenMinInsulin).formatted()
                 )
                 .foregroundColor(.secondary)
                 .gridColumnAlignment(.leading)