-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
diff --git a/LoopFollow/Controllers/NightScout.swift b/LoopFollow/Controllers/NightScout.swift | ||
index 29c7520..762afb7 100644 | ||
--- a/LoopFollow/Controllers/NightScout.swift | ||
+++ b/LoopFollow/Controllers/NightScout.swift | ||
@@ -193,8 +193,25 @@ extension MainViewController { | ||
|
||
} | ||
|
||
+ var entriesResponse: [ShareGlucoseData]? | ||
let decoder = JSONDecoder() | ||
- let entriesResponse = try? decoder.decode([ShareGlucoseData].self, from: data) | ||
+ do { | ||
+ entriesResponse = try decoder.decode([ShareGlucoseData].self, from: data) | ||
+ } catch let DecodingError.dataCorrupted(context) { | ||
+ print("Data corrupted: \(context)") | ||
+ } catch let DecodingError.keyNotFound(key, context) { | ||
+ print("Key '\(key)' not found: \(context.debugDescription)") | ||
+ print("codingPath: \(context.codingPath)") | ||
+ } catch let DecodingError.valueNotFound(value, context) { | ||
+ print("Value '\(value)' not found: \(context.debugDescription)") | ||
+ print("codingPath: \(context.codingPath)") | ||
+ } catch let DecodingError.typeMismatch(type, context) { | ||
+ print("Type '\(type)' mismatch: \(context.debugDescription)") | ||
+ print("codingPath: \(context.codingPath)") | ||
+ } catch { | ||
+ print("Error decoding JSON: \(error)") | ||
+ } | ||
+ | ||
if var nsData = entriesResponse { | ||
DispatchQueue.main.async { | ||
// transform NS data to look like Dex data | ||
diff --git a/LoopFollow/Extensions/ShareClientExtension.swift b/LoopFollow/Extensions/ShareClientExtension.swift | ||
index c39f7f7..9bb3fab 100644 | ||
--- a/LoopFollow/Extensions/ShareClientExtension.swift | ||
+++ b/LoopFollow/Extensions/ShareClientExtension.swift | ||
@@ -10,9 +10,37 @@ import Foundation | ||
import ShareClient | ||
|
||
public struct ShareGlucoseData: Codable { | ||
- var sgv: Int | ||
- var date: TimeInterval | ||
- var direction: String? | ||
+ var sgv: Int | ||
+ var date: TimeInterval | ||
+ var direction: String? | ||
+ | ||
+ enum CodingKeys: String, CodingKey { | ||
+ case sgv | ||
+ case date | ||
+ case direction | ||
+ } | ||
+ | ||
+ public init(from decoder: Decoder) throws { | ||
+ let container = try decoder.container(keyedBy: CodingKeys.self) | ||
+ | ||
+ if let sgvAsDouble = try? container.decode(Double.self, forKey: .sgv) { | ||
+ sgv = Int(sgvAsDouble.rounded()) | ||
+ } else if let sgvAsInt = try? container.decode(Int.self, forKey: .sgv) { | ||
+ sgv = sgvAsInt | ||
+ } else { | ||
+ throw DecodingError.dataCorruptedError(forKey: .sgv, in: container, debugDescription: "Expected to decode an Integer or Double.") | ||
+ } | ||
+ | ||
+ // Decode the other properties | ||
+ date = try container.decode(TimeInterval.self, forKey: .date) | ||
+ direction = try container.decodeIfPresent(String.self, forKey: .direction) | ||
+ } | ||
+ | ||
+ public init(sgv: Int, date: TimeInterval, direction: String?) { | ||
+ self.sgv = sgv | ||
+ self.date = date | ||
+ self.direction = direction | ||
+ } | ||
} | ||
|
||
private var TrendTable: [String] = [ |