@ebarooni/capacitor-calendar 6.4.1 → 6.6.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.
Files changed (40) hide show
  1. package/README.md +53 -5
  2. package/android/build.gradle +2 -2
  3. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendar.kt +200 -117
  4. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendarPlugin.kt +85 -3
  5. package/assets/images/text-logo.png +0 -0
  6. package/dist/docs.json +467 -70
  7. package/dist/esm/definitions.d.ts +205 -57
  8. package/dist/esm/definitions.js.map +1 -1
  9. package/dist/esm/index.d.ts +18 -14
  10. package/dist/esm/index.js +12 -9
  11. package/dist/esm/index.js.map +1 -1
  12. package/dist/esm/schemas/enums/calendar-source-type.d.ts +38 -0
  13. package/dist/esm/schemas/enums/calendar-source-type.js +40 -0
  14. package/dist/esm/schemas/enums/calendar-source-type.js.map +1 -0
  15. package/dist/esm/schemas/enums/calendar-type.d.ts +28 -0
  16. package/dist/esm/schemas/enums/calendar-type.js +30 -0
  17. package/dist/esm/schemas/enums/calendar-type.js.map +1 -0
  18. package/dist/esm/schemas/enums/event-span.d.ts +16 -0
  19. package/dist/esm/schemas/enums/event-span.js +18 -0
  20. package/dist/esm/schemas/enums/event-span.js.map +1 -0
  21. package/dist/esm/schemas/interfaces/calendar-event.d.ts +14 -3
  22. package/dist/esm/schemas/interfaces/calendar-event.js.map +1 -1
  23. package/dist/esm/schemas/interfaces/calendar-source.d.ts +15 -0
  24. package/dist/esm/schemas/interfaces/calendar-source.js +2 -0
  25. package/dist/esm/schemas/interfaces/calendar-source.js.map +1 -0
  26. package/dist/esm/schemas/interfaces/calendar.d.ts +43 -2
  27. package/dist/esm/schemas/interfaces/calendar.js.map +1 -1
  28. package/dist/esm/web.d.ts +55 -11
  29. package/dist/esm/web.js +13 -2
  30. package/dist/esm/web.js.map +1 -1
  31. package/dist/plugin.cjs.js +101 -2
  32. package/dist/plugin.cjs.js.map +1 -1
  33. package/dist/plugin.js +101 -2
  34. package/dist/plugin.js.map +1 -1
  35. package/ios/Plugin/CapacitorCalendar.swift +238 -21
  36. package/ios/Plugin/CapacitorCalendarPlugin.m +4 -0
  37. package/ios/Plugin/CapacitorCalendarPlugin.swift +141 -15
  38. package/ios/Plugin/CapacitorReminders.swift +62 -8
  39. package/ios/Plugin/EventCreationParameters.swift +5 -2
  40. package/package.json +28 -18
@@ -6,7 +6,7 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
6
6
  private let bridge: (any CAPBridgeProtocol)?
7
7
  private let eventStore: EKEventStore
8
8
  private var currentCreateEventContinuation: CheckedContinuation<[String], any Error>?
9
- private var currentSelectCalendarsContinuation: CheckedContinuation<[[String: String]], any Error>?
9
+ private var currentSelectCalendarsContinuation: CheckedContinuation<[[String: Any]], any Error>?
10
10
 
11
11
  init(bridge: (any CAPBridgeProtocol)?, eventStore: EKEventStore) {
12
12
  self.bridge = bridge
@@ -33,8 +33,20 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
33
33
  if let isAllDay = parameters.isAllDay {
34
34
  newEvent.isAllDay = isAllDay
35
35
  }
36
- if let alertOffsetInMinutes = parameters.alertOffsetInMinutes, alertOffsetInMinutes >= 0 {
37
- newEvent.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alertOffsetInMinutes * 60)))
36
+ if let alertOffsetInMinutesSingle = parameters.alertOffsetInMinutesSingle, alertOffsetInMinutesSingle >= 0 {
37
+ newEvent.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alertOffsetInMinutesSingle * 60)))
38
+ } else if let alertOffsetInMinutesMultiple = parameters.alertOffsetInMinutesMultiple {
39
+ for alert in alertOffsetInMinutesMultiple {
40
+ if alert >= 0 {
41
+ newEvent.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alert * 60)))
42
+ }
43
+ }
44
+ }
45
+ if let notes = parameters.notes {
46
+ newEvent.notes = notes
47
+ }
48
+ if let urlString = parameters.url, let url = URL(string: urlString) {
49
+ newEvent.url = url
38
50
  }
