@ebarooni/capacitor-calendar 8.10.0 → 8.11.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 (54) hide show
  1. package/README.md +94 -54
  2. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendarPlugin.kt +18 -0
  3. package/android/src/main/java/dev/barooni/capacitor/calendar/PluginError.kt +4 -0
  4. package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CapacitorCalendar.kt +128 -2
  5. package/android/src/main/java/dev/barooni/capacitor/calendar/models/data/EventRecurrenceRule.kt +4 -3
  6. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/GetEventByIdInput.kt +13 -0
  7. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/ListEventsInRangeInput.kt +5 -0
  8. package/android/src/main/java/dev/barooni/capacitor/calendar/models/results/GetEventByIdResult.kt +15 -0
  9. package/android/src/main/java/dev/barooni/capacitor/calendar/models/results/ListEventsInRangeResult.kt +47 -43
  10. package/android/src/main/java/dev/barooni/capacitor/calendar/utils/ImplementationHelper.kt +35 -2
  11. package/dist/docs.json +204 -6
  12. package/dist/esm/index.d.ts +4 -1
  13. package/dist/esm/index.js.map +1 -1
  14. package/dist/esm/schemas/interfaces/delete-event-options.d.ts +2 -1
  15. package/dist/esm/schemas/interfaces/delete-event-options.js.map +1 -1
  16. package/dist/esm/schemas/interfaces/delete-event-with-prompt-options.d.ts +2 -1
  17. package/dist/esm/schemas/interfaces/delete-event-with-prompt-options.js.map +1 -1
  18. package/dist/esm/schemas/interfaces/get-event-by-id-options.d.ts +11 -0
  19. package/dist/esm/schemas/interfaces/get-event-by-id-options.js +2 -0
  20. package/dist/esm/schemas/interfaces/get-event-by-id-options.js.map +1 -0
  21. package/dist/esm/schemas/interfaces/get-event-by-id-result.d.ts +13 -0
  22. package/dist/esm/schemas/interfaces/get-event-by-id-result.js +2 -0
  23. package/dist/esm/schemas/interfaces/get-event-by-id-result.js.map +1 -0
  24. package/dist/esm/schemas/interfaces/list-events-in-range-options.d.ts +8 -0
  25. package/dist/esm/schemas/interfaces/list-events-in-range-options.js.map +1 -1
  26. package/dist/esm/schemas/interfaces/modify-event-options.d.ts +11 -0
  27. package/dist/esm/schemas/interfaces/modify-event-options.js.map +1 -1
  28. package/dist/esm/schemas/interfaces/modify-event-with-prompt-options.d.ts +10 -0
  29. package/dist/esm/schemas/interfaces/modify-event-with-prompt-options.js.map +1 -1
  30. package/dist/esm/sub-definitions/event-operations.d.ts +33 -0
  31. package/dist/esm/sub-definitions/event-operations.js.map +1 -1
  32. package/dist/esm/web.d.ts +3 -0
  33. package/dist/esm/web.js +3 -0
  34. package/dist/esm/web.js.map +1 -1
  35. package/dist/plugin.cjs.js +3 -0
  36. package/dist/plugin.cjs.js.map +1 -1
  37. package/dist/plugin.js +3 -0
  38. package/dist/plugin.js.map +1 -1
  39. package/ios/Plugin/CapacitorCalendarPlugin.swift +17 -1
  40. package/ios/Plugin/Implementation/CapacitorCalendar.swift +41 -7
  41. package/ios/Plugin/Models/Inputs/CreateEventInput.swift +1 -1
  42. package/ios/Plugin/Models/Inputs/CreateEventWithPromptInput.swift +61 -70
  43. package/ios/Plugin/Models/Inputs/DeleteEventInput.swift +7 -0
  44. package/ios/Plugin/Models/Inputs/DeleteEventWithPromptInput.swift +7 -0
  45. package/ios/Plugin/Models/Inputs/GetEventByIdInput.swift +16 -0
  46. package/ios/Plugin/Models/Inputs/ListEventsInRangeInput.swift +20 -0
  47. package/ios/Plugin/Models/Inputs/ModifyEventInput.swift +13 -23
  48. package/ios/Plugin/Models/Inputs/ModifyEventWithPromptInput.swift +10 -5
  49. package/ios/Plugin/Models/Results/GetEventByIdResult.swift +27 -0
  50. package/ios/Plugin/Models/Structs/RecurrenceInput.swift +14 -0
  51. package/ios/Plugin/PluginConfig.swift +1 -0
  52. package/ios/Plugin/PluginError.swift +7 -0
  53. package/ios/Plugin/Utils/ImplementationHelper.swift +33 -2
  54. package/package.json +1 -1
