Prechádzať zdrojové kódy

Fix suspension drawing; add filtering to avoid history duplicates

Deniz Cengiz 2 rokov pred
rodič
commit
766b5c9d83

+ 26 - 1
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -67,7 +67,7 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                             // Duplicate found, do not store the event
                             print("Duplicate event found with timestamp: \(event.date)")
 
-                            if let existingEvent = existingEvents.first(where: { $0.type == PumpEvent.bolus.rawValue }) {
+                            if let existingEvent = existingEvents.first(where: { $0.type == EventType.bolus.rawValue }) {
                                 if existingEvent.timestamp == event.date {
                                     if let existingAmount = existingEvent.bolus?.amount, amount < existingAmount as Decimal {
                                         // Update existing event with new smaller value
@@ -119,26 +119,51 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                         newTempBasal.tempType = TempType.absolute.rawValue
 
                     case .suspend:
+                        guard existingEvents.isEmpty else {
+                            // Duplicate found, do not store the event
+                            print("Duplicate event found with timestamp: \(event.date)")
+                            continue
+                        }
                         let newPumpEvent = PumpEventStored(context: self.context)
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.pumpSuspend.rawValue
 
                     case .resume:
+                        guard existingEvents.isEmpty else {
+                            // Duplicate found, do not store the event
+                            print("Duplicate event found with timestamp: \(event.date)")
+                            continue
+                        }
                         let newPumpEvent = PumpEventStored(context: self.context)
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.pumpResume.rawValue
 
                     case .rewind:
+                        guard existingEvents.isEmpty else {
+                            // Duplicate found, do not store the event
+                            print("Duplicate event found with timestamp: \(event.date)")
+                            continue
+                        }
                         let newPumpEvent = PumpEventStored(context: self.context)
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.rewind.rawValue
 
                     case .prime:
+                        guard existingEvents.isEmpty else {
+                            // Duplicate found, do not store the event
+                            print("Duplicate event found with timestamp: \(event.date)")
+                            continue
+                        }
                         let newPumpEvent = PumpEventStored(context: self.context)
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.prime.rawValue
 
                     case .alarm:
+                        guard existingEvents.isEmpty else {
+                            // Duplicate found, do not store the event
+                            print("Duplicate event found with timestamp: \(event.date)")
+                            continue
+                        }
                         let newPumpEvent = PumpEventStored(context: self.context)
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.pumpAlarm.rawValue

+ 1 - 14
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -670,25 +670,12 @@ extension Home.StateModel {
             manualTempBasal = apsManager.isManualTempBasal
             tempBasals = insulinFromPersistence.filter({ $0.tempBasal != nil })
 
-            let lastTempBasal = Array(tempBasals.suffix(2))
-
-            guard lastTempBasal.count == 2 else {
-                return
-            }
-
-            guard
-                let lastTempBasalDose = lastTempBasal.first(where: { $0.tempBasal?.tempType == EventType.tempBasal.rawValue }),
-                let lastDate = lastTempBasalDose.timestamp
-            else {
-                return
-            }
-
             // suspension and resume events
             suspensions = insulinFromPersistence
                 .filter({ $0.type == EventType.pumpSuspend.rawValue || $0.type == EventType.pumpResume.rawValue })
             let lastSuspension = suspensions.last
 
-            pumpSuspended = lastDate > lastSuspension?.timestamp ?? .distantPast && lastSuspension?.type == EventType.pumpSuspend
+            pumpSuspended = tempBasals.last?.timestamp ?? Date() > lastSuspension?.timestamp ?? .distantPast && lastSuspension?.type == EventType.pumpSuspend
                 .rawValue
 
         } catch {

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

@@ -541,7 +541,6 @@ extension MainChartView {
 
     private func drawSuspensions() -> some ChartContent {
         let suspensions = state.suspensions
-
         return ForEach(suspensions) { suspension in
             let now = Date()
 
@@ -589,7 +588,7 @@ extension MainChartView {
             return (timestamp, nextTemp.timestamp ?? Date(), rate) // end defaults to current time
         }
     }
-    
+
     private func drawTempBasals() -> some ChartContent {
         ForEach(prepareTempBasals(), id: \.rate) { basal in
             RectangleMark(

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

@@ -36,7 +36,7 @@ extension Home {
 
         @Environment(\.managedObjectContext) var moc
         @Environment(\.colorScheme) var colorScheme
-        
+
         // TODO: rework this stuff -> remove fetch requests; no one wants this here.
         @FetchRequest(
             entity: Override.entity(),
@@ -61,7 +61,7 @@ extension Home {
         ) var enactedSliderTT: FetchedResults<TempTargetsSlider>
 
         // TODO: end todo
-        
+
         var bolusProgressFormatter: NumberFormatter {
             let formatter = NumberFormatter()
             formatter.numberStyle = .decimal
@@ -199,7 +199,7 @@ extension Home {
 
             return rateString + " " + NSLocalizedString(" U/hr", comment: "Unit per hour with space") + manualBasalString
         }
-        
+
         var overrideString: String? {
             guard let fetched = fetchedPercent.first, fetched.enabled else {
                 return nil