@ebarooni/capacitor-calendar 6.4.1 → 6.6.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 (40) hide show
  1. package/README.md +53 -5
  2. package/android/build.gradle +2 -2
  3. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendar.kt +200 -117
  4. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendarPlugin.kt +85 -3
  5. package/assets/images/text-logo.png +0 -0
  6. package/dist/docs.json +467 -70
  7. package/dist/esm/definitions.d.ts +205 -57
  8. package/dist/esm/definitions.js.map +1 -1
  9. package/dist/esm/index.d.ts +18 -14
  10. package/dist/esm/index.js +12 -9
  11. package/dist/esm/index.js.map +1 -1
  12. package/dist/esm/schemas/enums/calendar-source-type.d.ts +38 -0
  13. package/dist/esm/schemas/enums/calendar-source-type.js +40 -0
  14. package/dist/esm/schemas/enums/calendar-source-type.js.map +1 -0
  15. package/dist/esm/schemas/enums/calendar-type.d.ts +28 -0
  16. package/dist/esm/schemas/enums/calendar-type.js +30 -0
  17. package/dist/esm/schemas/enums/calendar-type.js.map +1 -0
  18. package/dist/esm/schemas/enums/event-span.d.ts +16 -0
  19. package/dist/esm/schemas/enums/event-span.js +18 -0
  20. package/dist/esm/schemas/enums/event-span.js.map +1 -0
  21. package/dist/esm/schemas/interfaces/calendar-event.d.ts +14 -3
  22. package/dist/esm/schemas/interfaces/calendar-event.js.map +1 -1
  23. package/dist/esm/schemas/interfaces/calendar-source.d.ts +15 -0
  24. package/dist/esm/schemas/interfaces/calendar-source.js +2 -0
  25. package/dist/esm/schemas/interfaces/calendar-source.js.map +1 -0
  26. package/dist/esm/schemas/interfaces/calendar.d.ts +43 -2
  27. package/dist/esm/schemas/interfaces/calendar.js.map +1 -1
  28. package/dist/esm/web.d.ts +55 -11
  29. package/dist/esm/web.js +13 -2
  30. package/dist/esm/web.js.map +1 -1
  31. package/dist/plugin.cjs.js +101 -2
  32. package/dist/plugin.cjs.js.map +1 -1
  33. package/dist/plugin.js +101 -2
  34. package/dist/plugin.js.map +1 -1
  35. package/ios/Plugin/CapacitorCalendar.swift +238 -21
  36. package/ios/Plugin/CapacitorCalendarPlugin.m +4 -0
  37. package/ios/Plugin/CapacitorCalendarPlugin.swift +141 -15
  38. package/ios/Plugin/CapacitorReminders.swift +62 -8
  39. package/ios/Plugin/EventCreationParameters.swift +5 -2
  40. package/package.json +28 -18
package/README.md CHANGED
@@ -27,29 +27,73 @@
27
27
 
28
28
  ## Table of Contents
29
29
 
