Browse Source

Fix bolus cancellation not updating bolus entry WIP

dnzxy 2 years ago
parent
commit
23f7ff8775
1 changed files with 16 additions and 3 deletions
  1. 16 3
      FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

+ 16 - 3
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -51,15 +51,28 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                     switch event.type {
                     case .bolus:
 
+                        guard let dose = event.dose else { continue }
+                        let amount = Decimal(dose.unitsInDeliverableIncrements)
+
                         guard existingEvents.isEmpty else {
                             // Duplicate found, do not store the event
                             print("Duplicate event found with timestamp: \(event.date)")
+
+                            // TODO: fix amount rounding to allowed bolus increments
+                            if let existingEvent = existingEvents.first(where: { $0.type == PumpEvent.bolus.rawValue }) {
+                                if existingEvent.timestamp == event.date {
+                                    if let existingAmount = existingEvent.bolus?.amount, amount < existingAmount as Decimal {
+                                        // Update existing event with new smaller value
+                                        existingEvent.bolus?.amount = amount as NSDecimalNumber
+                                        existingEvent.bolus?.isSMB = dose.automatic ?? true
+
+                                        print("Updated existing event with smaller value: \(amount)")
+                                    }
+                                }
+                            }
                             continue
                         }
 
-                        guard let dose = event.dose else { continue }
-                        let amount = Decimal(string: dose.unitsInDeliverableIncrements.description)
-
                         let newPumpEvent = PumpEventStored(context: self.context)
                         newPumpEvent.timestamp = event.date
                         newPumpEvent.type = PumpEvent.bolus.rawValue