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

feat: move autosens from therapy->isf to algorithm->autosens

Andreas Stokholm 1 год назад
Родитель
Сommit
41c25f44fb

+ 3 - 1
FreeAPS/Sources/Modules/AutosensSettings/AutosensSettingsDataFlow.swift

@@ -2,4 +2,6 @@ enum AutosensSettings {
     enum Config {}
 }
 
-protocol AutosensSettingsProvider: Provider {}
+protocol AutosensSettingsProvider: Provider {
+    var autosense: Autosens { get }
+}

+ 7 - 1
FreeAPS/Sources/Modules/AutosensSettings/AutosensSettingsProvider.swift

@@ -1,3 +1,9 @@
 extension AutosensSettings {
-    final class Provider: BaseProvider, AutosensSettingsProvider {}
+    final class Provider: BaseProvider, AutosensSettingsProvider {
+        var autosense: Autosens {
+            storage.retrieve(OpenAPS.Settings.autosense, as: Autosens.self)
+                ?? Autosens(from: OpenAPS.defaults(for: OpenAPS.Settings.autosense))
+                ?? Autosens(ratio: 1, newisf: nil, timestamp: nil)
+        }
+    }
 }

+ 38 - 0
FreeAPS/Sources/Modules/AutosensSettings/AutosensSettingsStateModel.swift

@@ -1,3 +1,4 @@
+import CoreData
 import Observation
 import SwiftUI
 
@@ -5,9 +6,16 @@ extension AutosensSettings {
     @Observable final class StateModel: BaseStateModel<Provider> {
         @ObservationIgnored @Injected() var settings: SettingsManager!
         @ObservationIgnored @Injected() var storage: FileStorage!
+        @ObservationIgnored @Injected() var determinationStorage: DeterminationStorage!
 
         var units: GlucoseUnits = .mgdL
 
+        private(set) var autosensISF: Decimal?
+        private(set) var autosensRatio: Decimal = 0
+        var determinationsFromPersistence: [OrefDetermination] = []
+
+        let viewContext = CoreDataStack.shared.persistentContainer.viewContext
+
         var autosensMax: Decimal = 1.2
         var autosensMin: Decimal = 0.7
         var rewindResetsAutosens: Bool = true
@@ -22,6 +30,13 @@ extension AutosensSettings {
             autosensMax = settings.preferences.autosensMax
             autosensMin = settings.preferences.autosensMin
             rewindResetsAutosens = settings.preferences.rewindResetsAutosens
+
+            if let newISF = provider.autosense.newisf {
+                autosensISF = newISF
+            }
+
+            autosensRatio = provider.autosense.ratio
+            setupDeterminationsArray()
         }
 
         var isSettingUnchanged: Bool {
@@ -42,6 +57,29 @@ extension AutosensSettings {
                 storage.save(newSettings, as: OpenAPS.Settings.preferences)
             }
         }
+
+        private func setupDeterminationsArray() {
+            Task {
+                let ids = await determinationStorage.fetchLastDeterminationObjectID(
+                    predicate: NSPredicate.enactedDetermination
+                )
+                await updateDeterminationsArray(with: ids)
+            }
+        }
+
+        @MainActor private func updateDeterminationsArray(with IDs: [NSManagedObjectID]) {
+            do {
+                let objects = try IDs.compactMap { id in
+                    try viewContext.existingObject(with: id) as? OrefDetermination
+                }
+                determinationsFromPersistence = objects
+
+            } catch {
+                debugPrint(
+                    "Home State: \(#function) \(DebuggingIdentifiers.failed) error while updating the glucose array: \(error.localizedDescription)"
+                )
+            }
+        }
     }
 }
 

+ 52 - 0
FreeAPS/Sources/Modules/AutosensSettings/View/AutosensSettingsRootView.swift

@@ -32,8 +32,60 @@ extension AutosensSettings {
                 )
         }
 