@@ -3,6 +3,7 @@ import Capacitor
3
3
  struct DeleteEventInput {
4
4
  private let commit: Bool
5
5
  private let id: String
6
+ private let instanceDate: Double?
6
7
  private let span: EventSpan
7
8
 
8
9
  init(call: CAPPluginCall) throws {
@@ -11,6 +12,7 @@ struct DeleteEventInput {
11
12
  throw PluginError.idMissing
12
13
  }
13
14
  self.id = id
15
+ self.instanceDate = call.getDouble("instanceDate")
14
16
  if let spanInt = call.getInt("span"), let span = EventSpan(rawValue: spanInt) {
15
17
  self.span = span
16
18
  } else {
@@ -26,6 +28,11 @@ struct DeleteEventInput {
26
28
  return id
27
29
  }
28
30
 
31
+ func getInstanceDate() -> Date? {
32
+ guard let instanceDate = instanceDate else { return nil }
33
+ return ImplementationHelper.dateFromTimestamp(instanceDate)
34
+ }
35
+
29
36
  func getSpan() -> EventSpan {
30
37
  return span
31
38
  }
@@ -3,6 +3,7 @@ import Capacitor
3
3
  struct DeleteEventWithPromptInput {
4
4
  private let commit: Bool
5
5
  private let id: String
6
+ private let instanceDate: Double?
6
7
  private let span: EventSpan
7
8
  private let title: String
8
9
  private let message: String
@@ -15,6 +16,7 @@ struct DeleteEventWithPromptInput {
15
16
  throw PluginError.idMissing
16
17
  }
17
18
  self.id = id
19
+ self.instanceDate = call.getDouble("instanceDate")
18
20
  if let spanInt = call.getInt("span"), let span = EventSpan(rawValue: spanInt) {
19
21
  self.span = span
20
22
  } else {
@@ -40,6 +42,11 @@ struct DeleteEventWithPromptInput {
40
42
  return id
41
43
  }
42
44
 
45
+ func getInstanceDate() -> Date? {
46
+ guard let instanceDate = instanceDate else { return nil }
47
+ return ImplementationHelper.dateFromTimestamp(instanceDate)
48
+ }
49
+
43
50
  func getSpan() -> EventSpan {
44
51
  return span
45
52
  }
@@ -0,0 +1,16 @@
1
+ import Capacitor
2
+
3
+ struct GetEventByIdInput {
4
+ private let id: String
5
+
6
+ init(call: CAPPluginCall) throws {
7
+ guard let id = call.getString("id") else {
8
+ throw PluginError.idMissing
9
+ }
10
+ self.id = id
11
+ }
12
+
13
+ func getId() -> String {
14
+ return id
15
+ }
16
+ }
@@ -1,8 +1,10 @@
1
1
  import Capacitor
2
+ import EventKit
2
3
 
3
4
  struct ListEventsInRangeInput {
4
5
  private var from: Double
5
6
  private var to: Double
7
+ private var calendarIds: [String]?
6
8
 
7
9
  init(call: CAPPluginCall) throws {
8
10
  guard let from = call.getDouble("from") else {
@@ -13,6 +15,12 @@ struct ListEventsInRangeInput {
13
15
  throw PluginError.toDateMissing
14
16
  }
15
17
  self.to = to
18
+ if let rawIds = call.getArray("calendarIds") {
19
+ guard let ids = rawIds as? [String] else {
20
+ throw PluginError.calendarNotFound
21
+ }
22
+ self.calendarIds = ids
23
+ }
16
24
  }
17
25
 
18
26
  func getFrom() -> Date {
@@ -22,4 +30,16 @@ struct ListEventsInRangeInput {
22
30
  func getTo() -> Date {
23
31
  return ImplementationHelper.dateFromTimestamp(to)
24
32
  }
33
+
34
+ func getCalendars(from eventStore: EKEventStore) throws -> [EKCalendar]? {
35
+ guard let ids = calendarIds, !ids.isEmpty else {
36
+ return nil
37
+ }
38
+ return try ids.map { id in
39
+ guard let calendar = eventStore.calendar(withIdentifier: id) else {
40
+ throw PluginError.calendarNotFound
41
+ }
42
+ return calendar
43
+ }
44
+ }
25
45
  }
@@ -8,6 +8,7 @@ struct ModifyEventInput {
8
8
  private var description: String?
9
9
  private var endDate: Double?
10
10
  private let id: String
11
+ private let instanceDate: Double?
11
12
  private var isAllDay: Bool?
12
13
  private var location: String?
13
14
  private let recurrence: RecurrenceInput?
@@ -36,13 +37,14 @@ struct ModifyEventInput {
36
37
  throw PluginError.idMissing
37
38
  }
38
39
  self.id = id
40
+ self.instanceDate = call.getDouble("instanceDate")
39
41
  if let isAllDay = call.getBool("isAllDay") as Bool? {
40
42
  self.isAllDay = isAllDay
41
43
  }
42
44
  if let location = call.getString("location") {
43
45
  self.location = location
44
46
  }
45
- self.recurrence = CreateEventWithPromptInput.getRecurrence(from: call)
47
+ self.recurrence = try CreateEventWithPromptInput.getRecurrence(from: call)
46
48
  if let spanInt = call.getInt("span"), let span = EKSpan(rawValue: spanInt) {
47
49
  self.span = span
48
50
  } else {
@@ -72,10 +74,8 @@ struct ModifyEventInput {
72
74
  }
73
75
 
74
76
  func getEvent(from eventStore: EKEventStore) throws -> EKEvent {
75
- guard let event = eventStore.event(withIdentifier: id) else {
76
- throw PluginError.eventNotFound
77
- }
78
- return event
77
+ let occurrenceStart = instanceDate.map { ImplementationHelper.dateFromTimestamp($0) }
78
+ return try ImplementationHelper.findEvent(id: id, instanceDate: occurrenceStart, eventStore: eventStore)
79
79
  }
80
80
 
81
81
  func getTitle() -> String? {
@@ -90,24 +90,9 @@ struct ModifyEventInput {
90
90
  return location
91
91
  }
92
92
 
93
- func getRecurrenceRule() -> [EKRecurrenceRule] {
94
- guard let recurrence = recurrence else {
95
- return []
96
- }
97
-
98
- let rule = EKRecurrenceRule(
99
- recurrenceWith: recurrence.frequency.toEKFrequency(),
100
- interval: recurrence.interval,
101
- daysOfTheWeek: recurrence.byWeekDay,
102
- daysOfTheMonth: recurrence.byMonthDay,
103
- monthsOfTheYear: recurrence.byMonth,
104
- weeksOfTheYear: recurrence.weeksOfTheYear,
105
- daysOfTheYear: recurrence.daysOfTheYear,
106
- setPositions: nil,
107
- end: recurrence.end
108
- )
109
-
110
- return [rule]
93
+ /// Returns nil when `recurrence` is omitted, so the event keeps its existing rules.
94
+ func getRecurrenceRule() -> [EKRecurrenceRule]? {
95
+ return recurrence.map { [$0.toEKRecurrenceRule()] }
111
96
  }
112
97
 
113
98
  func getStartDate() -> Date? {
@@ -133,6 +118,11 @@ struct ModifyEventInput {
133
118
  return availability
134
119
  }
135
120
 
121
+ /// True when `instanceDate` selects one occurrence of a series.
122
+ func targetsOccurrence() -> Bool {
123
+ return instanceDate != nil
124
+ }
125
+
136
126
  func getSpan() -> EKSpan {
137
127
  return span
138
128
  }
@@ -4,17 +4,19 @@ import EventKit
4
4
  struct ModifyEventWithPromptInput {
5
5
  private let input: CreateEventWithPromptInput
6
6
  private let id: String
7
+ private let instanceDate: Double?
7
8
  private var title: String?
8
9
  private var isAllDay: Bool?
9
10
  private var alerts: [Double]?
10
11
  private var calendarId: String?
11
12
 
12
13
  init(call: CAPPluginCall) throws {
13
- self.input = CreateEventWithPromptInput(call: call)
14
+ self.input = try CreateEventWithPromptInput(call: call)
14
15
  guard let id = call.getString("id") else {
15
16
  throw PluginError.idMissing
16
17
  }
17
18
  self.id = id
19
+ self.instanceDate = call.getDouble("instanceDate")
18
20
  let title = input.getTitle()
19
21
  if !title.isEmpty {
20
22
  self.title = title
@@ -31,10 +33,8 @@ struct ModifyEventWithPromptInput {
31
33
  }
32
34
 
33
35
  func getEvent(from eventStore: EKEventStore) throws -> EKEvent {
34
- guard let event = eventStore.event(withIdentifier: id) else {
35
- throw PluginError.eventNotFound
36
- }
37
- return event
36
+ let occurrenceStart = instanceDate.map { ImplementationHelper.dateFromTimestamp($0) }
37
+ return try ImplementationHelper.findEvent(id: id, instanceDate: occurrenceStart, eventStore: eventStore)
38
38
  }
39
39
 
40
40
  func getTitle() -> String? {
@@ -61,6 +61,11 @@ struct ModifyEventWithPromptInput {
61
61
  return isAllDay
62
62
  }
63
63
 
64
+ /// Returns nil when `recurrence` is omitted, so the event keeps its existing rules.
65
+ func getRecurrenceRule() -> [EKRecurrenceRule]? {
66
+ return input.getRecurrenceRuleIfPresent()
67
+ }
68
+
64
69
  func getAlerts() -> [EKAlarm]? {
65
70
  if self.alerts != nil {
66
71
  return input.getAlerts()
@@ -0,0 +1,27 @@
1
+ import Capacitor
2
+ import EventKit
3
+
4
+ struct GetEventByIdResult: JSResult {
5
+ private let event: EKEvent?
6
+ private let eventStore: EKEventStore
7
+
8
+ init(_ event: EKEvent?, eventStore: EKEventStore) {
9
+ self.event = event
10
+ self.eventStore = eventStore
11
+ }
12
+
13
+ func toJSON() -> JSObject {
14
+ var result = JSObject()
15
+ if let event = event {
16
+ var seriesStartDateCache: [String: Double] = [:]
17
+ result["event"] = ImplementationHelper.eventToJSObject(
18
+ event,
19
+ eventStore: eventStore,
20
+ seriesStartDateCache: &seriesStartDateCache
21
+ )
22
+ } else {
23
+ result["event"] = NSNull()
24
+ }
25
+ return result
26
+ }
27
+ }
@@ -38,4 +38,18 @@ struct RecurrenceInput {
38
38
 
39
39
  let weeksOfTheYear: [NSNumber]?
40
40
  let daysOfTheYear: [NSNumber]?
41
+
42
+ func toEKRecurrenceRule() -> EKRecurrenceRule {
43
+ return EKRecurrenceRule(
44
+ recurrenceWith: frequency.toEKFrequency(),
45
+ interval: interval,
46
+ daysOfTheWeek: byWeekDay,
47
+ daysOfTheMonth: byMonthDay,
48
+ monthsOfTheYear: byMonth,
49
+ weeksOfTheYear: weeksOfTheYear,
50
+ daysOfTheYear: daysOfTheYear,
51
+ setPositions: nil,
52
+ end: end
53
+ )
54
+ }
41
55
  }
@@ -24,6 +24,7 @@ struct PluginConfig {
24
24
  CAPPluginMethod(name: "fetchAllRemindersSources", returnType: CAPPluginReturnPromise),
25
25
  CAPPluginMethod(name: "getDefaultCalendar", returnType: CAPPluginReturnPromise),
26
26
  CAPPluginMethod(name: "getDefaultRemindersList", returnType: CAPPluginReturnPromise),
27
+ CAPPluginMethod(name: "getEventById", returnType: CAPPluginReturnPromise),
27
28
  CAPPluginMethod(name: "getReminderById", returnType: CAPPluginReturnPromise),
28
29
  CAPPluginMethod(name: "getRemindersFromLists", returnType: CAPPluginReturnPromise),
29
30
  CAPPluginMethod(name: "getRemindersLists", returnType: CAPPluginReturnPromise),
@@ -27,6 +27,7 @@ enum PluginError: LocalizedError {
27
27
  case missingInterval
28
28
  case noFieldsToModify
29
29
  case processFailed
30
+ case recurrenceNotChangeableForOccurrence
30
31
  case reminderNotFound
31
32
  case scopeMissing
32
33
  case titleMissing
@@ -90,6 +91,11 @@ enum PluginError: LocalizedError {
90
91
  return NSLocalizedString("At least one of title or color must be provided.", comment: "No fields to modify error")
91
92
  case .processFailed:
92
93
  return NSLocalizedString("Process failed.", comment: "Process failed error")
94
+ case .recurrenceNotChangeableForOccurrence:
95
+ return NSLocalizedString(
96
+ "Recurrence cannot be changed for a single occurrence. Use THIS_AND_FUTURE_EVENTS.",
97
+ comment: "Recurrence not changeable for occurrence error"
98
+ )
93
99
  case .reminderNotFound:
94
100
  return NSLocalizedString("Reminder not found.", comment: "Reminder not found error")
95
101
  case .scopeMissing:
@@ -124,6 +130,7 @@ enum PluginError: LocalizedError {
124
130
  .missingFrequency,
125
131
  .missingInterval,
126
132
  .noFieldsToModify,
133
+ .recurrenceNotChangeableForOccurrence,
127
134
  .scopeMissing,
128
135
  .titleMissing,
129
136
  .toDateMissing:
@@ -197,10 +197,41 @@ struct ImplementationHelper {
197
197
  try eventStore.remove(reminder, commit: commit)
198
198
  }
199
199
 
200
- static func deleteEvent(commit: Bool, _ id: String, _ span: EventSpan, _ eventStore: EKEventStore) throws {
201
- guard let event = eventStore.event(withIdentifier: id) else {
200
+ /// Finds the event for `id`.
201
+ /// Without `instanceDate`, a recurring series resolves to its first occurrence.
202
+ /// With `instanceDate`, resolves to the occurrence that starts at that time.
203
+ static func findEvent(id: String, instanceDate: Date?, eventStore: EKEventStore) throws -> EKEvent {
204
+ guard let instanceDate = instanceDate else {
205
+ guard let event = eventStore.event(withIdentifier: id) else {
206
+ throw PluginError.eventNotFound
207
+ }
208
+ return event
209
+ }
210
+
211
+ let toleranceInSeconds: TimeInterval = 1
212
+ let predicate = eventStore.predicateForEvents(
213
+ withStart: instanceDate.addingTimeInterval(-toleranceInSeconds),
214
+ end: instanceDate.addingTimeInterval(toleranceInSeconds),
215
+ calendars: nil
216
+ )
217
+ let occurrence = eventStore.events(matching: predicate).first { candidate in
218
+ candidate.eventIdentifier == id
219
+ && abs(candidate.startDate.timeIntervalSince(instanceDate)) < toleranceInSeconds
220
+ }
221
+ guard let occurrence = occurrence else {
202
222
  throw PluginError.eventNotFound
203
223
  }
224
+ return occurrence
225
+ }
226
+
227
+ static func deleteEvent(
228
+ commit: Bool,
229
+ _ id: String,
230
+ _ span: EventSpan,
231
+ _ eventStore: EKEventStore,
232
+ instanceDate: Date? = nil
233
+ ) throws {
234
+ let event = try findEvent(id: id, instanceDate: instanceDate, eventStore: eventStore)
204
235
  switch span {
205
236
  case .thisEvent:
206
237
  try eventStore.remove(event, span: .thisEvent, commit: commit)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ebarooni/capacitor-calendar",
3
- "version": "8.10.0",
3
+ "version": "8.11.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",