@ebarooni/capacitor-calendar 8.11.0 → 8.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +279 -103
- package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendarPlugin.kt +37 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CalendarChangeObserver.kt +181 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CapacitorCalendar.kt +58 -8
- package/android/src/main/java/dev/barooni/capacitor/calendar/models/data/EventDeleteInfo.kt +6 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/StartListeningInput.kt +20 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/utils/ImplementationHelper.kt +88 -67
- package/dist/docs.json +1383 -976
- package/dist/esm/definitions.d.ts +2 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +12 -2
- package/dist/esm/index.js +4 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/schemas/enums/capacitor-calendar-error-code.d.ts +2 -2
- package/dist/esm/schemas/enums/capacitor-calendar-error-code.js +2 -2
- package/dist/esm/schemas/enums/capacitor-calendar-error-code.js.map +1 -1
- package/dist/esm/schemas/interfaces/calendar-changed-event.d.ts +52 -0
- package/dist/esm/schemas/interfaces/calendar-changed-event.js +2 -0
- package/dist/esm/schemas/interfaces/calendar-changed-event.js.map +1 -0
- package/dist/esm/schemas/interfaces/calendar-links.d.ts +32 -0
- package/dist/esm/schemas/interfaces/calendar-links.js +2 -0
- package/dist/esm/schemas/interfaces/calendar-links.js.map +1 -0
- package/dist/esm/schemas/interfaces/create-event-options.d.ts +18 -0
- package/dist/esm/schemas/interfaces/create-event-options.js.map +1 -1
- package/dist/esm/schemas/interfaces/create-event-with-prompt-options.d.ts +9 -0
- package/dist/esm/schemas/interfaces/create-event-with-prompt-options.js.map +1 -1
- package/dist/esm/schemas/interfaces/create-ics-file-options.d.ts +20 -0
- package/dist/esm/schemas/interfaces/create-ics-file-options.js +2 -0
- package/dist/esm/schemas/interfaces/create-ics-file-options.js.map +1 -0
- package/dist/esm/schemas/interfaces/open-calendar-options.d.ts +9 -0
- package/dist/esm/schemas/interfaces/open-calendar-options.js.map +1 -1
- package/dist/esm/schemas/interfaces/share-ics-file-options.d.ts +17 -0
- package/dist/esm/schemas/interfaces/share-ics-file-options.js +2 -0
- package/dist/esm/schemas/interfaces/share-ics-file-options.js.map +1 -0
- package/dist/esm/schemas/interfaces/start-listening-options.d.ts +46 -0
- package/dist/esm/schemas/interfaces/start-listening-options.js +2 -0
- package/dist/esm/schemas/interfaces/start-listening-options.js.map +1 -0
- package/dist/esm/schemas/types/calendar-day-view-provider.d.ts +7 -0
- package/dist/esm/schemas/types/calendar-day-view-provider.js +2 -0
- package/dist/esm/schemas/types/calendar-day-view-provider.js.map +1 -0
- package/dist/esm/schemas/types/calendar-link-provider.d.ts +6 -0
- package/dist/esm/schemas/types/calendar-link-provider.js +2 -0
- package/dist/esm/schemas/types/calendar-link-provider.js.map +1 -0
- package/dist/esm/sub-definitions/calendar-listening.d.ts +49 -0
- package/dist/esm/sub-definitions/calendar-listening.js +2 -0
- package/dist/esm/sub-definitions/calendar-listening.js.map +1 -0
- package/dist/esm/sub-definitions/calendar-operations.d.ts +2 -1
- package/dist/esm/sub-definitions/calendar-operations.js.map +1 -1
- package/dist/esm/web/calendar-links.d.ts +26 -0
- package/dist/esm/web/calendar-links.js +114 -0
- package/dist/esm/web/calendar-links.js.map +1 -0
- package/dist/esm/web/create-ics-file.d.ts +21 -0
- package/dist/esm/web/create-ics-file.js +30 -0
- package/dist/esm/web/create-ics-file.js.map +1 -0
- package/dist/esm/web/ics.d.ts +11 -0
- package/dist/esm/web/ics.js +48 -32
- package/dist/esm/web/ics.js.map +1 -1
- package/dist/esm/web/share-ics-file.d.ts +21 -0
- package/dist/esm/web/share-ics-file.js +46 -0
- package/dist/esm/web/share-ics-file.js.map +1 -0
- package/dist/esm/web.d.ts +4 -1
- package/dist/esm/web.js +18 -4
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +292 -72
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +292 -72
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/CapacitorCalendarPlugin.swift +19 -0
- package/ios/Plugin/Implementation/CalendarChangeObserver.swift +259 -0
- package/ios/Plugin/Implementation/CapacitorCalendar.swift +15 -1
- package/ios/Plugin/Models/Inputs/DeleteReminderInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/DeleteReminderWithPromptInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/DeleteRemindersListInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/GetReminderByIdInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/ModifyReminderInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/StartListeningInput.swift +26 -0
- package/ios/Plugin/Models/Inputs/UpdateRemindersListInput.swift +1 -1
- package/ios/Plugin/PluginConfig.swift +2 -0
- package/ios/Plugin/PluginError.swift +25 -3
- package/ios/Plugin/Utils/ImplementationHelper.swift +16 -30
- package/ios/Plugin/Utils/PluginCallRejecter.swift +13 -0
- package/package.json +1 -1
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import Capacitor
|
|
2
|
+
import EventKit
|
|
3
|
+
|
|
4
|
+
/// Watches the event store. After each change it waits for the changes to settle,
|
|
5
|
+
/// reads the watched window again and compares it with the last snapshot.
|
|
6
|
+
/// iOS does not say what changed, so the comparison finds it.
|
|
7
|
+
final class CalendarChangeObserver {
|
|
8
|
+
private struct Entry {
|
|
9
|
+
let id: String
|
|
10
|
+
let fingerprint: String
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
private static let debounceInterval: TimeInterval = 0.4
|
|
14
|
+
|
|
15
|
+
private let eventStore: EKEventStore
|
|
16
|
+
private let emit: (JSObject) -> Void
|
|
17
|
+
private let hasListeners: () -> Bool
|
|
18
|
+
private let queue = DispatchQueue(label: "dev.barooni.capacitor.calendar.change-observer")
|
|
19
|
+
private var notificationToken: NSObjectProtocol?
|
|
20
|
+
private var pendingCheck: DispatchWorkItem?
|
|
21
|
+
private var range: ListEventsInRangeInput?
|
|
22
|
+
private var includeEvents = true
|
|
23
|
+
private var includeReminders = false
|
|
24
|
+
private var eventSnapshot: [String: Entry] = [:]
|
|
25
|
+
private var calendarSnapshot: [String: String] = [:]
|
|
26
|
+
private var listenerSeen = false
|
|
27
|
+
|
|
28
|
+
init(eventStore: EKEventStore, hasListeners: @escaping () -> Bool, emit: @escaping (JSObject) -> Void) {
|
|
29
|
+
self.eventStore = eventStore
|
|
30
|
+
self.hasListeners = hasListeners
|
|
31
|
+
self.emit = emit
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
deinit {
|
|
35
|
+
if let notificationToken {
|
|
36
|
+
NotificationCenter.default.removeObserver(notificationToken)
|
|
37
|
+
}
|
|
38
|
+
pendingCheck?.cancel()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/// Stops a running watch, reads the baseline and starts to observe.
|
|
42
|
+
/// Throws when the baseline cannot be read. Then nothing is observed.
|
|
43
|
+
func start(_ input: StartListeningInput) throws {
|
|
44
|
+
try queue.sync {
|
|
45
|
+
stopLocked()
|
|
46
|
+
let range = input.getRange()
|
|
47
|
+
let events = try readEvents(range)
|
|
48
|
+
self.range = range
|
|
49
|
+
includeEvents = input.getIncludeEvents()
|
|
50
|
+
includeReminders = input.getIncludeReminders()
|
|
51
|
+
eventSnapshot = toEntries(events)
|
|
52
|
+
calendarSnapshot = readCalendars()
|
|
53
|
+
notificationToken = NotificationCenter.default.addObserver(
|
|
54
|
+
forName: .EKEventStoreChanged,
|
|
55
|
+
object: eventStore,
|
|
56
|
+
queue: nil
|
|
57
|
+
) { [weak self] _ in
|
|
58
|
+
self?.scheduleCheck()
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
func stop() {
|
|
64
|
+
queue.sync {
|
|
65
|
+
stopLocked()
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private func stopLocked() {
|
|
70
|
+
if let notificationToken {
|
|
71
|
+
NotificationCenter.default.removeObserver(notificationToken)
|
|
72
|
+
}
|
|
73
|
+
notificationToken = nil
|
|
74
|
+
pendingCheck?.cancel()
|
|
75
|
+
pendingCheck = nil
|
|
76
|
+
range = nil
|
|
77
|
+
eventSnapshot = [:]
|
|
78
|
+
calendarSnapshot = [:]
|
|
79
|
+
listenerSeen = false
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private func scheduleCheck() {
|
|
83
|
+
queue.async { [weak self] in
|
|
84
|
+
guard let self, self.range != nil else {
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
self.pendingCheck?.cancel()
|
|
88
|
+
let workItem = DispatchWorkItem { [weak self] in
|
|
89
|
+
self?.check()
|
|
90
|
+
}
|
|
91
|
+
self.pendingCheck = workItem
|
|
92
|
+
self.queue.asyncAfter(deadline: .now() + Self.debounceInterval, execute: workItem)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private func check() {
|
|
97
|
+
guard let range else {
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
// After `removeAllListeners` nobody listens. Then the watch ends by itself.
|
|
101
|
+
guard hasListeners() else {
|
|
102
|
+
if listenerSeen {
|
|
103
|
+
stopLocked()
|
|
104
|
+
}
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
listenerSeen = true
|
|
108
|
+
let freshEvents: [EKEvent]
|
|
109
|
+
do {
|
|
110
|
+
freshEvents = try readEvents(range)
|
|
111
|
+
} catch {
|
|
112
|
+
// The snapshot stays as it is. The next check compares with it again.
|
|
113
|
+
emit(["precise": false])
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
let freshEntries = toEntries(freshEvents)
|
|
117
|
+
let freshCalendars = readCalendars()
|
|
118
|
+
let diff = diffEvents(freshEvents, freshEntries: freshEntries)
|
|
119
|
+
let calendarsChanged = freshCalendars != calendarSnapshot
|
|
120
|
+
|
|
121
|
+
eventSnapshot = freshEntries
|
|
122
|
+
calendarSnapshot = freshCalendars
|
|
123
|
+
|
|
124
|
+
// The store does not say if a reminder changed. Without an event or calendar change,
|
|
125
|
+
// a reminder change is the likely cause.
|
|
126
|
+
let remindersChanged = includeReminders && hasRemindersAccess() && !diff.hasChange && !calendarsChanged
|
|
127
|
+
guard diff.hasChange || calendarsChanged || remindersChanged else {
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
emit(makePayload(diff, calendarsChanged: calendarsChanged, remindersChanged: remindersChanged))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private struct EventDiff {
|
|
134
|
+
let created: [EKEvent]
|
|
135
|
+
let updated: [EKEvent]
|
|
136
|
+
let deletedIds: [String]
|
|
137
|
+
/// An occurrence left the window while its series stays. The lists cannot express it.
|
|
138
|
+
let occurrenceRemoved: Bool
|
|
139
|
+
|
|
140
|
+
var hasChange: Bool {
|
|
141
|
+
return !created.isEmpty || !updated.isEmpty || !deletedIds.isEmpty || occurrenceRemoved
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private func diffEvents(_ freshEvents: [EKEvent], freshEntries: [String: Entry]) -> EventDiff {
|
|
146
|
+
let oldCounts = countById(eventSnapshot)
|
|
147
|
+
let freshCounts = countById(freshEntries)
|
|
148
|
+
// A new start time for a known id is a moved event or occurrence, not a new event.
|
|
149
|
+
let created = freshEvents.filter { event in
|
|
150
|
+
eventSnapshot[key(for: event)] == nil && oldCounts[event.eventIdentifier ?? ""] == nil
|
|
151
|
+
}
|
|
152
|
+
let updated = freshEvents.filter { event in
|
|
153
|
+
guard let old = eventSnapshot[key(for: event)] else {
|
|
154
|
+
return oldCounts[event.eventIdentifier ?? ""] != nil
|
|
155
|
+
}
|
|
156
|
+
return old.fingerprint != freshEntries[key(for: event)]?.fingerprint
|
|
157
|
+
}
|
|
158
|
+
var deletedIds: [String] = []
|
|
159
|
+
for (entryKey, entry) in eventSnapshot where freshEntries[entryKey] == nil {
|
|
160
|
+
if freshCounts[entry.id] == nil && !deletedIds.contains(entry.id) {
|
|
161
|
+
deletedIds.append(entry.id)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
let occurrenceRemoved = freshCounts.contains { id, count in count < (oldCounts[id] ?? 0) }
|
|
165
|
+
return EventDiff(
|
|
166
|
+
created: created,
|
|
167
|
+
updated: updated,
|
|
168
|
+
deletedIds: deletedIds,
|
|
169
|
+
occurrenceRemoved: occurrenceRemoved
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private func makePayload(_ diff: EventDiff, calendarsChanged: Bool, remindersChanged: Bool) -> JSObject {
|
|
174
|
+
var payload = JSObject()
|
|
175
|
+
var seriesStartDateCache: [String: Double] = [:]
|
|
176
|
+
if includeEvents && !diff.created.isEmpty {
|
|
177
|
+
payload["created"] = toJSArray(diff.created, cache: &seriesStartDateCache)
|
|
178
|
+
}
|
|
179
|
+
if includeEvents && !diff.updated.isEmpty {
|
|
180
|
+
payload["updated"] = toJSArray(diff.updated, cache: &seriesStartDateCache)
|
|
181
|
+
}
|
|
182
|
+
if !diff.deletedIds.isEmpty {
|
|
183
|
+
var ids = JSArray()
|
|
184
|
+
diff.deletedIds.forEach { ids.append($0) }
|
|
185
|
+
payload["deletedIds"] = ids
|
|
186
|
+
}
|
|
187
|
+
if calendarsChanged {
|
|
188
|
+
payload["calendarsChanged"] = true
|
|
189
|
+
}
|
|
190
|
+
if remindersChanged {
|
|
191
|
+
payload["remindersChanged"] = true
|
|
192
|
+
}
|
|
193
|
+
// A reminder signal carries no list. Events that are not sent are not listed either.
|
|
194
|
+
payload["precise"] = !remindersChanged && !diff.occurrenceRemoved
|
|
195
|
+
&& (includeEvents || (diff.created.isEmpty && diff.updated.isEmpty))
|
|
196
|
+
return payload
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private func readEvents(_ range: ListEventsInRangeInput) throws -> [EKEvent] {
|
|
200
|
+
let predicate = eventStore.predicateForEvents(
|
|
201
|
+
withStart: range.getFrom(),
|
|
202
|
+
end: range.getTo(),
|
|
203
|
+
calendars: try range.getCalendars(from: eventStore)
|
|
204
|
+
)
|
|
205
|
+
return eventStore.events(matching: predicate)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private func readCalendars() -> [String: String] {
|
|
209
|
+
var result: [String: String] = [:]
|
|
210
|
+
for calendar in eventStore.calendars(for: .event) {
|
|
211
|
+
let color = ImplementationHelper.cgColorToHex(calendar.cgColor) ?? ""
|
|
212
|
+
result[calendar.calendarIdentifier] = "\(calendar.title)|\(color)|\(calendar.allowsContentModifications)"
|
|
213
|
+
}
|
|
214
|
+
return result
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
private func hasRemindersAccess() -> Bool {
|
|
218
|
+
switch EKEventStore.authorizationStatus(for: .reminder) {
|
|
219
|
+
case .authorized, .fullAccess:
|
|
220
|
+
return true
|
|
221
|
+
default:
|
|
222
|
+
return false
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// One event id has one entry for each occurrence of a series.
|
|
227
|
+
private func key(for event: EKEvent) -> String {
|
|
228
|
+
let start = ImplementationHelper.dateToMillis(event.startDate) ?? 0
|
|
229
|
+
return "\(event.eventIdentifier ?? "")|\(start)"
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private func countById(_ entries: [String: Entry]) -> [String: Int] {
|
|
233
|
+
var result: [String: Int] = [:]
|
|
234
|
+
for entry in entries.values {
|
|
235
|
+
result[entry.id, default: 0] += 1
|
|
236
|
+
}
|
|
237
|
+
return result
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private func toEntries(_ events: [EKEvent]) -> [String: Entry] {
|
|
241
|
+
var result: [String: Entry] = [:]
|
|
242
|
+
for event in events {
|
|
243
|
+
let modified = ImplementationHelper.dateToMillis(event.lastModifiedDate) ?? 0
|
|
244
|
+
let end = ImplementationHelper.dateToMillis(event.endDate) ?? 0
|
|
245
|
+
let calendarId = event.calendar?.calendarIdentifier ?? ""
|
|
246
|
+
let fingerprint = "\(modified)|\(end)|\(calendarId)|\(event.title ?? "")|\(event.location ?? "")"
|
|
247
|
+
result[key(for: event)] = Entry(id: event.eventIdentifier ?? "", fingerprint: fingerprint)
|
|
248
|
+
}
|
|
249
|
+
return result
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private func toJSArray(_ events: [EKEvent], cache: inout [String: Double]) -> JSArray {
|
|
253
|
+
var array = JSArray()
|
|
254
|
+
for event in events {
|
|
255
|
+
array.append(ImplementationHelper.eventToJSObject(event, eventStore: eventStore, seriesStartDateCache: &cache))
|
|
256
|
+
}
|
|
257
|
+
return array
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -10,6 +10,12 @@ class CapacitorCalendar: NSObject {
|
|
|
10
10
|
private let plugin: CapacitorCalendarPlugin
|
|
11
11
|
private var selectCalendarsWithPromptCompletion: ((SelectCalendarsWithPromptResult?, Error?) -> Void)?
|
|
12
12
|
|
|
13
|
+
private lazy var calendarChangeObserver = CalendarChangeObserver(
|
|
14
|
+
eventStore: eventStore,
|
|
15
|
+
hasListeners: { [weak plugin] in plugin?.hasListeners("calendarChanged") ?? false },
|
|
16
|
+
emit: { [weak plugin] payload in plugin?.notifyListeners("calendarChanged", data: payload) }
|
|
17
|
+
)
|
|
18
|
+
|
|
13
19
|
init(plugin: CapacitorCalendarPlugin) {
|
|
14
20
|
self.plugin = plugin
|
|
15
21
|
}
|
|
@@ -295,7 +301,7 @@ class CapacitorCalendar: NSObject {
|
|
|
295
301
|
return
|
|
296
302
|
}
|
|
297
303
|
guard selectCalendarsWithPromptCompletion == nil else {
|
|
298
|
-
completion(nil, PluginError.
|
|
304
|
+
completion(nil, PluginError.calendarChooserAlreadyPresented)
|
|
299
305
|
return
|
|
300
306
|
}
|
|
301
307
|
|
|
@@ -601,6 +607,14 @@ class CapacitorCalendar: NSObject {
|
|
|
601
607
|
return ListEventsInRangeResult(events, eventStore: eventStore)
|
|
602
608
|
}
|
|
603
609
|
|
|
610
|
+
func startListening(_ input: StartListeningInput) throws {
|
|
611
|
+
try calendarChangeObserver.start(input)
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
func stopListening() {
|
|
615
|
+
calendarChangeObserver.stop()
|
|
616
|
+
}
|
|
617
|
+
|
|
604
618
|
func getEventById(
|
|
605
619
|
_ input: GetEventByIdInput,
|
|
606
620
|
completion: @escaping (GetEventByIdResult?, Error?) -> Void
|
|
@@ -9,7 +9,7 @@ struct DeleteReminderWithPromptInput {
|
|
|
9
9
|
|
|
10
10
|
init(call: CAPPluginCall) throws {
|
|
11
11
|
guard let id = call.getString("id") else {
|
|
12
|
-
throw PluginError.
|
|
12
|
+
throw PluginError.reminderIdMissing
|
|
13
13
|
}
|
|
14
14
|
self.id = id
|
|
15
15
|
guard let title = call.getString("title") else {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Capacitor
|
|
2
|
+
import EventKit
|
|
3
|
+
|
|
4
|
+
struct StartListeningInput {
|
|
5
|
+
private let range: ListEventsInRangeInput
|
|
6
|
+
private let includeEvents: Bool
|
|
7
|
+
private let includeReminders: Bool
|
|
8
|
+
|
|
9
|
+
init(call: CAPPluginCall) throws {
|
|
10
|
+
self.range = try ListEventsInRangeInput(call: call)
|
|
11
|
+
self.includeEvents = call.getBool("includeEvents") ?? true
|
|
12
|
+
self.includeReminders = call.getBool("includeReminders") ?? false
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
func getRange() -> ListEventsInRangeInput {
|
|
16
|
+
return range
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
func getIncludeEvents() -> Bool {
|
|
20
|
+
return includeEvents
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
func getIncludeReminders() -> Bool {
|
|
24
|
+
return includeReminders
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -43,6 +43,8 @@ struct PluginConfig {
|
|
|
43
43
|
CAPPluginMethod(name: "requestReadOnlyCalendarAccess", returnType: CAPPluginReturnPromise),
|
|
44
44
|
CAPPluginMethod(name: "requestWriteOnlyCalendarAccess", returnType: CAPPluginReturnPromise),
|
|
45
45
|
CAPPluginMethod(name: "selectCalendarsWithPrompt", returnType: CAPPluginReturnPromise),
|
|
46
|
+
CAPPluginMethod(name: "startListening", returnType: CAPPluginReturnPromise),
|
|
47
|
+
CAPPluginMethod(name: "stopListening", returnType: CAPPluginReturnPromise),
|
|
46
48
|
CAPPluginMethod(name: "updateRemindersList", returnType: CAPPluginReturnPromise)
|
|
47
49
|
]
|
|
48
50
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Foundation
|
|
2
2
|
|
|
3
3
|
enum PluginError: LocalizedError {
|
|
4
|
+
case calendarChooserAlreadyPresented
|
|
4
5
|
case calendarIdMissing
|
|
5
6
|
case calendarNotFound
|
|
6
7
|
case calendarNotModifiable
|
|
@@ -18,6 +19,7 @@ enum PluginError: LocalizedError {
|
|
|
18
19
|
case invalidScope
|
|
19
20
|
case invalidUrl
|
|
20
21
|
case ioError(String)
|
|
22
|
+
case listIdMissing
|
|
21
23
|
case listIdsMissing
|
|
22
24
|
case listNotFound
|
|
23
25
|
case listNotModifiable
|
|
@@ -26,8 +28,10 @@ enum PluginError: LocalizedError {
|
|
|
26
28
|
case missingFrequency
|
|
27
29
|
case missingInterval
|
|
28
30
|
case noFieldsToModify
|
|
31
|
+
case permissionDenied(String)
|
|
29
32
|
case processFailed
|
|
30
33
|
case recurrenceNotChangeableForOccurrence
|
|
34
|
+
case reminderIdMissing
|
|
31
35
|
case reminderNotFound
|
|
32
36
|
case scopeMissing
|
|
33
37
|
case titleMissing
|
|
@@ -39,6 +43,8 @@ enum PluginError: LocalizedError {
|
|
|
39
43
|
|
|
40
44
|
var errorDescription: String? {
|
|
41
45
|
switch self {
|
|
46
|
+
case .calendarChooserAlreadyPresented:
|
|
47
|
+
return NSLocalizedString("Calendar chooser is already presented.", comment: "Calendar chooser already presented error")
|
|
42
48
|
case .calendarIdMissing:
|
|
43
49
|
return NSLocalizedString("Calendar ID must be provided.", comment: "Calendar ID missing error")
|
|
44
50
|
case .calendarNotFound:
|
|
@@ -73,6 +79,8 @@ enum PluginError: LocalizedError {
|
|
|
73
79
|
return NSLocalizedString("Invalid URL.", comment: "Invalid URL error")
|
|
74
80
|
case .ioError(let message):
|
|
75
81
|
return message
|
|
82
|
+
case .listIdMissing:
|
|
83
|
+
return NSLocalizedString("List ID must be provided.", comment: "List ID missing error")
|
|
76
84
|
case .listIdsMissing:
|
|
77
85
|
return NSLocalizedString("List IDs must be provided.", comment: "List IDs missing error")
|
|
78
86
|
case .listNotFound:
|
|
@@ -89,6 +97,8 @@ enum PluginError: LocalizedError {
|
|
|
89
97
|
return NSLocalizedString("Interval must be provided.", comment: "Interval missing error")
|
|
90
98
|
case .noFieldsToModify:
|
|
91
99
|
return NSLocalizedString("At least one of title or color must be provided.", comment: "No fields to modify error")
|
|
100
|
+
case .permissionDenied(let message):
|
|
101
|
+
return message
|
|
92
102
|
case .processFailed:
|
|
93
103
|
return NSLocalizedString("Process failed.", comment: "Process failed error")
|
|
94
104
|
case .recurrenceNotChangeableForOccurrence:
|
|
@@ -96,6 +106,8 @@ enum PluginError: LocalizedError {
|
|
|
96
106
|
"Recurrence cannot be changed for a single occurrence. Use THIS_AND_FUTURE_EVENTS.",
|
|
97
107
|
comment: "Recurrence not changeable for occurrence error"
|
|
98
108
|
)
|
|
109
|
+
case .reminderIdMissing:
|
|
110
|
+
return NSLocalizedString("Reminder ID must be provided.", comment: "Reminder ID missing error")
|
|
99
111
|
case .reminderNotFound:
|
|
100
112
|
return NSLocalizedString("Reminder not found.", comment: "Reminder not found error")
|
|
101
113
|
case .scopeMissing:
|
|
@@ -125,12 +137,14 @@ enum PluginError: LocalizedError {
|
|
|
125
137
|
.invalidFrequency,
|
|
126
138
|
.invalidScope,
|
|
127
139
|
.invalidUrl,
|
|
140
|
+
.listIdMissing,
|
|
128
141
|
.listIdsMissing,
|
|
129
142
|
.messageMissing,
|
|
130
143
|
.missingFrequency,
|
|
131
144
|
.missingInterval,
|
|
132
145
|
.noFieldsToModify,
|
|
133
146
|
.recurrenceNotChangeableForOccurrence,
|
|
147
|
+
.reminderIdMissing,
|
|
134
148
|
.scopeMissing,
|
|
135
149
|
.titleMissing,
|
|
136
150
|
.toDateMissing:
|
|
@@ -144,15 +158,23 @@ enum PluginError: LocalizedError {
|
|
|
144
158
|
case .failedToRetrieveCalendarId,
|
|
145
159
|
.failedToRetrieveEventId,
|
|
146
160
|
.ioError,
|
|
147
|
-
.processFailed
|
|
161
|
+
.processFailed,
|
|
162
|
+
.unhandledPermissionState:
|
|
148
163
|
return "EB-CLDR-0004"
|
|
149
164
|
case .failedToLaunchCalendar,
|
|
150
165
|
.failedToLaunchReminders,
|
|
151
|
-
.unableToOpenUrl
|
|
166
|
+
.unableToOpenUrl,
|
|
167
|
+
.calendarChooserAlreadyPresented,
|
|
168
|
+
.viewControllerMissing:
|
|
152
169
|
return "EB-CLDR-0005"
|
|
170
|
+
case .permissionDenied:
|
|
171
|
+
return "EB-CLDR-0020"
|
|
153
172
|
case .missingDeclaration:
|
|
154
173
|
return "EB-CLDR-0100"
|
|
155
|
-
|
|
174
|
+
case .calendarNotModifiable,
|
|
175
|
+
.customError,
|
|
176
|
+
.listNotModifiable,
|
|
177
|
+
.unimplemented:
|
|
156
178
|
return nil
|
|
157
179
|
}
|
|
158
180
|
}
|
|
@@ -47,18 +47,22 @@ struct ImplementationHelper {
|
|
|
47
47
|
return result
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
let permissionState: CAPPermissionState = granted ? .granted : .denied
|
|
59
|
-
continuation.resume(returning: permissionState)
|
|
50
|
+
/// Resolves an EventKit access request. An authorization denial resolves `denied` and does not throw.
|
|
51
|
+
private static func accessRequestHandler(
|
|
52
|
+
_ continuation: CheckedContinuation<CAPPermissionState, Error>
|
|
53
|
+
) -> (Bool, Error?) -> Void {
|
|
54
|
+
return { granted, error in
|
|
55
|
+
if let error = error, !PluginCallRejecter.isPermissionDenied(error) {
|
|
56
|
+
continuation.resume(throwing: error)
|
|
60
57
|
return
|
|
61
58
|
}
|
|
59
|
+
continuation.resume(returning: granted ? .granted : .denied)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static func requestWriteOnlyCalendarAccess(eventStore: EKEventStore) async throws -> CAPPermissionState {
|
|
64
|
+
return try await withCheckedThrowingContinuation { continuation in
|
|
65
|
+
let requestAccessHandler = accessRequestHandler(continuation)
|
|
62
66
|
|
|
63
67
|
if #available(iOS 17.0, *) {
|
|
64
68
|
eventStore.requestWriteOnlyAccessToEvents(completion: requestAccessHandler)
|
|
@@ -70,16 +74,7 @@ struct ImplementationHelper {
|
|
|
70
74
|
|
|
71
75
|
static func requestFullCalendarAccess(eventStore: EKEventStore) async throws -> CAPPermissionState {
|
|
72
76
|
return try await withCheckedThrowingContinuation { continuation in
|
|
73
|
-
let requestAccessHandler
|
|
74
|
-
if let error = error {
|
|
75
|
-
continuation.resume(throwing: error)
|
|
76
|
-
return
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
let permissionState: CAPPermissionState = granted ? .granted : .denied
|
|
80
|
-
continuation.resume(returning: permissionState)
|
|
81
|
-
return
|
|
82
|
-
}
|
|
77
|
+
let requestAccessHandler = accessRequestHandler(continuation)
|
|
83
78
|
|
|
84
79
|
if #available(iOS 17.0, *) {
|
|
85
80
|
eventStore.requestFullAccessToEvents(completion: requestAccessHandler)
|
|
@@ -91,16 +86,7 @@ struct ImplementationHelper {
|
|
|
91
86
|
|
|
92
87
|
static func requestFullRemindersAccess(eventStore: EKEventStore) async throws -> CAPPermissionState {
|
|
93
88
|
return try await withCheckedThrowingContinuation { continuation in
|
|
94
|
-
let requestAccessHandler
|
|
95
|
-
if let error = error {
|
|
96
|
-
continuation.resume(throwing: error)
|
|
97
|
-
return
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
let permissionState: CAPPermissionState = granted ? .granted : .denied
|
|
101
|
-
continuation.resume(returning: permissionState)
|
|
102
|
-
return
|
|
103
|
-
}
|
|
89
|
+
let requestAccessHandler = accessRequestHandler(continuation)
|
|
104
90
|
|
|
105
91
|
if #available(iOS 17.0, *) {
|
|
106
92
|
eventStore.requestFullAccessToReminders(completion: requestAccessHandler)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Capacitor
|
|
2
|
+
import EventKit
|
|
2
3
|
import Foundation
|
|
3
4
|
|
|
4
5
|
/// Maps thrown errors to `PluginError` and rejects the Capacitor call.
|
|
@@ -14,6 +15,11 @@ enum PluginCallRejecter {
|
|
|
14
15
|
if let error = error as? PluginError {
|
|
15
16
|
return error
|
|
16
17
|
} else if let error = error {
|
|
18
|
+
if isPermissionDenied(error) {
|
|
19
|
+
return .permissionDenied(
|
|
20
|
+
wrapSystemError("Calendar access was denied.", error.localizedDescription)
|
|
21
|
+
)
|
|
22
|
+
}
|
|
17
23
|
return .ioError(
|
|
18
24
|
wrapSystemError(
|
|
19
25
|
"An I/O error occurred while accessing the calendar.",
|
|
@@ -25,6 +31,13 @@ enum PluginCallRejecter {
|
|
|
25
31
|
}
|
|
26
32
|
}
|
|
27
33
|
|
|
34
|
+
/// True when EventKit rejects the call because the app has no calendar or reminders access.
|
|
35
|
+
static func isPermissionDenied(_ error: Error) -> Bool {
|
|
36
|
+
let nsError = error as NSError
|
|
37
|
+
return nsError.domain == EKErrorDomain
|
|
38
|
+
&& nsError.code == EKError.Code.eventStoreNotAuthorized.rawValue
|
|
39
|
+
}
|
|
40
|
+
|
|
28
41
|
/// Lead with the stable plugin sentence; append a different non-blank OS message in parentheses.
|
|
29
42
|
static func wrapSystemError(_ canonicalMessage: String, _ systemMessage: String?) -> String {
|
|
30
43
|
let detail = systemMessage?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ebarooni/capacitor-calendar",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.12.0",
|
|
4
4
|
"description": "A Capacitor plugin for native calendar events on iOS and Android (with reminders on iOS), and .ics event creation on the web.",
|
|
5
5
|
"author": "Ehsan Barooni",
|
|
6
6
|
"license": "MIT",
|