39
51
 
40
52
  return try await withCheckedThrowingContinuation { continuation in
@@ -54,7 +66,132 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
54
66
  }
55
67
  }
56
68
 
57
- public func selectCalendarsWithPrompt(selectionStyle: Int, displayStyle: Int) async throws -> [[String: String]] {
69
+ public func modifyEventWithPrompt(id: String, update: EventCreationParameters? = nil) async throws -> [String] {
70
+ return try await withCheckedThrowingContinuation { continuation in
71
+ guard let viewController = bridge?.viewController else {
72
+ continuation.resume(throwing: CapacitorCalendarPluginError.viewControllerUnavailable)
73
+ return
74
+ }
75
+
76
+ guard let event = eventStore.event(withIdentifier: id) else {
77
+ continuation.resume(throwing: CapacitorCalendarPluginError.undefinedEvent)
78
+ return
79
+ }
80
+
81
+ if update != nil {
82
+ if (update?.title) != nil {
83
+ event.title = update?.title
84
+ }
85
+ if let calendarId = update?.calendarId, let calendar = eventStore.calendar(withIdentifier: calendarId) {
86
+ event.calendar = calendar
87
+ }
88
+ if update?.location != nil {
89
+ event.location = update?.location
90
+ }
91
+ if let startDate = update?.startDate {
92
+ event.startDate = Date(timeIntervalSince1970: startDate / 1000)
93
+ }
94
+ if let endDate = update?.endDate {
95
+ event.endDate = Date(timeIntervalSince1970: endDate / 1000)
96
+ }
97
+ if let isAllDay = update?.isAllDay {
98
+ event.isAllDay = isAllDay
99
+ }
100
+ if let notes = update?.notes {
101
+ event.notes = notes
102
+ }
103
+ if let urlString = update?.url, let url = URL(string: urlString) {
104
+ event.url = url
105
+ }
106
+ if let alertOffsetInMinutesSingle = update?.alertOffsetInMinutesSingle, alertOffsetInMinutesSingle >= 0 {
107
+ if event.hasAlarms {
108
+ for alarm in event.alarms! {
109
+ event.removeAlarm(alarm)
110
+ }
111
+ }
112
+ event.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alertOffsetInMinutesSingle * 60)))
113
+ } else if let alertOffsetInMinutesMultiple = update?.alertOffsetInMinutesMultiple {
114
+ if event.hasAlarms {
115
+ for alarm in event.alarms! {
116
+ event.removeAlarm(alarm)
117
+ }
118
+ }
119
+ for alert in alertOffsetInMinutesMultiple {
120
+ if alert >= 0 {
121
+ event.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alert * 60)))
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ Task { @MainActor in
128
+ let eventEditViewController = EKEventEditViewController()
129
+ eventEditViewController.event = event
130
+ eventEditViewController.eventStore = eventStore
131
+ eventEditViewController.editViewDelegate = self
132
+ currentCreateEventContinuation = continuation
133
+ viewController.present(eventEditViewController, animated: true, completion: nil)
134
+ }
135
+ }
136
+ }
137
+
138
+ public func modifyEvent(id: String, span: EKSpan, update: EventCreationParameters) throws {
139
+ guard let event = eventStore.event(withIdentifier: id) else {
140
+ throw CapacitorCalendarPluginError.undefinedEvent
141
+ }
142
+
143
+ if (update.title) != nil {
144
+ event.title = update.title
145
+ }
146
+ if let calendarId = update.calendarId, let calendar = eventStore.calendar(withIdentifier: calendarId) {
147
+ event.calendar = calendar
148
+ }
149
+ if update.location != nil {
150
+ event.location = update.location
151
+ }
152
+ if let startDate = update.startDate {
153
+ event.startDate = Date(timeIntervalSince1970: startDate / 1000)
154
+ }
155
+ if let endDate = update.endDate {
156
+ event.endDate = Date(timeIntervalSince1970: endDate / 1000)
157
+ }
158
+ if let isAllDay = update.isAllDay {
159
+ event.isAllDay = isAllDay
160
+ }
161
+ if let notes = update.notes {
162
+ event.notes = notes
163
+ }
164
+ if let urlString = update.url, let url = URL(string: urlString) {
165
+ event.url = url
166
+ }
167
+ if let alertOffsetInMinutesSingle = update.alertOffsetInMinutesSingle, alertOffsetInMinutesSingle >= 0 {
168
+ if event.hasAlarms {
169
+ for alarm in event.alarms! {
170
+ event.removeAlarm(alarm)
171
+ }
172
+ }
173
+ event.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alertOffsetInMinutesSingle * 60)))
174
+ } else if let alertOffsetInMinutesMultiple = update.alertOffsetInMinutesMultiple {
175
+ if event.hasAlarms {
176
+ for alarm in event.alarms! {
177
+ event.removeAlarm(alarm)
178
+ }
179
+ }
180
+ for alert in alertOffsetInMinutesMultiple {
181
+ if alert >= 0 {
182
+ event.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alert * 60)))
183
+ }
184
+ }
185
+ }
186
+
187
+ do {
188
+ try eventStore.save(event, span: span)
189
+ } catch {
190
+ throw CapacitorCalendarPluginError.undefinedEvent
191
+ }
192
+ }
193
+
194
+ public func selectCalendarsWithPrompt(selectionStyle: Int, displayStyle: Int) async throws -> [[String: Any]] {
58
195
  return try await withCheckedThrowingContinuation { continuation in
59
196
  guard let viewController = bridge?.viewController else {
60
197
  continuation.resume(throwing: CapacitorCalendarPluginError.viewControllerUnavailable)
@@ -86,17 +223,30 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
86
223
  }
87
224
  }
88
225
 
89
- public func listCalendars() -> [[String: String]] {
226
+ public func listCalendars() -> [[String: Any]] {
90
227
  return convertEKCalendarsToDictionaries(calendars: Set(eventStore.calendars(for: .event)))
91
228
  }
92
229
 
93
- public func getDefaultCalendar() throws -> [String: String]? {
230
+ public func getDefaultCalendar() throws -> [String: Any]? {
94
231
  let defaultCalendar = eventStore.defaultCalendarForNewEvents
95
232
  if let defaultCalendar = defaultCalendar {
96
- return [
233
+ var calendarDict: [String: Any] = [
97
234
  "id": defaultCalendar.calendarIdentifier,
98
- "title": defaultCalendar.title
235
+ "title": defaultCalendar.title,
236
+ "color": hexStringFromColor(color: defaultCalendar.cgColor),
237
+ "isImmutable": defaultCalendar.isImmutable,
238
+ "allowsContentModifications": defaultCalendar.allowsContentModifications,
239
+ "type": defaultCalendar.type.rawValue,
240
+ "isSubscribed": defaultCalendar.isSubscribed
99
241
  ]
242
+ if let calendarSource = defaultCalendar.source {
243
+ calendarDict["source"] = [
244
+ "type": calendarSource.sourceType.rawValue,
245
+ "id": calendarSource.sourceIdentifier,
246
+ "title": calendarSource.title
247
+ ]
248
+ }
249
+ return calendarDict
100
250
  } else {
101
251
  return nil
102
252
  }
@@ -127,8 +277,20 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
127
277
  if let isAllDay = parameters.isAllDay {
128
278
  newEvent.isAllDay = isAllDay
129
279
  }
130
- if let alertOffsetInMinutes = parameters.alertOffsetInMinutes, alertOffsetInMinutes >= 0 {
131
- newEvent.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alertOffsetInMinutes * 60)))
280
+ if let alertOffsetInMinutesSingle = parameters.alertOffsetInMinutesSingle, alertOffsetInMinutesSingle >= 0 {
281
+ newEvent.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alertOffsetInMinutesSingle * 60)))
282
+ } else if let alertOffsetInMinutesMultiple = parameters.alertOffsetInMinutesMultiple {
283
+ for alert in alertOffsetInMinutesMultiple {
284
+ if alert >= 0 {
285
+ newEvent.addAlarm(EKAlarm(relativeOffset: TimeInterval(-alert * 60)))
286
+ }
287
+ }
288
+ }
289
+ if let notes = parameters.notes {
290
+ newEvent.notes = notes
291
+ }
292
+ if let urlString = parameters.url, let url = URL(string: urlString) {
293
+ newEvent.url = url
132
294
  }
