소스 검색

Merge branch 'watch' of github.com:nightscout/Trio-dev into local-testing-02022025

Deniz Cengiz 1 년 전
부모
커밋
1b2a85b2b5

+ 2 - 5
Trio Watch App Extension/Views/BolusProgressOverlay.swift

@@ -17,9 +17,6 @@ struct BolusProgressOverlay: View {
     )
 
     var body: some View {
-        let bolusIncrement = Double(truncating: state.bolusIncrement as NSNumber)
-        let adjustedBolusAmount = floor(state.activeBolusAmount / bolusIncrement) * bolusIncrement
-
         VStack(spacing: 10) {
             VStack {
                 Text("Bolusing")
@@ -32,8 +29,8 @@ struct BolusProgressOverlay: View {
 
                 Text(String(
                     format: "%.2f U of %.2f U",
-                    state.bolusProgress * adjustedBolusAmount,
-                    adjustedBolusAmount
+                    state.deliveredAmount,
+                    state.activeBolusAmount
                 ))
                     .font(.footnote)
                     .foregroundStyle(.secondary)

+ 4 - 0
Trio Watch App Extension/Views/TrioMainWatchView.swift

@@ -91,6 +91,10 @@ struct TrioMainWatchView: View {
                 GlucoseChartView(glucoseValues: state.glucoseValues)
                     .tag(1)
             }
+            .onAppear {
+                state.bolusProgress = 0
+                state.activeBolusAmount = 0
+            }
             .background(trioBackgroundColor)
             .tabViewStyle(.verticalPage)
             .digitalCrownRotation($currentPage.doubleBinding(), from: 0, through: 1, by: 1)

+ 11 - 17
Trio Watch App Extension/WatchState.swift

@@ -31,11 +31,12 @@ import WatchConnectivity
     var carbsAmount: Int = 0
     var fatAmount: Int = 0
     var proteinAmount: Int = 0
-    var bolusAmount = 0.0
-    var activeBolusAmount = 0.0
-    var confirmationProgress = 0.0
+    var bolusAmount: Double = 0.0
+    var confirmationProgress: Double = 0.0
 
     var bolusProgress: Double = 0.0
+    var activeBolusAmount: Double = 0.0
+    var deliveredAmount: Double = 0.0
     var isBolusCanceled = false
 
     // Safety limits
@@ -64,16 +65,8 @@ import WatchConnectivity
     var mealBolusStep: MealBolusStep = .savingCarbs
     var isMealBolusCombo: Bool = false
 
-    private var _showBolusProgressOverlay: Bool = false
     var showBolusProgressOverlay: Bool {
-        get {
-            return (!showAcknowledgmentBanner || !showCommsAnimation || !showCommsAnimation) &&
-                   bolusProgress > 0 && bolusProgress < 1.0 &&
-                   !isBolusCanceled
-        }
-        set {
-            _showBolusProgressOverlay = newValue
-        }
+        (!showAcknowledgmentBanner || !showCommsAnimation) && bolusProgress > 0 && bolusProgress < 1.0 && !isBolusCanceled
     }
 
     
@@ -149,12 +142,10 @@ import WatchConnectivity
             }
 
             if activationState == .activated {
-                self.forceConditionalWatchStateUpdate()
-                // reset bolus progress visibility
-                self.showBolusProgressOverlay = false
-                
                 print("⌚️ Watch session activated with state: \(activationState.rawValue)")
 
+                self.forceConditionalWatchStateUpdate()
+
                 self.isReachable = session.isReachable
 
                 print("⌚️ Watch isReachable after activation: \(session.isReachable)")
@@ -215,7 +206,8 @@ import WatchConnectivity
                     // Handle bolus progress updates
         } else if
             let progress = message[WatchMessageKeys.bolusProgress] as? Double,
-            let activeBolusAmount = message[WatchMessageKeys.activeBolusAmount] as? Double
+            let activeBolusAmount = message[WatchMessageKeys.activeBolusAmount] as? Double,
+            let deliveredAmount = message[WatchMessageKeys.deliveredAmount] as? Double
         {
             DispatchQueue.main.async {
                 if !self.isBolusCanceled {
@@ -226,6 +218,8 @@ import WatchConnectivity
                     if self.activeBolusAmount == 0 {
                         self.activeBolusAmount = activeBolusAmount
                     }
+
+                    self.deliveredAmount = deliveredAmount
                 }
             }
             return

+ 1 - 0
Trio/Sources/Models/WatchMessageKeys.swift

@@ -13,6 +13,7 @@ enum WatchMessageKeys {
     static let bolusCanceled = "bolusCanceled"
     static let bolusProgress = "bolusProgress"
     static let activeBolusAmount = "activeBolusAmount"
+    static let deliveredAmount = "deliveredAmount"
 
     // Recommendation Keys
     static let requestBolusRecommendation = "requestBolusRecommendation"

+ 34 - 6
Trio/Sources/Services/WatchManager/AppleWatchManager.swift

@@ -459,7 +459,7 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
 
     func session(_: WCSession, didReceiveMessage message: [String: Any]) {
         DispatchQueue.main.async { [weak self] in
-            // Prüfe zuerst auf Watch State Update Request
+            // Check Watch State Update Request first
             if let requestWatchUpdate = message[WatchMessageKeys.requestWatchUpdate] as? String,
                requestWatchUpdate == WatchMessageKeys.watchState
             {
@@ -529,7 +529,7 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
                     }
                     debug(.watchManager, "📱 Bolus cancelled from watch")
 
-                    // perform determine basal sync, otherwise you have could end up with too much iob when opening the calculator again
+                    // perform determine basal sync, otherwise you could end up with too much IOB when opening the calculator again
                     await self?.apsManager.determineBasalSync()
                 }
             }
@@ -907,16 +907,44 @@ final class BaseWatchManager: NSObject, WCSessionDelegate, Injectable, WatchMana
 
     /// Sends bolus progress updates to the Watch
     /// - Parameter progress: The current bolus progress as a Decimal
+//    private func sendBolusProgressToWatch(progress: Decimal?) async {
+//        guard let session = session, session.isReachable, let progress = progress else { return }
+//
+//        let message: [String: Any] = [
+//            WatchMessageKeys.bolusProgress: Double(truncating: progress as NSNumber),
+//            WatchMessageKeys.activeBolusAmount: activeBolusAmount
+//        ]
+//
+//        session.sendMessage(message, replyHandler: nil) { error in
+//            debug(.watchManager, "❌ Error sending bolus progress: \(error.localizedDescription)")
+//        }
+//    }
     private func sendBolusProgressToWatch(progress: Decimal?) async {
-        guard let session = session, session.isReachable, let progress = progress else { return }
+        guard let session = session, let progress = progress, let pumpManager = apsManager.pumpManager else { return }
 
         let message: [String: Any] = [
             WatchMessageKeys.bolusProgress: Double(truncating: progress as NSNumber),
-            WatchMessageKeys.activeBolusAmount: activeBolusAmount
+            WatchMessageKeys.activeBolusAmount: activeBolusAmount,
+            WatchMessageKeys.deliveredAmount: pumpManager
+                .roundToSupportedBolusVolume(units: activeBolusAmount * Double(truncating: progress as NSNumber))
         ]
+        // If the session is not yet activated, try to activate
+        if session.activationState != .activated {
+            session.activate()
+            // Then, queue data for eventual delivery in the background
+            session.transferUserInfo(message)
+            return
+        }
 
-        session.sendMessage(message, replyHandler: nil) { error in
-            debug(.watchManager, "❌ Error sending bolus progress: \(error.localizedDescription)")
+        // If we reach here, session should be .activated
+        if session.isReachable {
+            // Real-time ephemeral
+            session.sendMessage(message, replyHandler: nil) { error in
+                debug(.watchManager, "❌ Error sending bolus progress: \(error.localizedDescription)")
+            }
+        } else {
+            // Fallback to be double safe: queue userInfo for eventual delivery
+            session.transferUserInfo(message)
         }
     }