-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathEKReminder+Encodable.swift
More file actions
70 lines (61 loc) · 2.5 KB
/
EKReminder+Encodable.swift
File metadata and controls
70 lines (61 loc) · 2.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import EventKit
extension EKReminder: Encodable {
private enum EncodingKeys: String, CodingKey {
case externalId
case lastModified
case creationDate
case title
case notes
case url
case location
case locationTitle
case completionDate
case isCompleted
case priority
case startDate
case dueDate
case list
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: EncodingKeys.self)
try container.encode(self.calendarItemExternalIdentifier, forKey: .externalId)
try container.encode(self.title, forKey: .title)
try container.encode(self.isCompleted, forKey: .isCompleted)
try container.encode(self.priority, forKey: .priority)
try container.encode(self.calendar.title, forKey: .list)
try container.encodeIfPresent(self.notes, forKey: .notes)
// url field is nil
// https://developer.apple.com/forums/thread/128140
try container.encodeIfPresent(self.url, forKey: .url)
try container.encodeIfPresent(format(self.completionDate), forKey: .completionDate)
for alarm in self.alarms ?? [] {
if let location = alarm.structuredLocation {
try container.encodeIfPresent(location.title, forKey: .locationTitle)
if let geoLocation = location.geoLocation {
let geo = "\(geoLocation.coordinate.latitude), \(geoLocation.coordinate.longitude)"
try container.encode(geo, forKey: .location)
}
break
}
}
if let startDateComponents = self.startDateComponents {
try container.encodeIfPresent(format(startDateComponents.date), forKey: .startDate)
}
if let dueDateComponents = self.dueDateComponents {
try container.encodeIfPresent(format(dueDateComponents.date), forKey: .dueDate)
}
if let lastModifiedDate = self.lastModifiedDate {
try container.encode(format(lastModifiedDate), forKey: .lastModified)
}
if let creationDate = self.creationDate {
try container.encode(format(creationDate), forKey: .creationDate)
}
}
private func format(_ date: Date?) -> String? {
if #available(macOS 12.0, *) {
return date?.ISO8601Format()
} else {
return date?.description(with: .current)
}
}
}