Browse Source

Merge remote-tracking branch 'ivalkou/dev' into Crowdin

Jon B.M 4 years ago
parent
commit
5c916a5c5e

+ 13 - 0
FreeAPS/Sources/APS/APSManager.swift

@@ -106,6 +106,12 @@ final class BaseAPSManager: APSManager, Injectable {
         openAPS = OpenAPS(storage: storage)
         subscribe()
         lastLoopDateSubject.send(lastLoopDate)
+
+        isLooping
+            .sink { value in
+                self.deviceDataManager.loopInProgress = value
+            }
+            .store(in: &lifetime)
     }
 
     private func subscribe() {
@@ -305,6 +311,13 @@ final class BaseAPSManager: APSManager, Injectable {
             if case let .failure(error) = completion {
                 warning(.apsManager, "Bolus failed with error: \(error.localizedDescription)")
                 self.processError(APSError.pumpError(error))
+                if !isSMB {
+                    self.processQueue.async {
+                        self.broadcaster.notify(BolusFailureObserver.self, on: self.processQueue) {
+                            $0.bolusDidFail()
+                        }
+                    }
+                }
             } else {
                 debug(.apsManager, "Bolus succeeded")
                 if !isSMB {

+ 13 - 3
FreeAPS/Sources/APS/DeviceDataManager.swift

@@ -12,6 +12,7 @@ import UserNotifications
 
 protocol DeviceDataManager: GlucoseSource {
     var pumpManager: PumpManagerUI? { get set }
+    var loopInProgress: Bool { get set }
     var pumpDisplayState: CurrentValueSubject<PumpDisplayState?, Never> { get }
     var recommendsLoop: PassthroughSubject<Void, Never> { get }
     var bolusTrigger: PassthroughSubject<Bool, Never> { get }
@@ -51,6 +52,7 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
     let pumpNewStatus = PassthroughSubject<Void, Never>()
     @SyncAccess private var pumpUpdateCancellable: AnyCancellable?
     private var pumpUpdatePromise: Future<Bool, Never>.Promise?
+    @SyncAccess var loopInProgress: Bool = false
 
     var pumpManager: PumpManagerUI? {
         didSet {
@@ -100,7 +102,12 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
 
     func heartbeat(date: Date) {
         guard pumpUpdateCancellable == nil else {
-            warning(.deviceManager, "Pump updating already in progress")
+            warning(.deviceManager, "Pump updating already in progress. Skip updating.")
+            return
+        }
+
+        guard !loopInProgress else {
+            warning(.deviceManager, "Loop in progress. Skip updating.")
             return
         }
 
@@ -139,9 +146,12 @@ final class BaseDeviceDataManager: DeviceDataManager, Injectable {
         pumpUpdateCancellable = nil
         pumpUpdatePromise = nil
         if !recommendsLoop {
-            warning(.deviceManager, "Loop recommendation time out or got error. Starting loop right now.")
+            warning(.deviceManager, "Loop recommendation time out or got error. Trying to loop right now.")
+        }
+        guard !loopInProgress else {
+            warning(.deviceManager, "Loop already in progress. Skip recommendation.")
+            return
         }
-
         self.recommendsLoop.send()
     }
 

+ 6 - 0
FreeAPS/Sources/Localizations/Main/en.lproj/Localizable.strings

@@ -872,6 +872,12 @@ Enact a temp Basal or a temp target */
 /* About this source */
 "About this source" = "About this source";
 
+/* */
+"Bolus failed" = "Bolus failed";
+
+/* */
+"Bolus failed or inaccurate. Check pump history before repeating." = "Bolus failed or inaccurate. Check pump history before repeating.";
+
 
 /* Headers for settings ----------------------- */
 "OpenAPS main settings" = "OpenAPS main settings";

+ 6 - 0
FreeAPS/Sources/Localizations/Main/ru.lproj/Localizable.strings

@@ -872,6 +872,12 @@ Enact a temp Basal or a temp target */
 /* About this source */
 "About this source" = "Об этом источнике";
 
+/* */
+"Bolus failed" = "Болюс не выполнен";
+
+/* */
+"Bolus failed or inaccurate. Check pump history before repeating." = "Болюс не выполнен или неточный. Перед повторением проверьте историю помпы.";
+
 
 /* Headers for settings ----------------------- */
 "OpenAPS main settings" = "Основные настройки OpenAPS";

+ 34 - 0
FreeAPS/Sources/Services/UserNotifiactions/UserNotificationsManager.swift

@@ -18,12 +18,17 @@ enum NotificationAction: String {
     case snooze
 }
 
+protocol BolusFailureObserver {
+    func bolusDidFail()
+}
+
 final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, Injectable {
     private enum Identifier: String {
         case glucocoseNotification = "FreeAPS.glucoseNotification"
         case carbsRequiredNotification = "FreeAPS.carbsRequiredNotification"
         case noLoopFirstNotification = "FreeAPS.noLoopFirstNotification"
         case noLoopSecondNotification = "FreeAPS.noLoopSecondNotification"
+        case bolusFailedNotification = "FreeAPS.bolusFailedNotification"
     }
 
     @Injected() private var settingsManager: SettingsManager!
@@ -44,6 +49,7 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
         injectServices(resolver)
         broadcaster.register(GlucoseObserver.self, observer: self)
         broadcaster.register(SuggestionObserver.self, observer: self)
+        broadcaster.register(BolusFailureObserver.self, observer: self)
 
         requestNotificationPermissionsIfNeeded()
         sendGlucoseNotification()
@@ -144,6 +150,28 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
         }
     }
 
+    private func notifyBolusFailure() {
+        ensureCanSendNotification {
+            let title = NSLocalizedString("Bolus failed", comment: "Bolus failed")
+            let body = NSLocalizedString(
+                "Bolus failed or inaccurate. Check pump history before repeating.",
+                comment: "Bolus failed or inaccurate. Check pump history before repeating."
+            )
+
+            let content = UNMutableNotificationContent()
+            content.title = title
+            content.body = body
+            content.sound = .default
+
+            self.addRequest(
+                identifier: .noLoopFirstNotification,
+                content: content,
+                deleteOld: true,
+                trigger: nil
+            )
+        }
+    }
+
     private func sendGlucoseNotification() {
         addAppBadge(glucose: nil)
 
@@ -373,6 +401,12 @@ extension BaseUserNotificationsManager: SuggestionObserver {
     }
 }
 
+extension BaseUserNotificationsManager: BolusFailureObserver {
+    func bolusDidFail() {
+        notifyBolusFailure()
+    }
+}
+
 extension BaseUserNotificationsManager: UNUserNotificationCenterDelegate {
     func userNotificationCenter(
         _: UNUserNotificationCenter,