Преглед изворни кода

Change NightscoutTreatment.from to an init in an extension.

Brian Wieder пре 2 година
родитељ
комит
1ec7081d68

+ 2 - 2
FreeAPS/Sources/APS/Storage/PumpHistoryStorage.swift

@@ -223,9 +223,9 @@ final class BasePumpHistoryStorage: PumpHistoryStorage, Injectable {
                 nextEvent = events[i + 1]
             }
             if event.type == .tempBasal, nextEvent?.type == .tempBasalDuration {
-                treatments.append(NightscoutTreatment.from(event: event, tempBasalDuration: nextEvent))
+                treatments.append(NightscoutTreatment(event: event, tempBasalDuration: nextEvent))
             } else {
-                treatments.append(NightscoutTreatment.from(event: event))
+                treatments.append(NightscoutTreatment(event: event))
             }
         }
 

+ 33 - 33
FreeAPS/Sources/Models/NightscoutTreatment.swift

@@ -34,7 +34,33 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
         (lhs.createdAt ?? Date()) == (rhs.createdAt ?? Date())
     }
 
-    static func from(event: PumpHistoryEvent, tempBasalDuration: PumpHistoryEvent? = nil) -> Self? {
+    func hash(into hasher: inout Hasher) {
+        hasher.combine(createdAt ?? Date())
+    }
+}
+
+extension NightscoutTreatment {
+    private enum CodingKeys: String, CodingKey {
+        case duration
+        case rawDuration = "raw_duration"
+        case rawRate = "raw_rate"
+        case absolute
+        case rate
+        case eventType
+        case createdAt = "created_at"
+        case enteredBy
+        case bolus
+        case insulin
+        case notes
+        case carbs
+        case fat
+        case protein
+        case foodType
+        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
@@ -43,7 +69,7 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
         }
         switch event.type {
         case .tempBasal:
-            return NightscoutTreatment(
+            self.init(
                 duration: basalDurationEvent?.durationMin,
                 rawDuration: basalDurationEvent,
                 rawRate: event,
@@ -63,7 +89,7 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
             )
         case .bolus:
             let eventType = determineBolusEventType(for: event)
-            return NightscoutTreatment(
+            self.init(
                 duration: event.duration,
                 rawDuration: nil,
                 rawRate: nil,
@@ -82,7 +108,7 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
                 targetBottom: nil
             )
         case .journalCarbs:
-            return NightscoutTreatment(
+            self.init(
                 duration: nil,
                 rawDuration: nil,
                 rawRate: nil,
@@ -101,7 +127,7 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
                 targetBottom: nil
             )
         case .prime:
-            return NightscoutTreatment(
+            self.init(
                 duration: event.duration,
                 rawDuration: nil,
                 rawRate: nil,
@@ -120,7 +146,7 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
                 targetBottom: nil
             )
         case .rewind:
-            return NightscoutTreatment(
+            self.init(
                 duration: nil,
                 rawDuration: nil,
                 rawRate: nil,
@@ -139,7 +165,7 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
                 targetBottom: nil
             )
         case .pumpAlarm:
-            return NightscoutTreatment(
+            self.init(
                 duration: 30, // minutes
                 rawDuration: nil,
                 rawRate: nil,
@@ -161,30 +187,4 @@ struct NightscoutTreatment: JSON, Hashable, Equatable {
             return nil
         }
     }
-
-    func hash(into hasher: inout Hasher) {
-        hasher.combine(createdAt ?? Date())
-    }
-}
-
-extension NightscoutTreatment {
-    private enum CodingKeys: String, CodingKey {
-        case duration
-        case rawDuration = "raw_duration"
-        case rawRate = "raw_rate"
-        case absolute
-        case rate
-        case eventType
-        case createdAt = "created_at"
-        case enteredBy
-        case bolus
-        case insulin
-        case notes
-        case carbs
-        case fat
-        case protein
-        case foodType
-        case targetTop
-        case targetBottom
-    }
 }