فهرست منبع

NavigationLazyView

Ivan Valkou 5 سال پیش
والد
کامیت
d7b5e10a11

+ 6 - 4
FreeAPS/Sources/APS/APSManager.swift

@@ -318,10 +318,12 @@ final class BaseAPSManager: APSManager, Injectable {
     }
 
     private func reportEnacted(suggestion: Suggestion, received: Bool) {
-        var enacted = suggestion
-        enacted.timestamp = Date()
-        enacted.recieved = received
-        try? storage.save(enacted, as: OpenAPS.Enact.enacted)
+        if received, suggestion.deliverAt != nil {
+            var enacted = suggestion
+            enacted.timestamp = Date()
+            enacted.recieved = received
+            try? storage.save(enacted, as: OpenAPS.Enact.enacted)
+        }
         nightscout.uploadStatus()
     }
 }

+ 1 - 0
FreeAPS/Sources/Models/Autosens.swift

@@ -2,5 +2,6 @@ import Foundation
 
 struct Autosens: JSON {
     let ratio: Decimal
+    let newISF: Decimal?
     var timestamp: Date?
 }

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

@@ -26,4 +26,5 @@ enum ISFEditor {
 protocol ISFEditorProvider: Provider {
     var profile: InsulinSensitivities { get }
     func saveProfile(_ profile: InsulinSensitivities)
+    var autosense: Autosens { get }
 }

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

@@ -13,5 +13,11 @@ extension ISFEditor {
         func saveProfile(_ profile: InsulinSensitivities) {
             try? storage.save(profile, as: OpenAPS.Settings.insulinSensitivities)
         }
+
+        var autosense: Autosens {
+            (try? storage.retrieve(OpenAPS.Settings.autosense, as: Autosens.self))
+                ?? Autosens(from: OpenAPS.defaults(for: OpenAPS.Settings.autosense))
+                ?? Autosens(ratio: 1, newISF: nil, timestamp: nil)
+        }
     }
 }

+ 10 - 0
FreeAPS/Sources/Modules/ISFEditor/ISFEditorViewModel.swift

@@ -4,6 +4,7 @@ extension ISFEditor {
     class ViewModel<Provider>: BaseViewModel<Provider>, ObservableObject where Provider: ISFEditorProvider {
         @Injected() var settingsManager: SettingsManager!
         @Published var items: [Item] = []
+        private(set) var autosensISF: Double?
 
         let timeValues = stride(from: 0.0, to: 1.days.timeInterval, by: 30.minutes.timeInterval).map { $0 }
 
@@ -31,6 +32,15 @@ extension ISFEditor {
                 let rateIndex = rateValues.firstIndex(of: Double(value.sensitivity)) ?? 0
                 return Item(rateIndex: rateIndex, timeIndex: timeIndex)
             }
+
+            if let newISF = provider.autosense.newISF, provider.autosense.ratio != 1 {
+                switch units {
+                case .mgdL:
+                    autosensISF = Double(newISF)
+                case .mmolL:
+                    autosensISF = round(Double(newISF * GlucoseUnits.exchangeRate) * 10) / 10
+                }
+            }
         }
 
         func add() {

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

@@ -15,11 +15,21 @@ extension ISFEditor {
         private var rateFormatter: NumberFormatter {
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
+            formatter.maximumFractionDigits = 1
             return formatter
         }
 
         var body: some View {
             Form {
+                if let newISF = viewModel.autosensISF {
+                    Section(header: Text("Autosens")) {
+                        HStack {
+                            Text("New ISF")
+                            Spacer()
+                            Text(rateFormatter.string(from: newISF as NSNumber) ?? "0" + " \(viewModel.units)/U")
+                        }
+                    }
+                }
                 Section(header: Text("Schedule")) {
                     list
                     addButton

+ 14 - 3
FreeAPS/Sources/Views/ViewModifiers.swift

@@ -37,15 +37,26 @@ struct CapsulaBackground: ViewModifier {
     }
 }
 
+struct NavigationLazyView<Content: View>: View {
+    let build: () -> Content
+    init(_ build: @autoclosure @escaping () -> Content) {
+        self.build = build
+    }
+
+    var body: Content {
+        build()
+    }
+}
+
 struct Link<T>: ViewModifier where T: View {
-    private let destination: T
-    init(destination: T) {
+    private let destination: () -> T
+    init(destination: @autoclosure @escaping () -> T) {
         self.destination = destination
     }
 
     func body(content: Content) -> some View {
         ZStack {
-            NavigationLink(destination: destination) {
+            NavigationLink(destination: NavigationLazyView(destination())) {
                 EmptyView()
             }.hidden()
             content