Преглед изворни кода

make cancel bolus func async

polscm32 пре 1 година
родитељ
комит
f8783f2ed5
2 измењених фајлова са 38 додато и 24 уклоњено
  1. 33 21
      FreeAPS/Sources/APS/APSManager.swift
  2. 5 3
      FreeAPS/Sources/Modules/Home/HomeStateModel.swift

+ 33 - 21
FreeAPS/Sources/APS/APSManager.swift

@@ -29,7 +29,7 @@ protocol APSManager {
     func determineBasalSync()
     func roundBolus(amount: Decimal) -> Decimal
     var lastError: CurrentValueSubject<Error?, Never> { get }
-    func cancelBolus()
+    func cancelBolus() async
     func enactAnnouncement(_ announcement: Announcement)
 }
 
@@ -518,22 +518,19 @@ final class BaseAPSManager: APSManager, Injectable {
         }
     }
 
-    func cancelBolus() {
+    func cancelBolus() async {
         guard let pump = pumpManager, pump.status.pumpStatus.bolusing else { return }
         debug(.apsManager, "Cancel bolus")
-        pump.cancelBolus().sink { completion in
-            if case let .failure(error) = completion {
-                debug(.apsManager, "Bolus cancellation failed with error: \(error.localizedDescription)")
-                self.processError(APSError.pumpError(error))
-            } else {
-                debug(.apsManager, "Bolus cancelled")
-            }
-
-            self.bolusReporter?.removeObserver(self)
-            self.bolusReporter = nil
-            self.bolusProgress.send(nil)
-        } receiveValue: { _ in }
-            .store(in: &lifetime)
+        do {
+            _ = try await pump.cancelBolus()
+            debug(.apsManager, "Bolus cancelled")
+        } catch {
+            debug(.apsManager, "Bolus cancellation failed with error: \(error.localizedDescription)")
+            processError(APSError.pumpError(error))
+        }
+        bolusReporter?.removeObserver(self)
+        bolusReporter = nil
+        bolusProgress.send(nil)
     }
 
     func enactTempBasal(rate: Double, duration: TimeInterval) {
@@ -1396,23 +1393,38 @@ private extension PumpManager {
         }
     }
 
-    func cancelBolus() -> AnyPublisher<DoseEntry?, Error> {
-        Future { promise in
+    func cancelBolus() async throws -> DoseEntry? {
+        try await withCheckedThrowingContinuation { continuation in
             self.cancelBolus { result in
                 switch result {
                 case let .success(dose):
                     debug(.apsManager, "Cancel Bolus succeeded")
-                    promise(.success(dose))
+                    continuation.resume(returning: dose)
                 case let .failure(error):
                     debug(.apsManager, "Cancel Bolus failed")
-                    promise(.failure(error))
+                    continuation.resume(throwing: APSError.pumpError(error))
                 }
             }
         }
-        .mapError { APSError.pumpError($0) }
-        .eraseToAnyPublisher()
     }
 
+//    func cancelBolus() -> AnyPublisher<DoseEntry?, Error> {
+//        Future { promise in
+//            self.cancelBolus { result in
+//                switch result {
+//                case let .success(dose):
+//                    debug(.apsManager, "Cancel Bolus succeeded")
+//                    promise(.success(dose))
+//                case let .failure(error):
+//                    debug(.apsManager, "Cancel Bolus failed")
+//                    promise(.failure(error))
+//                }
+//            }
+//        }
+//        .mapError { APSError.pumpError($0) }
+//        .eraseToAnyPublisher()
+//    }
+
     func suspendDelivery() -> AnyPublisher<Void, Error> {
         Future { promise in
             self.suspendDelivery { error in

+ 5 - 3
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -216,10 +216,12 @@ extension Home {
         }
 
         func cancelBolus() {
-            apsManager.cancelBolus()
+            Task {
+                await apsManager.cancelBolus()
 
-            // perform determine basal sync, otherwise you have could end up with too much iob when opening the calculator again
-            apsManager.determineBasalSync()
+                // perform determine basal sync, otherwise you have could end up with too much iob when opening the calculator again
+                apsManager.determineBasalSync()
+            }
         }
 
         func cancelProfile() {