-
Notifications
You must be signed in to change notification settings - Fork 1
/
lf_svg_decimals.patch
76 lines (72 loc) · 3.09 KB
/
lf_svg_decimals.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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] = [