Просмотр исходного кода

Fix Data Races in LA Bridge, Attempt to fix cob update problem in home view, fix Iob chart offset, ensure that textfield updates are on the main thread (fixes publishing from background .... error)

polscm32 aka Marvout 1 год назад
Родитель
Сommit
ed20c9144d

+ 2 - 2
FreeAPS/Sources/Modules/Home/View/Chart/BasalChart.swift

@@ -25,11 +25,11 @@ extension MainChartView {
                 drawTempBasals(dummy: false)
                 drawBasalProfile()
                 drawSuspensions()
-            }.onChange(of: state.tempBasals) { _ in
+            }.onChange(of: state.tempBasals) {
                 calculateBasals()
                 calculateTempBasalsInBackground()
             }
-            .onChange(of: state.maxBasal) { _ in
+            .onChange(of: state.maxBasal) {
                 calculateBasals()
             }
             .onChange(of: state.autotunedBasalProfile) { _ in

+ 28 - 0
FreeAPS/Sources/Modules/Home/View/Chart/IobChart.swift

@@ -26,17 +26,45 @@ extension MainChartView {
                     }
                 }
             }
+            .offset(y: deviceOffset)
             .frame(minHeight: geo.size.height * 0.12)
             .frame(width: fullWidth(viewWidth: screenSize.width))
             .chartXScale(domain: startMarker ... endMarker)
             .backport.chartXSelection(value: $selection)
             .chartXAxis { basalChartXAxis }
+            .chartXAxis(.hidden)
             .chartYAxis { cobChartYAxis }
             .chartYScale(domain: state.minValueIobChart ... state.maxValueIobChart)
             .chartYAxis(.hidden)
         }
     }
 
+    var deviceOffset: CGFloat {
+        switch UIDevice.current.userInterfaceIdiom {
+        case .phone:
+            // Use the native screen height in pixels to calculate the offset for different devices
+            // The offset is based on the empirically determined offset for the iPhone 16 Pro simulator (2622 pixels in height, offset = 11)
+            // The offset for other devices is calculated proportionally to the iPhone 16 Pro:
+            // Offset for another device = (Height of the other device / Height of iPhone 16 Pro) * 11
+            let screenHeight = UIScreen.main.nativeBounds.height
+            if screenHeight == 2622 {
+                return 11 // iPhone 16 Pro
+            } else if screenHeight == 2868 {
+                return 12.1 // iPhone 16 Pro Max
+            } else if screenHeight == 2556 {
+                return 10.7 // iPhone 15 Pro
+            } else if screenHeight == 2796 {
+                return 11.7 // iPhone 15 Pro Max / 15 Plus / 16 Plus
+            } else if screenHeight == 2340 {
+                return 9.8 // iPhone 12 Mini
+            } else {
+                return 11 // Default for other phones
+            }
+        default:
+            return 11
+        }
+    }
+
     func drawIOB() -> some ChartContent {
         ForEach(state.enactedAndNonEnactedDeterminations) { iob in
             let rawAmount = iob.iob?.doubleValue ?? 0

+ 5 - 5
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -98,17 +98,17 @@ struct MainChartView: View {
                                 iobChart
                             }
 
-                        }.onChange(of: screenHours) { _ in
+                        }.onChange(of: screenHours) {
                             scroller.scrollTo("MainChart", anchor: .trailing)
                         }
-                        .onChange(of: state.glucoseFromPersistence.last?.glucose) { _ in
+                        .onChange(of: state.glucoseFromPersistence.last?.glucose) {
                             scroller.scrollTo("MainChart", anchor: .trailing)
                             updateStartEndMarkers()
                         }
-                        .onChange(of: state.enactedAndNonEnactedDeterminations.first?.deliverAt) { _ in
+                        .onChange(of: state.enactedAndNonEnactedDeterminations.first?.deliverAt) {
                             scroller.scrollTo("MainChart", anchor: .trailing)
                         }
-                        .onChange(of: units) { _ in
+                        .onChange(of: units) {
                             // TODO: - Refactor this to only update the Y Axis Scale
                             state.setupGlucoseArray()
                         }
@@ -219,7 +219,7 @@ extension MainChartView {
                 }
             }
             .id("MainChart")
-            .onChange(of: state.insulinFromPersistence) { _ in
+            .onChange(of: state.insulinFromPersistence) {
                 state.roundedTotalBolus = state.calculateTINS()
             }
             .onChange(of: tempTargets) { _ in

+ 3 - 2
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -431,8 +431,9 @@ extension Home {
                         .foregroundColor(.loopYellow)
                     Text(
                         (
-                            numberFormatter
-                                .string(from: (state.enactedAndNonEnactedDeterminations.first?.cob ?? 0) as NSNumber) ?? "0"
+                            numberFormatter.string(
+                                from: NSNumber(value: state.enactedAndNonEnactedDeterminations.first?.cob ?? 0)
+                            ) ?? "0"
                         ) +
                             NSLocalizedString(" g", comment: "gram of carbs")
                     )

+ 6 - 8
FreeAPS/Sources/Services/LiveActivity/LiveActivityBridge.swift

@@ -85,6 +85,11 @@ import UIKit
             guard let self = self else { return }
             self.overridesDidUpdate()
         }.store(in: &subscriptions)
+
+        coreDataPublisher?.filterByEntityName("OrefDetermination").sink { [weak self] _ in
+            guard let self = self else { return }
+            self.cobOrIobDidUpdate()
+        }.store(in: &subscriptions)
     }
 
     private func registerSubscribers() {
@@ -106,7 +111,7 @@ import UIKit
         }
     }
 
-    @objc private func overridesDidUpdate() {
+    private func overridesDidUpdate() {
         Task {
             await fetchAndMapOverride()
             if let determination = determination {
@@ -120,13 +125,6 @@ import UIKit
             // Fetch and map glucose to GlucoseData struct
             await fetchAndMapGlucose()
 
-            // Fetch and map Determination to DeterminationData struct
-            await fetchAndMapDetermination()
-
-            // Fetch and map Override to OverrideData struct
-            /// shows if there is an active Override
-            await fetchAndMapOverride()
-
             // Push the update to the Live Activity
             glucoseDidUpdate(glucoseFromPersistence ?? [])
         }

+ 6 - 2
FreeAPS/Sources/Views/TextFieldWithToolBar.swift

@@ -225,10 +225,14 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
                 let hasTrailingZeros = (hasDecimalSeparator && proposedText[lastCharIndex] == "0") || isDecimalSeparator
                 if !hasTrailingZeros
                 {
-                    parent.text = number.decimalValue
+                    DispatchQueue.main.async {
+                        self.parent.text = number.decimalValue
+                    }
                 }
             } else {
-                parent.text = 0
+                DispatchQueue.main.async {
+                    self.parent.text = 0
+                }
             }
         }