Explorar o código

Add proper alerting when basal rates cannot be stored due to pump connectivity issues

Deniz Cengiz hai 1 ano
pai
achega
70e784d8f2

+ 2 - 2
FreeAPS/Sources/Modules/BasalProfileEditor/BasalProfileEditorProvider.swift

@@ -18,8 +18,8 @@ extension BasalProfileEditor {
 
         func saveProfile(_ profile: [BasalProfileEntry]) -> AnyPublisher<Void, Error> {
             guard let pump = deviceManager?.pumpManager else {
-                storage.save(profile, as: OpenAPS.Settings.basalProfile)
-                return Just(()).setFailureType(to: Error.self).eraseToAnyPublisher()
+                debugPrint("\(DebuggingIdentifiers.failed) No pump found; cannot save basal profile!")
+                return Fail(error: NSError()).eraseToAnyPublisher()
             }
 
             let syncValues = profile.map {

+ 22 - 11
FreeAPS/Sources/Modules/BasalProfileEditor/BasalProfileEditorStateModel.swift

@@ -8,6 +8,7 @@ extension BasalProfileEditor {
         @Published var initialItems: [Item] = []
         @Published var items: [Item] = []
         @Published var total: Decimal = 0.0
+        @Published var showAlert: Bool = false
 
         let timeValues = stride(from: 0.0, to: 1.days.timeInterval, by: 30.minutes.timeInterval).map { $0 }
 
@@ -72,25 +73,35 @@ extension BasalProfileEditor {
 
             syncInProgress = true
             let profile = items.map { item -> BasalProfileEntry in
-                let fotmatter = DateFormatter()
-                fotmatter.timeZone = TimeZone(secondsFromGMT: 0)
-                fotmatter.dateFormat = "HH:mm:ss"
+                let formatter = DateFormatter()
+                formatter.timeZone = TimeZone(secondsFromGMT: 0)
+                formatter.dateFormat = "HH:mm:ss"
                 let date = Date(timeIntervalSince1970: self.timeValues[item.timeIndex])
                 let minutes = Int(date.timeIntervalSince1970 / 60)
                 let rate = self.rateValues[item.rateIndex]
-                return BasalProfileEntry(start: fotmatter.string(from: date), minutes: minutes, rate: rate)
+                return BasalProfileEntry(start: formatter.string(from: date), minutes: minutes, rate: rate)
             }
             provider.saveProfile(profile)
                 .receive(on: DispatchQueue.main)
-                .sink { _ in
+                .sink { completion in
                     self.syncInProgress = false
-                    self.initialItems = self.items.map { Item(rateIndex: $0.rateIndex, timeIndex: $0.timeIndex) }
-
-                    Task.detached(priority: .low) {
-                        debug(.nightscout, "Attempting to upload basal rates to Nightscout")
-                        await self.nightscout.uploadProfiles()
+                    switch completion {
+                    case .finished:
+                        // Successfully saved and synced
+                        self.initialItems = self.items.map { Item(rateIndex: $0.rateIndex, timeIndex: $0.timeIndex) }
+
+                        Task.detached(priority: .low) {
+                            debug(.nightscout, "Attempting to upload basal rates to Nightscout")
+                            await self.nightscout.uploadProfiles()
+                        }
+                    case .failure:
+                        // Handle the error, show error message
+                        self.showAlert = true
                     }
-                } receiveValue: {}
+                } receiveValue: {
+                    // Handle any successful value if needed
+                    print("We were successful")
+                }
                 .store(in: &lifetime)
         }
 

+ 7 - 0
FreeAPS/Sources/Modules/BasalProfileEditor/View/BasalProfileEditorRootView.swift

@@ -77,6 +77,13 @@ extension BasalProfileEditor {
                     }
                 }.listRowBackground(shouldDisableButton ? Color(.systemGray4) : Color(.systemBlue))
             }
+            .alert(isPresented: $state.showAlert) {
+                Alert(
+                    title: Text("Unable to Save"),
+                    message: Text("Trio could not communicate with your pump. Changes to your basal profile were not saved."),
+                    dismissButton: .default(Text("Close"))
+                )
+            }
             .onChange(of: state.items) { _ in
                 state.calcTotal()
             }