Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/Artificial-Pancreas/iAPS int…
Browse files Browse the repository at this point in the history
…o dev-bo
  • Loading branch information
actions-user committed Oct 10, 2023
2 parents 4d5e9f1 + ab761b9 commit 21a0ffa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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?
Expand Down Expand Up @@ -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")
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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() })
}
}
}
}

0 comments on commit 21a0ffa

Please sign in to comment.