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

update LA when cob or iob changes

polscm32 aka Marvout пре 1 година
родитељ
комит
e6435537d3

+ 27 - 0
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -83,7 +83,34 @@ final class OpenAPS {
                     }
             }
         }
+
+        // First save the current Determination to Core Data
         await attemptToSaveContext()
+
+        // After that check for changes in iob and cob and if there are any post a custom Notification
+        /// this is currently used to update Live Activity so that it stays up to date and not one loop cycle behind
+        await checkForCobIobUpdate(determination)
+    }
+
+    func checkForCobIobUpdate(_ determination: Determination) async {
+        let previousDeterminations = await CoreDataStack.shared.fetchEntitiesAsync(
+            ofType: OrefDetermination.self,
+            onContext: context,
+            predicate: NSPredicate.predicateFor30MinAgoForDetermination,
+            key: "deliverAt",
+            ascending: false,
+            fetchLimit: 2
+        )
+
+        // We need to get the second last Determination for this comparison because we have saved the current Determination already to Core Data
+        if let previousDetermination = previousDeterminations.dropFirst().first {
+            let iobChanged = previousDetermination.iob != decimalToNSDecimalNumber(determination.iob)
+            let cobChanged = previousDetermination.cob != Int16(Int(determination.cob ?? 0))
+
+            if iobChanged || cobChanged {
+                Foundation.NotificationCenter.default.post(name: .didUpdateCobIob, object: nil)
+            }
+        }
     }
 
     func attemptToSaveContext() async {

+ 1 - 1
FreeAPS/Sources/Services/LiveActivity/Data/DataManager.swift

@@ -23,7 +23,7 @@ extension LiveActivityBridge {
         let result = await CoreDataStack.shared.fetchEntitiesAsync(
             ofType: OrefDetermination.self,
             onContext: context,
-            predicate: NSPredicate.enactedDetermination,
+            predicate: NSPredicate.predicateFor30MinAgoForDetermination,
             key: "deliverAt",
             ascending: false,
             fetchLimit: 1,

+ 2 - 1
FreeAPS/Sources/Services/LiveActivity/LiveActivityBridge.swift

@@ -56,6 +56,7 @@ import UIKit
     private func setupNotifications() {
         let notificationCenter = Foundation.NotificationCenter.default
         notificationCenter.addObserver(self, selector: #selector(handleBatchInsert), name: .didPerformBatchInsert, object: nil)
+        notificationCenter.addObserver(self, selector: #selector(cobOrIobDidUpdate), name: .didUpdateCobIob, object: nil)
         notificationCenter
             .addObserver(forName: UIApplication.didEnterBackgroundNotification, object: nil, queue: nil) { [weak self] _ in
                 self?.forceActivityUpdate()
@@ -70,7 +71,7 @@ import UIKit
         setupGlucoseArray()
     }
 
-    @objc private func determinationDidUpdate() {
+    @objc private func cobOrIobDidUpdate() {
         Task {
             await fetchAndMapDetermination()
             if let determination = determination {

+ 5 - 11
LiveActivity/LiveActivity.swift

@@ -34,21 +34,15 @@ struct LiveActivity: Widget {
 
     @ViewBuilder private func changeLabel(context: ActivityViewContext<LiveActivityAttributes>) -> some View {
         if !context.state.change.isEmpty {
-            if context.isStale {
-                Text(context.state.change).foregroundStyle(.primary.opacity(0.5)).font(.headline)
-                    .strikethrough(pattern: .solid, color: .red.opacity(0.6)).font(.callout)
-            } else {
-                HStack {
-                    Text(context.state.change).font(.headline)
-                }
-            }
+            Text(context.state.change).foregroundStyle(.primary.opacity(0.5)).font(.headline)
+                .strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
         } else {
             Text("--")
         }
     }
 
     @ViewBuilder func mealLabel(
-        context _: ActivityViewContext<LiveActivityAttributes>,
+        context: ActivityViewContext<LiveActivityAttributes>,
         additionalState: LiveActivityAttributes.ContentAdditionalState
     ) -> some View {
         HStack {
@@ -68,13 +62,13 @@ struct LiveActivity: Widget {
                 HStack {
                     Text(
                         carbsFormatter.string(from: additionalState.cob as NSNumber) ?? "--"
-                    ).fontWeight(.bold).font(.headline)
+                    ).fontWeight(.bold).font(.headline).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
                     Text(NSLocalizedString(" g", comment: "grams of carbs")).foregroundStyle(.secondary).font(.footnote)
                 }
                 HStack {
                     Text(
                         bolusFormatter.string(from: additionalState.iob as NSNumber) ?? "--"
-                    ).font(.headline).fontWeight(.bold)
+                    ).font(.headline).fontWeight(.bold).strikethrough(context.isStale, pattern: .solid, color: .red.opacity(0.6))
                     Text(NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)"))
                         .foregroundStyle(.secondary).font(.footnote)
                 }

+ 1 - 0
Model/Helper/CustomNotification.swift

@@ -6,4 +6,5 @@ extension Notification.Name {
     static let didPerformBatchDelete = Notification.Name("didPerformBatchDelete")
     static let didUpdateDetermination = Notification.Name("didUpdateDetermination")
     static let didUpdateOverrideConfiguration = Notification.Name("didUpdateOverrideConfiguration")
+    static let didUpdateCobIob = Notification.Name("didUpdateCobIob")
 }