Browse Source

update basal chart

Ivan Valkou 5 years ago
parent
commit
87c4caa876

+ 18 - 1
FreeAPS/Sources/APS/APSManager.swift

@@ -12,6 +12,7 @@ protocol APSManager {
     var pumpManager: PumpManagerUI? { get set }
     var pumpDisplayState: CurrentValueSubject<PumpDisplayState?, Never> { get }
     func enactTempBasal(rate: Double, duration: TimeInterval)
+    func makeProfiles() -> AnyPublisher<Bool, Never>
 }
 
 final class BaseAPSManager: APSManager, Injectable {
@@ -134,7 +135,7 @@ final class BaseAPSManager: APSManager, Injectable {
         let now = Date()
         let temp = currentTemp(date: now)
 
-        let mainPublisher = openAPS.makeProfiles(useAutotune: settings.useAutotune)
+        let mainPublisher = makeProfiles()
             .flatMap { _ in
                 self.openAPS.determineBasal(currentTemp: temp, clock: now)
             }
@@ -165,6 +166,22 @@ final class BaseAPSManager: APSManager, Injectable {
         return mainPublisher
     }
 
+    func makeProfiles() -> AnyPublisher<Bool, Never> {
+        openAPS.makeProfiles(useAutotune: settings.useAutotune)
+            .map { tunedProfile in
+                if let basalProfile = tunedProfile?.basalProfile {
+                    self.processQueue.async {
+                        self.broadcaster.notify(BasalProfileObserver.self, on: self.processQueue) {
+                            $0.basalProfileDidChange(basalProfile)
+                        }
+                    }
+                }
+
+                return tunedProfile != nil
+            }
+            .eraseToAnyPublisher()
+    }
+
     func enactBolus(amount: Double) {
         guard let pump = pumpManager, verifyStatus() else { return }
 

+ 7 - 2
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -144,7 +144,7 @@ final class OpenAPS {
         }
     }
 
-    func makeProfiles(useAutotune: Bool) -> Future<Void, Never> {
+    func makeProfiles(useAutotune: Bool) -> Future<Autotune?, Never> {
         Future { promise in
             self.processQueue.async {
                 let preferences = self.loadFileFromStorage(name: Settings.preferences)
@@ -184,7 +184,12 @@ final class OpenAPS {
                 try? self.storage.save(pumpProfile, as: Settings.pumpProfile)
                 try? self.storage.save(profile, as: Settings.profile)
 
-                promise(.success(()))
+                if let tunedProfile = Autotune(from: profile) {
+                    promise(.success(tunedProfile))
+                    return
+                }
+
+                promise(.success(nil))
             }
         }
     }

+ 11 - 5
FreeAPS/Sources/Modules/AutotuneConfig/AutotuneConfigViewModel.swift

@@ -1,8 +1,10 @@
+import Combine
 import SwiftUI
 
 extension AutotuneConfig {
     class ViewModel<Provider>: BaseViewModel<Provider>, ObservableObject where Provider: AutotuneConfigProvider {
         @Injected() var settingsManager: SettingsManager!
+        @Injected() var apsManager: APSManager!
         @Published var useAutotune = false
         @Published var autotune: Autotune?
         private(set) var units: GlucoseUnits = .mmolL
@@ -14,18 +16,22 @@ extension AutotuneConfig {
 
             $useAutotune
                 .removeDuplicates()
-                .sink { [weak self] use in
-                    self?.settingsManager.settings.useAutotune = use
+                .flatMap { use -> AnyPublisher<Bool, Never> in
+                    self.settingsManager.settings.useAutotune = use
+                    return self.apsManager.makeProfiles()
                 }
+                .sink { _ in }
                 .store(in: &lifetime)
         }
 
         func run() {
             provider.runAutotune()
                 .receive(on: DispatchQueue.main)
-                .sink { [weak self] result in
-                    self?.autotune = result
-                }.store(in: &lifetime)
+                .flatMap { result -> AnyPublisher<Bool, Never> in
+                    self.autotune = result
+                    return self.apsManager.makeProfiles()
+                }
+                .sink { _ in }.store(in: &lifetime)
         }
 
         func delete() {

+ 12 - 2
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -130,6 +130,9 @@ struct MainChartView: View {
         .onChange(of: maxBasal) { _ in
             calculateBasalPoints(fullSize: fullSize)
         }
+        .onChange(of: basalProfile) { _ in
+            calculateBasalPoints(fullSize: fullSize)
+        }
         .onChange(of: didAppearTrigger) { _ in
             calculateBasalPoints(fullSize: fullSize)
         }
@@ -291,21 +294,23 @@ struct MainChartView: View {
             path.addLine(to: CGPoint(x: 0, y: Config.basalHeight))
         }
 
+        let endDateTime = dayAgoTime + 1.days.timeInterval + 6.hours.timeInterval
         let regularBasalPoints = findRegularBasalPoints(
             timeBegin: dayAgoTime,
-            timeEnd: dayAgoTime + 1.days.timeInterval + 6.hours.timeInterval,
+            timeEnd: endDateTime,
             fullSize: fullSize
         )
 
         regularBasalPath = Path { path in
             var yPoint: CGFloat = Config.basalHeight
-            path.move(to: CGPoint(x: 0, y: yPoint))
+            path.move(to: CGPoint(x: -50, y: yPoint))
 
             for point in regularBasalPoints {
                 path.addLine(to: CGPoint(x: point.x, y: yPoint))
                 path.addLine(to: point)
                 yPoint = point.y
             }
+            path.addLine(to: CGPoint(x: timeToXCoordinate(endDateTime, fullSize: fullSize), y: yPoint))
         }
     }
 
@@ -327,6 +332,11 @@ struct MainChartView: View {
                 time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval + 1.days.timeInterval).timeIntervalSince1970,
                 rate: $0.rate
             )
+        } + basalProfile.map {
+            (
+                time: startOfDay.addingTimeInterval($0.minutes.minutes.timeInterval + 2.days.timeInterval).timeIntervalSince1970,
+                rate: $0.rate
+            )
         }
 
         let basalTruncatedPoints = basalNormalized.windows(ofCount: 2)