@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.
package/README.md CHANGED
@@ -7,24 +7,33 @@
7
7
  </em>
8
8
  </p>
9
9
  <p align="center">
10
- <a href="https://capacitor-calendar.netlify.app/"><strong>https://capacitor-calendar.netlify.app</strong></a>
10
+ <a href="https://capacitor-calendar.pages.dev/"><strong>https://capacitor-calendar.pages.dev</strong></a>
11
11
  <br>
12
12
  </p>
13
- <p align="center">
14
- <a href="https://app.netlify.com/sites/capacitor-calendar/deploys">
15
- <img src="https://api.netlify.com/api/v1/badges/0f1a6d09-740f-4f52-889c-38ff57d11380/deploy-status" alt="Netlify Status">
16
- </a>&nbsp;
17
- </p>
18
13
 
19
14
  ## Table of Contents
20
15
 
21
- - [Demo](#demo--click-for-details-)
22
- - [Upcoming Features](#upcoming-features)
23
- - [Support Range](#support-range)
24
16
  - [Install](#install)
17
+ - [Support Range](#support-range)
18
+ - [Demo](#demo--click-for-details-)
19
+ - [MVP](#mvp)
25
20
  - [Permissions](#permissions)
26
21
  - [API](#api)
27
22
 
23
+ ## Install
24
+
25
+ ```bash
26
+ npm install @ebarooni/capacitor-calendar
27
+ npx cap sync
28
+ ```
29
+
30
+ ## Support Range
31
+
32
+ | Platform | range |
33
+ |:----------:|:------------------------------------:|
34
+ | iOS | &ge; 13 |
35
+ | Android | &ge; 4 (SDK 14 – Ice Cream Sandwich) |
36
+
28
37
  ## [Demo (click for details)](./example/README.md)
29
38
 
30
39
 
@@ -35,26 +44,15 @@
35
44
  On iOS, `readCalendar` permission is not needed when you are creating an event using the native prompt.
36
45
  The video is just for showing the functionality, otherwise the `createEventWithPrompt` method works without the `readCalendar` authorization.
37
46
 
38
- ## Upcoming Features
39
-
40
- - [ ] Create calendar events without native prompts
41
- - [ ] Create reminders (iOS)
42
- - [ ] Find calendar events
43
- - [ ] Delete calendar events
44
-
45
- ## Support Range
47
+ ## MVP
46
48
 
47
- | Platform | range |
48
- |:----------:|:------------------------------------:|
49
- | iOS | &ge; 13 |
50
- | Android | &ge; 4 (SDK 14 – Ice Cream Sandwich) |
51
-
52
- ## Install
53
-
54
- ```bash
55
- npm install @ebarooni/capacitor-calendar
56
- npx cap sync
57
- ```
49
+ - ✅ Choose calendars with prompt (only supported on iOS)
50
+ - ✅ Get list of available calendars
51
+ - ✅ Get default calendar
52
+ - ⌛ Create calendar events without native prompts
53
+ - ⌛️ Create reminders (only supported on iOS)
54
+ - ⌛️ Find calendar events
55
+ - ⌛️ Delete calendar events
58
56
 
59
57
  ## Permissions
60
58
 
@@ -73,6 +71,9 @@ permissions can be found below:
73
71
  * [`requestPermission(...)`](#requestpermission)
74
72
  * [`requestAllPermissions()`](#requestallpermissions)
75
73
  * [`createEventWithPrompt()`](#createeventwithprompt)
74
+ * [`selectCalendarsWithPrompt(...)`](#selectcalendarswithprompt)
75
+ * [`listCalendars()`](#listcalendars)
76
+ * [`getDefaultCalendar()`](#getdefaultcalendar)
76
77
  * [Interfaces](#interfaces)
77
78
  * [Type Aliases](#type-aliases)
78
79
  * [Enums](#enums)
@@ -90,9 +91,9 @@ checkPermission(options: { alias: keyof CalendarPermissionStatus; }) => Promise<
90
91
 
91
92
  Checks the current authorization status of a specific permission.
92
93
 
93
- | Param | Type | Description |
94
- | ------------- | --------------------------------------- | ----------------------------------------- |
95
- | **`options`** | <code>{ alias: 'readCalendar'; }</code> | An object with the name of the permission |
94
+ | Param | Type | Description |
95
+ | ------------- | ----------------------------------------------------------------------------------------------- | ----------------------------------------- |
96
+ | **`options`** | <code>{ alias: keyof <a href="#calendarpermissionstatus">CalendarPermissionStatus</a>; }</code> | An object with the name of the permission |
96
97
 
97
98
  **Returns:** <code>Promise&lt;{ result: <a href="#permissionstate">PermissionState</a>; }&gt;</code>
98
99
 
@@ -121,9 +122,9 @@ requestPermission(options: { alias: keyof CalendarPermissionStatus; }) => Promis
121
122
  Requests authorization to a specific permission, if not already granted.
122
123
  If the permission is already granted, it will directly return the status.
123
124
 
124
- | Param | Type | Description |
125
- | ------------- | --------------------------------------- | ----------------------------------------- |
126
- | **`options`** | <code>{ alias: 'readCalendar'; }</code> | An object with the name of the permission |
125
+ | Param | Type | Description |
126
+ | ------------- | ----------------------------------------------------------------------------------------------- | ----------------------------------------- |
127
+ | **`options`** | <code>{ alias: keyof <a href="#calendarpermissionstatus">CalendarPermissionStatus</a>; }</code> | An object with the name of the permission |
127
128
 
128
129
  **Returns:** <code>Promise&lt;{ result: <a href="#permissionstate">PermissionState</a>; }&gt;</code>
129
130
 
@@ -146,7 +147,7 @@ Requests authorization to all the required permissions for the plugin, if they h
146
147
  ### createEventWithPrompt()
147
148
 
148
149
  ```typescript
149
- createEventWithPrompt() => Promise<{ result: CalendarEventActionResult; }>
150
+ createEventWithPrompt() => Promise<{ eventCreated: boolean; }>
150
151
  ```
151
152
 
152
153
  Creates an event in the calendar by using the native calendar.
@@ -154,7 +155,50 @@ On iOS opens a native sheet and on Android opens an intent.
154
155
  This method does not need any read or write authorization from the user on iOS. However, the entries in the Info.plist file are still needed.
155
156
  On Android, the user has to authorize for read access.
156
157
 
157
- **Returns:** <code>Promise&lt;{ result: <a href="#calendareventactionresult">CalendarEventActionResult</a>; }&gt;</code>
158
+ **Returns:** <code>Promise&lt;{ eventCreated: boolean; }&gt;</code>
159
+
160
+ --------------------
161
+
162
+
163
+ ### selectCalendarsWithPrompt(...)
164
+
165
+ ```typescript
166
+ selectCalendarsWithPrompt(options: { displayStyle: CalendarChooserDisplayStyle; selectionStyle: CalendarChooserSelectionStyle; }) => Promise<{ result: Calendar[]; }>
167
+ ```
168
+
169
+ Presents a prompt to the user to select calendars. This method is available only on iOS.
170
+
171
+ | Param | Type | Description |
172
+ | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
173
+ | **`options`** | <code>{ displayStyle: <a href="#calendarchooserdisplaystyle">CalendarChooserDisplayStyle</a>; selectionStyle: <a href="#calendarchooserselectionstyle">CalendarChooserSelectionStyle</a>; }</code> | - Options for customizing the display and selection styles of the calendar chooser. |
174
+
175
+ **Returns:** <code>Promise&lt;{ result: Calendar[]; }&gt;</code>
176
+
177
+ --------------------
178
+
179
+
180
+ ### listCalendars()
181
+
182
+ ```typescript
183
+ listCalendars() => Promise<{ result: Calendar[]; }>
184
+ ```
185
+
186
+ Retrieves a list of calendars available on the device.
187
+
188
+ **Returns:** <code>Promise&lt;{ result: Calendar[]; }&gt;</code>
189
+
190
+ --------------------
191
+
192
+
193
+ ### getDefaultCalendar()
194
+
195
+ ```typescript
196
+ getDefaultCalendar() => Promise<{ result: Calendar; }>
197
+ ```
198
+
199
+ Retrieves the default calendar set on the device.
200
+
201
+ **Returns:** <code>Promise&lt;{ result: <a href="#calendar">Calendar</a>; }&gt;</code>
158
202
 
159
203
  --------------------
160
204
 
@@ -166,9 +210,20 @@ On Android, the user has to authorize for read access.
166
210
 
167
211
  Describes the permission status for reading from the calendar.
168
212
 
169
- | Prop | Type |
170
- | ------------------ | ----------------------------------------------------------- |
171
- | **`readCalendar`** | <code><a href="#permissionstate">PermissionState</a></code> |
213
+ | Prop | Type |
214
+ | ------------------- | ----------------------------------------------------------- |
215
+ | **`readCalendar`** | <code><a href="#permissionstate">PermissionState</a></code> |
216
+ | **`writeCalendar`** | <code><a href="#permissionstate">PermissionState</a></code> |
217
+
218
+
219
+ #### Calendar
220
+
221
+ Represents a calendar object with an ID and title.
222
+
223
+ | Prop | Type |
224
+ | ----------- | ------------------- |
225
+ | **`id`** | <code>string</code> |
226
+ | **`title`** | <code>string</code> |
172
227
 
173
228
 
174
229
  ### Type Aliases
@@ -182,12 +237,19 @@ Describes the permission status for reading from the calendar.
182
237
  ### Enums
183
238
 
184
239
 
185
- #### CalendarEventActionResult
240
+ #### CalendarChooserDisplayStyle
241
+
242
+ | Members | Description |
243
+ | ----------------------------- | -------------------------------------------------------- |
244
+ | **`ALL_CALENDARS`** | Display all calendars available for selection. |
245
+ | **`WRITABLE_CALENDARS_ONLY`** | Display only writable calendars available for selection. |
246
+
247
+
248
+ #### CalendarChooserSelectionStyle
186
249
 
187
- | Members | Value |
188
- | -------------- | ----------------------- |
189
- | **`Saved`** | <code>'saved'</code> |
190
- | **`Canceled`** | <code>'canceled'</code> |
191
- | **`Error`** | <code>'error'</code> |
250
+ | Members | Description |
251
+ | -------------- | ------------------------------------------------------- |
252
+ | **`SINGLE`** | Allows only a single selection in the calendar chooser. |
253
+ | **`MULTIPLE`** | Allows multiple selections in the calendar chooser. |
192
254
 
193
255
  </docgen-api>
@@ -0,0 +1,91 @@
1
+ package dev.barooni.capacitor.calendar
2
+
3
+ import android.content.ContentResolver
4
+ import android.content.Context
5
+ import android.provider.CalendarContract
6
+ import com.getcapacitor.JSArray
7
+ import com.getcapacitor.JSObject
8
+
9
+ class CapacitorCalendar() {
10
+ var eventsCount: Int = 0
11
+
12
+ @Throws(Exception::class)
13
+ fun getTotalEventsCount(context: Context): Int {
14
+ val contentResolver: ContentResolver = context.contentResolver
15
+ val uri = CalendarContract.Events.CONTENT_URI
16
+ val projection = arrayOf(CalendarContract.Events._ID)
17
+ val cursor = contentResolver.query(uri, projection, null, null, null)
18
+ val count = cursor?.count ?: 0
19
+ cursor?.close()
20
+ return count
21
+ }
22
+
23
+ @Throws(Exception::class)
24
+ fun listCalendars(context: Context): JSArray {
25
+ val projection = arrayOf(
26
+ CalendarContract.Calendars._ID,
27
+ CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
28
+ )
29
+
30
+ val calendars = JSArray()
31
+
32
+ context.contentResolver.query(
33
+ CalendarContract.Calendars.CONTENT_URI,
34
+ projection,
35
+ null,
36
+ null,
37
+ null
38
+ )?.use { cursor ->
39
+ val idColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars._ID)
40
+ val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
41
+
42
+ while (cursor.moveToNext()) {
43
+ val id = cursor.getLong(idColumnIndex)
44
+ val title = cursor.getString(nameColumnIndex)
45
+ val calendar = JSObject().apply {
46
+ put("id", id)
47
+ put("title", title)
48
+ }
49
+
50
+ calendars.put(calendar)
51
+ }
52
+ } ?: throw Exception("Cursor is null")
53
+
54
+ return calendars
55
+ }
56
+
57
+ @Throws(Exception::class)
58
+ fun getDefaultCalendar(context: Context): JSObject {
59
+ val projection = arrayOf(
60
+ CalendarContract.Calendars._ID,
61
+ CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
62
+ )
63
+
64
+ val selection = "${CalendarContract.Calendars.IS_PRIMARY} = ?"
65
+ val selectionArgs = arrayOf("1")
66
+
67
+ context.contentResolver.query(
68
+ CalendarContract.Calendars.CONTENT_URI,
69
+ projection,
70
+ selection,
71
+ selectionArgs,
72
+ null
73
+ )?.use { cursor ->
74
+ if (cursor.moveToFirst()) {
75
+ val idColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars._ID)
76
+ val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
77
+ val id = cursor.getLong(idColumnIndex)
78
+ val title = cursor.getString(nameColumnIndex)
79
+
80
+ val calendarObject = JSObject().apply {
81
+ put("id", id)
82
+ put("title", title)
83
+ }
84
+ return calendarObject
85
+ } else {
86
+ throw Exception("No primary calendar found")
87
+ }
88
+ }
89
+ throw Exception("No primary calendar found")
90
+ }
91
+ }
@@ -1,13 +1,10 @@
1
1
  package dev.barooni.capacitor.calendar
2
2
 
3
3
  import android.Manifest
4
- import android.content.ContentResolver
5
- import android.content.Context
6
4
  import android.content.Intent
7
5
  import android.provider.CalendarContract
8
6
  import androidx.activity.result.ActivityResult
9
7
  import com.getcapacitor.JSObject
10
- import com.getcapacitor.PermissionState
11
8
  import com.getcapacitor.Plugin
12
9
  import com.getcapacitor.PluginCall
13
10
  import com.getcapacitor.PluginMethod
@@ -25,45 +22,64 @@ import com.getcapacitor.annotation.PermissionCallback
25
22
  strings = [
26
23
  Manifest.permission.READ_CALENDAR
27
24
  ]
25
+ ),
26
+ Permission(
27
+ alias = "writeCalendar",
28
+ strings = [
29
+ Manifest.permission.WRITE_CALENDAR
30
+ ]
28
31
  )
29
32
  ]
30
33
  )
31
- class CapacitorCalendarPlugin : Plugin() {
32
- private enum class CalendarEventActionResult(val value: String) {
33
- SAVED("saved"),
34
- CANCELED("canceled"),
35
- ERROR("error")
36
- }
37
-
38
- private var totalNumberOfEvents: Int = 0
34
+ class CapacitorCalendarPlugin: Plugin() {
35
+ private var implementation = CapacitorCalendar()
39
36
 
40
37
  @PluginMethod
41
38
  fun createEventWithPrompt(call: PluginCall) {
42
39
  try {
43
- if (getPermissionState("readCalendar") != PermissionState.GRANTED) {
44
- throw Exception("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Missing readCalendar permission")
45
- } else {
46
- totalNumberOfEvents = getTotalNumberOfEvents(context)
47
- openCalendarIntent(call)
48
- }
40
+ implementation.eventsCount = implementation.getTotalEventsCount(context)
41
+ return startActivityForResult(
42
+ call,
43
+ Intent(Intent.ACTION_INSERT).setData(CalendarContract.Events.CONTENT_URI),
44
+ "openCalendarIntentActivityCallback"
45
+ )
49
46
  } catch (error: Exception) {
50
- call.reject(error.message)
47
+ call.reject("", "[CapacitorCalendar.${::openCalendarIntentActivityCallback.name}] Could not create the event")
51
48
  return
52
49
  }
53
50
  }
54
51
 
52
+ @ActivityCallback
53
+ private fun openCalendarIntentActivityCallback(call: PluginCall?, result: ActivityResult) {
54
+ if (call == null) {
55
+ throw Exception("[CapacitorCalendar.${::createEventWithPrompt.name}] Call is not defined")
56
+ }
57
+ val currentEventsCount: Int = implementation.getTotalEventsCount(context)
58
+ val createEventResult = if (currentEventsCount > implementation.eventsCount) {
59
+ true
60
+ } else if (implementation.eventsCount == currentEventsCount) {
61
+ false
62
+ } else {
63
+ call.reject("", "[CapacitorCalendar.${::openCalendarIntentActivityCallback.name}] Could not create the event")
64
+ return
65
+ }
66
+ val ret = JSObject()
67
+ ret.put("eventCreated", createEventResult)
68
+ call.resolve(ret)
69
+ }
70
+
55
71
  @PluginMethod
56
72
  fun checkPermission(call: PluginCall) {
57
73
  try {
58
74
  val permissionName = call.getString("alias")
59
- ?: throw Exception("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Permission name is not defined")
60
- val state = getPermissionState(permissionName)
61
- ?: throw Exception("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Could not determine the status of the requested permission")
75
+ ?: throw Exception("[CapacitorCalendar.${::checkPermission.name}] Permission name is not defined")
76
+ val permissionState = getPermissionState(permissionName)
77
+ ?: throw Exception("[CapacitorCalendar.${::checkPermission.name}] Could not determine the status of the requested permission")
62
78
  val ret = JSObject()
63
- ret.put("result", state)
79
+ ret.put("result", permissionState)
64
80
  call.resolve(ret)
65
81
  } catch (error: Exception) {
66
- call.reject(error.message)
82
+ call.reject("", error.message)
67
83
  return
68
84
  }
69
85
  }
@@ -71,9 +87,9 @@ class CapacitorCalendarPlugin : Plugin() {
71
87
  @PluginMethod
72
88
  fun checkAllPermissions(call: PluginCall) {
73
89
  try {
74
- checkPermissions(call)
90
+ return checkPermissions(call)
75
91
  } catch (_: Exception) {
76
- call.reject("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Could not determine the status of the requested permissions")
92
+ call.reject("", "[CapacitorCalendar.${::checkAllPermissions.name}] Could not determine the status of the requested permissions")
77
93
  return
78
94
  }
79
95
  }
@@ -81,79 +97,68 @@ class CapacitorCalendarPlugin : Plugin() {
81
97
  @PluginMethod
82
98
  fun requestPermission(call: PluginCall) {
83
99
  try {
84
- val permissionName = call.getString("alias")
85
- ?: throw Exception("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Permission name is not defined")
86
- requestPermissionForAlias(
87
- permissionName,
100
+ val alias = call.getString("alias")
101
+ ?: throw Exception("[CapacitorCalendar.${::requestPermission.name}] Permission name is not defined")
102
+ return requestPermissionForAlias(
103
+ alias,
88
104
  call,
89
- "permissionsCallback"
105
+ "requestPermissionCallback"
90
106
  )
91
107
  } catch (error: Exception) {
92
- call.reject(error.message)
108
+ call.reject("", error.message)
93
109
  return
94
110
  }
95
111
  }
96
112
 
113
+ @PermissionCallback
114
+ private fun requestPermissionCallback(call: PluginCall) {
115
+ val permissionName = call.getString("alias")
116
+ try {
117
+ val ret = JSObject()
118
+ ret.put("result", getPermissionState(permissionName))
119
+ call.resolve(ret)
120
+ } catch (_: Exception) {
121
+ throw Exception("${::requestPermissionCallback.name} Could not authorize $permissionName")
122
+ }
123
+ }
124
+
97
125
  @PluginMethod
98
126
  fun requestAllPermissions(call: PluginCall) {
99
127
  try {
100
- requestPermissions(call)
128
+ return requestPermissions(call)
101
129
  } catch (_: Exception) {
102
- call.reject("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Could not request permissions")
130
+ call.reject("", "[CapacitorCalendar.requestAllPermissions] Could not request permissions")
103
131
  return
104
132
  }
105
133
  }
106
134
 
107
- private fun openCalendarIntent(call: PluginCall) {
108
- val intent = Intent(Intent.ACTION_INSERT).setData(CalendarContract.Events.CONTENT_URI)
109
- startActivityForResult(call, intent, "openCalendarIntentActivityCallback")
110
- }
111
-
112
- private fun getTotalNumberOfEvents(context: Context): Int {
113
- val contentResolver: ContentResolver = context.contentResolver
114
- val uri = CalendarContract.Events.CONTENT_URI
115
- val projection = arrayOf(CalendarContract.Events._ID)
116
- val cursor = contentResolver.query(uri, projection, null, null, null)
117
- return if (cursor == null) {
118
- 0
119
- } else {
120
- try {
121
- cursor.count
122
- } catch (_: Exception) {
123
- throw Exception("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Failed to get the total events count")
124
- } finally {
125
- cursor.close()
126
- }
127
- }
135
+ @PluginMethod
136
+ fun selectCalendarsWithPrompt(call: PluginCall) {
137
+ call.unimplemented("[CapacitorCalendar.${::selectCalendarsWithPrompt.name}] Not implemented on Android")
138
+ return
128
139
  }
129
140
 
130
- @ActivityCallback
131
- private fun openCalendarIntentActivityCallback(call: PluginCall?, result: ActivityResult) {
132
- if (call == null) {
133
- throw Exception("[CapacitorCalendar.${Thread.currentThread().stackTrace[1].methodName}] Call is not defined")
134
- }
135
- val ret = JSObject()
136
- val currentEventsCount: Int = getTotalNumberOfEvents(context)
137
- val action: String = if (currentEventsCount > totalNumberOfEvents) {
138
- CalendarEventActionResult.SAVED.value
139
- } else if (totalNumberOfEvents == currentEventsCount) {
140
- CalendarEventActionResult.CANCELED.value
141
- } else {
142
- CalendarEventActionResult.ERROR.value
141
+ @PluginMethod
142
+ fun listCalendars(call: PluginCall) {
143
+ try {
144
+ val calendars = implementation.listCalendars(context)
145
+ val ret = JSObject()
146
+ ret.put("result", calendars)
147
+ call.resolve(ret)
148
+ } catch (_: Exception) {
149
+ call.reject("", "[CapacitorCalendar.${::listCalendars.name}] Failed to get the list of calendars")
143
150
  }
144
- ret.put("result", action)
145
- call.resolve(ret)
146
151
  }
147
152
 
148
- @PermissionCallback
149
- private fun permissionsCallback(call: PluginCall) {
150
- val permissionName = call.getString("alias")
153
+ @PluginMethod
154
+ fun getDefaultCalendar(call: PluginCall) {
151
155
  try {
156
+ val primaryCalendar = implementation.getDefaultCalendar(context)
152
157
  val ret = JSObject()
153
- ret.put("result", getPermissionState(permissionName))
158
+ ret.put("result", primaryCalendar)
154
159
  call.resolve(ret)
155
160
  } catch (_: Exception) {
156
- throw Exception("${Thread.currentThread().stackTrace[1].methodName} Could not authorize $permissionName")
161
+ call.reject("", "[CapacitorCalendar.${::getDefaultCalendar.name}] No default calendar found")
157
162
  }
158
163
  }
159
164
  }