Просмотр исходного кода

Add: AddBolus and Carb dot overlap shift

Jon Fawcett 5 лет назад
Родитель
Сommit
e0d5afd119

+ 17 - 2
LoopFollow/Controllers/Graphs.swift

@@ -571,8 +571,17 @@ extension MainViewController {
             formatter.minimumFractionDigits = 0
             formatter.maximumFractionDigits = 2
             formatter.minimumIntegerDigits = 0
+            
+            // Check overlapping carbs to shift left if needed
+            let bolusShift = findNextBolusTime(timeWithin: 240, needle: bolusData[i].date, haystack: bolusData, startingIndex: i)
+            var dateTimeStamp = bolusData[i].date
+            if bolusShift {
+                // Move it half the distance between BG readings
+                dateTimeStamp = dateTimeStamp - 150
+            }
+            
   
-            let dot = ChartDataEntry(x: Double(bolusData[i].date), y: Double(bolusData[i].sgv), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
+            let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(bolusData[i].sgv), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
             BGChart.data?.dataSets[dataIndex].addEntry(dot)
 
         }
@@ -600,9 +609,15 @@ extension MainViewController {
                 valueString += " " + String(hours) + "h"
             }
             
+            // Check overlapping carbs to shift left if needed
+            let carbShift = findNextCarbTime(timeWithin: 250, needle: carbData[i].date, haystack: carbData, startingIndex: i)
+            var dateTimeStamp = carbData[i].date
+            if carbShift {
+                dateTimeStamp = dateTimeStamp - 250
+            }
             
             
-            let dot = ChartDataEntry(x: Double(carbData[i].date), y: Double(carbData[i].sgv), data: valueString)
+            let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(carbData[i].sgv), data: valueString)
             BGChart.data?.dataSets[dataIndex].addEntry(dot)
             
             

+ 2 - 2
LoopFollow/Controllers/NightScout.swift

@@ -1278,7 +1278,7 @@ extension MainViewController {
             dateFormatter.locale = Locale(identifier: "en_US")
             dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
             let dateString = dateFormatter.date(from: strippedZone)
-            let dateTimeStamp = dateString!.timeIntervalSince1970
+            var dateTimeStamp = dateString!.timeIntervalSince1970
             
             guard let carbs = currentEntry?["carbs"] as? Double else {
                 if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "ERROR: Null Carb entry")}
@@ -1289,7 +1289,7 @@ extension MainViewController {
             
             var offset = -50
             if sgv.sgv < Double(250) {
-                let bolusTime = findNearestBolusbyTime(needle: dateTimeStamp, haystack: bolusData, startingIndex: lastFoundBolus)
+                let bolusTime = findNearestBolusbyTime(timeWithin: 300, needle: dateTimeStamp, haystack: bolusData, startingIndex: lastFoundBolus)
                 lastFoundBolus = bolusTime.foundIndex
                 
                 if bolusTime.offset {

+ 3 - 3
LoopFollow/helpers/Globals.swift

@@ -15,8 +15,8 @@ struct globalVariables {
     
     // Graph Settings
     static let dotBG: Float = 3
-    static let dotCarb: Float = 6
-    static let dotBolus: Float = 6
-    static let dotOther: Float = 6
+    static let dotCarb: Float = 5
+    static let dotBolus: Float = 5
+    static let dotOther: Float = 5
     
 }

+ 33 - 3
LoopFollow/helpers/carbBolusArrays.swift

@@ -27,20 +27,50 @@ extension MainViewController {
     }
     
     
-    func findNearestBolusbyTime(needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> (offset: Bool, foundIndex: Int) {
+    func findNearestBolusbyTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> (offset: Bool, foundIndex: Int) {
         
         // If we can't find a match or things fail, put it at 100 BG
         for i in startingIndex..<haystack.count {
             // i has reached the end without a result. return 0
             let timeDiff = needle - haystack[i].date
-            if timeDiff <= 300 && timeDiff >= -300 { return (true, i)}
+            if timeDiff <= Double(timeWithin) && timeDiff >= Double(-timeWithin) { return (true, i)}
             
             if i == haystack.count - 1 { return (false, 0) }
-            if timeDiff < -300 { return (false, 0)}
+            if timeDiff < Double(-timeWithin) { return (false, 0)}
             
         }
         
         return (false, 0 )
     }
     
+    func findNextCarbTime(timeWithin: Int, needle: TimeInterval, haystack: [carbGraphStruct], startingIndex: Int) -> Bool {
+        
+        if startingIndex > haystack.count - 2 { return false }
+        if haystack[startingIndex + 1].date -  needle < Double(timeWithin) {
+            return true
+        }
+
+        return false
+    }
+    
+    func findNextBolusTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> Bool {
+        
+        var last = false
+        var next = true
+        if startingIndex > haystack.count - 2 { return false }
+        if startingIndex == 0 { return false }
+        
+        // Nothing to right that requires shift
+        if haystack[startingIndex + 1].date -  needle > Double(timeWithin) {
+            return false
+        } else {
+            // Nothing to left preventing shift
+            if needle - haystack[startingIndex - 1].date > Double(timeWithin) {
+                return true
+            }
+        }
+        
+        return false
+    }
+    
 }