30
- - [Install](#install)
30
+ - [Installation](#installation)
31
+ - [Stable](#stable)
32
+ - [Capacitor 6](#capacitor-6)
33
+ - [Capacitor 5](#capacitor-5)
34
+ - [Development](#development)
35
+ - [Setup](#setup)
36
+ - [Install](#install-1)
31
37
  - [Demo](#demo--click-for-details-)
32
38
  - [Permissions](#permissions)
33
39
  - [API](#-api)
34
40
  - [Documentation](#-documentation)
35
41
  - [Contributions](#-contributions)
36
42
 
37
- ## Install
43
+ ## Installation
38
44
 
39
- ### Capacitor 6
45
+ To use this package, follow the instructions for installing either the stable or development release.
46
+ Stable releases are recommended for production, while development releases provide access to the latest features and
47
+ fixes which are still under development.
48
+
49
+ ### Stable
50
+
51
+ The stable releases are published to the npm package registry.
52
+
53
+ #### Capacitor 6
40
54
 
41
55
  ```bash
42
56
  npm install @ebarooni/capacitor-calendar@latest
43
57
  npx cap sync
44
58
  ```
45
59
 
46
- ### Capacitor 5
60
+ #### Capacitor 5
61
+
62
+ > [!IMPORTANT]
63
+ > This plugin no longer receives updates for Capacitor v5 and only supports the latest version.
47
64
 
48
65
  ```bash
49
66
  npm install @ebarooni/capacitor-calendar@^5
50
67
  npx cap sync
51
68
  ```
52
69
 
70
+ ### Development
71
+
72
+ Development releases are published to the GitHub package registry. These releases are automatically generated from the
73
+ latest state of the `develop branch every time a pull request is merged into it.
74
+
75
+ > [!WARNING]
76
+ > Development releases are not recommended for production use as they may contain untested or experimental changes.
77
+
78
+ #### Setup
79
+
80
+ Create an `.npmrc` file and place it in the root of your project with the following content:
81
+
82
+ ```
83
+ //npm.pkg.github.com/:_authToken=$GITHUB_PAT
84
+ @ebarooni:registry=https://npm.pkg.github.com
85
+ always-auth=true
86
+ ```
87
+
88
+ Replace `$GITHUB_PAT` with your own personal access token with at least `read:packages` scope.
89
+
90
+ #### Install
91
+
92
+ ```bash
93
+ npm install @ebarooni/capacitor-calendar
94
+ npx cap sync
95
+ ```
96
+
53
97
  ## [Demo (click for details)](./example/README.md)
54
98
 
55
99
  | iOS 17 | Android 14 |
@@ -97,6 +141,10 @@ permissions can be found below:
97
141
  * [`requestReadOnlyCalendarAccess()`](#requestreadonlycalendaraccess)
98
142
  * [`requestFullCalendarAccess()`](#requestfullcalendaraccess)
99
143
  * [`requestFullRemindersAccess()`](#requestfullremindersaccess)
144
+ * [`modifyEventWithPrompt(...)`](#modifyeventwithprompt)
145
+ * [`modifyEvent(...)`](#modifyevent)
146
+ * [`fetchAllCalendarSources()`](#fetchallcalendarsources)
147
+ * [`fetchAllRemindersSources()`](#fetchallreminderssources)
100
148
  * [Interfaces](#interfaces)
101
149
  * [Type Aliases](#type-aliases)
102
150
  * [Enums](#enums)
@@ -112,7 +160,7 @@ For detailed explanations, usage examples, and additional information:
112
160
 
113
161
  ## 💙 Contributions
114
162
 
115
- > [!WARNING]
163
+ > [!NOTE]
116
164
  > Thank you for your interest in contributing to the project! At the moment, the focus is on reaching the first major
117
165
  > release. Until then, the contributions will not be accepted. This approach allows to set a solid
118
166
  > foundation and maintain consistency throughout the development process.
@@ -14,7 +14,7 @@ buildscript {
14
14
  mavenCentral()
15
15
  }
16
16
  dependencies {
17
- classpath 'com.android.tools.build:gradle:8.3.2'
17
+ classpath 'com.android.tools.build:gradle:8.5.1'
18
18
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
19
19
  }
20
20
  }
@@ -57,7 +57,7 @@ dependencies {
57
57
  implementation fileTree(dir: 'libs', include: ['*.jar'])
58
58
  implementation project(':capacitor-android')
59
59
  implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
60
- implementation 'androidx.core:core-ktx:+'
60
+ implementation 'androidx.core:core-ktx:1.13.1'
61
61
  testImplementation "junit:junit:$junitVersion"
62
62
  androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
63
63
  androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
@@ -6,12 +6,13 @@ import android.content.Context
6
6
  import android.content.Intent
7
7
  import android.net.Uri
8
8
  import android.provider.CalendarContract
9
+ import android.util.Log
9
10
  import com.getcapacitor.JSArray
10
11
  import com.getcapacitor.JSObject
11
12
  import java.util.Calendar
12
13
  import java.util.TimeZone
13
14
 
14
- class CapacitorCalendar() {
15
+ class CapacitorCalendar {
15
16
  var eventIdsArray: List<Long> = emptyList()
16
17
 
17
18
  @Throws(Exception::class)
@@ -31,9 +32,7 @@ class CapacitorCalendar() {
31
32
  }
32
33
 
33
34
  @Throws(Exception::class)
34
- fun getNewEventIds(newIds: List<Long>): List<Long> {
35
- return newIds.filterNot { it in eventIdsArray }
36
- }
35
+ fun getNewEventIds(newIds: List<Long>): List<Long> = newIds.filterNot { it in eventIdsArray }
37
36
 
38
37
  @Throws(Exception::class)
39
38
  fun listCalendars(context: Context): JSArray {
@@ -41,32 +40,37 @@ class CapacitorCalendar() {
41
40
  arrayOf(
42
41
  CalendarContract.Calendars._ID,
43
42
  CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
43
+ CalendarContract.Calendars.CALENDAR_COLOR,
44
44
  )
45
45
 
46
46
  val calendars = JSArray()
47
47
 
48
- context.contentResolver.query(
49
- CalendarContract.Calendars.CONTENT_URI,
50
- projection,
51
- null,
52
- null,
53
- null,
54
- )?.use { cursor ->
55
- val idColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars._ID)
56
- val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
57
-
58
- while (cursor.moveToNext()) {
59
- val id = cursor.getLong(idColumnIndex)
60
- val title = cursor.getString(nameColumnIndex)
61
- val calendar =
62
- JSObject().apply {
63
- put("id", "$id")
64
- put("title", title)
65
- }
48
+ context.contentResolver
49
+ .query(
50
+ CalendarContract.Calendars.CONTENT_URI,
51
+ projection,
52
+ null,
53
+ null,
54
+ null,
55
+ )?.use { cursor ->
56
+ val idColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars._ID)
57
+ val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
58
+ val calendarColorColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_COLOR)
66
59
 
67
- calendars.put(calendar)
68
- }
69
- } ?: throw Exception("Cursor is null")
60
+ while (cursor.moveToNext()) {
61
+ val id = cursor.getLong(idColumnIndex)
62
+ val title = cursor.getString(nameColumnIndex)
63
+ val calendarColor = cursor.getInt(calendarColorColumnIndex)
64
+ val calendar =
65
+ JSObject().apply {
66
+ put("id", "$id")
67
+ put("title", title)
68
+ put("color", String.format("#%06X", 0xFFFFFF and calendarColor))
69
+ }
70
+
71
+ calendars.put(calendar)
72
+ }
73
+ } ?: throw Exception("Cursor is null")
70
74
 
71
75
  return calendars
72
76
  }
@@ -82,29 +86,30 @@ class CapacitorCalendar() {
82
86
  val selection = "${CalendarContract.Calendars.IS_PRIMARY} = ?"
83
87
  val selectionArgs = arrayOf("1")
84
88
 
85
- context.contentResolver.query(
86
- CalendarContract.Calendars.CONTENT_URI,
87
- projection,
88
- selection,
89
- selectionArgs,
90
- null,
91
- )?.use { cursor ->
92
- if (cursor.moveToFirst()) {
93
- val idColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars._ID)
94
- val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
95
- val id = cursor.getLong(idColumnIndex)
96
- val title = cursor.getString(nameColumnIndex)
89
+ context.contentResolver
90
+ .query(
91
+ CalendarContract.Calendars.CONTENT_URI,
92
+ projection,
93
+ selection,
94
+ selectionArgs,
95
+ null,
96
+ )?.use { cursor ->
97
+ if (cursor.moveToFirst()) {
98
+ val idColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars._ID)
99
+ val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME)
100
+ val id = cursor.getLong(idColumnIndex)
101
+ val title = cursor.getString(nameColumnIndex)
97
102
 
98
- val calendarObject =
99
- JSObject().apply {
100
- put("id", id.toString())
101
- put("title", title)
102
- }
103
- return calendarObject
104
- } else {
105
- return null
103
+ val calendarObject =
104
+ JSObject().apply {
105
+ put("id", id.toString())
106
+ put("title", title)
107
+ }
108
+ return calendarObject
109
+ } else {
110
+ return null
111
+ }
106
112
  }
107
- }
108
113
  throw Exception("No primary calendar found")
109
114
  }
110
115
 
@@ -117,8 +122,11 @@ class CapacitorCalendar() {
117
122
  startDate: Long?,
118
123
  endDate: Long?,
119
124
  isAllDay: Boolean?,
120
- alertOffsetInMinutes: Float?,
121
- ): Uri? {
125
+ alertOffsetInMinutesSingle: Float?,
126
+ alertOffsetInMinutesMultiple: JSArray?,
127
+ url: String?,
128
+ notes: String?,
129
+ ): Uri {
122
130
  val startMillis = startDate ?: Calendar.getInstance().timeInMillis
123
131
  val endMillis = endDate ?: (startMillis + 3600 * 1000)
124
132
 
@@ -131,33 +139,89 @@ class CapacitorCalendar() {
131
139
  put(CalendarContract.Events.CALENDAR_ID, calendarId ?: getDefaultCalendar(context)?.getString("id"))
132
140
  put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id)
133
141
  isAllDay?.let { put(CalendarContract.Events.ALL_DAY, if (it) 1 else 0) }
142
+ put(CalendarContract.Events.DESCRIPTION, listOfNotNull(notes, url?.let { "URL: $it" }).joinToString("\n"))
134
143
  }
135
144
 
136
145
  val eventUri = context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, eventValues)
146
+ val eventId = eventUri?.lastPathSegment?.toLong() ?: throw IllegalArgumentException("Failed to convert event id to long")
137
147
 
138
- if (alertOffsetInMinutes == null || alertOffsetInMinutes < 0) {
139
- return eventUri
148
+ when {
149
+ alertOffsetInMinutesSingle != null && alertOffsetInMinutesSingle > -1 -> {
150
+ val alertValues = createAlertValues(eventId, alertOffsetInMinutesSingle)
151
+ context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, alertValues)
152
+ }
153
+ alertOffsetInMinutesMultiple != null -> {
154
+ alertOffsetInMinutesMultiple
155
+ .toList<Any>()
156
+ .mapNotNull { alert ->
157
+ try {
158
+ val alertFloat = alert.toString().toFloat()
159
+ if (alertFloat > -1) alertFloat else null
160
+ } catch (e: NumberFormatException) {
161
+ Log.e("Error", "Failed to convert alert to float: $alert", e)
162
+ null
163
+ }
164
+ }.forEach { alertFloat ->
165
+ val alertValues = createAlertValues(eventId, alertFloat)
166
+ context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, alertValues)
167
+ }
168
+ }
140
169
  }
141
170
 
142
- val eventId = eventUri?.lastPathSegment?.toLong() ?: throw IllegalArgumentException("Failed to convert event id to long")
143
- val alertValues =
171
+ return eventUri
172
+ }
173
+
174
+ @Throws(Exception::class)
175
+ fun modifyEvent(
176
+ context: Context,
177
+ id: Long,
178
+ update: JSObject,
179
+ ): Boolean {
180
+ val title = update.getString("title")
181
+ val calendarId = update.getString("calendarId")
182
+ val location = update.getString("location")
183
+ val startDate = update.getLong("startDate")
184
+ val endDate = update.getLong("endDate")
185
+ val isAllDay = update.getBoolean("isAllDay")
186
+ val url = update.getString("url")
187
+ val notes = update.getString("notes")
188
+
189
+ val eventUri: Uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, id)
190
+
191
+ val values =
144
192
  ContentValues().apply {
145
- put(CalendarContract.Reminders.EVENT_ID, eventId)
146
- put(CalendarContract.Reminders.MINUTES, alertOffsetInMinutes)
147
- put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
193
+ if (title != null) put(CalendarContract.Events.TITLE, title)
194
+ if (calendarId != null) put(CalendarContract.Events.CALENDAR_ID, calendarId)
195
+ if (location != null) put(CalendarContract.Events.EVENT_LOCATION, location)
196
+ if (startDate != null) put(CalendarContract.Events.DTSTART, startDate)
197
+ if (endDate != null) put(CalendarContract.Events.DTEND, endDate)
198
+ if (isAllDay != null) put(CalendarContract.Events.ALL_DAY, if (isAllDay) 1 else 0)
199
+ if (notes !=
200
+ null
201
+ ) {
202
+ put(CalendarContract.Events.DESCRIPTION, listOfNotNull(notes, url?.let { "URL: $it" }).joinToString("\n"))
203
+ }
148
204
  }
149
205
 
150
- context.contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, alertValues)
151
-
152
- return eventUri
206
+ val rows: Int = context.contentResolver.update(eventUri, values, null, null)
207
+ return rows > 0
153
208
  }
154
209
 
210
+ private fun createAlertValues(
211
+ eventId: Long,
212
+ alertOffset: Float,
213
+ ): ContentValues =
214
+ ContentValues().apply {
215
+ put(CalendarContract.Reminders.EVENT_ID, eventId)
216
+ put(CalendarContract.Reminders.MINUTES, alertOffset)
217
+ put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
218
+ }
219
+
155
220
  @Throws(Exception::class)
156
- fun openCalendar(timestamp: Long): Intent {
157
- return Intent(Intent.ACTION_VIEW).apply {
221
+ fun openCalendar(timestamp: Long): Intent =
222
+ Intent(Intent.ACTION_VIEW).apply {
158
223
  data = Uri.parse("content://com.android.calendar/time/$timestamp")
159
224
  }
160
- }
161
225
 
162
226
  @Throws(Exception::class)
163
227
  fun listEventsInRange(
@@ -170,7 +234,7 @@ class CapacitorCalendar() {
170
234
  CalendarContract.Events._ID,
171
235
  CalendarContract.Events.TITLE,
172
236
  CalendarContract.Events.EVENT_LOCATION,
173
- CalendarContract.Events.EVENT_COLOR,
237
+ CalendarContract.Events.CALENDAR_COLOR,
174
238
  CalendarContract.Events.ORGANIZER,
175
239
  CalendarContract.Events.DESCRIPTION,
176
240
  CalendarContract.Events.DTSTART,
@@ -186,61 +250,74 @@ class CapacitorCalendar() {
186
250
 
187
251
  val events = JSArray()
188
252
 
189
- context.contentResolver.query(
190
- CalendarContract.Events.CONTENT_URI,
191
- projection,
192
- selection,
193
- selectionArgs,
194
- null,
195
- )?.use { cursor ->
196
- val idColumnIndex = cursor.getColumnIndex(CalendarContract.Events._ID)
197
- val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Events.TITLE)
198
- val locationColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_LOCATION)
199
- val eventColorColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_COLOR)
200
- val organizerColumnIndex = cursor.getColumnIndex(CalendarContract.Events.ORGANIZER)
201
- val descriptionColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)
202
- val dtStartColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DTSTART)
203
- val dtEndColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DTEND)
204
- val eventTimezoneColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_TIMEZONE)
205
- val eventEndTimezoneColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_END_TIMEZONE)
206
- val durationColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DURATION)
207
- val isAllDayColumnIndex = cursor.getColumnIndex(CalendarContract.Events.ALL_DAY)
208
- val calendarIdColumnIndex = cursor.getColumnIndex(CalendarContract.Events.CALENDAR_ID)
209
-
210
- while (cursor.moveToNext()) {
211
- val id = cursor.getLong(idColumnIndex)
212
- val title = cursor.getString(nameColumnIndex)
213
- val location = cursor.getString(locationColumnIndex)
214
- val eventColor = cursor.getInt(eventColorColumnIndex)
215
- val organizer = cursor.getString(organizerColumnIndex)
216
- val desc = cursor.getString(descriptionColumnIndex)
217
- val dtStart = cursor.getLong(dtStartColumnIndex)
218
- val dtEnd = cursor.getLong(dtEndColumnIndex)
219
- val eventTimezone = cursor.getString(eventTimezoneColumnIndex)
220
- val eventEndTimezone = cursor.getString(eventEndTimezoneColumnIndex)
221
- val duration = cursor.getString(durationColumnIndex)
222
- val allDay = cursor.getInt(isAllDayColumnIndex) == 1
223
- val calendarId = cursor.getLong(calendarIdColumnIndex)
224
-
225
- val event =
226
- JSObject().apply {
227
- put("id", id.toString())
228
- title?.takeIf { it.isNotEmpty() }?.let { put("title", it) }
229
- location?.takeIf { it.isNotEmpty() }?.let { put("location", it) }
230
- eventColor.takeIf { it != 0 }?.let { put("eventColor", String.format("#%06X", 0xFFFFFF and it)) }
231
- organizer?.takeIf { it.isNotEmpty() }?.let { put("organizer", it) }
232
- desc?.takeIf { it.isNotEmpty() }?.let { put("description", it) }
233
- dtStart.takeIf { it != 0.toLong() }?.let { put("startDate", it) }
234
- dtEnd.takeIf { it != 0.toLong() }?.let { put("endDate", it) }
235
- eventTimezone?.takeIf { it.isNotEmpty() }?.let { put("eventTimezone", it) }
236
- eventEndTimezone?.takeIf { it.isNotEmpty() }?.let { put("eventEndTimezone", it) }
237
- duration?.takeIf { it.isNotEmpty() }?.let { put("duration", it) }
238
- put("isAllDay", allDay)
239
- calendarId.takeIf { it != 0.toLong() }?.let { put("calendarId", it.toString()) }
240
- }
241
- events.put(event)
242
- }
243
- } ?: throw Exception("Cursor is null")
253
+ context.contentResolver
254
+ .query(
255
+ CalendarContract.Events.CONTENT_URI,
256
+ projection,
257
+ selection,
258
+ selectionArgs,
259
+ null,
260
+ )?.use { cursor ->
261
+ val idColumnIndex = cursor.getColumnIndex(CalendarContract.Events._ID)
262
+ val nameColumnIndex = cursor.getColumnIndex(CalendarContract.Events.TITLE)
263
+ val locationColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_LOCATION)
264
+ val calendarColorColumnIndex = cursor.getColumnIndex(CalendarContract.Events.CALENDAR_COLOR)
265
+ val organizerColumnIndex = cursor.getColumnIndex(CalendarContract.Events.ORGANIZER)
266
+ val descriptionColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)
267
+ val dtStartColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DTSTART)
268
+ val dtEndColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DTEND)
269
+ val eventTimezoneColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_TIMEZONE)
270
+ val eventEndTimezoneColumnIndex = cursor.getColumnIndex(CalendarContract.Events.EVENT_END_TIMEZONE)
271
+ val durationColumnIndex = cursor.getColumnIndex(CalendarContract.Events.DURATION)
272
+ val isAllDayColumnIndex = cursor.getColumnIndex(CalendarContract.Events.ALL_DAY)
273
+ val calendarIdColumnIndex = cursor.getColumnIndex(CalendarContract.Events.CALENDAR_ID)
274
+
275
+ while (cursor.moveToNext()) {
276
+ val id = cursor.getLong(idColumnIndex)
277
+ val title = cursor.getString(nameColumnIndex)
278
+ val location = cursor.getString(locationColumnIndex)
279
+ val calendarColor = cursor.getInt(calendarColorColumnIndex)
280
+ val organizer = cursor.getString(organizerColumnIndex)
281
+ val desc = cursor.getString(descriptionColumnIndex)
282
+ val dtStart = cursor.getLong(dtStartColumnIndex)
283
+ val dtEnd = cursor.getLong(dtEndColumnIndex)
284
+ val eventTimezone = cursor.getString(eventTimezoneColumnIndex)
285
+ val eventEndTimezone = cursor.getString(eventEndTimezoneColumnIndex)
286
+ val duration = cursor.getString(durationColumnIndex)
287
+ val allDay = cursor.getInt(isAllDayColumnIndex) == 1
288
+ val calendarId = cursor.getLong(calendarIdColumnIndex)
289
+
290
+ val event =
291
+ JSObject().apply {
292
+ put("id", id.toString())
293
+ title?.takeIf { it.isNotEmpty() }?.let { put("title", it) }
294
+ location?.takeIf { it.isNotEmpty() }?.let { put("location", it) }
295
+ calendarColor.takeIf { it != 0 }?.let { put("eventColor", String.format("#%06X", 0xFFFFFF and it)) }
296
+ organizer?.takeIf { it.isNotEmpty() }?.let { put("organizer", it) }
297
+ desc?.takeIf { it.isNotEmpty() }?.let { put("description", it) }
298
+ dtStart.takeIf { it != 0.toLong() }?.let { put("startDate", it) }
299
+ dtEnd.takeIf { it != 0.toLong() }?.let { put("endDate", it) }
300
+ eventTimezone?.takeIf { it.isNotEmpty() }?.let { timezone ->
301
+ val abbreviation = getTimeZoneAbbreviation(timezone)
302
+ val obj = JSObject()
303
+ obj.put("region", timezone)
304
+ obj.put("abbreviation", abbreviation)
305
+ put("eventTimezone", obj)
306
+ }
307
+ eventEndTimezone?.takeIf { it.isNotEmpty() }?.let {
308
+ val abbreviation = getTimeZoneAbbreviation(it)
309
+ val obj = JSObject()
310
+ obj.put("region", it)
311
+ obj.put("abbreviation", abbreviation)
312
+ put("eventEndTimezone", obj)
313
+ }
314
+ duration?.takeIf { it.isNotEmpty() }?.let { put("duration", it) }
315
+ put("isAllDay", allDay)
316
+ calendarId.takeIf { it != 0.toLong() }?.let { put("calendarId", it.toString()) }
317
+ }
318
+ events.put(event)
319
+ }
320
+ } ?: throw Exception("Cursor is null")
244
321
  return events
245
322
  }
246
323
 
@@ -272,4 +349,10 @@ class CapacitorCalendar() {
272
349
  ret.put("failed", failedToDeleteEvents)
273
350
  return ret
274
351
  }
352
+
353
+ fun getTimeZoneAbbreviation(timeZoneId: String): String {
354
+ val timeZone = TimeZone.getTimeZone(timeZoneId)
355
+ val now = Calendar.getInstance(timeZone)
356
+ return timeZone.getDisplayName(timeZone.inDaylightTime(now.time), TimeZone.SHORT)
357
+ }
275
358
  }
@@ -1,7 +1,9 @@
1
1
  package dev.barooni.capacitor.calendar
2
2
 
3
3
  import android.Manifest
4
+ import android.content.ContentUris
4
5
  import android.content.Intent
6
+ import android.net.Uri
5
7
  import android.provider.CalendarContract
6
8
  import androidx.activity.result.ActivityResult
7
9
  import com.getcapacitor.JSArray
@@ -52,6 +54,8 @@ class CapacitorCalendarPlugin : Plugin() {
52
54
  val startDate = call.getLong("startDate")
53
55
  val endDate = call.getLong("endDate")
54
56
  val isAllDay = call.getBoolean("isAllDay", false)
57
+ val url = call.getString("url")
58
+ val notes = call.getString("notes")
55
59
 
56
60
  val intent = Intent(Intent.ACTION_INSERT).setData(CalendarContract.Events.CONTENT_URI)
57
61
 
@@ -61,6 +65,7 @@ class CapacitorCalendarPlugin : Plugin() {
61
65
  startDate?.let { intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, it) }
62
66
  endDate?.let { intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, it) }
63
67
  isAllDay?.let { intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, it) }
68
+ intent.putExtra(CalendarContract.Events.DESCRIPTION, listOfNotNull(notes, url?.let { "URL: $it" }).joinToString("\n"))
64
69
 
65
70
  return startActivityForResult(
66
71
  call,
@@ -91,6 +96,77 @@ class CapacitorCalendarPlugin : Plugin() {
91
96
  call.resolve(ret)
92
97
  }
93
98
 
99
+ @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
100
+ fun modifyEventWithPrompt(call: PluginCall) {
101
+ try {
102
+ val stringId =
103
+ call.getString("id") ?: throw Exception("[CapacitorCalendar.${::modifyEventWithPrompt.name}] Event ID not defined")
104
+ val update = call.getObject("update")
105
+ val uri: Uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, stringId.toLong())
106
+ val intent =
107
+ Intent(Intent.ACTION_EDIT)
108
+ .setData(uri)
109
+
110
+ if (update != null) {
111
+ val title = update.getString("title")
112
+ val calendarId = update.getString("calendarId")
113
+ val location = update.getString("location")
114
+ val startDate = update.getLong("startDate")
115
+ val endDate = update.getLong("endDate")
116
+ val isAllDay = update.getBoolean("isAllDay")
117
+ val url = update.getString("url")
118
+ val notes = update.getString("notes")
119
+
120
+ intent.putExtra(CalendarContract.Events.TITLE, title)
121
+ calendarId?.let { intent.putExtra(CalendarContract.Events.CALENDAR_ID, it) }
122
+ location?.let { intent.putExtra(CalendarContract.Events.EVENT_LOCATION, it) }
123
+ startDate?.let { intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, it) }
124
+ endDate?.let { intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, it) }
125
+ isAllDay?.let { intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, it) }
126
+ intent.putExtra(CalendarContract.Events.DESCRIPTION, listOfNotNull(notes, url?.let { "URL: $it" }).joinToString("\n"))
127
+ }
128
+
129
+ return startActivityForResult(
130
+ call,
131
+ intent,
132
+ "openEventEditIntentActivityCallback",
133
+ )
134
+ } catch (error: Exception) {
135
+ call.reject("", error.message)
136
+ return
137
+ }
138
+ }
139
+
140
+ @ActivityCallback
141
+ private fun openEventEditIntentActivityCallback(
142
+ call: PluginCall?,
143
+ result: ActivityResult,
144
+ ) {
145
+ if (call == null) {
146
+ throw Exception("[CapacitorCalendar.${::createEventWithPrompt.name}] Call is not defined")
147
+ }
148
+ val ret = JSObject()
149
+ ret.put("result", JSArray())
150
+ call.resolve(ret)
151
+ }
152
+
153
+ @PluginMethod(returnType = PluginMethod.RETURN_NONE)
154
+ fun modifyEvent(call: PluginCall) {
155
+ try {
156
+ val stringId = call.getString("id") ?: throw Exception("[CapacitorCalendar.${::modifyEvent.name}] Event ID not defined")
157
+ val update = call.getObject("update") ?: throw Exception("[CapacitorCalendar.${::modifyEvent.name}] Update not provided")
158
+ val updated = implementation.modifyEvent(context, stringId.toLong(), update)
159
+ if (updated) {
160
+ call.resolve()
161
+ } else {
162
+ throw Exception("[CapacitorCalendar.${::modifyEvent.name}] Event not updated")
163
+ }
164
+ } catch (error: Exception) {
165
+ call.reject("", error.message)
166
+ return
167
+ }
168
+ }
169
+
94
170
  @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
95
171
  fun checkPermission(call: PluginCall) {
96
172
  try {
@@ -217,7 +293,7 @@ class CapacitorCalendarPlugin : Plugin() {
217
293
  }
218
294
 
219
295
  @PermissionCallback
220
- private fun requestFullCalendarAccessCallbackCallback(call: PluginCall) {
296
+ private fun requestFullCalendarAccessCallback(call: PluginCall) {
221
297
  val permissionName = "readWriteCalendar"
222
298
  try {
223
299
  val ret = JSObject()
@@ -279,7 +355,10 @@ class CapacitorCalendarPlugin : Plugin() {
279
355
  val startDate = call.getLong("startDate")
280
356
  val endDate = call.getLong("endDate")
281
357
  val isAllDay = call.getBoolean("isAllDay", false)
282
- val alertOffsetInMinutes = call.getFloat("alertOffsetInMinutes")
358
+ val alertOffsetInMinutesSingle = call.getFloat("alertOffsetInMinutes")
359
+ val alertOffsetInMinutesMultiple = call.getArray("alertOffsetInMinutes")
360
+ val url = call.getString("url")
361
+ val notes = call.getString("notes")
283
362
 
284
363
  val eventUri =
285
364
  implementation.createEvent(
@@ -290,7 +369,10 @@ class CapacitorCalendarPlugin : Plugin() {
290
369
  startDate,
291
370
  endDate,
292
371
  isAllDay,
293
- alertOffsetInMinutes,
372
+ alertOffsetInMinutesSingle,
373
+ alertOffsetInMinutesMultiple,
374
+ url,
375
+ notes,
294
376
  )
295
377
 
296
378
  val id = eventUri?.lastPathSegment ?: throw IllegalArgumentException("Failed to insert event into calendar")
Binary file