Bladeren bron

fix permanent progress view when glucose is stale, use 1s instead of 1 minute back for mock bolus entry

polscm32 aka Marvout 1 jaar geleden
bovenliggende
commit
05f1261f85

+ 10 - 5
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -143,7 +143,7 @@ final class OpenAPS {
     }
 
     private func fetchAndProcessCarbs(additionalCarbs: Decimal? = nil) async -> String {
-        var results = await CoreDataStack.shared.fetchEntitiesAsync(
+        let results = await CoreDataStack.shared.fetchEntitiesAsync(
             ofType: CarbEntryStored.self,
             onContext: context,
             predicate: NSPredicate.predicateForOneDayAgo,
@@ -168,7 +168,7 @@ final class OpenAPS {
                 ] as [String: Any]
 
                 // Assuming jsonArray is a String, convert it to a list of dictionaries first
-                if var jsonData = jsonArray.data(using: .utf8) {
+                if let jsonData = jsonArray.data(using: .utf8) {
                     var jsonList = try? JSONSerialization.jsonObject(with: jsonData, options: []) as? [[String: Any]]
                     jsonList?.append(additionalEntry)
 
@@ -244,9 +244,14 @@ final class OpenAPS {
     }
 
     private func createIOBDTO(iob: Decimal) -> PumpEventDTO {
-        let oneMinuteAgo = Calendar.current.date(byAdding: .minute, value: -1, to: Date())! // this ensures that oref actually uses the mock entry to calculate iob
-        let dateFormatted = PumpEventStored.dateFormatter.string(from: oneMinuteAgo)
-        
+        let oneSecondAgo = Calendar.current
+            .date(
+                byAdding: .second,
+                value: -1,
+                to: Date()
+            )! // adding -1s to the current Date ensures that oref actually uses the mock entry to calculate iob and not guard it away
+        let dateFormatted = PumpEventStored.dateFormatter.string(from: oneSecondAgo)
+
         let bolusDTO = BolusDTO(
             id: UUID().uuidString,
             timestamp: dateFormatted,

+ 6 - 0
FreeAPS/Sources/APS/Storage/GlucoseStorage.swift

@@ -7,6 +7,7 @@ import Swinject
 
 protocol GlucoseStorage {
     func storeGlucose(_ glucose: [BloodGlucose])
+    func isGlucoseDataFresh(_ glucoseDate: Date?) -> Bool
     func syncDate() -> Date
     func filterTooFrequentGlucose(_ glucose: [BloodGlucose], at: Date) -> [BloodGlucose]
     func lastGlucoseDate() -> Date
@@ -162,6 +163,11 @@ final class BaseGlucoseStorage: GlucoseStorage, Injectable {
         }
     }
 
+    func isGlucoseDataFresh(_ glucoseDate: Date?) -> Bool {
+        guard let glucoseDate = glucoseDate else { return false }
+        return glucoseDate > Date().addingTimeInterval(-6 * 60)
+    }
+
     func syncDate() -> Date {
         let fr = GlucoseStored.fetchRequest()
         fr.predicate = NSPredicate.predicateForOneDayAgo

+ 4 - 3
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -288,9 +288,10 @@ extension Bolus {
                 await saveMeal()
 
                 // if glucose data is stale end the custom loading animation by hiding the modal
-//                guard glucoseOfLast20Min.first?.date ?? now >= Date().addingTimeInterval(-12.minutes.timeInterval) else {
-//                    return hideModal()
-//                }
+                guard glucoseStorage.isGlucoseDataFresh(glucoseFromPersistence.first?.date) else {
+                    waitForSuggestion = false
+                    return hideModal()
+                }
             }
         }
 

+ 4 - 1
FreeAPS/Sources/Modules/Bolus/View/ForeCastChart.swift

@@ -34,9 +34,12 @@ struct ForeCastChart: View {
                             .foregroundStyle(.secondary)
                     }
                 } else {
-                    Text("")
+                    Text("---")
                         .font(.footnote)
                         .foregroundStyle(.primary)
+                    Text("\(units.rawValue)")
+                        .font(.footnote)
+                        .foregroundStyle(.secondary)
                 }
             }
         }

+ 5 - 0
FreeAPS/Sources/Modules/DataTable/DataTableStateModel.swift

@@ -8,6 +8,7 @@ extension DataTable {
         @Injected() var unlockmanager: UnlockManager!
         @Injected() private var storage: FileStorage!
         @Injected() var pumpHistoryStorage: PumpHistoryStorage!
+        @Injected() var glucoseStorage: GlucoseStorage!
         @Injected() var healthKitManager: HealthKitManager!
 
         let coredataContext = CoreDataStack.shared.newTaskContext()
@@ -31,6 +32,10 @@ extension DataTable {
             broadcaster.register(DeterminationObserver.self, observer: self)
         }
 
+        func isGlucoseDataFresh(_ glucoseDate: Date?) -> Bool {
+            glucoseStorage.isGlucoseDataFresh(glucoseDate)
+        }
+
         // Carb and FPU deletion from history
         /// marked as MainActor to be able to publish changes from the background
         /// - Parameter: NSManagedObjectID to be able to transfer the object safely from one thread to another thread

+ 3 - 1
FreeAPS/Sources/Modules/DataTable/View/DataTableRootView.swift

@@ -130,7 +130,9 @@ extension DataTable {
                         .background(color)
                 }.blur(radius: state.waitForSuggestion ? 8 : 0)
 
-                if state.waitForSuggestion {
+                // Show custom progress view
+                /// don't show it if glucose is stale as it will block the UI
+                if state.waitForSuggestion && state.isGlucoseDataFresh(glucoseStored.first?.date) {
                     CustomProgressView(text: progressText.rawValue)
                 }
             })

+ 6 - 0
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -12,6 +12,7 @@ extension Home {
         @Injected() var fetchGlucoseManager: FetchGlucoseManager!
         @Injected() var nightscoutManager: NightscoutManager!
         @Injected() var determinationStorage: DeterminationStorage!
+        @Injected() var glucoseStorage: GlucoseStorage!
         private let timer = DispatchTimer(timeInterval: 5)
         private(set) var filteredHours = 24
         @Published var manualGlucose: [BloodGlucose] = []
@@ -286,6 +287,11 @@ extension Home {
             provider.heartbeatNow()
         }
 
+        func showProgressView() {
+            glucoseStorage
+                .isGlucoseDataFresh(glucoseFromPersistence.first?.date) ? (waitForSuggestion = true) : (waitForSuggestion = false)
+        }
+
         func cancelBolus() {
             Task {
                 await apsManager.cancelBolus()

+ 1 - 1
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -697,7 +697,7 @@ extension Home {
                         Spacer()
 
                         Button {
-                            state.waitForSuggestion = true
+                            state.showProgressView()
                             state.cancelBolus()
                         } label: {
                             Image(systemName: "xmark.app")