@ebarooni/capacitor-calendar 0.8.1 → 0.10.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
@@ -17,6 +17,7 @@
17
17
  - [Demo](#demo--click-for-details-)
18
18
  - [MVP](#mvp)
19
19
  - [Permissions](#permissions)
20
+ - [Contributions](#contributions)
20
21
  - [API](#api)
21
22
 
22
23
  ## Install
@@ -53,8 +54,7 @@ The video is just for showing the functionality, otherwise the `createEventWithP
53
54
  - ✅️ Create reminders (iOS)
54
55
  - ✅ Open calendar app at a specific date
55
56
  - ✅ Open reminders app (iOS)
56
- - ⌛️ Open reminders app at a specific list using id (iOS)
57
- - ⌛️ Find calendar events
57
+ - ✅️ Find calendar events
58
58
  - ⌛️ Delete calendar events
59
59
 
60
60
  ## Permissions
@@ -65,6 +65,17 @@ permissions can be found below:
65
65
  - [iOS](./ios/PERMISSIONS.md)
66
66
  - [Android](./android/PERMISSIONS.md)
67
67
 
68
+ ## Contributions
69
+
70
+ Thank you for your interest in contributing to the project! At the moment, the focus is on reaching the first major
71
+ release. Until then, the contributions will not be accepted. This approach allows to set a solid
72
+ foundation and maintain consistency throughout the development process.
73
+
74
+ Community input is highly valued, and you are encouraged to engage with the project by providing feedback and suggestions.
75
+ Feel free to open issues for bugs you've discovered or enhancements you'd like to see.
76
+
77
+ Stay tuned for updates. Looking forward to collaborating with you in the future once contributions are opened up in the future!
78
+
68
79
  ## API
69
80
 
70
81
  <docgen-index>
@@ -83,6 +94,7 @@ permissions can be found below:
83
94
  * [`createReminder(...)`](#createreminder)
84
95
  * [`openCalendar(...)`](#opencalendar)
85
96
  * [`openReminders()`](#openreminders)
97
+ * [`listEventsInRange(...)`](#listeventsinrange)
86
98
  * [Interfaces](#interfaces)
87
99
  * [Type Aliases](#type-aliases)
88
100
  * [Enums](#enums)
@@ -156,15 +168,13 @@ Requests authorization to all the required permissions for the plugin, if they h
156
168
  ### createEventWithPrompt()
157
169
 
158
170
  ```typescript
159
- createEventWithPrompt() => Promise<{ eventCreated: boolean; }>
171
+ createEventWithPrompt() => Promise<{ result: string[]; }>
160
172
  ```
161
173
 
162
174
  Creates an event in the calendar by using the native calendar.
163
175
  On iOS opens a native sheet and on Android opens an intent.
164
- 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.
165
- On Android, the user has to authorize for read access.
166
176
 
167
- **Returns:** <code>Promise&lt;{ eventCreated: boolean; }&gt;</code>
177
+ **Returns:** <code>Promise&lt;{ result: string[]; }&gt;</code>
168
178
 
169
179
  --------------------
170
180
 
@@ -215,7 +225,7 @@ Retrieves the default calendar set on the device.
215
225
  ### createEvent(...)
216
226
 
217
227
  ```typescript
218
- createEvent(options: { title: string; calendarId?: string; location?: string; startDate?: number; endDate?: number; isAllDay?: boolean; }) => Promise<{ eventCreated: boolean; }>
228
+ createEvent(options: { title: string; calendarId?: string; location?: string; startDate?: number; endDate?: number; isAllDay?: boolean; }) => Promise<{ result: string[]; }>
219
229
  ```
220
230
 
221
231
  Creates an event with the provided options.
@@ -224,7 +234,7 @@ Creates an event with the provided options.
224
234
  | ------------- | --------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- |
225
235
  | **`options`** | <code>{ title: string; calendarId?: string; location?: string; startDate?: number; endDate?: number; isAllDay?: boolean; }</code> | - Options for creating the event. |
226
236
 
227
- **Returns:** <code>Promise&lt;{ eventCreated: boolean; }&gt;</code>
237
+ **Returns:** <code>Promise&lt;{ result: string[]; }&gt;</code>
228
238
 
229
239
  --------------------
230
240
 
@@ -299,6 +309,23 @@ Opens the reminders app. Since the user leaves your app, use this method with ca
299
309
  --------------------
300
310
 
301
311
 
312
+ ### listEventsInRange(...)
313
+
314
+ ```typescript
315
+ listEventsInRange(options: { startDate: number; endDate: number; }) => Promise<{ result: CalendarEvent[]; }>
316
+ ```
317
+
318
+ Retrieves the list of calendar events present in the given date range.
319
+
320
+ | Param | Type | Description |
321
+ | ------------- | ---------------------------------------------------- | ------------------------------------ |
322
+ | **`options`** | <code>{ startDate: number; endDate: number; }</code> | Options for defining the date range. |
323
+
324
+ **Returns:** <code>Promise&lt;{ result: CalendarEvent[]; }&gt;</code>
325
+
326
+ --------------------
327
+
328
+
302
329
  ### Interfaces
303
330
 
304
331
 
@@ -327,6 +354,27 @@ Represents a calendar object.
327
354
  | **`end`** | <code>number</code> | When provided, the reminder will stop repeating at the given time. |
328
355
 
329
356
 
357
+ #### CalendarEvent
358
+
359
+ Represents an event in the calendar.
360
+
361
+ | Prop | Type |
362
+ | ---------------------- | -------------------- |
363
+ | **`id`** | <code>string</code> |
364
+ | **`title`** | <code>string</code> |
365
+ | **`location`** | <code>string</code> |
366
+ | **`eventColor`** | <code>string</code> |
367
+ | **`organizer`** | <code>string</code> |
368
+ | **`description`** | <code>string</code> |
369
+ | **`startDate`** | <code>number</code> |
370
+ | **`endDate`** | <code>number</code> |
371
+ | **`eventTimezone`** | <code>string</code> |
372
+ | **`eventEndTimezone`** | <code>string</code> |
373
+ | **`duration`** | <code>string</code> |
374
+ | **`isAllDay`** | <code>boolean</code> |
375
+ | **`calendarId`** | <code>string</code> |
376
+
377
+
330
378
  ### Type Aliases
331
379
 
332
380
 
@@ -1,6 +1,5 @@
1
1
  package dev.barooni.capacitor.calendar
2
2
 
3
- import android.content.ContentResolver
4
3
  import android.content.ContentValues
5
4
  import android.content.Context
6
5
  import android.content.Intent
@@ -12,17 +11,27 @@ import java.util.Calendar
12
11
  import java.util.TimeZone
13
12
 
14
13
  class CapacitorCalendar() {
15
- var eventsCount: Int = 0
14
+ var eventIdsArray: List<Long> = emptyList()
16
15
 
17
16
  @Throws(Exception::class)
18
- fun getTotalEventsCount(context: Context): Int {
19
- val contentResolver: ContentResolver = context.contentResolver
20
- val uri = CalendarContract.Events.CONTENT_URI
17
+ fun fetchCalendarEventIDs(context: Context): List<Long> {
21
18
  val projection = arrayOf(CalendarContract.Events._ID)
22
- val cursor = contentResolver.query(uri, projection, null, null, null)
23
- val count = cursor?.count ?: 0
24
- cursor?.close()
25
- return count
19
+ val uri = CalendarContract.Events.CONTENT_URI
20
+ val cursor = context.contentResolver.query(uri, projection, null, null, null)
21
+ val eventIds = mutableListOf<Long>()
22
+
23
+ cursor?.use {
24
+ while (it.moveToNext()) {
25
+ val eventId = it.getLong(0)
26
+ eventIds.add(eventId)
27
+ }
28
+ }
29
+ return eventIds
30
+ }
31
+
32
+ @Throws(Exception::class)
33
+ fun getNewEventIds(newIds: List<Long>): List<Long> {
34
+ return newIds.filterNot { it in eventIdsArray }
26
35
  }
27
36
 
28
37
  @Throws(Exception::class)
@@ -87,7 +96,7 @@ class CapacitorCalendar() {
87
96
 
88
97
  val calendarObject =
89
98
  JSObject().apply {
90
- put("id", id)
99
+ put("id", id.toString())
91
100
  put("title", title)
92
101
  }
93
102
  return calendarObject
@@ -131,4 +140,89 @@ class CapacitorCalendar() {
131
140
  data = Uri.parse("content://com.android.calendar/time/$timestamp")
132
141
  }
133
142
  }
143
+
144
+ @Throws(Exception::class)
145
+ fun listEventsInRange(
146
+ context: Context,
147
+ startDate: Long,
148
+ endDate: Long,
149
+ ): JSArray {
150
+ val projection =
151
+ arrayOf(
152
+ CalendarContract.Events._ID,
153
+ CalendarContract.Events.TITLE,
154
+ CalendarContract.Events.EVENT_LOCATION,
155
+ CalendarContract.Events.EVENT_COLOR,
156
+ CalendarContract.Events.ORGANIZER,
157
+ CalendarContract.Events.DESCRIPTION,
158
+ CalendarContract.Events.DTSTART,
159
+ CalendarContract.Events.DTEND,
160
+ CalendarContract.Events.EVENT_TIMEZONE,
161
+ CalendarContract.Events.EVENT_END_TIMEZONE,
162
+ CalendarContract.Events.DURATION,
163
+ CalendarContract.Events.ALL_DAY,
164
+ CalendarContract.Events.CALENDAR_ID,
165
+ )
166
+ val selection = "(${CalendarContract.Events.DTSTART} >= ?) AND (${CalendarContract.Events.DTEND} <= ?)"
167
+ val selectionArgs = arrayOf(startDate.toString(), endDate.toString())
168
+
169
+ val events = JSArray()
170
+
171
+ context.contentResolver.query(
172
+ CalendarContract.Events.CONTENT_URI,
173
+ projection,
174
+ selection,
175
+ selectionArgs,
176
+ null,
177
+ )?.use { cursor ->
178
+ val idColumnIndex = cursor.getColumnIndex(CalendarContract.Events._ID)
179
+ val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Events.TITLE)
180
+ val locationColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_LOCATION)
181
+ val eventColorColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_COLOR)
182
+ val organizerColumnIndex = cursor.getColumnIndex(CalendarContract.Events.ORGANIZER)
183
+ val descriptionColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)
184
+ val dtStartColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DTSTART)
185
+ val dtEndColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DTEND)
186
+ val eventTimezoneColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_TIMEZONE)
187
+ val eventEndTimezoneColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_END_TIMEZONE)
188
+ val durationColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DURATION)
189
+ val isAllDayColumnIndex = cursor.getColumnIndex(CalendarContract.Events.ALL_DAY)
190
+ val calendarIdColumnIndex = cursor.getColumnIndex(CalendarContract.Events.CALENDAR_ID)
191
+
192
+ while (cursor.moveToNext()) {
193
+ val id = cursor.getLong(idColumnIndex)
194
+ val title = cursor.getString(nameColumnIndex)
195
+ val location = cursor.getString(locationColumnIndex)
196
+ val eventColor = cursor.getInt(eventColorColumnIndex)
197
+ val organizer = cursor.getString(organizerColumnIndex)
198
+ val desc = cursor.getString(descriptionColumnIndex)
199
+ val dtStart = cursor.getLong(dtStartColumnIndex)
200
+ val dtEnd = cursor.getLong(dtEndColumnIndex)
201
+ val eventTimezone = cursor.getString(eventTimezoneColumnIndex)
202
+ val eventEndTimezone = cursor.getString(eventEndTimezoneColumnIndex)
203
+ val duration = cursor.getString(durationColumnIndex)
204
+ val allDay = cursor.getInt(isAllDayColumnIndex) == 1
205
+ val calendarId = cursor.getLong(calendarIdColumnIndex)
206
+
207
+ val event =
208
+ JSObject().apply {
209
+ put("id", id.toString())
210
+ title?.takeIf { it.isNotEmpty() }?.let { put("title", it) }
211
+ location?.takeIf { it.isNotEmpty() }?.let { put("location", it) }
212
+ eventColor.takeIf { it != 0 }?.let { put("eventColor", String.format("#%06X", 0xFFFFFF and it)) }
213
+ organizer?.takeIf { it.isNotEmpty() }?.let { put("organizer", it) }
214
+ desc?.takeIf { it.isNotEmpty() }?.let { put("description", it) }
215
+ dtStart.takeIf { it != 0.toLong() }?.let { put("startDate", it) }
216
+ dtEnd.takeIf { it != 0.toLong() }?.let { put("endDate", it) }
217
+ eventTimezone?.takeIf { it.isNotEmpty() }?.let { put("eventTimezone", it) }
218
+ eventEndTimezone?.takeIf { it.isNotEmpty() }?.let { put("eventEndTimezone", it) }
219
+ duration?.takeIf { it.isNotEmpty() }?.let { put("duration", it) }
220
+ put("isAllDay", allDay)
221
+ calendarId.takeIf { it != 0.toLong() }?.let { put("calendarId", it.toString()) }
222
+ }
223
+ events.put(event)
224
+ }
225
+ } ?: throw Exception("Cursor is null")
226
+ return events
227
+ }
134
228
  }
@@ -4,6 +4,7 @@ import android.Manifest
4
4
  import android.content.Intent
5
5
  import android.provider.CalendarContract
6
6
  import androidx.activity.result.ActivityResult
7
+ import com.getcapacitor.JSArray
7
8
  import com.getcapacitor.JSObject
8
9
  import com.getcapacitor.Plugin
9
10
  import com.getcapacitor.PluginCall
@@ -33,10 +34,10 @@ import com.getcapacitor.annotation.PermissionCallback
33
34
  class CapacitorCalendarPlugin : Plugin() {
34
35
  private var implementation = CapacitorCalendar()
35
36
 
36
- @PluginMethod
37
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
37
38
  fun createEventWithPrompt(call: PluginCall) {
38
39
  try {
39
- implementation.eventsCount = implementation.getTotalEventsCount(context)
40
+ implementation.eventIdsArray = implementation.fetchCalendarEventIDs(context)
40
41
  return startActivityForResult(
41
42
  call,
42
43
  Intent(Intent.ACTION_INSERT).setData(CalendarContract.Events.CONTENT_URI),
@@ -56,22 +57,17 @@ class CapacitorCalendarPlugin : Plugin() {
56
57
  if (call == null) {
57
58
  throw Exception("[CapacitorCalendar.${::createEventWithPrompt.name}] Call is not defined")
58
59
  }
59
- val currentEventsCount: Int = implementation.getTotalEventsCount(context)
60
- val createEventResult =
61
- if (currentEventsCount > implementation.eventsCount) {
62
- true
63
- } else if (implementation.eventsCount == currentEventsCount) {
64
- false
65
- } else {
66
- call.reject("", "[CapacitorCalendar.${::openCalendarIntentActivityCallback.name}] Could not create the event")
67
- return
68
- }
60
+
61
+ val newEventIds = implementation.getNewEventIds(implementation.fetchCalendarEventIDs(context))
62
+ val newIdsArray = JSArray()
63
+ newEventIds.forEach { id -> newIdsArray.put(id.toString()) }
64
+
69
65
  val ret = JSObject()
70
- ret.put("eventCreated", createEventResult)
66
+ ret.put("result", newIdsArray)
71
67
  call.resolve(ret)
72
68
  }
73
69
 
74
- @PluginMethod
70
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
75
71
  fun checkPermission(call: PluginCall) {
76
72
  try {
77
73
  val permissionName =
@@ -91,7 +87,7 @@ class CapacitorCalendarPlugin : Plugin() {
91
87
  }
92
88
  }
93
89
 
94
- @PluginMethod
90
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
95
91
  fun checkAllPermissions(call: PluginCall) {
96
92
  try {
97
93
  return checkPermissions(call)
@@ -101,7 +97,7 @@ class CapacitorCalendarPlugin : Plugin() {
101
97
  }
102
98
  }
103
99
 
104
- @PluginMethod
100
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
105
101
  fun requestPermission(call: PluginCall) {
106
102
  try {
107
103
  val alias =
@@ -130,7 +126,7 @@ class CapacitorCalendarPlugin : Plugin() {
130
126
  }
131
127
  }
132
128
 
133
- @PluginMethod
129
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
134
130
  fun requestAllPermissions(call: PluginCall) {
135
131
  try {
136
132
  return requestPermissions(call)
@@ -146,7 +142,7 @@ class CapacitorCalendarPlugin : Plugin() {
146
142
  return
147
143
  }
148
144
 
149
- @PluginMethod
145
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
150
146
  fun listCalendars(call: PluginCall) {
151
147
  try {
152
148
  val calendars = implementation.listCalendars(context)
@@ -158,7 +154,7 @@ class CapacitorCalendarPlugin : Plugin() {
158
154
  }
159
155
  }
160
156
 
161
- @PluginMethod
157
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
162
158
  fun getDefaultCalendar(call: PluginCall) {
163
159
  try {
164
160
  val primaryCalendar = implementation.getDefaultCalendar(context)
@@ -170,7 +166,7 @@ class CapacitorCalendarPlugin : Plugin() {
170
166
  }
171
167
  }
172
168
 
173
- @PluginMethod
169
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
174
170
  fun createEvent(call: PluginCall) {
175
171
  try {
176
172
  val title =
@@ -183,8 +179,12 @@ class CapacitorCalendarPlugin : Plugin() {
183
179
  val isAllDay = call.getBoolean("isAllDay", false)
184
180
 
185
181
  val eventUri = implementation.createEvent(context, title, calendarId, location, startDate, endDate, isAllDay)
182
+ val id = eventUri?.lastPathSegment ?: throw IllegalArgumentException("Failed to insert event into calendar")
186
183
  val ret = JSObject()
187
- ret.put("eventCreated", eventUri != null)
184
+ val array = JSArray()
185
+ array.put(id)
186
+
187
+ ret.put("result", array)
188
188
  call.resolve(ret)
189
189
  } catch (error: Exception) {
190
190
  call.reject("", "[CapacitorCalendar.${::createEvent.name}] Unable to create event")
@@ -220,4 +220,22 @@ class CapacitorCalendarPlugin : Plugin() {
220
220
  return
221
221
  }
222
222
  }
223
+
224
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
225
+ fun listEventsInRange(call: PluginCall) {
226
+ try {
227
+ val startDate =
228
+ call.getLong("startDate")
229
+ ?: throw Exception("[CapacitorCalendar.${::createEvent.name}] A start date was not provided")
230
+ val endDate =
231
+ call.getLong("endDate")
232
+ ?: throw Exception("[CapacitorCalendar.${::createEvent.name}] An end date was not provided")
233
+ val ret = JSObject()
234
+ ret.put("result", implementation.listEventsInRange(context, startDate, endDate))
235
+ call.resolve(ret)
236
+ } catch (error: Exception) {
237
+ call.reject("", "[CapacitorCalendar.${::createEvent.name}] Could not get the list of events in requested range")
238
+ return
239
+ }
240
+ }
223
241
  }