133
295
 
134
296
  do {
@@ -310,7 +472,7 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
310
472
  }
311
473
  }
312
474
 
313
- public func createCalendar(title: String, color: String?) throws -> String {
475
+ public func createCalendar(title: String, color: String?, sourceId: String?) throws -> String {
314
476
  let newCalendar = EKCalendar(for: .event, eventStore: eventStore)
315
477
  newCalendar.title = title
316
478
  if let calendarColor = color {
@@ -318,7 +480,15 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
318
480
  } else {
319
481
  newCalendar.cgColor = eventStore.defaultCalendarForNewEvents?.cgColor
320
482
  }
321
- newCalendar.source = eventStore.defaultCalendarForNewEvents?.source
483
+ if let calendarSourceId = sourceId {
484
+ let matchingSource = eventStore.sources.first(where: { $0.sourceIdentifier == calendarSourceId })
485
+ if let requestedSource = matchingSource {
486
+ newCalendar.source = requestedSource
487
+ }
488
+
489
+ } else {
490
+ newCalendar.source = eventStore.defaultCalendarForNewEvents?.source
491
+ }
322
492
 
323
493
  do {
324
494
  try eventStore.saveCalendar(newCalendar, commit: true)
@@ -370,14 +540,41 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
370
540
  }
371
541
  }
372
542
 
373
- private func convertEKCalendarsToDictionaries(calendars: Set<EKCalendar>) -> [[String: String]] {
374
- var result: [[String: String]] = []
543
+ public func fetchAllCalendarSources() throws -> [[String: Any]] {
544
+ var result: [[String: Any]] = []
545
+
546
+ for source in eventStore.sources {
547
+ let sourceDict: [String: Any] = [
548
+ "id": source.sourceIdentifier,
549
+ "title": source.title,
550
+ "type": source.sourceType.rawValue
551
+ ]
552
+ result.append(sourceDict)
553
+ }
554
+
555
+ return result
556
+ }
557
+
558
+ private func convertEKCalendarsToDictionaries(calendars: Set<EKCalendar>) -> [[String: Any]] {
559
+ var result: [[String: Any]] = []
375
560
 
376
561
  for calendar in calendars {
377
- let calendarDict: [String: String] = [
562
+ var calendarDict: [String: Any] = [
378
563
  "id": calendar.calendarIdentifier,
379
- "title": calendar.title
564
+ "title": calendar.title,
565
+ "color": hexStringFromColor(color: calendar.cgColor),
566
+ "isImmutable": calendar.isImmutable,
567
+ "allowsContentModifications": calendar.allowsContentModifications,
568
+ "type": calendar.type.rawValue,
569
+ "isSubscribed": calendar.isSubscribed
380
570
  ]
571
+ if let calendarSource = calendar.source {
572
+ calendarDict["source"] = [
573
+ "type": calendarSource.sourceType.rawValue,
574
+ "id": calendarSource.sourceIdentifier,
575
+ "title": calendarSource.title
576
+ ]
577
+ }
381
578
  result.append(calendarDict)
382
579
  }
383
580
 
@@ -401,18 +598,38 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarCho
401
598
  dict["description"] = notes
402
599
  }
403
600
  if let startDate = event.startDate {
404
- dict["startDate"] = startDate.timeIntervalSince1970
601
+ dict["startDate"] = startDate.timeIntervalSince1970 * 1000
405
602
  }
406
603
  if let endDate = event.endDate {
407
- dict["endDate"] = endDate.timeIntervalSince1970
604
+ dict["endDate"] = endDate.timeIntervalSince1970 * 1000
605
+ }
606
+ if let timezone = event.timeZone, let region = event.timeZone?.identifier, let abbreviation = timezone.abbreviation() {
607
+ dict["eventTimezone"] = ["region": region, "abbreviation": abbreviation]
608
+ dict["eventEndTimezone"] = ["region": region, "abbreviation": abbreviation]
408
609
  }
409
- if let timezone = event.timeZone, (timezone.abbreviation()?.isEmpty) == nil {
410
- dict["eventTimezone"] = timezone.abbreviation()
411
- dict["eventEndTimezone"] = timezone.abbreviation()
610
+ if let color = event.calendar.cgColor {
611
+ dict["eventColor"] = hexStringFromColor(color: color)
612
+ }
613
+ if let url = event.url {
614
+ dict["url"] = url
412
615
  }
413
616
  dict["isAllDay"] = event.isAllDay
414
617
  dict["calendarId"] = event.calendar.calendarIdentifier
415
618
  return dict
416
619
  }
417
620
  }
621
+
622
+ private func hexStringFromColor(color: CGColor) -> String {
623
+ guard let components = color.components, components.count >= 3 else {
624
+ return "#000000"
625
+ }
626
+
627
+ let red = Float(components[0])
628
+ let green = Float(components[1])
629
+ let blue = Float(components[2])
630
+ return String(format: "#%02lX%02lX%02lX",
631
+ lroundf(red * 255),
632
+ lroundf(green * 255),
633
+ lroundf(blue * 255))
634
+ }
418
635
  }
@@ -28,4 +28,8 @@ CAP_PLUGIN(CapacitorCalendarPlugin, "CapacitorCalendar",
28
28
  CAP_PLUGIN_METHOD(requestFullCalendarAccess, CAPPluginReturnPromise);
29
29
  CAP_PLUGIN_METHOD(requestFullRemindersAccess, CAPPluginReturnPromise);
30
30
  CAP_PLUGIN_METHOD(requestReadOnlyCalendarAccess, CAPPluginReturnPromise);
31
+ CAP_PLUGIN_METHOD(modifyEventWithPrompt, CAPPluginReturnPromise);
32
+ CAP_PLUGIN_METHOD(modifyEvent, CAPPluginReturnNone);
33
+ CAP_PLUGIN_METHOD(fetchAllCalendarSources, CAPPluginReturnPromise);
34
+ CAP_PLUGIN_METHOD(fetchAllRemindersSources, CAPPluginReturnPromise);
31
35
  )
@@ -130,19 +130,27 @@ public class CapacitorCalendarPlugin: CAPPlugin {
130
130
  let endDate = call.getDouble("endDate")
131
131
  let isAllDay = call.getBool("isAllDay")
132
132
  let calendarId = call.getString("calendarId")
133
- let alertOffsetInMinutes = call.getDouble("alertOffsetInMinutes")
134
-
135
- let eventParameters = EventCreationParameters(
136
- title: title,
137
- calendarId: calendarId,
138
- location: location,
139
- startDate: startDate,
140
- endDate: endDate,
141
- isAllDay: isAllDay,
142
- alertOffsetInMinutes: alertOffsetInMinutes
143
- )
133
+ let notes = call.getString("notes")
134
+ let url = call.getString("url")
144
135
 
145
136
  Task {
137
+ var eventParameters = EventCreationParameters(
138
+ title: title,
139
+ calendarId: calendarId,
140
+ location: location,
141
+ startDate: startDate,
142
+ endDate: endDate,
143
+ isAllDay: isAllDay,
144
+ notes: notes,
145
+ url: url
146
+ )
147
+
148
+ if let alertOffsetInMinutesSingle = call.getDouble("alertOffsetInMinutes") as Double? {
149
+ eventParameters.alertOffsetInMinutesSingle = alertOffsetInMinutesSingle
150
+ } else if let alertOffsetInMinutesMultiple = call.getArray("alertOffsetInMinutes") as? [Double]? {
151
+ eventParameters.alertOffsetInMinutesMultiple = alertOffsetInMinutesMultiple
152
+ }
153
+
146
154
  do {
147
155
  let result = try await calendar.createEventWithPrompt(with: eventParameters)
148
156
  call.resolve(["result": result])
@@ -153,6 +161,55 @@ public class CapacitorCalendarPlugin: CAPPlugin {
153
161
  }
154
162
  }
155
163
 
164
+ @objc public func modifyEventWithPrompt(_ call: CAPPluginCall) {
165
+ guard let id = call.getString("id") else {
166
+ call.reject("[CapacitorCalendar.\(#function)] Missing event id")
167
+ return
168
+ }
169
+
170
+ Task {
171
+ do {
172
+ let result: [String]
173
+
174
+ if let update = call.getObject("update") {
175
+
176
+ let title = update["title"] as? String
177
+ let calendarId = update["calendarId"] as? String
178
+ let location = update["location"] as? String
179
+ let startDate = update["startDate"] as? Double
180
+ let endDate = update["endDate"] as? Double
181
+ let isAllDay = update["isAllDay"] as? Bool
182
+ let notes = update["notes"] as? String
183
+ let url = update["url"] as? String
184
+ let alertOffsetInMinutesSingle = update["alertOffsetInMinutes"] as? Double
185
+ let alertOffsetInMinutesMultiple = update["alertOffsetInMinutes"] as? [Double]
186
+
187
+ var eventParameters = EventCreationParameters(
188
+ title: title,
189
+ calendarId: calendarId,
190
+ location: location,
191
+ startDate: startDate,
192
+ endDate: endDate,
193
+ isAllDay: isAllDay,
194
+ alertOffsetInMinutesSingle: alertOffsetInMinutesSingle,
195
+ alertOffsetInMinutesMultiple: alertOffsetInMinutesMultiple,
196
+ notes: notes,
197
+ url: url
198
+ )
199
+
200
+ result = try await calendar.modifyEventWithPrompt(id: id, update: eventParameters)
201
+ } else {
202
+ result = try await calendar.modifyEventWithPrompt(id: id)
203
+ }
204
+
205
+ call.resolve(["result": result])
206
+ } catch {
207
+ call.reject("[CapacitorCalendar.\(#function)] Unable to modify event")
208
+ return
209
+ }
210
+ }
211
+ }
212
+
156
213
  @objc public func selectCalendarsWithPrompt(_ call: CAPPluginCall) {
157
214
  guard let selectionStyle = call.getInt("selectionStyle") else {
158
215
  call.reject("[CapacitorCalendar.\(#function)] Selection style was not provided")
@@ -197,18 +254,26 @@ public class CapacitorCalendarPlugin: CAPPlugin {
197
254
  let endDate = call.getDouble("endDate")
198
255
  let isAllDay = call.getBool("isAllDay")
199
256
  let calendarId = call.getString("calendarId")
200
- let alertOffsetInMinutes = call.getDouble("alertOffsetInMinutes")
257
+ let notes = call.getString("notes")
258
+ let url = call.getString("url")
201
259
 
202
- let eventParameters = EventCreationParameters(
260
+ var eventParameters = EventCreationParameters(
203
261
  title: title,
204
262
  calendarId: calendarId,
205
263
  location: location,
206
264
  startDate: startDate,
207
265
  endDate: endDate,
208
266
  isAllDay: isAllDay,
209
- alertOffsetInMinutes: alertOffsetInMinutes
267
+ notes: notes,
268
+ url: url
210
269
  )
211
270
 
271
+ if let alertOffsetInMinutesSingle = call.getDouble("alertOffsetInMinutes") as Double? {
272
+ eventParameters.alertOffsetInMinutesSingle = alertOffsetInMinutesSingle
273
+ } else if let alertOffsetInMinutesMultiple = call.getArray("alertOffsetInMinutes") as? [Double]? {
274
+ eventParameters.alertOffsetInMinutesMultiple = alertOffsetInMinutesMultiple
275
+ }
276
+
212
277
  do {
213
278
  let id = try calendar.createEvent(with: eventParameters)
214
279
  call.resolve(["result": id])
@@ -218,6 +283,48 @@ public class CapacitorCalendarPlugin: CAPPlugin {
218
283
  }
219
284
  }
220
285
 
286
+ @objc public func modifyEvent(_ call: CAPPluginCall) {
287
+ guard let eventId = call.getString("id") else {
288
+ call.reject("[CapacitorCalendar.\(#function)] An id for the event was not provided")
289
+ return
290
+ }
291
+ guard let update = call.getObject("update") else {
292
+ call.reject("[CapacitorCalendar.\(#function)] An update for the event was not provided")
293
+ return
294
+ }
295
+ let span = call.getInt("span", 0)
296
+ let title = update["title"] as? String
297
+ let calendarId = update["calendarId"] as? String
298
+ let location = update["location"] as? String
299
+ let startDate = update["startDate"] as? Double
300
+ let endDate = update["endDate"] as? Double
301
+ let isAllDay = update["isAllDay"] as? Bool
302
+ let notes = update["notes"] as? String
303
+ let url = update["url"] as? String
304
+ let alertOffsetInMinutesSingle = update["alertOffsetInMinutes"] as? Double
305
+ let alertOffsetInMinutesMultiple = update["alertOffsetInMinutes"] as? [Double]
306
+
307
+ do {
308
+ var eventParameters = EventCreationParameters(
309
+ title: title,
310
+ calendarId: calendarId,
311
+ location: location,
312
+ startDate: startDate,
313
+ endDate: endDate,
314
+ isAllDay: isAllDay,
315
+ alertOffsetInMinutesSingle: alertOffsetInMinutesSingle,
316
+ alertOffsetInMinutesMultiple: alertOffsetInMinutesMultiple,
317
+ notes: notes,
318
+ url: url
319
+ )
320
+ let result: Void = try calendar.modifyEvent(id: eventId, span: EKSpan(rawValue: span) ?? .thisEvent, update: eventParameters)
321
+ call.resolve(["result": result])
322
+ } catch {
323
+ call.reject("[CapacitorCalendar.\(#function)] Unable to modify event")
324
+ return
325
+ }
326
+ }
327
+
221
328
  @objc public func getDefaultRemindersList(_ call: CAPPluginCall) {
222
329
  do {
223
330
  try call.resolve(["result": reminders.getDefaultRemindersList() ?? NSNull()])
@@ -361,9 +468,10 @@ public class CapacitorCalendarPlugin: CAPPlugin {
361
468
  return
362
469
  }
363
470
  let color = call.getString("color")
471
+ let sourceId = call.getString("sourceId")
364
472
 
365
473
  do {
366
- let id = try calendar.createCalendar(title: title, color: color)
474
+ let id = try calendar.createCalendar(title: title, color: color, sourceId: sourceId)
367
475
  call.resolve(["result": id])
368
476
  } catch {
369
477
  call.reject("[CapacitorCalendar.\(#function)] Could not create calendar")
@@ -461,4 +569,22 @@ public class CapacitorCalendarPlugin: CAPPlugin {
461
569
  }
462
570
  }
463
571
  }
572
+
573
+ @objc public func fetchAllCalendarSources(_ call: CAPPluginCall) {
574
+ do {
575
+ call.resolve(["result": try calendar.fetchAllCalendarSources()])
576
+ } catch {
577
+ call.reject("[CapacitorCalendar.\(#function)] Unable to fetch calendar sources")
578
+ return
579
+ }
580
+ }
581
+
582
+ @objc public func fetchAllRemindersSources(_ call: CAPPluginCall) {
583
+ do {
584
+ call.resolve(["result": try reminders.fetchAllRemindersSources()])
585
+ } catch {
586
+ call.reject("[CapacitorCalendar.\(#function)] Unable to fetch calendar sources")
587
+ return
588
+ }
589
+ }
464
590
  }
@@ -24,13 +24,26 @@ public class CapacitorReminders: NSObject {
24
24
  self.eventStore = eventStore
25
25
  }
26
26
 
27
- public func getDefaultRemindersList() throws -> [String: String]? {
27
+ public func getDefaultRemindersList() throws -> [String: Any]? {
28
28
  let defaultRemindersList = eventStore.defaultCalendarForNewReminders()
29
29
  if let defaultRemindersList = defaultRemindersList {
30
- return [
30
+ var calendarDict: [String: Any] = [
31
31
  "id": defaultRemindersList.calendarIdentifier,
32
- "title": defaultRemindersList.title
32
+ "title": defaultRemindersList.title,
33
+ "color": hexStringFromColor(color: defaultRemindersList.cgColor),
34
+ "isImmutable": defaultRemindersList.isImmutable,
35
+ "allowsContentModifications": defaultRemindersList.allowsContentModifications,
36
+ "type": defaultRemindersList.type.rawValue,
37
+ "isSubscribed": defaultRemindersList.isSubscribed
33
38
  ]
39
+ if let calendarSource = defaultRemindersList.source {
40
+ calendarDict["source"] = [
41
+ "type": calendarSource.sourceType.rawValue,
42
+ "id": calendarSource.sourceIdentifier,
43
+ "title": calendarSource.title
44
+ ]
45
+ }
46
+ return calendarDict
34
47
  } else {
35
48
  return nil
36
49
  }
@@ -63,7 +76,7 @@ public class CapacitorReminders: NSObject {
63
76
  }
64
77
  }
65
78
 
66
- public func getRemindersLists() -> [[String: String]] {
79
+ public func getRemindersLists() -> [[String: Any]] {
67
80
  return convertEKCalendarsToDictionaries(calendars: Set(eventStore.calendars(for: .reminder)))
68
81
  }
69
82
 
@@ -229,14 +242,41 @@ public class CapacitorReminders: NSObject {
229
242
  }
230
243
  }
231
244
 
232
- private func convertEKCalendarsToDictionaries(calendars: Set<EKCalendar>) -> [[String: String]] {
233
- var result: [[String: String]] = []
245
+ public func fetchAllRemindersSources() throws -> [[String: Any]] {
246
+ var result: [[String: Any]] = []
247
+
248
+ for source in eventStore.sources {
249
+ let sourceDict: [String: Any] = [
250
+ "id": source.sourceIdentifier,
251
+ "title": source.title,
252
+ "type": source.sourceType.rawValue
253
+ ]
254
+ result.append(sourceDict)
255
+ }
256
+
257
+ return result
258
+ }
259
+
260
+ private func convertEKCalendarsToDictionaries(calendars: Set<EKCalendar>) -> [[String: Any]] {
261
+ var result: [[String: Any]] = []
234
262
 
235
263
  for calendar in calendars {
236
- let calendarDict: [String: String] = [
264
+ var calendarDict: [String: Any] = [
237
265
  "id": calendar.calendarIdentifier,
238
- "title": calendar.title
266
+ "title": calendar.title,
267
+ "color": hexStringFromColor(color: calendar.cgColor),
268
+ "isImmutable": calendar.isImmutable,
269
+ "allowsContentModifications": calendar.allowsContentModifications,
270
+ "type": calendar.type.rawValue,
271
+ "isSubscribed": calendar.isSubscribed
239
272
  ]
273
+ if let calendarSource = calendar.source {
274
+ calendarDict["source"] = [
275
+ "type": calendarSource.sourceType.rawValue,
276
+ "id": calendarSource.sourceIdentifier,
277
+ "title": calendarSource.title
278
+ ]
279
+ }
240
280
  result.append(calendarDict)
241
281
  }
242
282
 
@@ -355,4 +395,18 @@ public class CapacitorReminders: NSObject {
355
395
  return obj
356
396
  }
357
397
  }
398
+
399
+ private func hexStringFromColor(color: CGColor) -> String {
400
+ guard let components = color.components, components.count >= 3 else {
401
+ return "#000000"
402
+ }
403
+
404
+ let red = Float(components[0])
405
+ let green = Float(components[1])
406
+ let blue = Float(components[2])
407
+ return String(format: "#%02lX%02lX%02lX",
408
+ lroundf(red * 255),
409
+ lroundf(green * 255),
410
+ lroundf(blue * 255))
411
+ }
358
412
  }
@@ -9,11 +9,14 @@
9
9
  import Foundation
10
10
 
11
11
  public struct EventCreationParameters {
12
- public var title: String
12
+ public var title: String?
13
13
  public var calendarId: String?
14
14
  public var location: String?
15
15
  public var startDate: Double?
16
16
  public var endDate: Double?
17
17
  public var isAllDay: Bool?
18
- public var alertOffsetInMinutes: Double?
18
+ public var alertOffsetInMinutesSingle: Double?
19
+ public var alertOffsetInMinutesMultiple: [Double]?
20
+ public var notes: String?
21
+ public var url: String?
19
22
  }