Browse Source

Refactor delta and minPredBG with let bindings

in bolus calc

Co-Authored-By: Sam King <kingst@gmail.com>
Mike Plante 1 year ago
parent
commit
1057bdf4ec

+ 2 - 2
Model/Helper/Determination+helper.swift

@@ -25,9 +25,9 @@ extension OrefDetermination {
         if let minPredBGPart = reasonParts.first(where: { $0.contains("minPredBG") }) {
         if let minPredBGPart = reasonParts.first(where: { $0.contains("minPredBG") }) {
             // Extract the number after "minPredBG"
             // Extract the number after "minPredBG"
             let components = minPredBGPart.components(separatedBy: "minPredBG ")
             let components = minPredBGPart.components(separatedBy: "minPredBG ")
-            if components.count > 1 {
+            if let valueComponent = components.dropFirst().first {
                 // Get everything after "minPredBG " and convert to Decimal
                 // Get everything after "minPredBG " and convert to Decimal
-                let valueString = components[1].trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.-").inverted)
+                let valueString = valueComponent.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.-").inverted)
                 return Decimal(string: valueString)
                 return Decimal(string: valueString)
             }
             }
         }
         }

+ 1 - 3
Trio/Sources/Modules/Treatments/TreatmentsStateModel.swift

@@ -700,10 +700,8 @@ extension Treatments.StateModel {
 
 
         // Calculate delta using newest and oldest readings within 20-minute window
         // Calculate delta using newest and oldest readings within 20-minute window
         let delta: Decimal
         let delta: Decimal
-        if recentObjects.count >= 2 {
+        if let newestInWindow = recentObjects.first?.glucose, let oldestInWindow = recentObjects.last?.glucose {
             // Newest is at index 0, oldest is at the last index
             // Newest is at index 0, oldest is at the last index
-            let newestInWindow = recentObjects.first?.glucose ?? 0
-            let oldestInWindow = recentObjects.last?.glucose ?? 0
             delta = Decimal(newestInWindow) - Decimal(oldestInWindow)
             delta = Decimal(newestInWindow) - Decimal(oldestInWindow)
         } else {
         } else {
             // Not enough data points in the window
             // Not enough data points in the window