WatchHealthUploadTimeList.swift
1.5 KB
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
import Foundation
/// Upload boundaries returned by GET `/client/doublefeel/health/v2/data_upload/common/`.
/// The API has existed in both direct and `{ "data": ... }` response shapes.
struct WatchHealthUploadTimeList: Decodable {
struct Item: Decodable {
let dataType: Int?
let latestDataTime: TimeInterval?
enum CodingKeys: String, CodingKey {
case dataType = "data_type"
case latestDataTime = "latest_data_time"
}
}
let latestDataTimeList: [Item]
private enum CodingKeys: String, CodingKey {
case latestDataTimeList = "latest_data_time_list"
case data
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let list = try container.decodeIfPresent([Item].self, forKey: .latestDataTimeList) {
latestDataTimeList = list
return
}
if let wrapped = try container.decodeIfPresent(WatchHealthUploadTimeList.self, forKey: .data) {
latestDataTimeList = wrapped.latestDataTimeList
return
}
throw DecodingError.dataCorruptedError(
forKey: .latestDataTimeList,
in: container,
debugDescription: "Missing latest_data_time_list"
)
}
func latestDate(for dataType: Int) -> Date? {
latestDataTimeList
.first { $0.dataType == dataType }?
.latestDataTime
.map(Date.init(timeIntervalSince1970:))
}
}