Bladeren bron

Move NightscoutTreatment PumpHistoryEvent init into PumpHistoryStorage

NightscoutTreatment should be agnostic, so moving the init extension into PumpHistoryStorage allows for better sepearation of concerns.
Brian Wieder 2 jaren geleden
bovenliggende
commit
38a50d8154
2 gewijzigde bestanden met toevoegingen van 130 en 128 verwijderingen
  1. 130 0
      FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift
  2. 0 128
      FreeAPS/Sources/Models/NightscoutTreatment.swift

+ 130 - 0
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -268,3 +268,133 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
         storeEvents(events)
     }
 }
+
+extension NightscoutTreatment {
+    init?(event: PumpHistoryEvent, tempBasalDuration: PumpHistoryEvent? = nil) {
+        var basalDurationEvent: PumpHistoryEvent?
+        if tempBasalDuration != nil, tempBasalDuration?.timestamp == event.timestamp, event.type == .tempBasal,
+           tempBasalDuration?.type == .tempBasalDuration
+        {
+            basalDurationEvent = tempBasalDuration
+        }
+        switch event.type {
+        case .tempBasal:
+            self.init(
+                duration: basalDurationEvent?.durationMin,
+                rawDuration: basalDurationEvent,
+                rawRate: event,
+                absolute: event.rate,
+                rate: event.rate,
+                eventType: .nsTempBasal,
+                createdAt: event.timestamp,
+                enteredBy: NightscoutTreatment.local,
+                bolus: nil,
+                insulin: nil,
+                notes: nil,
+                carbs: nil,
+                fat: nil,
+                protein: nil,
+                targetTop: nil,
+                targetBottom: nil
+            )
+        case .bolus:
+            let eventType = determineBolusEventType(for: event)
+            self.init(
+                duration: event.duration,
+                rawDuration: nil,
+                rawRate: nil,
+                absolute: nil,
+                rate: nil,
+                eventType: eventType,
+                createdAt: event.timestamp,
+                enteredBy: NightscoutTreatment.local,
+                bolus: event,
+                insulin: event.amount,
+                notes: nil,
+                carbs: nil,
+                fat: nil,
+                protein: nil,
+                targetTop: nil,
+                targetBottom: nil
+            )
+        case .journalCarbs:
+            self.init(
+                duration: nil,
+                rawDuration: nil,
+                rawRate: nil,
+                absolute: nil,
+                rate: nil,
+                eventType: .nsCarbCorrection,
+                createdAt: event.timestamp,
+                enteredBy: NightscoutTreatment.local,
+                bolus: nil,
+                insulin: nil,
+                notes: nil,
+                carbs: Decimal(event.carbInput ?? 0),
+                fat: nil,
+                protein: nil,
+                targetTop: nil,
+                targetBottom: nil
+            )
+        case .prime:
+            self.init(
+                duration: event.duration,
+                rawDuration: nil,
+                rawRate: nil,
+                absolute: nil,
+                rate: nil,
+                eventType: .nsSiteChange,
+                createdAt: event.timestamp,
+                enteredBy: NightscoutTreatment.local,
+                bolus: event,
+                insulin: nil,
+                notes: nil,
+                carbs: nil,
+                fat: nil,
+                protein: nil,
+                targetTop: nil,
+                targetBottom: nil
+            )
+        case .rewind:
+            self.init(
+                duration: nil,
+                rawDuration: nil,
+                rawRate: nil,
+                absolute: nil,
+                rate: nil,
+                eventType: .nsInsulinChange,
+                createdAt: event.timestamp,
+                enteredBy: NightscoutTreatment.local,
+                bolus: nil,
+                insulin: nil,
+                notes: nil,
+                carbs: nil,
+                fat: nil,
+                protein: nil,
+                targetTop: nil,
+                targetBottom: nil
+            )
+        case .pumpAlarm:
+            self.init(
+                duration: 30, // minutes
+                rawDuration: nil,
+                rawRate: nil,
+                absolute: nil,
+                rate: nil,
+                eventType: .nsAnnouncement,
+                createdAt: event.timestamp,
+                enteredBy: NightscoutTreatment.local,
+                bolus: nil,
+                insulin: nil,
+                notes: "Alarm \(String(describing: event.note)) \(event.type)",
+                carbs: nil,
+                fat: nil,
+                protein: nil,
+                targetTop: nil,
+                targetBottom: nil
+            )
+        default:
+            return nil
+        }
+    }
+}

