@@ -103,8 +103,28 @@ public struct PCKHealthKitTask: PCKVersionable {
103
103
104
104
public var originalData : Data ?
105
105
106
+ #if canImport(HealthKit)
106
107
/// A structure specifying how this task is linked with HealthKit.
107
- public var healthKitLinkage : OCKHealthKitLinkage ?
108
+ public var healthKitLinkage : OCKHealthKitLinkage ? {
109
+ get {
110
+ guard let data = healthKitLinkageString? . data ( using: . utf8) else {
111
+ return nil
112
+ }
113
+ return try ? JSONDecoder ( ) . decode ( OCKHealthKitLinkage . self,
114
+ from: data)
115
+ } set {
116
+ guard let json = try ? JSONEncoder ( ) . encode ( newValue) ,
117
+ let encodedString = String ( data: json, encoding: . utf8) else {
118
+ healthKitLinkageString = nil
119
+ return
120
+ }
121
+ healthKitLinkageString = encodedString
122
+ }
123
+ }
124
+ #endif
125
+
126
+ /// A string specifying how this task is linked with HealthKit.
127
+ public var healthKitLinkageString : String ?
108
128
109
129
/// If true, completion of this task will be factored into the patient's overall adherence. True by default.
110
130
public var impactsAdherence : Bool ?
@@ -135,12 +155,18 @@ public struct PCKHealthKitTask: PCKVersionable {
135
155
}
136
156
137
157
enum CodingKeys : String , CodingKey {
138
- case objectId, createdAt, updatedAt
158
+ case objectId, createdAt, updatedAt,
159
+ className, ACL, uuid
139
160
case entityId, schemaVersion, createdDate, updatedDate,
140
161
deletedDate, timezone, userInfo, groupIdentifier,
141
162
tags, source, asset, remoteID, notes, logicalClock
142
163
case previousVersionUUIDs, nextVersionUUIDs, effectiveDate
143
- case title, carePlan, carePlanUUID, impactsAdherence, instructions, schedule, healthKitLinkage
164
+ case title, carePlan, carePlanUUID, impactsAdherence,
165
+ instructions, schedule, healthKitLinkageString
166
+ case previousVersions, nextVersions
167
+ #if canImport(HealthKit)
168
+ case healthKitLinkage
169
+ #endif
144
170
}
145
171
146
172
public init ( ) {
@@ -215,18 +241,61 @@ public struct PCKHealthKitTask: PCKVersionable {
215
241
}
216
242
}
217
243
218
- extension PCKHealthKitTask {
219
- public func encode( to encoder: Encoder ) throws {
244
+ public extension PCKHealthKitTask {
245
+ init ( from decoder: Decoder ) throws {
246
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
247
+ self . objectId = try container. decodeIfPresent ( String . self, forKey: . objectId)
248
+ self . createdAt = try container. decodeIfPresent ( Date . self, forKey: . createdAt)
249
+ self . updatedAt = try container. decodeIfPresent ( Date . self, forKey: . updatedAt)
250
+ self . ACL = try container. decodeIfPresent ( ParseACL . self, forKey: . ACL)
251
+ self . healthKitLinkageString = try container. decodeIfPresent ( String . self, forKey: . healthKitLinkageString)
252
+ #if canImport(HealthKit)
253
+ if healthKitLinkageString == nil {
254
+ self . healthKitLinkage = try container. decodeIfPresent ( OCKHealthKitLinkage . self, forKey: . healthKitLinkage)
255
+ }
256
+ #endif
257
+ self . carePlan = try container. decodeIfPresent ( PCKCarePlan . self, forKey: . carePlan)
258
+ self . carePlanUUID = try container. decodeIfPresent ( UUID . self, forKey: . carePlanUUID)
259
+ self . title = try container. decodeIfPresent ( String . self, forKey: . title)
260
+ self . logicalClock = try container. decodeIfPresent ( Int . self, forKey: . logicalClock)
261
+ self . impactsAdherence = try container. decodeIfPresent ( Bool . self, forKey: . impactsAdherence)
262
+ self . instructions = try container. decodeIfPresent ( String . self, forKey: . instructions)
263
+ self . schedule = try container. decodeIfPresent ( OCKSchedule . self, forKey: . schedule)
264
+ self . entityId = try container. decodeIfPresent ( String . self, forKey: . entityId)
265
+ self . createdDate = try container. decodeIfPresent ( Date . self, forKey: . createdDate)
266
+ self . updatedDate = try container. decodeIfPresent ( Date . self, forKey: . updatedDate)
267
+ self . deletedDate = try container. decodeIfPresent ( Date . self, forKey: . deletedDate)
268
+ self . effectiveDate = try container. decodeIfPresent ( Date . self, forKey: . effectiveDate)
269
+ self . timezone = try container. decodeIfPresent ( TimeZone . self, forKey: . timezone)
270
+ self . previousVersions = try container. decodeIfPresent ( [ Pointer < Self > ] . self, forKey: . previousVersions)
271
+ self . nextVersions = try container. decodeIfPresent ( [ Pointer < Self > ] . self, forKey: . nextVersions)
272
+ self . previousVersionUUIDs = try container. decodeIfPresent ( [ UUID ] . self, forKey: . previousVersionUUIDs)
273
+ self . nextVersionUUIDs = try container. decodeIfPresent ( [ UUID ] . self, forKey: . nextVersionUUIDs)
274
+ self . userInfo = try container. decodeIfPresent ( [ String : String ] . self, forKey: . userInfo)
275
+ self . remoteID = try container. decodeIfPresent ( String . self, forKey: . remoteID)
276
+ self . source = try container. decodeIfPresent ( String . self, forKey: . source)
277
+ self . asset = try container. decodeIfPresent ( String . self, forKey: . asset)
278
+ self . schemaVersion = try container. decodeIfPresent ( OCKSemanticVersion . self, forKey: . schemaVersion)
279
+ self . groupIdentifier = try container. decodeIfPresent ( String . self, forKey: . groupIdentifier)
280
+ self . tags = try container. decodeIfPresent ( [ String ] . self, forKey: . tags)
281
+ self . notes = try container. decodeIfPresent ( [ OCKNote ] . self, forKey: . notes)
282
+ }
283
+
284
+ func encode( to encoder: Encoder ) throws {
220
285
var container = encoder. container ( keyedBy: CodingKeys . self)
221
286
if encodingForParse {
222
287
try container. encodeIfPresent ( carePlan? . toPointer ( ) , forKey: . carePlan)
288
+ try container. encodeIfPresent ( healthKitLinkageString, forKey: . healthKitLinkageString)
289
+ } else {
290
+ #if canImport(HealthKit)
291
+ try container. encodeIfPresent ( healthKitLinkage, forKey: . healthKitLinkage)
292
+ #endif
223
293
}
224
294
try container. encodeIfPresent ( title, forKey: . title)
225
295
try container. encodeIfPresent ( carePlanUUID, forKey: . carePlanUUID)
226
296
try container. encodeIfPresent ( impactsAdherence, forKey: . impactsAdherence)
227
297
try container. encodeIfPresent ( instructions, forKey: . instructions)
228
298
try container. encodeIfPresent ( schedule, forKey: . schedule)
229
- try container. encodeIfPresent ( healthKitLinkage, forKey: . healthKitLinkage)
230
299
try encodeVersionable ( to: encoder)
231
300
}
232
301
}
0 commit comments