Prechádzať zdrojové kódy

Add proper handling for unit or pump model change; no longer resets settings

Deniz Cengiz 1 rok pred
rodič
commit
767c82822d

+ 40 - 0
Trio/Sources/Modules/Onboarding/OnboardingStateModel.swift

@@ -181,6 +181,46 @@ extension Onboarding {
             return formatter
         }
 
+        /// Remaps therapy items affected by a glucose unit change (mg/dL vs mmol/L).
+        ///
+        /// This function updates glucose target and insulin sensitivity (ISF) items to use the closest valid index
+        /// from the newly available rate arrays, preserving the original value intent.
+        ///
+        /// Call this after the user changes the unit selection.
+        ///
+        /// See also: `UnitSelectionStepView` `.onChange()` handlers.
+        func remapTherapyItemsForChangedUnits() {
+            // Targets
+            targetItems = targetItems.map { item in
+                let newLowIndex = closestIndex(for: targetRateValues[item.lowIndex], in: targetRateValues)
+                let newTimeIndex = closestIndex(for: targetTimeValues[item.timeIndex], in: targetTimeValues)
+                return TargetsEditor.Item(lowIndex: newLowIndex, highIndex: newLowIndex, timeIndex: newTimeIndex)
+            }
+
+            // ISF
+            isfItems = isfItems.map { item in
+                let newRateIndex = closestIndex(for: isfRateValues[item.rateIndex], in: isfRateValues)
+                let newTimeIndex = closestIndex(for: isfTimeValues[item.timeIndex], in: isfTimeValues)
+                return ISFEditor.Item(rateIndex: newRateIndex, timeIndex: newTimeIndex)
+            }
+        }
+
+        /// Remaps therapy items affected by a pump model change.
+        ///
+        /// This function updates basal profile items to use the closest valid index
+        /// from the updated basal rate and time arrays, preserving the user's settings.
+        ///
+        /// Call this after the user selects a new pump model.
+        ///
+        /// See also: `UnitSelectionStepView` `.onChange()` handlers.
+        func remapTherapyItemsForChangedPumpModel() {
+            basalProfileItems = basalProfileItems.map { item in
+                let newRateIndex = closestIndex(for: basalProfileRateValues[item.rateIndex], in: basalProfileRateValues)
+                let newTimeIndex = closestIndex(for: basalProfileTimeValues[item.timeIndex], in: basalProfileTimeValues)
+                return BasalProfileEditor.Item(rateIndex: newRateIndex, timeIndex: newTimeIndex)
+            }
+        }
+
         // MARK: - Fetch existing therapy settings from file
 
         /// Loads existing therapy settings from the provider and maps them into UI editor items.

+ 5 - 7
Trio/Sources/Modules/Onboarding/View/OnboardingSteps/UnitSelectionStepView.swift

@@ -31,15 +31,13 @@ struct UnitSelectionStepView: View {
                     }
                 }
                 .onChange(of: state.pumpOptionForOnboardingUnits, { _, newValue in
-                    // Reset therapy settings and related values when pump model changes
-                    state.targetItems = []
-                    state.basalProfileItems = []
-                    state.carbRatioItems = []
-                    state.isfItems = []
-
-                    // Conditionally set rewind setting, if pump model is MDT
+                    state.remapTherapyItemsForChangedPumpModel()
+                    // Conditionally set rewind setting, if pump model is Medtronic (.minimed) or Dana (i/RS)
                     state.rewindResetsAutosens = (newValue == .minimed || newValue == .dana)
                 })
+                .onChange(of: state.units, { _, _ in
+                    state.remapTherapyItemsForChangedUnits()
+                })
             }
             .padding()
             .background(Color.chart.opacity(0.65))