+        private var rateFormatter: NumberFormatter {
+            let formatter = NumberFormatter()
+            formatter.numberStyle = .decimal
+            formatter.maximumFractionDigits = 2
+            return formatter
+        }
+
         var body: some View {
             List {
+                if let newISF = state.autosensISF {
+                    Section(
+                        header: !state.settingsManager.preferences
+                            .useNewFormula ? Text("Autosens") : Text("Dynamic Sensitivity")
+                    ) {
+                        let dynamicRatio = state.determinationsFromPersistence.first?.sensitivityRatio
+                        let dynamicISF = state.determinationsFromPersistence.first?.insulinSensitivity
+                        HStack {
+                            Text("Sensitivity Ratio")
+                            Spacer()
+                            Text(
+                                rateFormatter
+                                    .string(from: (
+                                        (
+                                            !state.settingsManager.preferences.useNewFormula ? state
+                                                .autosensRatio as NSDecimalNumber : dynamicRatio
+                                        ) ?? 1
+                                    ) as NSNumber) ?? "1"
+                            )
+                        }
+                        HStack {
+                            Text("Calculated Sensitivity")
+                            Spacer()
+                            if state.units == .mgdL {
+                                Text(
+                                    !state.settingsManager.preferences
+                                        .useNewFormula ? newISF.description : (dynamicISF ?? 0).description
+                                )
+                            } else {
+                                Text((
+                                    !state.settingsManager.preferences
+                                        .useNewFormula ? newISF.formattedAsMmolL : dynamicISF?.decimalValue.formattedAsMmolL
+                                ) ?? "0")
+                            }
+                            Text(state.units.rawValue + "/U").foregroundColor(.secondary)
+                        }
+                        HStack {
+                            Text("This is a snapshot in time and should not be used as your ISF setting.")
+                                .font(.footnote)
+                                .foregroundColor(.secondary)
+                                .lineLimit(nil)
+                        }
+                    }.listRowBackground(Color.chart)
+                }
+
                 SettingInputSection(
                     decimalValue: $state.autosensMax,
                     booleanValue: $booleanPlaceholder,

+ 0 - 1
FreeAPS/Sources/Modules/ISFEditor/ISFEditorDataFlow.swift

@@ -27,6 +27,5 @@ enum ISFEditor {
 protocol ISFEditorProvider: Provider {
     var profile: InsulinSensitivities { get }
     func saveProfile(_ profile: InsulinSensitivities)
-    var autosense: Autosens { get }
     var autotune: Autotune? { get }
 }

+ 0 - 6
FreeAPS/Sources/Modules/ISFEditor/ISFEditorProvider.swift

@@ -35,12 +35,6 @@ extension ISFEditor {
             storage.save(profile, as: OpenAPS.Settings.insulinSensitivities)
         }
 
-        var autosense: Autosens {
-            storage.retrieve(OpenAPS.Settings.autosense, as: Autosens.self)
-                ?? Autosens(from: OpenAPS.defaults(for: OpenAPS.Settings.autosense))
-                ?? Autosens(ratio: 1, newisf: nil, timestamp: nil)
-        }
-
         var autotune: Autotune? {
             storage.retrieve(OpenAPS.Settings.autotune, as: Autotune.self)
         }

+ 0 - 34
FreeAPS/Sources/Modules/ISFEditor/ISFEditorStateModel.swift

@@ -10,13 +10,9 @@ extension ISFEditor {
         var items: [Item] = []
         var initialItems: [Item] = []
         var shouldDisplaySaving: Bool = false
-        private(set) var autosensISF: Decimal?
-        private(set) var autosensRatio: Decimal = 0
         var autotune: Autotune?
-        var determinationsFromPersistence: [OrefDetermination] = []
 
         let context = CoreDataStack.shared.newTaskContext()
-        let viewContext = CoreDataStack.shared.persistentContainer.viewContext
 
         let timeValues = stride(from: 0.0, to: 1.days.timeInterval, by: 30.minutes.timeInterval).map { $0 }
 
@@ -55,13 +51,6 @@ extension ISFEditor {
             initialItems = items.map { Item(rateIndex: $0.rateIndex, timeIndex: $0.timeIndex) }
 
             autotune = provider.autotune
-
-            if let newISF = provider.autosense.newisf {
-                autosensISF = newISF
-            }
-
-            autosensRatio = provider.autosense.ratio
-            setupDeterminationsArray()
         }
 
         func add() {
@@ -119,29 +108,6 @@ extension ISFEditor {
                 }
             }
         }
-
-        private func setupDeterminationsArray() {
-            Task {
-                let ids = await determinationStorage.fetchLastDeterminationObjectID(
-                    predicate: NSPredicate.enactedDetermination
-                )
-                await updateDeterminationsArray(with: ids)
-            }
-        }
-
-        @MainActor private func updateDeterminationsArray(with IDs: [NSManagedObjectID]) {
-            do {
-                let objects = try IDs.compactMap { id in
-                    try viewContext.existingObject(with: id) as? OrefDetermination
-                }
-                determinationsFromPersistence = objects
-
-            } catch {
-                debugPrint(
-                    "Home State: \(#function) \(DebuggingIdentifiers.failed) error while updating the glucose array: \(error.localizedDescription)"
-                )
-            }
-        }
     }
 }
 

+ 0 - 45
FreeAPS/Sources/Modules/ISFEditor/View/ISFEditorRootView.swift

@@ -33,13 +33,6 @@ extension ISFEditor {
             return formatter
         }
 
-        private var rateFormatter: NumberFormatter {
-            let formatter = NumberFormatter()
-            formatter.numberStyle = .decimal
-            formatter.maximumFractionDigits = 2
-            return formatter
-        }
-
         var saveButton: some View {
             ZStack {
                 let shouldDisableButton = state.items.isEmpty || !state.hasChanges
@@ -97,44 +90,6 @@ extension ISFEditor {
                         }
                     }.listRowBackground(Color.chart)
                 }
-                if let newISF = state.autosensISF {
-                    Section(
-                        header: !state.settingsManager.preferences
-                            .useNewFormula ? Text("Autosens") : Text("Dynamic Sensitivity")
-                    ) {
-                        let dynamicRatio = state.determinationsFromPersistence.first?.sensitivityRatio
-                        let dynamicISF = state.determinationsFromPersistence.first?.insulinSensitivity
-                        HStack {
-                            Text("Sensitivity Ratio")
-                            Spacer()
-                            Text(
-                                rateFormatter
-                                    .string(from: (
-                                        (
-                                            !state.settingsManager.preferences.useNewFormula ? state
-                                                .autosensRatio as NSDecimalNumber : dynamicRatio
-                                        ) ?? 1
-                                    ) as NSNumber) ?? "1"
-                            )
-                        }
-                        HStack {
-                            Text("Calculated Sensitivity")
-                            Spacer()
-                            if state.units == .mgdL {
-                                Text(
-                                    !state.settingsManager.preferences
-                                        .useNewFormula ? newISF.description : (dynamicISF ?? 0).description
-                                )
-                            } else {
-                                Text((
-                                    !state.settingsManager.preferences
-                                        .useNewFormula ? newISF.formattedAsMmolL : dynamicISF?.decimalValue.formattedAsMmolL
-                                ) ?? "0")
-                            }
-                            Text(state.units.rawValue + "/U").foregroundColor(.secondary)
-                        }
-                    }.listRowBackground(Color.chart)
-                }
 
                 Section(header: Text("Schedule")) {
                     list