WatchSharedModels.swift
9.56 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import Foundation
import SwiftUI
import HealthKit
enum HRVStatus: String, Codable {
case fullOfEnergy = "活力满满"
case normal = "状态正常"
case overpressure = "压力过载"
}
struct HRVData: Codable {
var value: Double?
var hrvBaseline: Double?
var date: Date?
var status: HRVStatus? {
guard let value else { return nil }
guard let hrvBaseline, hrvBaseline > 0 else { return .normal }
let ratio = value / hrvBaseline
if ratio >= 1.05 {
return .fullOfEnergy
}
if ratio < 0.9 {
return .overpressure
}
return .normal
}
}
enum UserCharacter: Int, Codable {
case `default` = 0
case cat = 1
case dog = 2
case rabbit = 3
case elephant = 4
}
struct AvatarResource: Codable {
var rawValue: String?
var url: URL? {
guard let rawValue else { return nil }
return URL(string: rawValue)
}
init(rawValue: String?) {
self.rawValue = rawValue
}
init(from decoder: Decoder) throws {
if let value = try? decoder.singleValueContainer().decode(String.self) {
rawValue = value
return
}
let container = try decoder.container(keyedBy: DynamicCodingKey.self)
rawValue = Self.decodeString(from: container, keys: ["url", "avatar", "path"])
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(rawValue)
}
private static func decodeString(from container: KeyedDecodingContainer<DynamicCodingKey>, keys: [String]) -> String? {
for key in keys {
if let codingKey = DynamicCodingKey(stringValue: key),
let value = try? container.decodeIfPresent(String.self, forKey: codingKey) {
return value
}
}
return nil
}
}
struct UserInfo: Codable {
var id: Int?
var pairId: Int?
var nickname: String?
var avatar: AvatarResource?
var persona: UserCharacter?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DynamicCodingKey.self)
id = Self.decodeInt(from: container, keys: ["id", "user_id", "userId"])
pairId = Self.decodeInt(from: container, keys: ["pair_id", "pairId"])
nickname = Self.decodeString(from: container, keys: ["nickname", "nick_name", "name"])
avatar = try Self.decodeAvatar(from: container)
persona = UserCharacter(rawValue: Self.decodeInt(from: container, keys: ["persona", "character", "user_character", "userCharacter"]) ?? 0)
}
func toWatchMap() -> [String: Any] {
var map: [String: Any] = [:]
if let id { map["id"] = id }
if let pairId { map["pair_id"] = pairId }
if let nickname { map["nickname"] = nickname }
if let avatar = avatar?.rawValue { map["avatar"] = avatar }
if let persona { map["persona"] = persona.rawValue }
return map
}
private static func decodeAvatar(from container: KeyedDecodingContainer<DynamicCodingKey>) throws -> AvatarResource? {
for key in ["avatar", "avatar_url", "avatarUrl"] {
guard let codingKey = DynamicCodingKey(stringValue: key) else { continue }
if let value = try? container.decodeIfPresent(String.self, forKey: codingKey) {
return AvatarResource(rawValue: value)
}
if let value = try? container.decodeIfPresent(AvatarResource.self, forKey: codingKey) {
return value
}
}
return nil
}
}
struct WatchThemeModel: Codable {
var isDefaultTheme: Bool?
var positiveDescription: String?
var normalDescription: String?
var negativeDescription: String?
var positiveImageURL: String?
var normalImageURL: String?
var negativeImageURL: String?
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: DynamicCodingKey.self)
isDefaultTheme = Self.decodeBool(from: container, keys: ["isDefaultTheme", "is_default_theme", "isDefault", "is_default"])
positiveDescription = Self.decodeString(from: container, keys: ["positiveDescription", "positive_description", "excellentDescription", "excellent_description"])
normalDescription = Self.decodeString(from: container, keys: ["normalDescription", "normal_description"])
negativeDescription = Self.decodeString(from: container, keys: ["negativeDescription", "negative_description", "overpressureDescription", "overpressure_description"])
positiveImageURL = Self.decodeString(from: container, keys: ["positiveImageURL", "positiveImageUrl", "positive_image_url", "positiveThumbnailURL", "positive_thumbnail_url", "positiveThumnailImageURL", "positive_thumnail_image_url"])
normalImageURL = Self.decodeString(from: container, keys: ["normalImageURL", "normalImageUrl", "normal_image_url", "normalThumbnailURL", "normal_thumbnail_url", "normalThumnailImageURL", "normal_thumnail_image_url"])
negativeImageURL = Self.decodeString(from: container, keys: ["negativeImageURL", "negativeImageUrl", "negative_image_url", "negativeThumbnailURL", "negative_thumbnail_url", "negativeThumnailImageURL", "negative_thumnail_image_url"])
}
func hrvStatusDescription(hrvStatus: HRVStatus) -> String {
switch hrvStatus {
case .fullOfEnergy:
return positiveDescription ?? hrvStatus.rawValue
case .normal:
return normalDescription ?? hrvStatus.rawValue
case .overpressure:
return negativeDescription ?? hrvStatus.rawValue
}
}
func hrvStatusThumnailImageURL(hrvStatus: HRVStatus) -> String? {
switch hrvStatus {
case .fullOfEnergy:
return positiveImageURL
case .normal:
return normalImageURL
case .overpressure:
return negativeImageURL
}
}
}
extension String {
var url: URL? {
URL(string: self)
}
var int: Int? {
Int(self)
}
}
extension Int {
var string: String {
String(self)
}
var double: Double {
Double(self)
}
}
extension Double {
var int: Int {
Int(self)
}
}
extension Array where Element == Double {
func average() -> Double {
guard !isEmpty else { return 0 }
return reduce(0, +) / Double(count)
}
}
extension Text {
func wenyiheiFont(size: CGFloat) -> Text {
font(.custom("WenYue-XinQingNianTi-NC-W8", size: size))
}
}
struct SleepStage {
let stage: HKCategoryValueSleepAnalysis
let startTime: Date
let endTime: Date
let duration: TimeInterval
}
struct SleepData {
let startTime: Date
let endTime: Date
let duration: TimeInterval
let sleepStages: [SleepStage]
let totalSleepTime: TimeInterval
let sleepEfficiency: Double?
}
enum HealthDataType: Int {
case hrv = 1
case heartRate = 2
case spo2 = 3
case move = 4
case exercise = 5
case stand = 6
case steps = 7
case walkingHeartRate = 8
case restingHeartRate = 9
case sleepingHeartRate = 10
case sleepingWristTemperature = 11
case respiratoryRate = 12
case irregularHeartRhythm = 13
}
private struct DynamicCodingKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) {
self.stringValue = stringValue
}
init?(intValue: Int) {
self.stringValue = "\(intValue)"
self.intValue = intValue
}
}
private extension UserInfo {
static func decodeString(from container: KeyedDecodingContainer<DynamicCodingKey>, keys: [String]) -> String? {
for key in keys {
if let codingKey = DynamicCodingKey(stringValue: key),
let value = try? container.decodeIfPresent(String.self, forKey: codingKey) {
return value
}
}
return nil
}
static func decodeInt(from container: KeyedDecodingContainer<DynamicCodingKey>, keys: [String]) -> Int? {
for key in keys {
guard let codingKey = DynamicCodingKey(stringValue: key) else { continue }
if let value = try? container.decodeIfPresent(Int.self, forKey: codingKey) {
return value
}
if let value = try? container.decodeIfPresent(String.self, forKey: codingKey), let intValue = Int(value) {
return intValue
}
}
return nil
}
}
extension UserInfo {
var isPaired: Bool {
pairId != nil
}
}
private extension WatchThemeModel {
static func decodeString(from container: KeyedDecodingContainer<DynamicCodingKey>, keys: [String]) -> String? {
for key in keys {
if let codingKey = DynamicCodingKey(stringValue: key),
let value = try? container.decodeIfPresent(String.self, forKey: codingKey) {
return value
}
}
return nil
}
static func decodeBool(from container: KeyedDecodingContainer<DynamicCodingKey>, keys: [String]) -> Bool? {
for key in keys {
guard let codingKey = DynamicCodingKey(stringValue: key) else { continue }
if let value = try? container.decodeIfPresent(Bool.self, forKey: codingKey) {
return value
}
if let value = try? container.decodeIfPresent(Int.self, forKey: codingKey) {
return value != 0
}
if let value = try? container.decodeIfPresent(String.self, forKey: codingKey) {
return ["1", "true", "yes"].contains(value.lowercased())
}
}
return nil
}
}