+ 0 - 128
FreeAPS/Sources/Models/NightscoutTreatment.swift

@@ -59,132 +59,4 @@ extension NightscoutTreatment {
         case targetTop
         case targetBottom
     }
-
-    init?(event: PumpHistoryEvent, tempBasalDuration: PumpHistoryEvent? = nil) {
-        var basalDurationEvent: PumpHistoryEvent?
-        if tempBasalDuration != nil, tempBasalDuration?.timestamp == event.timestamp, event.type == .tempBasal,
-           tempBasalDuration?.type == .tempBasalDuration
-        {
-            basalDurationEvent = tempBasalDuration
-        }
-        switch event.type {
-        case .tempBasal:
-            self.init(
-                duration: basalDurationEvent?.durationMin,
-                rawDuration: basalDurationEvent,
-                rawRate: event,
-                absolute: event.rate,
-                rate: event.rate,
-                eventType: .nsTempBasal,
-                createdAt: event.timestamp,
-                enteredBy: NightscoutTreatment.local,
-                bolus: nil,
-                insulin: nil,
-                notes: nil,
-                carbs: nil,
-                fat: nil,
-                protein: nil,
-                targetTop: nil,
-                targetBottom: nil
-            )
-        case .bolus:
-            let eventType = determineBolusEventType(for: event)
-            self.init(
-                duration: event.duration,
-                rawDuration: nil,
-                rawRate: nil,
-                absolute: nil,
-                rate: nil,
-                eventType: eventType,
-                createdAt: event.timestamp,
-                enteredBy: NightscoutTreatment.local,
-                bolus: event,
-                insulin: event.amount,
-                notes: nil,
-                carbs: nil,
-                fat: nil,
-                protein: nil,
-                targetTop: nil,
-                targetBottom: nil
-            )
-        case .journalCarbs:
-            self.init(
-                duration: nil,
-                rawDuration: nil,
-                rawRate: nil,
-                absolute: nil,
-                rate: nil,
-                eventType: .nsCarbCorrection,
-                createdAt: event.timestamp,
-                enteredBy: NightscoutTreatment.local,
-                bolus: nil,
-                insulin: nil,
-                notes: nil,
-                carbs: Decimal(event.carbInput ?? 0),
-                fat: nil,
-                protein: nil,
-                targetTop: nil,
-                targetBottom: nil
-            )
-        case .prime:
-            self.init(
-                duration: event.duration,
-                rawDuration: nil,
-                rawRate: nil,
-                absolute: nil,
-                rate: nil,
-                eventType: .nsSiteChange,
-                createdAt: event.timestamp,
-                enteredBy: NightscoutTreatment.local,
-                bolus: event,
-                insulin: nil,
-                notes: nil,
-                carbs: nil,
-                fat: nil,
-                protein: nil,
-                targetTop: nil,
-                targetBottom: nil
-            )
-        case .rewind:
-            self.init(
-                duration: nil,
-                rawDuration: nil,
-                rawRate: nil,
-                absolute: nil,
-                rate: nil,
-                eventType: .nsInsulinChange,
-                createdAt: event.timestamp,
-                enteredBy: NightscoutTreatment.local,
-                bolus: nil,
-                insulin: nil,
-                notes: nil,
-                carbs: nil,
-                fat: nil,
-                protein: nil,
-                targetTop: nil,
-                targetBottom: nil
-            )
-        case .pumpAlarm:
-            self.init(
-                duration: 30, // minutes
-                rawDuration: nil,
-                rawRate: nil,
-                absolute: nil,
-                rate: nil,
-                eventType: .nsAnnouncement,
-                createdAt: event.timestamp,
-                enteredBy: NightscoutTreatment.local,
-                bolus: nil,
-                insulin: nil,
-                notes: "Alarm \(String(describing: event.note)) \(event.type)",
-                carbs: nil,
-                fat: nil,
-                protein: nil,
-                targetTop: nil,
-                targetBottom: nil
-            )
-        default:
-            return nil
-        }
-    }
 }