Przeglądaj źródła

Save Autotuned basal rates as normal basal rates and to pump.

* Button in Autotune settings. Hidden at the very bottom.
Jon B Mårtensson 2 lat temu
rodzic
commit
ab761b9463

+ 32 - 0
FreeAPS/Sources/Modules/AutotuneConfig/AutotuneConfigStateModel.swift

@@ -1,9 +1,11 @@
 import Combine
+import LoopKit
 import SwiftUI
 
 extension AutotuneConfig {
     final class StateModel: BaseStateModel<Provider> {
         @Injected() var apsManager: APSManager!
+        @Injected() private var storage: FileStorage!
         @Published var useAutotune = false
         @Published var onlyAutotuneBasals = false
         @Published var autotune: Autotune?
@@ -59,5 +61,35 @@ extension AutotuneConfig {
                 .cancellable()
                 .store(in: &lifetime)
         }
+
+        func replace() {
+            if let autotunedBasals = autotune {
+                let basals = autotunedBasals.basalProfile
+                    .map { basal -> BasalProfileEntry in
+                        BasalProfileEntry(
+                            start: String(basal.start.prefix(5)),
+                            minutes: basal.minutes,
+                            rate: basal.rate
+                        )
+                    }
+                guard let pump = apsManager.pumpManager else {
+                    storage.save(basals, as: OpenAPS.Settings.basalProfile)
+                    debug(.service, "Basals have been replaced with Autotuned Basals by user.")
+                    return
+                }
+                let syncValues = basals.map {
+                    RepeatingScheduleValue(startTime: TimeInterval($0.minutes * 60), value: Double($0.rate))
+                }
+                pump.syncBasalRateSchedule(items: syncValues) { result in
+                    switch result {
+                    case .success:
+                        self.storage.save(basals, as: OpenAPS.Settings.basalProfile)
+                        debug(.service, "Basals saved to pump!")
+                    case .failure:
+                        debug(.service, "Basals couldn't be save to pump")
+                    }
+                }
+            }
+        }
     }
 }

+ 17 - 0
FreeAPS/Sources/Modules/AutotuneConfig/View/AutotuneConfigRootView.swift

@@ -5,6 +5,7 @@ extension AutotuneConfig {
     struct RootView: BaseView {
         let resolver: Resolver
         @StateObject var state = StateModel()
+        @State var replaceAlert = false
 
         private var isfFormatter: NumberFormatter {
             let formatter = NumberFormatter()
@@ -94,11 +95,27 @@ extension AutotuneConfig {
                         label: { Text("Delete autotune data") }
                             .foregroundColor(.red)
                     }
+
+                    Section {
+                        Button {
+                            replaceAlert = true
+                        }
+                        label: { Text("Save as your Normal Basal Rates") }
+                    } header: {
+                        Text("Replace Normal Basal")
+                    }
                 }
             }
             .onAppear(perform: configureView)
             .navigationTitle("Autotune")
             .navigationBarTitleDisplayMode(.automatic)
+            .alert(Text("Are you sure?"), isPresented: $replaceAlert) {
+                Button("Yes", action: {
+                    state.replace()
+                    replaceAlert.toggle()
+                })
+                Button("No", action: { replaceAlert.toggle() })
+            }
         }
     }
 }