@ebarooni/capacitor-calendar 0.3.1 → 0.5.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 (48) hide show
  1. package/README.md +168 -28
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendar.kt +35 -0
  4. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendarPlugin.kt +40 -0
  5. package/dist/docs.json +1100 -48
  6. package/dist/esm/definitions.d.ts +149 -62
  7. package/dist/esm/definitions.js +1 -31
  8. package/dist/esm/definitions.js.map +1 -1
  9. package/dist/esm/index.d.ts +10 -1
  10. package/dist/esm/index.js +5 -1
  11. package/dist/esm/index.js.map +1 -1
  12. package/dist/esm/schemas/enums/calendar-chooser-display-style.d.ts +15 -0
  13. package/dist/esm/schemas/enums/calendar-chooser-display-style.js +17 -0
  14. package/dist/esm/schemas/enums/calendar-chooser-display-style.js.map +1 -0
  15. package/dist/esm/schemas/enums/calendar-chooser-selection-style.d.ts +15 -0
  16. package/dist/esm/schemas/enums/calendar-chooser-selection-style.js +17 -0
  17. package/dist/esm/schemas/enums/calendar-chooser-selection-style.js.map +1 -0
  18. package/dist/esm/schemas/enums/plugin-permission.d.ts +22 -0
  19. package/dist/esm/schemas/enums/plugin-permission.js +24 -0
  20. package/dist/esm/schemas/enums/plugin-permission.js.map +1 -0
  21. package/dist/esm/schemas/enums/reminder-recurrence-frequency.d.ts +6 -0
  22. package/dist/esm/schemas/enums/reminder-recurrence-frequency.js +8 -0
  23. package/dist/esm/schemas/enums/reminder-recurrence-frequency.js.map +1 -0
  24. package/dist/esm/schemas/interfaces/calendar.d.ts +12 -0
  25. package/dist/esm/schemas/interfaces/calendar.js +2 -0
  26. package/dist/esm/schemas/interfaces/calendar.js.map +1 -0
  27. package/dist/esm/schemas/interfaces/plugin-permissions-map.d.ts +4 -0
  28. package/dist/esm/schemas/interfaces/plugin-permissions-map.js +2 -0
  29. package/dist/esm/schemas/interfaces/plugin-permissions-map.js.map +1 -0
  30. package/dist/esm/schemas/interfaces/reminder-recurrence-rule.d.ts +36 -0
  31. package/dist/esm/schemas/interfaces/reminder-recurrence-rule.js +2 -0
  32. package/dist/esm/schemas/interfaces/reminder-recurrence-rule.js.map +1 -0
  33. package/dist/esm/schemas/interfaces/reminders-list.d.ts +3 -0
  34. package/dist/esm/schemas/interfaces/reminders-list.js +2 -0
  35. package/dist/esm/schemas/interfaces/reminders-list.js.map +1 -0
  36. package/dist/esm/web.d.ts +41 -5
  37. package/dist/esm/web.js +12 -0
  38. package/dist/esm/web.js.map +1 -1
  39. package/dist/plugin.cjs.js +58 -14
  40. package/dist/plugin.cjs.js.map +1 -1
  41. package/dist/plugin.js +58 -14
  42. package/dist/plugin.js.map +1 -1
  43. package/ios/Plugin/CapacitorCalendar.swift +33 -0
  44. package/ios/Plugin/CapacitorCalendarPlugin.m +4 -0
  45. package/ios/Plugin/CapacitorCalendarPlugin.swift +131 -22
  46. package/ios/Plugin/CapacitorReminders.swift +177 -0
  47. package/package.json +10 -5
  48. package/ios/Plugin/Calendar.swift +0 -14
@@ -9,7 +9,8 @@ import Capacitor
9
9
  @objc(CapacitorCalendarPlugin)
