Browse Source

Allow 0.025 U bolus increments for MiniMed x23 and x54 pumps

Mike Plante 5 tháng trước cách đây
mục cha
commit
68be3378c0

+ 2 - 1
Trio Watch App Extension/WatchState.swift

@@ -563,7 +563,8 @@ import WatchConnectivity
 
         if let bolusIncrement = message[WatchMessageKeys.bolusIncrement] {
             if let decimalValue = (bolusIncrement as? NSNumber)?.decimalValue {
-                self.bolusIncrement = decimalValue
+                // limit minimum to 0.05 to avoid dealing with 0.025 increments
+                self.bolusIncrement = max(decimalValue, 0.05)
             }
         }
 

+ 1 - 1
Trio/Sources/APS/DeviceDataManager.swift

@@ -102,7 +102,7 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
                         )
                 )
                 modifiedPreferences
-                    .bolusIncrement = bolusIncrement != 0.025 ? bolusIncrement : 0.1
+                    .bolusIncrement = bolusIncrement
                 storage.save(modifiedPreferences, as: OpenAPS.Settings.preferences)
 
                 if let omnipod = pumpManager as? OmnipodPumpManager {

+ 8 - 0
Trio/Sources/Helpers/Formatters.swift

@@ -37,6 +37,14 @@ extension Formatter {
         return formatter
     }()
 
+    static let decimalFormatterWithThreeFractionDigits: NumberFormatter = {
+        let formatter = NumberFormatter()
+        formatter.numberStyle = .decimal
+        formatter.locale = .current
+        formatter.maximumFractionDigits = 3
+        return formatter
+    }()
+
     static let dateFormatter: DateFormatter = {
         let dateFormatter = DateFormatter()
         dateFormatter.timeStyle = .short

+ 3 - 3
Trio/Sources/Modules/DataTable/View/DataTableRootView.swift

@@ -621,7 +621,7 @@ extension DataTable {
                     Image(systemName: "circle.fill").foregroundColor(Color.insulin)
                     Text(bolus.isSMB ? "SMB" : item.type ?? "Bolus")
                     Text(
-                        (Formatter.decimalFormatterWithTwoFractionDigits.string(from: amount) ?? "0") +
+                        (Formatter.decimalFormatterWithThreeFractionDigits.string(from: amount) ?? "0") +
                             String(localized: " U", comment: "Insulin unit")
                     )
                     .foregroundColor(.secondary)
@@ -632,7 +632,7 @@ extension DataTable {
                     Image(systemName: "circle.fill").foregroundColor(Color.insulin.opacity(0.4))
                     Text("Temp Basal")
                     Text(
-                        (Formatter.decimalFormatterWithTwoFractionDigits.string(from: rate) ?? "0") +
+                        (Formatter.decimalFormatterWithThreeFractionDigits.string(from: rate) ?? "0") +
                             String(localized: " U/hr", comment: "Unit insulin per hour")
                     )
                     .foregroundColor(.secondary)
@@ -657,7 +657,7 @@ extension DataTable {
                             alertTitle = String(localized: "Delete Insulin?", comment: "Alert title for deleting insulin")
                             alertMessage = Formatter.dateFormatter
                                 .string(from: item.timestamp ?? Date()) + ", " +
-                                (Formatter.decimalFormatterWithTwoFractionDigits.string(from: item.bolus?.amount ?? 0) ?? "0") +
+                                (Formatter.decimalFormatterWithThreeFractionDigits.string(from: item.bolus?.amount ?? 0) ?? "0") +
                                 String(localized: " U", comment: "Insulin unit")
 
                             if let bolus = item.bolus {

+ 10 - 4
Trio/Sources/Modules/Home/View/HomeRootView.swift

@@ -54,11 +54,17 @@ extension Home {
         )) var latestTempTarget: FetchedResults<TempTargetStored>
 
         var bolusProgressFormatter: NumberFormatter {
+            let fractionDigits: Int = switch state.settingsManager.preferences.bolusIncrement {
+            case 0.1: 1
+            case 0.025: 3
+            default: 2
+            }
+
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
             formatter.minimum = 0
-            formatter.maximumFractionDigits = state.settingsManager.preferences.bolusIncrement > 0.05 ? 1 : 2
-            formatter.minimumFractionDigits = state.settingsManager.preferences.bolusIncrement > 0.05 ? 1 : 2
+            formatter.maximumFractionDigits = fractionDigits
+            formatter.minimumFractionDigits = fractionDigits
             formatter.allowsFloats = true
             formatter.roundingIncrement = Double(state.settingsManager.preferences.bolusIncrement) as NSNumber
             return formatter
@@ -188,7 +194,7 @@ extension Home {
                 rate = tempRate
             }
 
-            let rateString = Formatter.decimalFormatterWithTwoFractionDigits.string(from: rate) ?? "0"
+            let rateString = Formatter.decimalFormatterWithThreeFractionDigits.string(from: rate) ?? "0"
             return rateString + String(localized: " U/hr", comment: "Unit per hour with space") +
                 manualBasalString
         }
@@ -768,7 +774,7 @@ extension Home {
                 let bolusString =
                     (bolusProgressFormatter.string(from: bolusFraction as NSNumber) ?? "0")
                         + String(localized: " of ", comment: "Bolus string partial message: 'x U of y U' in home view") +
-                        (Formatter.decimalFormatterWithTwoFractionDigits.string(from: bolusTotal as NSNumber) ?? "0")
+                        (Formatter.decimalFormatterWithThreeFractionDigits.string(from: bolusTotal as NSNumber) ?? "0")
                         + String(localized: " U", comment: "Insulin unit")
 
                 ZStack {

+ 1 - 1
Trio/Sources/Modules/Onboarding/OnboardingStateModel.swift

@@ -781,7 +781,7 @@ extension Onboarding {
                                 .bolusIncrement
                         )
                 )
-                preferences.bolusIncrement = bolusIncrement != 0.025 ? bolusIncrement : 0.1
+                preferences.bolusIncrement = bolusIncrement
             }
 
             settingsManager.preferences = preferences

+ 10 - 5
Trio/Sources/Modules/Treatments/View/PopupView.swift

@@ -915,7 +915,7 @@ struct PopupView: View {
 
                     // Final insulin recommendation
                     HStack(alignment: .firstTextBaseline, spacing: 4) {
-                        Text(insulinFormatter(state.insulinCalculated))
+                        Text(insulinFormatter(state.insulinCalculated, .down, true))
                             .largeSolutionStyle()
                             .foregroundStyle(state.insulinCalculated > 0 ? Color.accentColor : .primary)
 
@@ -939,19 +939,24 @@ struct PopupView: View {
     /// - Parameters:
     ///   - value: The insulin value to format
     ///   - roundingMode: The rounding mode to apply (default: .down for conservative dosing)
+    ///   - roundedForPump: Use a max of 3 fraction digits when rounding for pump to accomidate for 0.025 U increments if applicable
     /// - Returns: A formatted string with 2 decimal places
-    private func insulinFormatter(_ value: Decimal, _ roundingMode: NSDecimalNumber.RoundingMode = .down) -> String {
+    private func insulinFormatter(
+        _ value: Decimal,
+        _ roundingMode: NSDecimalNumber.RoundingMode = .down,
+        _ roundedForPump: Bool = false
+    ) -> String {
         let formatter = NumberFormatter()
         formatter.numberStyle = .decimal
         formatter.minimumFractionDigits = 2
-        formatter.maximumFractionDigits = 2
+        formatter.maximumFractionDigits = roundedForPump ? 3 : 2
         formatter.locale = Locale.current
 
         // Create a decimal handler with the specified rounding behavior.
-        // Always rounds to 2 decimal places (0.01 U precision).
+        // Rounds to 2 decimal places (0.01 U precision), except when rounding for pump
         let handler = NSDecimalNumberHandler(
             roundingMode: roundingMode,
-            scale: 2,
+            scale: roundedForPump ? 3 : 2,
             raiseOnExactness: false,
             raiseOnOverflow: false,
             raiseOnUnderflow: false,

+ 1 - 1
Trio/Sources/Modules/Treatments/View/TreatmentsRootView.swift

@@ -37,7 +37,7 @@ extension Treatments {
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
             formatter.maximumIntegerDigits = 2
-            formatter.maximumFractionDigits = 2
+            formatter.maximumFractionDigits = 3
             return formatter
         }