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

Rewrite of basal renedering

Jonas Björkert пре 1 година
родитељ
комит
38121e965d

+ 3 - 28
LoopFollow/Controllers/Nightscout/Treatments/Basals.swift

@@ -10,31 +10,6 @@ import Foundation
 
 extension MainViewController {
 
-    private func parseDate(_ rawString: String) -> Date? {
-        var mutableDate = rawString
-
-        if mutableDate.hasSuffix("Z") {
-            mutableDate = String(mutableDate.dropLast())
-        }
-        else if let offsetRange = mutableDate.range(of: "[\\+\\-]\\d{2}:\\d{2}$",
-                                                    options: .regularExpression) {
-            mutableDate.removeSubrange(offsetRange)
-        }
-
-        mutableDate = mutableDate.replacingOccurrences(
-            of: "\\.\\d+",
-            with: "",
-            options: .regularExpression
-        )
-
-        let dateFormatter = DateFormatter()
-        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
-        dateFormatter.locale = Locale(identifier: "en_US")
-        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
-
-        return dateFormatter.date(from: mutableDate)
-    }
-
     // NS Temp Basal Response Processor
     func processNSBasals(entries: [[String:AnyObject]]) {
         infoManager.clearInfoData(type: .basal)
@@ -53,7 +28,7 @@ extension MainViewController {
             let dateString = currentEntry["timestamp"] as? String
             ?? currentEntry["created_at"] as? String
             guard let rawDateStr = dateString,
-                  let dateParsed = parseDate(rawDateStr) else {
+                  let dateParsed = NightscoutUtils.parseDate(rawDateStr) else {
                 continue
             }
 
@@ -70,7 +45,7 @@ extension MainViewController {
                 let priorDateStr = priorEntry?["timestamp"] as? String
                 ?? priorEntry?["created_at"] as? String
                 if let rawPrior = priorDateStr,
-                   let priorDateParsed = parseDate(rawPrior) {
+                   let priorDateParsed = NightscoutUtils.parseDate(rawPrior) {
 
                     let priorDateTimeStamp = priorDateParsed.timeIntervalSince1970
                     let priorDuration = priorEntry?["duration"] as? Double ?? 0.0
@@ -131,7 +106,7 @@ extension MainViewController {
                 let nextDateStr = nextEntry?["timestamp"] as? String
                 ?? nextEntry?["created_at"] as? String
                 if let rawNext = nextDateStr,
-                   let nextDateParsed = parseDate(rawNext) {
+                   let nextDateParsed = NightscoutUtils.parseDate(rawNext) {
 
                     let nextDateTimeStamp = nextDateParsed.timeIntervalSince1970
                     if nextDateTimeStamp < (dateTimeStamp + (duration * 60)) {

+ 0 - 1
LoopFollow/Controllers/Nightscout/Treatments/Carbs.swift

@@ -74,7 +74,6 @@ extension MainViewController {
             }
             
             guard let date = NightscoutUtils.parseDate(carbDate) else {
-                print("Unable to parse date from: \(carbDate)")
                 continue
             }
             

+ 25 - 16
LoopFollow/Helpers/NightscoutUtils.swift

@@ -234,24 +234,33 @@ class NightscoutUtils {
         task.resume()
     }
 
-    static func parseDate(_ dateString: String) -> Date? {
-        let dateFormatterWithMilliseconds = DateFormatter()
-        dateFormatterWithMilliseconds.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
-        dateFormatterWithMilliseconds.timeZone = TimeZone(abbreviation: "UTC")
-        dateFormatterWithMilliseconds.locale = Locale(identifier: "en_US_POSIX")
-
-        let dateFormatterWithoutMilliseconds = DateFormatter()
-        dateFormatterWithoutMilliseconds.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
-        dateFormatterWithoutMilliseconds.timeZone = TimeZone(abbreviation: "UTC")
-        dateFormatterWithoutMilliseconds.locale = Locale(identifier: "en_US_POSIX")
-
-        if let date = dateFormatterWithMilliseconds.date(from: dateString) {
-            return date
-        } else if let date = dateFormatterWithoutMilliseconds.date(from: dateString) {
-            return date
+    static func parseDate(_ rawString: String) -> Date? {
+        var mutableDate = rawString
+
+        if mutableDate.hasSuffix("Z") {
+            mutableDate = String(mutableDate.dropLast())
+        }
+        else if let offsetRange = mutableDate.range(of: "[\\+\\-]\\d{2}:\\d{2}$",
+                                                    options: .regularExpression) {
+            mutableDate.removeSubrange(offsetRange)
         }
 
-        return nil
+        mutableDate = mutableDate.replacingOccurrences(
+            of: "\\.\\d+",
+            with: "",
+            options: .regularExpression
+        )
+
+        let dateFormatter = DateFormatter()
+        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
+        dateFormatter.locale = Locale(identifier: "en_US")
+        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
+
+        let result = dateFormatter.date(from: mutableDate)
+        if result == nil {
+            print("Unable to parse string: '\(mutableDate)'")
+        }
+        return result
     }
 
     static func retrieveJWTToken() async throws -> String {