10
10
  public class CapacitorCalendarPlugin: CAPPlugin {
11
11
  private let eventStore = EKEventStore()
12
- private lazy var implementation = CapacitorCalendar(bridge: self.bridge, eventStore: self.eventStore)
12
+ private lazy var calendar = CapacitorCalendar(bridge: self.bridge, eventStore: self.eventStore)
13
+ private lazy var reminders = CapacitorReminders(eventStore: self.eventStore)
13
14
 
14
15
  @objc public func checkPermission(_ call: CAPPluginCall) {
15
16
  guard let alias = call.getString("alias") else {
@@ -19,13 +20,19 @@ public class CapacitorCalendarPlugin: CAPPlugin {
19
20
 
20
21
  Task {
21
22
  do {
22
- let permissionsState = try await implementation.checkAllPermissions()
23
-
24
23
  switch alias {
25
24
  case "readCalendar":
25
+ let permissionsState = try await calendar.checkAllPermissions()
26
26
  call.resolve(["result": permissionsState["readCalendar"]!])
27
27
  case "writeCalendar":
28
+ let permissionsState = try await calendar.checkAllPermissions()
28
29
  call.resolve(["result": permissionsState["writeCalendar"]!])
30
+ case "readReminders":
31
+ let permissionsState = try await reminders.checkAllPermissions()
32
+ call.resolve(["result": permissionsState["readReminders"]!])
33
+ case "writeReminders":
34
+ let permissionsState = try await reminders.checkAllPermissions()
35
+ call.resolve(["result": permissionsState["writeReminders"]!])
29
36
  default:
30
37
  call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permission")
31
38
  return
@@ -40,8 +47,9 @@ public class CapacitorCalendarPlugin: CAPPlugin {
40
47
  @objc public func checkAllPermissions(_ call: CAPPluginCall) {
41
48
  Task {
42
49
  do {
43
- let permissionsState = try await implementation.checkAllPermissions()
44
- call.resolve(permissionsState)
50
+ let calendarPermissionsState = try await calendar.checkAllPermissions()
51
+ let remindersPermissionsState = try await reminders.checkAllPermissions()
52
+ call.resolve(calendarPermissionsState.merging(remindersPermissionsState) { (_, new) in new })
45
53
  } catch {
46
54
  call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permissions")
47
55
  return
@@ -59,10 +67,16 @@ public class CapacitorCalendarPlugin: CAPPlugin {
59
67
  do {
60
68
  switch alias {
61
69
  case "writeCalendar":
62
- let result = try await implementation.requestWriteAccessToEvents()
70
+ let result = try await calendar.requestWriteAccessToEvents()
63
71
  call.resolve(result)
64
72
  case "readCalendar":
65
- let result = try await implementation.requestFullAccessToEvents()
73
+ let result = try await calendar.requestFullAccessToEvents()
74
+ call.resolve(["result": result])
75
+ case "writeReminders":
76
+ let result = try await reminders.requestFullAccessToReminders()
77
+ call.resolve(["result": result])
78
+ case "readReminders":
79
+ let result = try await reminders.requestFullAccessToReminders()
66
80
  call.resolve(["result": result])
67
81
  default:
68
82
  call.reject("[CapacitorCalendar.\(#function)] Could not authorize \(alias)")
@@ -78,18 +92,24 @@ public class CapacitorCalendarPlugin: CAPPlugin {
78
92
  @objc public func requestAllPermissions(_ call: CAPPluginCall) {
79
93
  Task {
80
94
  do {
81
- let result = try await implementation.requestFullAccessToEvents()
82
- if (result == PermissionState.granted.rawValue) {
83
- call.resolve([
84
- "readCalendar": PermissionState.granted.rawValue,
85
- "writeCalendar": PermissionState.granted.rawValue
86
- ])
87
- } else {
88
- call.resolve([
89
- "readCalendar": PermissionState.denied.rawValue,
90
- "writeCalendar": PermissionState.denied.rawValue
91
- ])
95
+ let calendarResult = try await calendar.requestFullAccessToEvents()
96
+ let remindersResult = try await reminders.requestFullAccessToReminders()
97
+ var result: [String: String] = [
98
+ "readCalendar": PermissionState.denied.rawValue,
99
+ "writeCalendar": PermissionState.denied.rawValue,
100
+ "readReminders": PermissionState.denied.rawValue,
101
+ "writeReminders": PermissionState.denied.rawValue
102
+ ]
103
+ if calendarResult == PermissionState.granted.rawValue {
104
+ result["readCalendar"] = PermissionState.granted.rawValue
105
+ result["writeCalendar"] = PermissionState.granted.rawValue
106
+ }
107
+ if remindersResult == PermissionState.granted.rawValue {
108
+ result["readReminders"] = PermissionState.granted.rawValue
109
+ result["writeReminders"] = PermissionState.granted.rawValue
110
+
92
111
  }
112
+ call.resolve(result)
93
113
  } catch {
94
114
  call.reject("[CapacitorCalendar.\(#function)] Could not authorize all permissions")
95
115
  return
@@ -100,7 +120,7 @@ public class CapacitorCalendarPlugin: CAPPlugin {
100
120
  @objc public func createEventWithPrompt(_ call: CAPPluginCall) {
101
121
  Task {
102
122
  do {
103
- let result = try await implementation.createEventWithPrompt()
123
+ let result = try await calendar.createEventWithPrompt()
104
124
  call.resolve(["eventCreated": result])
105
125
  } catch {
106
126
  call.reject("[CapacitorCalendar.\(#function)] Unable to retrieve view controller")
@@ -121,7 +141,7 @@ public class CapacitorCalendarPlugin: CAPPlugin {
121
141
 
122
142
  Task {
123
143
  do {
124
- let result = try await implementation.selectCalendarsWithPrompt(selectionStyle: selectionStyle, displayStyle: displayStyle)
144
+ let result = try await calendar.selectCalendarsWithPrompt(selectionStyle: selectionStyle, displayStyle: displayStyle)
125
145
  call.resolve(["result": result])
126
146
  } catch {
127
147
  call.reject("[CapacitorCalendar.\(#function)] Calendars selection prompt got canceled")
@@ -131,14 +151,103 @@ public class CapacitorCalendarPlugin: CAPPlugin {
131
151
  }
132
152
 
133
153
  @objc public func listCalendars(_ call: CAPPluginCall) {
134
- call.resolve(["result": implementation.listCalendars()])
154
+ call.resolve(["result": calendar.listCalendars()])
135
155
  }
136
156
 
137
157
  @objc public func getDefaultCalendar(_ call: CAPPluginCall) {
138
158
  do {
139
- try call.resolve(["result": implementation.getDefaultCalendar()])
159
+ try call.resolve(["result": calendar.getDefaultCalendar()])
140
160
  } catch {
141
161
  call.reject("[CapacitorCalendar.\(#function)] No default calendar was found")
162
+ return
163
+ }
164
+ }
165
+
166
+ @objc public func createEvent(_ call: CAPPluginCall) {
167
+ guard let title = call.getString("title") else {
168
+ call.reject("[CapacitorCalendar.\(#function)] A title for the event was not provided")
169
+ return
170
+ }
171
+ let location = call.getString("location")
172
+ let startDate = call.getDate("startDate")
173
+ let endDate = call.getDate("endDate")
174
+ let isAllDay = call.getBool("isAllDay")
175
+ let calendarId = call.getString("calendarId")
176
+
177
+ do {
178
+ try calendar.createEvent(
179
+ title: title,
180
+ calendarId: calendarId,
181
+ location: location,
182
+ startDate: startDate,
183
+ endDate: endDate,
184
+ isAllDay: isAllDay
185
+ )
186
+ call.resolve(["eventCreated": true])
187
+ } catch {
188
+ call.reject("[CapacitorCalendar.\(#function)] Unable to create event")
189
+ return
190
+ }
191
+ }
192
+
193
+ @objc public func getDefaultRemindersList(_ call: CAPPluginCall) {
194
+ do {
195
+ try call.resolve(["result": reminders.getDefaultRemindersList()])
196
+ } catch {
197
+ call.reject("[CapacitorCalendar.\(#function)] No default Reminders list was found")
198
+ return
199
+ }
200
+ }
201
+
202
+ @objc public func getRemindersLists(_ call: CAPPluginCall) {
203
+ call.resolve(["result": reminders.getRemindersLists()])
204
+ }
205
+
206
+ @objc public func createReminder (_ call: CAPPluginCall) {
207
+ guard let title = call.getString("title") else {
208
+ call.reject("[CapacitorCalendar.\(#function)] A title for the reminder was not provided")
209
+ return
210
+ }
211
+ let listId = call.getString("listId")
212
+ let priority = call.getInt("priority")
213
+ let isCompleted = call.getBool("isCompleted")
214
+ let startDate = call.getDouble("startDate")
215
+ let dueDate = call.getDouble("dueDate")
216
+ let completionDate = call.getDouble("completionDate")
217
+ let notes = call.getString("notes")
218
+ let url = call.getString("url")
219
+ let location = call.getString("location")
220
+ var frequency: Int? = nil
221
+ var interval: Int? = nil
222
+ var end: Double? = nil
223
+ if let recurrence = call.getObject("recurrence") {
224
+ if let repFrequency = recurrence["frequency"] as? Int? {
225
+ frequency = repFrequency
226
+ } else {
227
+ call.reject("[CapacitorCalendar.\(#function)] Frequency must me provided when using recurrence")
228
+ return
229
+ }
230
+ if let repInterval = recurrence["interval"] as? Int {
231
+ if (repInterval < 1) {
232
+ call.reject("[CapacitorCalendar.\(#function)] Interval must be greater than 0")
233
+ return
234
+ }
235
+ interval = repInterval
236
+ } else {
237
+ call.reject("[CapacitorCalendar.\(#function)] Interval must me provided when using recurrence")
238
+ return
239
+ }
240
+ if let repEnd = recurrence["end"] as? Double {
241
+ end = repEnd
242
+ }
243
+ }
244
+
245
+ do {
246
+ try reminders.createReminder(title: title, listId: listId, priority: priority, isCompleted: isCompleted, startDate: startDate, dueDate: dueDate, completionDate: completionDate, notes: notes, url: url, location: location, frequency: frequency, interval: interval, end: end)
247
+ call.resolve(["reminderCreated": true])
248
+ } catch {
249
+ call.reject("[CapacitorCalendar.\(#function)] Unable to create reminder")
250
+ return
142
251
  }
143
252
  }
144
253
  }
@@ -0,0 +1,177 @@
1
+ //
2
+ // CapacitorReminders.swift
3
+ // Plugin
4
+ //
5
+ // Created by Ehsan Barooni on 15.03.24.
6
+ // Copyright © 2024 Max Lynch. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ import EventKit
11
+
12
+ public class CapacitorReminders: NSObject {
13
+ private let eventStore: EKEventStore
14
+ private let recurrenceFrequencyMapping: [Int: EKRecurrenceFrequency] = [
15
+ 0: .daily,
16
+ 1: .weekly,
17
+ 2: .monthly,
18
+ 3: .yearly
19
+ ]
20
+
21
+ init(eventStore: EKEventStore) {
22
+ self.eventStore = eventStore
23
+ }
24
+
25
+ public func getDefaultRemindersList() throws -> [String: String] {
26
+ let defaultRemindersList = eventStore.defaultCalendarForNewReminders()
27
+ if (defaultRemindersList != nil) {
28
+ return [
29
+ "id": defaultRemindersList!.calendarIdentifier,
30
+ "title": defaultRemindersList!.title
31
+ ]
32
+ } else {
33
+ throw CapacitorCalendarPluginError.noDefaultCalendar
34
+ }
35
+ }
36
+
37
+ public func checkAllPermissions() async throws -> [String: String] {
38
+ return try await withCheckedThrowingContinuation { continuation in
39
+ var permissionsState: [String: String]
40
+ switch EKEventStore.authorizationStatus(for: .reminder) {
41
+ case .authorized, .fullAccess:
42
+ permissionsState = [
43
+ "writeReminders": PermissionState.granted.rawValue,
44
+ "readReminders": PermissionState.granted.rawValue,
45
+ ]
46
+ case .denied, .restricted:
47
+ permissionsState = [
48
+ "writeReminders": PermissionState.denied.rawValue,
49
+ "readReminders": PermissionState.denied.rawValue,
50
+ ]
51
+ case .writeOnly, .notDetermined:
52
+ permissionsState = [
53
+ "writeReminders": PermissionState.prompt.rawValue,
54
+ "readReminders": PermissionState.prompt.rawValue,
55
+ ]
56
+ @unknown default:
57
+ continuation.resume(throwing: CapacitorCalendarPluginError.unknownPermissionStatus)
58
+ return
59
+ }
60
+ continuation.resume(returning: permissionsState)
61
+ }
62
+ }
63
+
64
+ public func getRemindersLists() -> [[String: String]] {
65
+ return convertEKCalendarsToDictionaries(calendars: Set(eventStore.calendars(for: .reminder)))
66
+ }
67
+
68
+ public func createReminder(title: String, listId: String?, priority: Int?, isCompleted: Bool?, startDate: Double?, dueDate: Double?, completionDate: Double?, notes: String?, url: String?, location: String?, frequency: Int?, interval: Int?, end: Double?) throws -> Void {
69
+ let newReminder = EKReminder(eventStore: eventStore)
70
+ newReminder.title = title
71
+ if let listId = listId, let list = eventStore.calendar(withIdentifier: listId) {
72
+ newReminder.calendar = list
73
+ } else {
74
+ newReminder.calendar = eventStore.defaultCalendarForNewReminders()
75
+ }
76
+ if let priority = priority {
77
+ if priority > 9 {
78
+ newReminder.priority = 9
79
+ } else if priority < 0 {
80
+ newReminder.priority = 0
81
+ } else {
82
+ newReminder.priority = priority
83
+ }
84
+ }
85
+ if let isCompleted = isCompleted {
86
+ newReminder.isCompleted = isCompleted
87
+ }
88
+ if let startDate = startDate {
89
+ newReminder.startDateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: Date(timeIntervalSince1970: startDate / 1000))
90
+ newReminder.startDateComponents?.timeZone = Calendar.current.timeZone
91
+ }
92
+ if let dueDate = dueDate {
93
+ newReminder.dueDateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: Date(timeIntervalSince1970: dueDate / 1000))
94
+ newReminder.dueDateComponents?.timeZone = Calendar.current.timeZone
95
+ }
96
+ if let completionDate = completionDate {
97
+ newReminder.completionDate = Date(timeIntervalSince1970: completionDate / 1000)
98
+ newReminder.timeZone = Calendar.current.timeZone
99
+ }
100
+ if let notes = notes {
101
+ newReminder.notes = notes
102
+ }
103
+ if let url = url {
104
+ newReminder.url = URL(string: url)
105
+ }
106
+ if let location = location {
107
+ newReminder.location = location
108
+ }
109
+ if let frequency = frequency, let interval = interval {
110
+ var endDate: EKRecurrenceEnd? = nil
111
+ if let end = end {
112
+ endDate = EKRecurrenceEnd(end: Date(timeIntervalSince1970: end / 1000))
113
+ }
114
+ newReminder.recurrenceRules = [EKRecurrenceRule(
115
+ recurrenceWith: recurrenceFrequencyMapping[frequency]!,
116
+ interval: interval,
117
+ end: endDate
118
+ )]
119
+ }
120
+
121
+ do {
122
+ try eventStore.save(newReminder, commit: true)
123
+ } catch {
124
+ throw CapacitorCalendarPluginError.unknownActionEventCreationPrompt
125
+ }
126
+ }
127
+
128
+ public func requestFullAccessToReminders() async throws -> String {
129
+ return try await withCheckedThrowingContinuation { continuation in
130
+ if #available(iOS 17.0, *) {
131
+ eventStore.requestFullAccessToReminders { granted, error in
132
+ if let error = error {
133
+ continuation.resume(throwing: CapacitorCalendarPluginError.eventStoreAuthorization)
134
+ return
135
+ }
136
+
137
+ var permissionState: String
138
+ if granted {
139
+ permissionState = PermissionState.granted.rawValue
140
+ } else {
141
+ permissionState = PermissionState.denied.rawValue
142
+ }
143
+ continuation.resume(returning: permissionState)
144
+ }
145
+ } else {
146
+ eventStore.requestAccess(to: .reminder) { granted, error in
147
+ if let error = error {
148
+ continuation.resume(throwing: CapacitorCalendarPluginError.eventStoreAuthorization)
149
+ return
150
+ }
151
+
152
+ var permissionState: String
153
+ if granted {
154
+ permissionState = PermissionState.granted.rawValue
155
+ } else {
156
+ permissionState = PermissionState.denied.rawValue
157
+ }
158
+ continuation.resume(returning: permissionState)
159
+ }
160
+ }
161
+ }
162
+ }
163
+
164
+ private func convertEKCalendarsToDictionaries(calendars: Set<EKCalendar>) -> [[String: String]] {
165
+ var result: [[String: String]] = []
166
+
167
+ for calendar in calendars {
168
+ let calendarDict: [String: String] = [
169
+ "id": calendar.calendarIdentifier,
170
+ "title": calendar.title
171
+ ]
172
+ result.append(calendarDict)
173
+ }
174
+
175
+ return result
176
+ }
177
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ebarooni/capacitor-calendar",
3
- "version": "0.3.1",
4
- "description": "The Capacitor Calendar Plugin exposes functionalities for interacting with the calendar of iOS and Android devices.",
3
+ "version": "0.5.0",
4
+ "description": "The Capacitor Calendar Plugin enables full calendar functionality on iOS and Android, with added reminder support for iOS devices.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
7
7
  "types": "dist/esm/index.d.ts",
@@ -26,7 +26,12 @@
26
26
  "capacitor",
27
27
  "plugin",
28
28
  "native",
29
- "calendar"
29
+ "calendar",
30
+ "reminders",
31
+ "eventkit",
32
+ "eventkitui",
33
+ "angular",
34
+ "ionic"
30
35
  ],
31
36
  "homepage": "https://capacitor-calendar.pages.dev",
32
37
  "scripts": {
@@ -40,7 +45,7 @@
40
45
  "prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
41
46
  "swiftlint": "node-swiftlint",
42
47
  "docgen": "docgen --api CapacitorCalendarPlugin --output-readme README.md --output-json dist/docs.json",
43
- "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.js",
48
+ "build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
44
49
  "clean": "rimraf ./dist",
45
50
  "watch": "tsc --watch",
46
51
  "prepublishOnly": "npm run build"
@@ -59,7 +64,7 @@
59
64
  "prettier": "~2.3.0",
60
65
  "prettier-plugin-java": "~1.0.2",
61
66
  "rimraf": "^3.0.2",
62
- "rollup": "^2.32.0",
67
+ "rollup": "^4.13.0",
63
68
  "swiftlint": "^1.0.1",
64
69
  "typescript": "~5.3.3"
65
70
  },
@@ -1,14 +0,0 @@
1
- //
2
- // Calendar.swift
3
- // Plugin
4
- //
5
- // Created by Ehsan Barooni on 01.03.24.
6
- // Copyright © 2024 Max Lynch. All rights reserved.
7
- //
8
-
9
- import Foundation
10
-
11
- public struct Calendar {
12
- let id: String
13
- let title: String
14
- }