@ebarooni/capacitor-calendar 0.1.0 → 0.2.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,4 +1,5 @@
1
1
  import Foundation
2
+ import EventKit
2
3
  import Capacitor
3
4
 
4
5
  /**
@@ -7,27 +8,125 @@ 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
+ }
31
130
  }
32
131
  }
33
132
  }
@@ -0,0 +1,17 @@
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
+ }
@@ -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.2.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 ..",