@ebarooni/capacitor-calendar 0.1.0 → 0.3.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.
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sources":["esm/definitions.js","esm/index.js","esm/web.js"],"sourcesContent":["/**\n * Enum for the possible results of actions performed on a calendar event.\n * \"saved\" for completion of an operation. \"Canceled\" for a canceled process and \"error\"\n * for when something went wrong.\n *\n * @enum\n */\nexport var CalendarEventActionResult;\n(function (CalendarEventActionResult) {\n CalendarEventActionResult[\"Saved\"] = \"saved\";\n CalendarEventActionResult[\"Canceled\"] = \"canceled\";\n CalendarEventActionResult[\"Error\"] = \"error\";\n})(CalendarEventActionResult || (CalendarEventActionResult = {}));\n//# sourceMappingURL=definitions.js.map","import { registerPlugin } from '@capacitor/core';\nconst CapacitorCalendar = registerPlugin('CapacitorCalendar', {\n web: () => import('./web').then(m => new m.CapacitorCalendarWeb()),\n});\nexport * from './definitions';\nexport { CapacitorCalendar };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class CapacitorCalendarWeb extends WebPlugin {\n checkPermission(_options) {\n throw this.unimplemented(`${this.checkPermission.name} is not implemented on the web`);\n }\n checkAllPermissions() {\n throw this.unimplemented(`${this.checkAllPermissions.name} is not implemented on the web`);\n }\n requestPermission(_options) {\n throw this.unimplemented(`${this.requestPermission.name} is not implemented on the web`);\n }\n requestAllPermissions() {\n throw this.unimplemented(`${this.requestAllPermissions.name} is not implemented on the web`);\n }\n createEventWithPrompt() {\n throw this.unimplemented(`${this.createEventWithPrompt.name} is not implemented on the web`);\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["CalendarEventActionResult","registerPlugin","WebPlugin"],"mappings":";;;IAAA;IACA;IACA;IACA;IACA;IACA;IACA;AACWA,+CAA0B;IACrC,CAAC,UAAU,yBAAyB,EAAE;IACtC,IAAI,yBAAyB,CAAC,OAAO,CAAC,GAAG,OAAO,C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;;;;;;;;;"}
1
+ {"version":3,"file":"plugin.js","sources":["esm/definitions.js","esm/index.js","esm/web.js"],"sourcesContent":["/**\n * Enum representing the selection style for the calendar chooser.\n * @readonly\n * @enum\n */\nexport var CalendarChooserSelectionStyle;\n(function (CalendarChooserSelectionStyle) {\n /**\n * Allows only a single selection in the calendar chooser.\n */\n CalendarChooserSelectionStyle[CalendarChooserSelectionStyle[\"SINGLE\"] = 0] = \"SINGLE\";\n /**\n * Allows multiple selections in the calendar chooser.\n */\n CalendarChooserSelectionStyle[CalendarChooserSelectionStyle[\"MULTIPLE\"] = 1] = \"MULTIPLE\";\n})(CalendarChooserSelectionStyle || (CalendarChooserSelectionStyle = {}));\n/**\n * Enum representing the display styles for the calendar chooser.\n * @enum\n */\nexport var CalendarChooserDisplayStyle;\n(function (CalendarChooserDisplayStyle) {\n /**\n * Display all calendars available for selection.\n */\n CalendarChooserDisplayStyle[CalendarChooserDisplayStyle[\"ALL_CALENDARS\"] = 0] = \"ALL_CALENDARS\";\n /**\n * Display only writable calendars available for selection.\n */\n CalendarChooserDisplayStyle[CalendarChooserDisplayStyle[\"WRITABLE_CALENDARS_ONLY\"] = 1] = \"WRITABLE_CALENDARS_ONLY\";\n})(CalendarChooserDisplayStyle || (CalendarChooserDisplayStyle = {}));\n//# sourceMappingURL=definitions.js.map","import { registerPlugin } from '@capacitor/core';\nconst CapacitorCalendar = registerPlugin('CapacitorCalendar', {\n web: () => import('./web').then(m => new m.CapacitorCalendarWeb()),\n});\nexport * from './definitions';\nexport { CapacitorCalendar };\n//# sourceMappingURL=index.js.map","import { WebPlugin } from '@capacitor/core';\nexport class CapacitorCalendarWeb extends WebPlugin {\n checkPermission(_options) {\n throw this.unimplemented(`${this.checkPermission.name} is not implemented on the web`);\n }\n checkAllPermissions() {\n throw this.unimplemented(`${this.checkAllPermissions.name} is not implemented on the web`);\n }\n requestPermission(_options) {\n throw this.unimplemented(`${this.requestPermission.name} is not implemented on the web`);\n }\n requestAllPermissions() {\n throw this.unimplemented(`${this.requestAllPermissions.name} is not implemented on the web`);\n }\n createEventWithPrompt() {\n throw this.unimplemented(`${this.createEventWithPrompt.name} is not implemented on the web`);\n }\n selectCalendarsWithPrompt() {\n throw this.unimplemented(`${this.createEventWithPrompt.name} is not implemented on the web`);\n }\n listCalendars() {\n throw this.unimplemented(`${this.listCalendars.name} is not implemented on the web`);\n }\n getDefaultCalendar() {\n throw this.unimplemented(`${this.getDefaultCalendar.name} is not implemented on the web`);\n }\n}\n//# sourceMappingURL=web.js.map"],"names":["CalendarChooserSelectionStyle","CalendarChooserDisplayStyle","registerPlugin","WebPlugin"],"mappings":";;;IAAA;IACA;IACA;IACA;IACA;AACWA,mDAA8B;IACzC,CAAC,UAAU,6BAA6B,EAAE;IAC1C;IACA;IACA;IACA,IAAI,6BAA6B,CAAC,6BAA6B,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC;IAC1F;IACA;IACA;IACA,IAAI,6BAA6B,CAAC,6BAA6B,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,GAAG,UAAU,CAAC;IAC9F,CAAC,EAAEA,qCAA6B,KAAKA,qCAA6B,GAAG,EAAE,CAAC,CAAC,CAAC;IAC1E;IACA;IACA;IACA;AACWC,iDAA4B;IACvC,CAAC,UAAU,2BAA2B,EAAE;IACxC;IACA;IACA;IACA,IAAI,2BAA2B,CAAC,2BAA2B,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,GAAG,eAAe,CAAC;IACpG;IACA;IACA;IACA,IAAI,2BAA2B,CAAC,2BAA2B,CAAC,yBAAyB,CAAC,GAAG,CAAC,CAAC,GAAG,yBAAyB,CAAC;IACxH,CAAC,EAAEA,mCAA2B,KAAKA,mCAA2B,GAAG,EAAE,CAAC,CAAC;;AC7BhE,UAAC,iBAAiB,GAAGC,mBAAc,CAAC,mBAAmB,EAAE;IAC9D,IAAI,GAAG,EAAE,MAAM,mDAAe,CAAC,IAAI,CAAC,CAAC,IAAI,IA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kB,GAAG;IACzB,QAAQ,MAAM,IAAI,CAAC,aAAa,CAAC,CAAC,EAAE,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC,CAAC;IAClG,KAAK;IACL;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,14 @@
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
+ }
@@ -3,156 +3,176 @@ import Capacitor
3
3
  import EventKitUI
4
4
 
5
5
 
6
- public class CapacitorCalendar: NSObject, EKEventEditViewDelegate {
7
- private enum CalendarEventActionResult: String {
8
- case saved = "saved"
9
- case canceled = "canceled"
10
- case error = "error"
11
- }
12
-
13
- private enum PermissionState: String {
14
- case granted = "granted"
15
- case denied = "denied"
16
- case prompt = "prompt"
17
- case promptWithRationale = "prompt-with-rationale"
18
- }
19
-
6
+ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate, EKCalendarChooserDelegate {
20
7
  private let bridge: (any CAPBridgeProtocol)?
21
- private let store = EKEventStore()
22
- private var eventCall: CAPPluginCall?
8
+ private let eventStore: EKEventStore
9
+ private var currentCreateEventContinuation: CheckedContinuation<Bool, any Error>?
10
+ private var currentSelectCalendarsContinuation: CheckedContinuation<[[String : String]], any Error>?
23
11
 
24
- init(bridge: (any CAPBridgeProtocol)?) {
12
+ init(bridge: (any CAPBridgeProtocol)?, eventStore: EKEventStore) {
25
13
  self.bridge = bridge
14
+ self.eventStore = eventStore
26
15
  }
27
16
 
28
- public func createEventWithPrompt(_ call: CAPPluginCall) {
29
- guard let viewController = bridge?.viewController else {
30
- call.reject("[CapacitorCalendar.\(#function)] Unable to retrieve view controller")
31
- return
17
+ public func createEventWithPrompt() async throws -> Bool {
18
+ return try await withCheckedThrowingContinuation { continuation in
19
+ guard let viewController = bridge?.viewController else {
20
+ continuation.resume(throwing: CapacitorCalendarPluginError.viewControllerUnavailable)
21
+ return
22
+ }
23
+
24
+ Task { @MainActor in
25
+ let eventEditViewController = EKEventEditViewController()
26
+ eventEditViewController.eventStore = eventStore
27
+ viewController.present(eventEditViewController, animated: true, completion: nil)
28
+ eventEditViewController.editViewDelegate = self
29
+ currentCreateEventContinuation = continuation
30
+ }
32
31
  }
33
-
34
- eventCall = call
35
- let eventEditViewController = EKEventEditViewController()
36
- eventEditViewController.eventStore = store
37
- viewController.present(eventEditViewController, animated: true, completion: nil)
38
- eventEditViewController.editViewDelegate = self
39
32
  }
40
33
 
41
- public func checkPermission(_ call: CAPPluginCall) {
42
- guard let permissionName = call.getString("alias") else {
43
- call.reject("[CapacitorCalendar.\(#function)] Permission name is not defined")
44
- return
45
- }
46
-
47
- var permissionState: String
48
- switch EKEventStore.authorizationStatus(for: .event) {
49
- case .authorized, .fullAccess:
50
- permissionState = PermissionState.granted.rawValue
51
- case .denied, .restricted:
52
- permissionState = PermissionState.denied.rawValue
53
- case .notDetermined, .writeOnly:
54
- permissionState = PermissionState.prompt.rawValue
55
- @unknown default:
56
- call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permission")
57
- return
34
+ public func selectCalendarsWithPrompt(selectionStyle: Int, displayStyle: Int) async throws -> [[String: String]] {
35
+ return try await withCheckedThrowingContinuation { continuation in
36
+ guard let viewController = bridge?.viewController else {
37
+ continuation.resume(throwing: CapacitorCalendarPluginError.viewControllerUnavailable)
38
+ return
39
+ }
40
+
41
+ Task { @MainActor in
42
+ let calendarChooser = EKCalendarChooser(
43
+ selectionStyle: EKCalendarChooserSelectionStyle(rawValue: selectionStyle)!,
44
+ displayStyle: EKCalendarChooserDisplayStyle(rawValue: displayStyle)!,
45
+ eventStore: eventStore
46
+ )
47
+ calendarChooser.showsDoneButton = true
48
+ calendarChooser.showsCancelButton = true
49
+ viewController.present(
50
+ UINavigationController(rootViewController: calendarChooser),
51
+ animated: true,
52
+ completion: nil
53
+ )
54
+ calendarChooser.delegate = self
55
+ currentSelectCalendarsContinuation = continuation
56
+ }
58
57
  }
59
- call.resolve(["result": permissionState])
60
58
  }
61
59
 
62
- public func checkAllPermissions(_ call: CAPPluginCall) {
63
- switch EKEventStore.authorizationStatus(for: .event) {
64
- case .authorized, .fullAccess:
65
- call.resolve(["readCalendar": PermissionState.granted.rawValue])
66
- case .denied, .restricted:
67
- call.resolve(["readCalendar": PermissionState.denied.rawValue])
68
- case .notDetermined, .writeOnly:
69
- call.resolve(["readCalendar": PermissionState.prompt.rawValue])
70
- @unknown default:
71
- call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permissions")
72
- return
73
- }
60
+ public func listCalendars() -> [[String: String]] {
61
+ return convertEKCalendarsToDictionaries(calendars: Set(eventStore.calendars(for: .event)))
74
62
  }
75
63
 
76
- public func requestPermission(_ call: CAPPluginCall) {
77
- guard let permissionName = call.getString("alias") else {
78
- call.reject("[CapacitorCalendar.\(#function)] Permission name is not defined")
79
- return
80
- }
81
-
82
- switch permissionName {
83
- case "readCalendar":
84
- return requestReadAccessToEvents(call)
85
- default:
86
- call.reject("[CapacitorCalendar.\(#function)] Could not authorize \(permissionName)")
87
- return
64
+ public func getDefaultCalendar() throws -> [String: String] {
65
+ let defaultCalendar = eventStore.defaultCalendarForNewEvents
66
+ if (defaultCalendar != nil) {
67
+ return [
68
+ "id": defaultCalendar!.calendarIdentifier,
69
+ "title": defaultCalendar!.title
70
+ ]
71
+ } else {
72
+ throw CapacitorCalendarPluginError.noDefaultCalendar
88
73
  }
89
74
  }
90
75
 
91
- public func requestAllPermissions(_ call: CAPPluginCall) {
92
- return requestAllAccessToEvents(call)
93
-
76
+ public func checkAllPermissions() async throws -> [String: String] {
77
+ return try await withCheckedThrowingContinuation { continuation in
78
+ var permissionsState: [String: String]
79
+ switch EKEventStore.authorizationStatus(for: .event) {
80
+ case .authorized, .fullAccess:
81
+ permissionsState = [
82
+ "readCalendar": PermissionState.granted.rawValue,
83
+ "writeCalendar": PermissionState.granted.rawValue
84
+ ]
85
+ case .denied, .restricted:
86
+ permissionsState = [
87
+ "readCalendar": PermissionState.denied.rawValue,
88
+ "writeCalendar": PermissionState.denied.rawValue
89
+ ]
90
+ case .writeOnly:
91
+ permissionsState = [
92
+ "readCalendar": PermissionState.prompt.rawValue,
93
+ "writeCalendar": PermissionState.granted.rawValue
94
+ ]
95
+ case .notDetermined:
96
+ permissionsState = [
97
+ "readCalendar": PermissionState.prompt.rawValue,
98
+ "writeCalendar": PermissionState.prompt.rawValue
99
+ ]
100
+ @unknown default:
101
+ continuation.resume(throwing: CapacitorCalendarPluginError.unknownPermissionStatus)
102
+ return
103
+ }
104
+ continuation.resume(returning: permissionsState)
105
+ }
94
106
  }
95
107
 
96
- private func requestReadAccessToEvents(_ call: CAPPluginCall) {
97
- if #available(iOS 17.0, *) {
98
- store.requestFullAccessToEvents { granted, error in
99
- if let error = error {
100
- call.reject("[CapacitorCalendar.\(#function)] Could not authorize readCalendar")
101
- return
102
- }
103
- var permissionState: String
104
- if granted {
105
- permissionState = PermissionState.granted.rawValue
106
- } else {
107
- permissionState = PermissionState.denied.rawValue
108
- }
109
- call.resolve(["result": permissionState])
110
- }
111
- } else {
112
- store.requestAccess(to: .event) { granted, error in
113
- if let error = error {
114
- call.reject("[CapacitorCalendar.\(#function)] Could not authorize readCalendar")
115
- return
108
+ public func requestWriteAccessToEvents() async throws -> [String: String] {
109
+ return try await withCheckedThrowingContinuation { continuation in
110
+ if #available(iOS 17.0, *) {
111
+ eventStore.requestWriteOnlyAccessToEvents { granted, error in
112
+ if let error = error {
113
+ continuation.resume(throwing: CapacitorCalendarPluginError.eventStoreAuthorization)
114
+ return
115
+ }
116
+
117
+ var permissionState: String
118
+ if granted {
119
+ permissionState = PermissionState.granted.rawValue
120
+ } else {
121
+ permissionState = PermissionState.denied.rawValue
122
+ }
123
+ continuation.resume(returning: ["result": permissionState])
116
124
  }
117
- var permissionState: String
118
- if granted {
119
- permissionState = PermissionState.granted.rawValue
120
- } else {
121
- permissionState = PermissionState.denied.rawValue
125
+ } else {
126
+ eventStore.requestAccess(to: .event) { granted, error in
127
+ if let error = error {
128
+ continuation.resume(throwing: CapacitorCalendarPluginError.eventStoreAuthorization)
129
+ return
130
+ }
131
+
132
+ var permissionState: String
133
+ if granted {
134
+ permissionState = PermissionState.granted.rawValue
135
+ } else {
136
+ permissionState = PermissionState.denied.rawValue
137
+ }
138
+ continuation.resume(returning: ["result": permissionState])
122
139
  }
123
- call.resolve(["result": permissionState])
124
140
  }
125
141
  }
126
142
  }
127
143
 
128
- private func requestAllAccessToEvents(_ call: CAPPluginCall) {
129
- if #available(iOS 17.0, *) {
130
- store.requestFullAccessToEvents { granted, error in
131
- if let error = error {
132
- call.reject("[CapacitorCalendar.\(#function)] Could not authorize readCalendar and writeCalendar")
133
- return
134
- }
135
- var permissionState: String
136
- if granted {
137
- permissionState = PermissionState.granted.rawValue
138
- } else {
139
- permissionState = PermissionState.denied.rawValue
140
- }
141
- call.resolve(["readCalendar": permissionState])
142
- }
143
- } else {
144
- store.requestAccess(to: .event) { granted, error in
145
- if let error = error {
146
- call.reject("[CapacitorCalendar.\(#function)] Could not authorize readCalendar and writeCalendar")
147
- return
144
+ public func requestFullAccessToEvents() async throws -> String {
145
+ return try await withCheckedThrowingContinuation { continuation in
146
+ if #available(iOS 17.0, *) {
147
+ eventStore.requestFullAccessToEvents { granted, error in
148
+ if let error = error {
149
+ continuation.resume(throwing: CapacitorCalendarPluginError.eventStoreAuthorization)
150
+ return
151
+ }
152
+
153
+ var permissionState: String
154
+ if granted {
155
+ permissionState = PermissionState.granted.rawValue
156
+ } else {
157
+ permissionState = PermissionState.denied.rawValue
158
+ }
159
+ continuation.resume(returning: permissionState)
148
160
  }
149
- var permissionState: String
150
- if granted {
151
- permissionState = PermissionState.granted.rawValue
152
- } else {
153
- permissionState = PermissionState.denied.rawValue
161
+ } else {
162
+ eventStore.requestAccess(to: .event) { granted, error in
163
+ if let error = error {
164
+ continuation.resume(throwing: CapacitorCalendarPluginError.eventStoreAuthorization)
165
+ return
166
+ }
167
+
168
+ var permissionState: String
169
+ if granted {
170
+ permissionState = PermissionState.granted.rawValue
171
+ } else {
172
+ permissionState = PermissionState.denied.rawValue
173
+ }
174
+ continuation.resume(returning: permissionState)
154
175
  }
155
- call.resolve(["readCalendar": permissionState])
156
176
  }
157
177
  }
158
178
  }
@@ -162,20 +182,40 @@ public class CapacitorCalendar: NSObject, EKEventEditViewDelegate {
162
182
  didCompleteWith action: EKEventEditViewAction
163
183
  ) {
164
184
  controller.dismiss(animated: true) {
165
- print(action)
166
185
  if action == .saved {
167
- self.eventCall?.resolve([
168
- "result": CalendarEventActionResult.saved.rawValue
169
- ])
186
+ self.currentCreateEventContinuation?.resume(returning: true)
170
187
  } else if action == .canceled {
171
- self.eventCall?.resolve([
172
- "result": CalendarEventActionResult.canceled.rawValue
173
- ])
188
+ self.currentCreateEventContinuation?.resume(returning: false)
174
189
  } else {
175
- self.eventCall?.resolve([
176
- "result": CalendarEventActionResult.error.rawValue
177
- ])
190
+ self.currentCreateEventContinuation?.resume(throwing: CapacitorCalendarPluginError.unknownActionEventCreationPrompt)
178
191
  }
179
192
  }
180
193
  }
194
+
195
+ public func calendarChooserDidFinish(_ calendarChooser: EKCalendarChooser) {
196
+ let selectedCalendars = convertEKCalendarsToDictionaries(calendars: calendarChooser.selectedCalendars)
197
+ bridge?.viewController?.dismiss(animated: true) {
198
+ self.currentSelectCalendarsContinuation?.resume(returning: selectedCalendars)
199
+ }
200
+ }
201
+
202
+ public func calendarChooserDidCancel(_ calendarChooser: EKCalendarChooser) {
203
+ bridge?.viewController?.dismiss(animated: true) {
204
+ self.currentSelectCalendarsContinuation?.resume(throwing: CapacitorCalendarPluginError.canceledCalendarsSelectionPrompt)
205
+ }
206
+ }
207
+
208
+ private func convertEKCalendarsToDictionaries(calendars: Set<EKCalendar>) -> [[String: String]] {
209
+ var result: [[String: String]] = []
210
+
211
+ for calendar in calendars {
212
+ let calendarDict: [String: String] = [
213
+ "id": calendar.calendarIdentifier,
214
+ "title": calendar.title
215
+ ]
216
+ result.append(calendarDict)
217
+ }
218
+
219
+ return result
220
+ }
181
221
  }
@@ -9,4 +9,7 @@ CAP_PLUGIN(CapacitorCalendarPlugin, "CapacitorCalendar",
9
9
  CAP_PLUGIN_METHOD(checkAllPermissions, CAPPluginReturnPromise);
10
10
  CAP_PLUGIN_METHOD(requestPermission, CAPPluginReturnPromise);
11
11
  CAP_PLUGIN_METHOD(requestAllPermissions, CAPPluginReturnPromise);
12
+ CAP_PLUGIN_METHOD(selectCalendarsWithPrompt, CAPPluginReturnPromise);
13
+ CAP_PLUGIN_METHOD(listCalendars, CAPPluginReturnPromise);
14
+ CAP_PLUGIN_METHOD(getDefaultCalendar, CAPPluginReturnPromise);
12
15
  )
@@ -1,4 +1,5 @@
1
1
  import Foundation
2
+ import EventKit
2
3
  import Capacitor
3
4
 
4
5
  /**
@@ -7,27 +8,137 @@ import Capacitor
7
8
  */
8
9
  @objc(CapacitorCalendarPlugin)
9
10
  public class CapacitorCalendarPlugin: CAPPlugin {
10
- private lazy var implementation = CapacitorCalendar(bridge: self.bridge)
11
+ private let eventStore = EKEventStore()
12
+ private lazy var implementation = CapacitorCalendar(bridge: self.bridge, eventStore: self.eventStore)
11
13
 
12
14
  @objc public func checkPermission(_ call: CAPPluginCall) {
13
- return implementation.checkPermission(call)
15
+ guard let alias = call.getString("alias") else {
16
+ call.reject("[CapacitorCalendar.\(#function)] Permission name is not defined")
17
+ return
18
+ }
19
+
20
+ Task {
21
+ do {
22
+ let permissionsState = try await implementation.checkAllPermissions()
23
+
24
+ switch alias {
25
+ case "readCalendar":
26
+ call.resolve(["result": permissionsState["readCalendar"]!])
27
+ case "writeCalendar":
28
+ call.resolve(["result": permissionsState["writeCalendar"]!])
29
+ default:
30
+ call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permission")
31
+ return
32
+ }
33
+ } catch {
34
+ call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permission")
35
+ return
36
+ }
37
+ }
14
38
  }
15
39
 
16
40
  @objc public func checkAllPermissions(_ call: CAPPluginCall) {
17
- return implementation.checkAllPermissions(call)
41
+ Task {
42
+ do {
43
+ let permissionsState = try await implementation.checkAllPermissions()
44
+ call.resolve(permissionsState)
45
+ } catch {
46
+ call.reject("[CapacitorCalendar.\(#function)] Could not determine the status of the requested permissions")
47
+ return
48
+ }
49
+ }
18
50
  }
19
51
 
20
52
  @objc public func requestPermission(_ call: CAPPluginCall) {
21
- return implementation.requestPermission(call)
53
+ guard let alias = call.getString("alias") else {
54
+ call.reject("[CapacitorCalendar.\(#function)] Permission name is not defined")
55
+ return
56
+ }
57
+
58
+ Task {
59
+ do {
60
+ switch alias {
61
+ case "writeCalendar":
62
+ let result = try await implementation.requestWriteAccessToEvents()
63
+ call.resolve(result)
64
+ case "readCalendar":
65
+ let result = try await implementation.requestFullAccessToEvents()
66
+ call.resolve(["result": result])
67
+ default:
68
+ call.reject("[CapacitorCalendar.\(#function)] Could not authorize \(alias)")
69
+ return
70
+ }
71
+ } catch {
72
+ call.reject("[CapacitorCalendar.\(#function)] Could not authorize \(alias)")
73
+ return
74
+ }
75
+ }
22
76
  }
23
77
 
24
78
  @objc public func requestAllPermissions(_ call: CAPPluginCall) {
25
- return implementation.requestAllPermissions(call)
79
+ Task {
80
+ 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
+ ])
92
+ }
93
+ } catch {
94
+ call.reject("[CapacitorCalendar.\(#function)] Could not authorize all permissions")
95
+ return
96
+ }
97
+ }
26
98
  }
27
99
 
28
100
  @objc public func createEventWithPrompt(_ call: CAPPluginCall) {
29
- DispatchQueue.main.async {
30
- return self.implementation.createEventWithPrompt(call)
101
+ Task {
102
+ do {
103
+ let result = try await implementation.createEventWithPrompt()
104
+ call.resolve(["eventCreated": result])
105
+ } catch {
106
+ call.reject("[CapacitorCalendar.\(#function)] Unable to retrieve view controller")
107
+ return
108
+ }
109
+ }
110
+ }
111
+
112
+ @objc public func selectCalendarsWithPrompt(_ call: CAPPluginCall) {
113
+ guard let selectionStyle = call.getInt("selectionStyle") else {
114
+ call.reject("[CapacitorCalendar.\(#function)] Selection style was not provided")
115
+ return
116
+ }
117
+ guard let displayStyle = call.getInt("displayStyle") else {
118
+ call.reject("[CapacitorCalendar.\(#function)] Display style was not provided")
119
+ return
120
+ }
121
+
122
+ Task {
123
+ do {
124
+ let result = try await implementation.selectCalendarsWithPrompt(selectionStyle: selectionStyle, displayStyle: displayStyle)
125
+ call.resolve(["result": result])
126
+ } catch {
127
+ call.reject("[CapacitorCalendar.\(#function)] Calendars selection prompt got canceled")
128
+ return
129
+ }
130
+ }
131
+ }
132
+
133
+ @objc public func listCalendars(_ call: CAPPluginCall) {
134
+ call.resolve(["result": implementation.listCalendars()])
135
+ }
136
+
137
+ @objc public func getDefaultCalendar(_ call: CAPPluginCall) {
138
+ do {
139
+ try call.resolve(["result": implementation.getDefaultCalendar()])
140
+ } catch {
141
+ call.reject("[CapacitorCalendar.\(#function)] No default calendar was found")
31
142
  }
32
143
  }
33
144
  }
@@ -0,0 +1,18 @@
1
+ //
2
+ // PermissionError.swift
3
+ // Plugin
4
+ //
5
+ // Created by Ehsan Barooni on 27.02.24.
6
+ // Copyright © 2024 Max Lynch. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+
11
+ public enum CapacitorCalendarPluginError: Error {
12
+ case unknownPermissionStatus
13
+ case eventStoreAuthorization
14
+ case viewControllerUnavailable
15
+ case unknownActionEventCreationPrompt
16
+ case canceledCalendarsSelectionPrompt
17
+ case noDefaultCalendar
18
+ }
@@ -0,0 +1,16 @@
1
+ //
2
+ // PermissionState.swift
3
+ // Plugin
4
+ //
5
+ // Created by Ehsan Barooni on 28.02.24.
6
+ // Copyright © 2024 Max Lynch. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+
11
+ public enum PermissionState: String {
12
+ case granted = "granted"
13
+ case denied = "denied"
14
+ case prompt = "prompt"
15
+ case promptWithRationale = "prompt-with-rationale"
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ebarooni/capacitor-calendar",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "The Capacitor Calendar Plugin exposes functionalities for interacting with the calendar of iOS and Android devices.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -28,7 +28,7 @@
28
28
  "native",
29
29
  "calendar"
30
30
  ],
31
- "homepage": "https://github.com/ebarooni/capacitor-calendar",
31
+ "homepage": "https://capacitor-calendar.pages.dev",
32
32
  "scripts": {
33
33
  "verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
34
34
  "verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",