Jonas Björkert 2 лет назад
Родитель
Сommit
466b3b8255
1 измененных файлов с 16 добавлено и 10 удалено
  1. 16 10
      LoopFollow/Controllers/Nightscout/Treatments/Overrides.swift

+ 16 - 10
LoopFollow/Controllers/Nightscout/Treatments/Overrides.swift

@@ -15,12 +15,9 @@ extension MainViewController {
         if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Process: Overrides") }
         overrideGraphData.removeAll()
         
-        entries.reversed().forEach { currentEntry in
+        entries.reversed().enumerated().forEach { (index, currentEntry) in
             guard let dateStr = currentEntry["timestamp"] as? String ?? currentEntry["created_at"] as? String else { return }
-            
-            guard let parsedDate = NightscoutUtils.parseDate(dateStr),
-                  let enteredBy = currentEntry["enteredBy"] as? String,
-                  let reason = currentEntry["reason"] as? String else { return }
+            guard let parsedDate = NightscoutUtils.parseDate(dateStr) else { return }
             
             var dateTimeStamp = parsedDate.timeIntervalSince1970
             let graphHours = 24 * UserDefaultsRepository.downloadDays.value
@@ -29,21 +26,30 @@ extension MainViewController {
             }
             
             let multiplier = currentEntry["insulinNeedsScaleFactor"] as? Double ?? 1.0
-            var duration = currentEntry["duration"] as? Double ?? 5.0
-            duration *= 60  // Convert duration to seconds
+            
+            var duration: Double = 5.0
+            if let _ = currentEntry["durationType"] as? String, index == entries.count - 1 {
+                duration = dateTimeUtils.getNowTimeIntervalUTC() - dateTimeStamp + (60 * 60)
+            } else {
+                duration = (currentEntry["duration"] as? Double ?? 5.0) * 60
+            }
             
             if duration < 300 { return }
             
+            guard let enteredBy = currentEntry["enteredBy"] as? String, let reason = currentEntry["reason"] as? String else { return }
+            
             var range: [Int] = []
             if let ranges = currentEntry["correctionRange"] as? [Int], ranges.count == 2 {
                 range = ranges
             } else {
-                if let low = currentEntry["targetBottom"] as? Int, let high = currentEntry["targetTop"] as? Int {
-                    range = [low, high]
-                } else { return }
+                let low = currentEntry["targetBottom"] as? Int
+                let high = currentEntry["targetTop"] as? Int
+                if (low == nil && high != nil) || (low != nil && high == nil) { return }
+                range = [low ?? 0, high ?? 0]
             }
             
             let endDate = dateTimeStamp + duration
+            
             let dot = DataStructs.overrideStruct(insulNeedsScaleFactor: multiplier, date: dateTimeStamp, endDate: endDate, duration: duration, correctionRange: range, enteredBy: enteredBy, reason: reason, sgv: -20)
             overrideGraphData.append(dot)
         }