@ebarooni/capacitor-calendar 7.3.0 → 7.5.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 (33) hide show
  1. package/README.md +49 -3
  2. package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendarPlugin.kt +70 -25
  3. package/android/src/main/java/dev/barooni/capacitor/calendar/PluginError.kt +45 -22
  4. package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CapacitorCalendar.kt +1 -1
  5. package/android/src/main/java/dev/barooni/capacitor/calendar/models/data/CalendarInfo.kt +1 -0
  6. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/CreateEventInput.kt +4 -1
  7. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/DeleteEventInput.kt +4 -1
  8. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/DeleteEventWithPromptInput.kt +4 -1
  9. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/ModifyEvent.kt +8 -2
  10. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/ModifyEventWithPromptInput.kt +1 -1
  11. package/android/src/main/java/dev/barooni/capacitor/calendar/models/results/GetDefaultCalendarResult.kt +1 -1
  12. package/android/src/main/java/dev/barooni/capacitor/calendar/models/results/ListCalendarsResult.kt +1 -1
  13. package/android/src/main/java/dev/barooni/capacitor/calendar/utils/DeclarationCheck.kt +59 -0
  14. package/android/src/main/java/dev/barooni/capacitor/calendar/utils/ImplementationHelper.kt +27 -2
  15. package/android/src/main/java/dev/barooni/capacitor/calendar/utils/PluginCallRejecter.kt +69 -0
  16. package/dist/docs.json +2 -2
  17. package/dist/esm/index.d.ts +2 -1
  18. package/dist/esm/index.js +2 -1
  19. package/dist/esm/index.js.map +1 -1
  20. package/dist/esm/schemas/enums/capacitor-calendar-error-code.d.ts +44 -0
  21. package/dist/esm/schemas/enums/capacitor-calendar-error-code.js +46 -0
  22. package/dist/esm/schemas/enums/capacitor-calendar-error-code.js.map +1 -0
  23. package/dist/esm/schemas/interfaces/calendar.d.ts +3 -1
  24. package/dist/esm/schemas/interfaces/calendar.js.map +1 -1
  25. package/dist/plugin.cjs.js +46 -0
  26. package/dist/plugin.cjs.js.map +1 -1
  27. package/dist/plugin.js +46 -0
  28. package/dist/plugin.js.map +1 -1
  29. package/ios/Plugin/CapacitorCalendarPlugin.swift +66 -22
  30. package/ios/Plugin/PluginError.swift +47 -0
  31. package/ios/Plugin/Utils/DeclarationCheck.swift +63 -0
  32. package/ios/Plugin/Utils/PluginCallRejecter.swift +36 -0
  33. package/package.json +1 -1
package/README.md CHANGED
@@ -9,10 +9,10 @@
9
9
  <img src="https://img.shields.io/npm/dm/@ebarooni/capacitor-calendar?style=flat-square" />
10
10
  </a>
11
11
  <a href="https://www.npmjs.com/package/@ebarooni/capacitor-calendar">
12
- <img src="https://img.shields.io/npm/v/@ebarooni/capacitor-calendar?style=flat-square" />
12
+ <img src="https://img.shields.io/npm/v/@ebarooni/capacitor-calendar/latest-7?style=flat-square" />
13
13
  </a>
14
14
  <a href="https://capacitorjs.com/">
15
- <img src="https://img.shields.io/badge/Capacitor-8.x-119EFF.svg?style=flat-square" />
15
+ <img src="https://img.shields.io/badge/Capacitor-7.x-119EFF.svg?style=flat-square" />
16
16
  </a>
17
17
  <a href="mcp/README.md">
18
18
  <img src="https://img.shields.io/badge/MCP-161b22?style=flat&logo=modelcontextprotocol&logoColor=5FB8AF" />
@@ -29,8 +29,10 @@
29
29
  - [Demo](#demo)
30
30
  - [Why this plugin?](#why-this-plugin)
31
31
  - [MCP Server](#mcp-server)
32
+ - [Capacitor Compatibility](#capacitor-compatibility)
32
33
  - [Installation](#installation)
33
34
  - [Setup](#setup)
35
+ - [Error codes](#error-codes)
34
36
  - [Quick Start](#quick-start)
35
37
  - [Usage Examples](#usage-examples)
36
38
  - [Documentation](#documentation)
@@ -80,10 +82,29 @@ docker run --rm -d --name capacitor-calendar-mcp -p 8080:8080 ghcr.io/ebarooni/c
80
82
 
81
83
  See [`mcp/README.md`](mcp/README.md) for client configuration and full details.
82
84
 
85
+ ## Capacitor Compatibility
86
+
87
+ Match the npm tag to your Capacitor major. The `latest` and `latest-7` packages are intended to stay in feature parity.
88
+
89
+ | Capacitor | npm tag | Version |
90
+ | :-------- | :--------- | :------------------------------------------------------------------------------------------------------------------------- |
91
+ | `8.x` | `latest` | ![npm@latest](https://img.shields.io/npm/v/@ebarooni/capacitor-calendar/latest?label=npm%40latest&style=flat-square) |
92
+ | `7.x` | `latest-7` | ![npm@latest-7](https://img.shields.io/npm/v/@ebarooni/capacitor-calendar/latest-7?label=npm%40latest-7&style=flat-square) |
93
+
83
94
  ## Installation
84
95
 
96
+ **Capacitor 8:**
97
+
85
98
  ```bash
86
99
  npm install @ebarooni/capacitor-calendar
100
+ # or: npm install @ebarooni/capacitor-calendar@latest
101
+ npx cap sync
102
+ ```
103
+
104
+ **Capacitor 7:**
105
+
106
+ ```bash
107
+ npm install @ebarooni/capacitor-calendar@latest-7
87
108
  npx cap sync
88
109
  ```
89
110
 
@@ -131,6 +152,31 @@ Add the appropriate usage description keys to `ios/App/App/Info.plist`. Starting
131
152
  - **iOS:** [Migrating to the Latest Calendar Access Levels](https://developer.apple.com/documentation/technotes/tn3152-migrating-to-the-latest-calendar-access-levels)
132
153
  - **Android:** [Calendar Provider User Permissions](https://developer.android.com/identity/providers/calendar-provider#manifest)
133
154
 
155
+ ## Error codes
156
+
157
+ On Android and iOS, a rejected call can expose `error.code` (stable category) and `error.message` (exact cause). Compare codes with the exported `CapacitorCalendarErrorCode` enum. Not every reject has a code yet; later releases add more catalog cases.
158
+
159
+ Defined codes:
160
+
161
+ | Code | Name | When |
162
+ | :------------- | :------------------- | :-------------------------------------------------------------------------------- |
163
+ | `EB-CLDR-0001` | `InvalidArgument` | A required argument is missing or invalid. |
164
+ | `EB-CLDR-0002` | `NotFound` | The requested calendar, event, list, reminder, or calendar source does not exist. |
165
+ | `EB-CLDR-0004` | `IoError` | A calendar store read or write failed. |
166
+ | `EB-CLDR-0005` | `NotSupported` | The device cannot open the calendar UI, reminders UI, or target URL. |
167
+ | `EB-CLDR-0020` | `PermissionDenied` | Calendar access was denied. |
168
+ | `EB-CLDR-0100` | `MissingDeclaration` | Required Info.plist usage description or AndroidManifest permission is not set. |
169
+
170
+ Android rejects a non-numeric or empty calendar or event `id` as `InvalidArgument`. iOS looks up the string in the system calendar store and may reject as `NotFound` when nothing matches.
171
+
172
+ `IoError`: Android and iOS map store failures such as failed create/delete ID retrieval and delete failures. Other calendar store errors that are not permission or launch failures also use this code. On iOS, EventKit store or system rejects use this code, including some authorization-related failures; Android permission-provider denials still use `PermissionDenied` (`EB-CLDR-0020`). On iOS, this code also covers the edit-prompt `processFailed` outcome.
173
+
174
+ `NotSupported`: Android maps a missing calendar activity handler (`ActivityNotFoundException`, for example `openCalendar`) to this code. iOS maps launch and URL failures (`Failed to launch calendar app.`, reminders, `Unable to open URL.`).
175
+
176
+ On Android, calendar operations that fail with a permission error reject with `PermissionDenied` (`EB-CLDR-0020`). Permission check and request APIs still resolve `"denied"` on Android and iOS; they do not reject with `0020`.
177
+
178
+ For Android `NotSupported` and `PermissionDenied` rejects, the message starts with a fixed sentence (`Failed to launch calendar app.` or `Calendar permission was denied.`). If the OS provides a different detail, it is appended in parentheses.
179
+
134
180
  ## Quick Start
135
181
 
136
182
  The sample below is for Android and iOS. For Web, see [Create and download an event on the web](#create-and-download-an-event-on-the-web).
@@ -1320,7 +1366,7 @@ Options for {@link CalendarAccess#requestPermission}.
1320
1366
  | **`color`** | <code>string \| null</code> | Hex color string, or null when none is available. Format: `#RRGGBB` when opaque; `#RRGGBBAA` when alpha is below fully opaque. | 7.1.0 | Android, iOS |
1321
1367
  | **`colorName`** | <code><a href="#systemcolorname">SystemColorName</a> \| null</code> | Named color if the stored color matches one of the system colors; otherwise null. May be null in dark mode or for custom hex. Always null on Android. | 7.3.0 | Android, iOS |
1322
1368
  | **`isImmutable`** | <code>boolean \| null</code> | | 7.1.0 | iOS |
1323
- | **`allowsContentModifications`** | <code>boolean \| null</code> | | 7.1.0 | iOS |
1369
+ | **`allowsContentModifications`** | <code>boolean \| null</code> | Whether events can be added to or changed in this calendar. | 7.1.0 | Android, iOS |
1324
1370
  | **`type`** | <code><a href="#calendartype">CalendarType</a> \| null</code> | | 7.1.0 | iOS |
1325
1371
  | **`isSubscribed`** | <code>boolean \| null</code> | | 7.1.0 | iOS |
1326
1372
  | **`source`** | <code><a href="#calendarsource">CalendarSource</a> \| null</code> | | 7.1.0 | iOS |
@@ -32,6 +32,8 @@ import dev.barooni.capacitor.calendar.models.results.ModifyEventWithPromptResult
32
32
  import dev.barooni.capacitor.calendar.models.results.RequestAllPermissionsResult
33
33
  import dev.barooni.capacitor.calendar.models.results.RequestPermissionResult
34
34
  import dev.barooni.capacitor.calendar.models.templates.JSResult
35
+ import dev.barooni.capacitor.calendar.utils.DeclarationCheck
36
+ import dev.barooni.capacitor.calendar.utils.PluginCallRejecter
35
37
 
36
38
  @CapacitorPlugin(
37
39
  name = "CapacitorCalendar",
@@ -59,6 +61,7 @@ import dev.barooni.capacitor.calendar.models.templates.JSResult
59
61
  )
60
62
  class CapacitorCalendarPlugin : Plugin() {
61
63
  private val implementation: CapacitorCalendar by lazy { CapacitorCalendar(this) }
64
+ private val declarationCheck: DeclarationCheck by lazy { DeclarationCheck(context) }
62
65
  private var eventIdOptional = false
63
66
 
64
67
  @PluginMethod
@@ -68,7 +71,7 @@ class CapacitorCalendarPlugin : Plugin() {
68
71
  val result = implementation.checkPermission(input)
69
72
  call.resolve(result.toJSON())
70
73
  } catch (error: Exception) {
71
- call.reject(error.message)
74
+ rejectCall(call, error)
72
75
  }
73
76
  }
74
77
 
@@ -78,7 +81,7 @@ class CapacitorCalendarPlugin : Plugin() {
78
81
  val result = implementation.checkAllPermissions()
79
82
  call.resolve(result.toJSON())
80
83
  } catch (error: Exception) {
81
- call.reject(error.message)
84
+ rejectCall(call, error)
82
85
  }
83
86
  }
84
87
 
@@ -96,9 +99,10 @@ class CapacitorCalendarPlugin : Plugin() {
96
99
  fun requestPermission(call: PluginCall) {
97
100
  try {
98
101
  val input = RequestPermissionInput.FromCall(call, ::requestPermissionCallback.name)
102
+ declarationCheck.ensureManifestPermissionsForScope(input.scope)
99
103
  implementation.requestPermission(input, ::requestPermissionForAlias)
100
104
  } catch (error: Exception) {
101
- call.reject(error.message)
105
+ rejectCall(call, error)
102
106
  }
103
107
  }
104
108
 
@@ -108,17 +112,21 @@ class CapacitorCalendarPlugin : Plugin() {
108
112
  val result = RequestPermissionResult.FromCall(call, ::getPermissionState)
109
113
  call.resolve(result.toJSON())
110
114
  } catch (error: Exception) {
111
- call.reject(error.message)
115
+ rejectCall(call, error)
112
116
  }
113
117
  }
114
118
 
115
119
  @PluginMethod
116
120
  fun requestAllPermissions(call: PluginCall) {
117
121
  try {
122
+ declarationCheck.ensureManifestPermissions(
123
+ Manifest.permission.READ_CALENDAR,
124
+ Manifest.permission.WRITE_CALENDAR,
125
+ )
118
126
  val input = RequestAllPermissionsInput(call, ::requestAllPermissionsCallback.name)
119
127
  implementation.requestAllPermissions(input, ::requestPermissionForAlias)
120
128
  } catch (error: Exception) {
121
- call.reject(error.message)
129
+ rejectCall(call, error)
122
130
  }
123
131
  }
124
132
 
@@ -128,13 +136,14 @@ class CapacitorCalendarPlugin : Plugin() {
128
136
  val result = RequestAllPermissionsResult(::getPermissionState)
129
137
  call.resolve(result.toJSON())
130
138
  } catch (error: Exception) {
131
- call.reject(error.message)
139
+ rejectCall(call, error)
132
140
  }
133
141
  }
134
142
 
135
143
  @PluginMethod
136
144
  fun requestWriteOnlyCalendarAccess(call: PluginCall) {
137
145
  try {
146
+ declarationCheck.ensureManifestPermissions(Manifest.permission.WRITE_CALENDAR)
138
147
  val input =
139
148
  RequestPermissionInput.FromScope(
140
149
  call,
@@ -143,7 +152,7 @@ class CapacitorCalendarPlugin : Plugin() {
143
152
  )
144
153
  implementation.requestPermission(input, ::requestPermissionForAlias)
145
154
  } catch (error: Exception) {
146
- call.reject(error.message)
155
+ rejectCall(call, error)
147
156
  }
148
157
  }
149
158
 
@@ -153,13 +162,14 @@ class CapacitorCalendarPlugin : Plugin() {
153
162
  val result = RequestPermissionResult.FromScope(CalendarPermissionScope.WRITE_CALENDAR, ::getPermissionState)
154
163
  call.resolve(result.toJSON())
155
164
  } catch (error: Exception) {
156
- call.reject(error.message)
165
+ rejectCall(call, error)
157
166
  }
158
167
  }
159
168
 
160
169
  @PluginMethod
161
170
  fun requestReadOnlyCalendarAccess(call: PluginCall) {
162
171
  try {
172
+ declarationCheck.ensureManifestPermissions(Manifest.permission.READ_CALENDAR)
163
173
  val input =
164
174
  RequestPermissionInput.FromScope(
165
175
  call,
@@ -168,7 +178,7 @@ class CapacitorCalendarPlugin : Plugin() {
168
178
  )
169
179
  implementation.requestPermission(input, ::requestPermissionForAlias)
170
180
  } catch (error: Exception) {
171
- call.reject(error.message)
181
+ rejectCall(call, error)
172
182
  }
173
183
  }
174
184
 
@@ -178,17 +188,21 @@ class CapacitorCalendarPlugin : Plugin() {
178
188
  val result = RequestPermissionResult.FromScope(CalendarPermissionScope.READ_CALENDAR, ::getPermissionState)
179
189
  call.resolve(result.toJSON())
180
190
  } catch (error: Exception) {
181
- call.reject(error.message)
191
+ rejectCall(call, error)
182
192
  }
183
193
  }
184
194
 
185
195
  @PluginMethod
186
196
  fun requestFullCalendarAccess(call: PluginCall) {
187
197
  try {
198
+ declarationCheck.ensureManifestPermissions(
199
+ Manifest.permission.READ_CALENDAR,
200
+ Manifest.permission.WRITE_CALENDAR,
201
+ )
188
202
  val input = RequestAllPermissionsInput(call, ::requestFullCalendarAccessCallback.name)
189
203
  implementation.requestAllPermissions(input, ::requestPermissionForAlias)
190
204
  } catch (error: Exception) {
191
- call.reject(error.message)
205
+ rejectCall(call, error)
192
206
  }
193
207
  }
194
208
 
@@ -198,7 +212,7 @@ class CapacitorCalendarPlugin : Plugin() {
198
212
  val result = RequestPermissionResult.FullCalendarAccess(::getPermissionState)
199
213
  call.resolve(result.toJSON())
200
214
  } catch (error: Exception) {
201
- call.reject(error.message)
215
+ rejectCall(call, error)
202
216
  }
203
217
  }
204
218
 
@@ -213,7 +227,7 @@ class CapacitorCalendarPlugin : Plugin() {
213
227
  val input = CreateEventWithPromptInput(call, "createEventWithPromptCallback")
214
228
  implementation.createEventWithPrompt(input, ::startActivityForResult)
215
229
  } catch (error: Exception) {
216
- call.reject(error.message)
230
+ rejectCall(call, error)
217
231
  }
218
232
  }
219
233
 
@@ -234,7 +248,7 @@ class CapacitorCalendarPlugin : Plugin() {
234
248
  val input = ModifyEventWithPromptInput(call, "modifyEventWithPromptCallback")
235
249
  implementation.modifyEventWithPrompt(input, ::startActivityForResult)
236
250
  } catch (error: Exception) {
237
- call.reject(error.message)
251
+ rejectCall(call, error)
238
252
  }
239
253
  }
240
254
 
@@ -252,11 +266,15 @@ class CapacitorCalendarPlugin : Plugin() {
252
266
  @PluginMethod
253
267
  fun createEvent(call: PluginCall) {
254
268
  try {
269
+ declarationCheck.ensureManifestPermissions(
270
+ Manifest.permission.READ_CALENDAR,
271
+ Manifest.permission.WRITE_CALENDAR,
272
+ )
255
273
  val input = CreateEventInput(call)
256
274
  val result = implementation.createEvent(input)
257
275
  call.resolve(result.toJSON())
258
276
  } catch (error: Exception) {
259
- call.reject(error.message)
277
+ rejectCall(call, error)
260
278
  }
261
279
  }
262
280
 
@@ -268,11 +286,15 @@ class CapacitorCalendarPlugin : Plugin() {
268
286
  @PluginMethod(returnType = PluginMethod.RETURN_NONE)
269
287
  fun modifyEvent(call: PluginCall) {
270
288
  try {
289
+ declarationCheck.ensureManifestPermissions(
290
+ Manifest.permission.READ_CALENDAR,
291
+ Manifest.permission.WRITE_CALENDAR,
292
+ )
271
293
  val input = ModifyEvent(call)
272
294
  implementation.modifyEvent(input)
273
295
  call.resolve()
274
296
  } catch (error: Exception) {
275
- call.reject(error.message)
297
+ rejectCall(call, error)
276
298
  }
277
299
  }
278
300
 
@@ -289,6 +311,7 @@ class CapacitorCalendarPlugin : Plugin() {
289
311
  @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
290
312
  fun listCalendars(call: PluginCall) {
291
313
  try {
314
+ declarationCheck.ensureManifestPermissions(Manifest.permission.READ_CALENDAR)
292
315
  implementation.listCalendars { result, error ->
293
316
  if (error != null) {
294
317
  rejectCall(call, error)
@@ -314,6 +337,7 @@ class CapacitorCalendarPlugin : Plugin() {
314
337
  @PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
315
338
  fun getDefaultCalendar(call: PluginCall) {
316
339
  try {
340
+ declarationCheck.ensureManifestPermissions(Manifest.permission.READ_CALENDAR)
317
341
  val input = GetDefaultCalendarInput(call)
318
342
  implementation.getDefaultCalendar(input) { result, error ->
319
343
  if (error != null) {
@@ -344,29 +368,35 @@ class CapacitorCalendarPlugin : Plugin() {
344
368
  implementation.openCalendar(input)
345
369
  call.resolve()
346
370
  } catch (error: Exception) {
347
- call.reject(error.message)
371
+ rejectCall(call, error)
348
372
  }
349
373
  }
350
374
 
351
375
  @PluginMethod
352
376
  fun createCalendar(call: PluginCall) {
353
377
  try {
378
+ // Insert-only path; no CalendarContract query.
379
+ declarationCheck.ensureManifestPermissions(Manifest.permission.WRITE_CALENDAR)
354
380
  val input = CreateCalendarInput(call)
355
381
  val result = implementation.createCalendar(input)
356
382
  call.resolve(result.toJSON())
357
383
  } catch (error: Exception) {
358
- call.reject(error.message)
384
+ rejectCall(call, error)
359
385
  }
360
386
  }
361
387
 
362
388
  @PluginMethod
363
389
  fun deleteCalendar(call: PluginCall) {
364
390
  try {
391
+ declarationCheck.ensureManifestPermissions(
392
+ Manifest.permission.READ_CALENDAR,
393
+ Manifest.permission.WRITE_CALENDAR,
394
+ )
365
395
  val input = DeleteCalendarInput(call)
366
396
  implementation.deleteCalendar(input)
367
397
  call.resolve()
368
398
  } catch (error: Exception) {
369
- call.reject(error.message)
399
+ rejectCall(call, error)
370
400
  }
371
401
  }
372
402
 
@@ -403,28 +433,40 @@ class CapacitorCalendarPlugin : Plugin() {
403
433
  @PluginMethod
404
434
  fun deleteEventsById(call: PluginCall) {
405
435
  try {
436
+ declarationCheck.ensureManifestPermissions(
437
+ Manifest.permission.READ_CALENDAR,
438
+ Manifest.permission.WRITE_CALENDAR,
439
+ )
406
440
  val input = DeleteEventsByIdInput(call)
407
441
  val result = implementation.deleteEventsById(input)
408
442
  call.resolve(result.toJSON())
409
443
  } catch (error: Exception) {
410
- call.reject(error.message)
444
+ rejectCall(call, error)
411
445
  }
412
446
  }
413
447
 
414
448
  @PluginMethod
415
449
  fun deleteEvent(call: PluginCall) {
416
450
  try {
451
+ declarationCheck.ensureManifestPermissions(
452
+ Manifest.permission.READ_CALENDAR,
453
+ Manifest.permission.WRITE_CALENDAR,
454
+ )
417
455
  val input = DeleteEventInput(call)
418
456
  implementation.deleteEvent(input)
419
457
  call.resolve()
420
458
  } catch (error: Exception) {
421
- call.reject(error.message)
459
+ rejectCall(call, error)
422
460
  }
423
461
  }
424
462
 
425
463
  @PluginMethod
426
464
  fun deleteEventWithPrompt(call: PluginCall) {
427
465
  try {
466
+ declarationCheck.ensureManifestPermissions(
467
+ Manifest.permission.READ_CALENDAR,
468
+ Manifest.permission.WRITE_CALENDAR,
469
+ )
428
470
  val input = DeleteEventWithPromptInput(call)
429
471
  implementation.deleteEventWithPrompt(
430
472
  input,
@@ -433,29 +475,32 @@ class CapacitorCalendarPlugin : Plugin() {
433
475
  },
434
476
  )
435
477
  } catch (error: Exception) {
436
- call.reject(error.message)
478
+ rejectCall(call, error)
437
479
  }
438
480
  }
439
481
 
440
482
  @PluginMethod
441
483
  fun listEventsInRange(call: PluginCall) {
442
484
  try {
485
+ declarationCheck.ensureManifestPermissions(Manifest.permission.READ_CALENDAR)
443
486
  val input = ListEventsInRangeInput(call)
444
487
  val result = implementation.listEventsInRange(input)
445
488
  call.resolve(result.toJSON())
446
489
  } catch (error: Exception) {
447
- call.reject(error.message)
490
+ rejectCall(call, error)
448
491
  }
449
492
  }
450
493
 
451
494
  @PluginMethod
452
495
  fun modifyCalendar(call: PluginCall) {
453
496
  try {
497
+ // Update-only path; no CalendarContract query.
498
+ declarationCheck.ensureManifestPermissions(Manifest.permission.WRITE_CALENDAR)
454
499
  val input = ModifyCalendarInput(call)
455
500
  implementation.modifyCalendar(input)
456
501
  call.resolve()
457
502
  } catch (error: Exception) {
458
- call.reject(error.message)
503
+ rejectCall(call, error)
459
504
  }
460
505
  }
461
506
 
@@ -473,7 +518,7 @@ class CapacitorCalendarPlugin : Plugin() {
473
518
  call: PluginCall,
474
519
  error: Exception?,
475
520
  ) {
476
- call.reject(error?.message ?: PluginError.CustomError("An unknown error has occurred.").message)
521
+ PluginCallRejecter.reject(call, error)
477
522
  }
478
523
 
479
524
  private fun resolveCall(call: PluginCall) {
@@ -2,60 +2,83 @@ package dev.barooni.capacitor.calendar
2
2
 
3
3
  sealed class PluginError(
4
4
  localizedDescription: String,
5
+ val code: String? = null,
5
6
  ) : Exception(localizedDescription) {
6
- data object AccountNameMissing : PluginError("Account name must be provided.")
7
+ data object AccountNameMissing : PluginError("Account name must be provided.", "EB-CLDR-0001")
7
8
 
8
- data object AttendeeEmailMissing : PluginError("Attendee email must be provided.")
9
+ data object AttendeeEmailMissing : PluginError("Attendee email must be provided.", "EB-CLDR-0001")
9
10
 
10
- data object CalendarIdMissing : PluginError("Calendar ID must be provided.")
11
+ data object CalendarIdMissing : PluginError("Calendar ID must be provided.", "EB-CLDR-0001")
11
12
 
12
- data object CalendarNotFound : PluginError("Calendar not found.")
13
+ data object CalendarNotFound : PluginError("Calendar not found.", "EB-CLDR-0002")
13
14
 
14
- data object ColorMissing : PluginError("Color must be provided.")
15
+ data object ColorMissing : PluginError("Color must be provided.", "EB-CLDR-0001")
15
16
 
16
17
  data class CustomError(
17
18
  val details: String,
18
19
  ) : PluginError(details)
19
20
 
20
- data object EventNotFound : PluginError("Event not found.")
21
+ data object EventNotFound : PluginError("Event not found.", "EB-CLDR-0002")
21
22
 
22
- data object FailedToDelete : PluginError("Failed to delete.")
23
+ data object FailedToDelete : PluginError("Failed to delete.", "EB-CLDR-0004")
23
24
 
24
- data object FailedToModify : PluginError("Failed to modify.")
25
+ data object FailedToModify : PluginError("Failed to modify.", "EB-CLDR-0004")
25
26
 
26
- data object FailedToRetrieveCalendarId : PluginError("Failed to retrieve calendar ID.")
27
+ data object FailedToRetrieveCalendarId : PluginError("Failed to retrieve calendar ID.", "EB-CLDR-0004")
27
28
 
28
- data object FailedToRetrieveEventId : PluginError("Failed to retrieve event ID.")
29
+ data object FailedToRetrieveEventId : PluginError("Failed to retrieve event ID.", "EB-CLDR-0004")
29
30
 
30
- data object FromDateMissing : PluginError("From date must be provided.")
31
+ data object FromDateMissing : PluginError("From date must be provided.", "EB-CLDR-0001")
31
32
 
32
33
  data object InstanceDateMissing : PluginError(
33
34
  "Instance date must be provided for this span on recurring events.",
35
+ "EB-CLDR-0001",
34
36
  )
35
37
 
36
- data object InvalidCalendarId : PluginError("Invalid calendar ID.")
38
+ data class IoError(
39
+ val details: String,
40
+ ) : PluginError(details, "EB-CLDR-0004")
41
+
42
+ data object InvalidCalendarId : PluginError("Invalid calendar ID.", "EB-CLDR-0001")
43
+
44
+ data object InvalidColor : PluginError("Invalid color format.", "EB-CLDR-0001")
45
+
46
+ data object InvalidEventId : PluginError("Invalid event ID.", "EB-CLDR-0001")
37
47
 
38
- data object InvalidColor : PluginError("Invalid color format.")
48
+ data object InvalidInvitees : PluginError("Invalid invitees. Array must contain only strings.", "EB-CLDR-0001")
39
49
 
40
- data object InvalidInvitees : PluginError("Invalid invitees. Array must contain only strings.")
50
+ data object InvalidScope : PluginError("Invalid scope.", "EB-CLDR-0001")
41
51
 
42
- data object InvalidScope : PluginError("Invalid scope.")
52
+ data object MessageMissing : PluginError("Message must be provided.", "EB-CLDR-0001")
43
53
 
44
- data object MessageMissing : PluginError("Message must be provided.")
54
+ data class MissingDeclaration(
55
+ val permission: String,
56
+ ) : PluginError(
57
+ "Missing AndroidManifest permission: $permission.",
58
+ "EB-CLDR-0100",
59
+ )
45
60
 
46
- data object MissingId : PluginError("Event ID must be provided.")
61
+ data object MissingId : PluginError("Event ID must be provided.", "EB-CLDR-0001")
47
62
 
48
- data object MissingScope : PluginError("Scope must be provided.")
63
+ data object MissingScope : PluginError("Scope must be provided.", "EB-CLDR-0001")
49
64
 
50
65
  data object NoCalendarsAvailable : PluginError("No calendars available.")
51
66
 
52
- data object NoFieldsToModify : PluginError("At least one of title or color must be provided.")
67
+ data object NoFieldsToModify : PluginError("At least one of title or color must be provided.", "EB-CLDR-0001")
53
68
 
54
- data object OwnerAccountMissing : PluginError("Owner account must be provided.")
69
+ data class NotSupported(
70
+ val details: String,
71
+ ) : PluginError(details, "EB-CLDR-0005")
72
+
73
+ data object OwnerAccountMissing : PluginError("Owner account must be provided.", "EB-CLDR-0001")
74
+
75
+ data class PermissionDenied(
76
+ val details: String,
77
+ ) : PluginError(details, "EB-CLDR-0020")
55
78
 
56
- data object TitleMissing : PluginError("Title must be provided.")
79
+ data object TitleMissing : PluginError("Title must be provided.", "EB-CLDR-0001")
57
80
 
58
- data object ToDateMissing : PluginError("To date must be provided.")
81
+ data object ToDateMissing : PluginError("To date must be provided.", "EB-CLDR-0001")
59
82
 
60
83
  data object UnhandledPermissionState : PluginError("Unhandled permission state.")
61
84
 
@@ -113,7 +113,7 @@ class CapacitorCalendar(
113
113
  input.recurrence?.let { put(CalendarContract.Events.RRULE, it.toRRule()) }
114
114
  }
115
115
  val uri: Uri? = cr.insert(CalendarContract.Events.CONTENT_URI, values)
116
- val eventId: Long = uri?.lastPathSegment?.toLong() ?: throw PluginError.FailedToRetrieveEventId
116
+ val eventId: Long = uri?.lastPathSegment?.toLongOrNull() ?: throw PluginError.FailedToRetrieveEventId
117
117
  input.attendees?.let { ImplementationHelper.insertAttendeesToEvent(eventId, cr, it) }
118
118
  input.alerts?.let { ImplementationHelper.insertAlertsToEvents(eventId, cr, it) }
119
119
  return CreateEventResult(eventId)
@@ -11,4 +11,5 @@ data class CalendarInfo(
11
11
  val maxReminders: Int?,
12
12
  val location: String?,
13
13
  val isPrimary: Boolean?,
14
+ val allowsContentModifications: Boolean?,
14
15
  )
@@ -13,7 +13,10 @@ data class CreateEventInput(
13
13
  val title: String = call.getString("title") ?: throw PluginError.TitleMissing
14
14
  val isAllDay: Int? = input.isAllDay?.let { if (it) 1 else 0 }
15
15
  val alerts: List<Int>? = ImplementationHelper.jsArrayToIntArray(call.getArray("alerts"))
16
- val calendarId: Long? = call.getString("calendarId")?.toLong()
16
+ val calendarId: Long? =
17
+ call.getString("calendarId")?.let {
18
+ it.toLongOrNull() ?: throw PluginError.InvalidCalendarId
19
+ }
17
20
  val location: String? = input.location
18
21
  val startDate: Long? = input.startDate
19
22
  val endDate: Long? = input.endDate
@@ -7,7 +7,10 @@ import dev.barooni.capacitor.calendar.models.enums.EventSpan
7
7
  data class DeleteEventInput(
8
8
  private val call: PluginCall,
9
9
  ) {
10
- val id = call.getString("id")?.toLongOrNull() ?: throw PluginError.MissingId
10
+ val id =
11
+ call.getString("id")?.let { idString ->
12
+ idString.toLongOrNull() ?: throw PluginError.InvalidEventId
13
+ } ?: throw PluginError.MissingId
11
14
  val instanceDate: Long? = call.getLong("instanceDate")
12
15
  val span: EventSpan = call.getInt("span")?.let { EventSpan.fromInt(it) } ?: EventSpan.THIS_EVENT
13
16
  }
@@ -7,7 +7,10 @@ import dev.barooni.capacitor.calendar.models.enums.EventSpan
7
7
  data class DeleteEventWithPromptInput(
8
8
  private val call: PluginCall,
9
9
  ) {
10
- val id: Long = call.getString("id")?.toLongOrNull() ?: throw PluginError.MissingId
10
+ val id: Long =
11
+ call.getString("id")?.let { idString ->
12
+ idString.toLongOrNull() ?: throw PluginError.InvalidEventId
13
+ } ?: throw PluginError.MissingId
11
14
  val title: String = call.getString("title") ?: throw PluginError.TitleMissing
12
15
  val message: String = call.getString("message") ?: throw PluginError.MessageMissing
13
16
  val confirmButtonText: String = call.getString("confirmButtonText") ?: "Delete"
@@ -9,9 +9,15 @@ import dev.barooni.capacitor.calendar.utils.ImplementationHelper
9
9
  data class ModifyEvent(
10
10
  private val call: PluginCall,
11
11
  ) {
12
- val id: Long = call.getString("id")?.toLong() ?: throw PluginError.MissingId
12
+ val id: Long =
13
+ call.getString("id")?.let { idString ->
14
+ idString.toLongOrNull() ?: throw PluginError.InvalidEventId
15
+ } ?: throw PluginError.MissingId
13
16
  val title: String? = call.getString("title")
14
- val calendarId: Long? = call.getString("calendarId")?.toLong()
17
+ val calendarId: Long? =
18
+ call.getString("calendarId")?.let {
19
+ it.toLongOrNull() ?: throw PluginError.InvalidCalendarId
20
+ }
15
21
  val location: String? = call.getString("location")
16
22
  val startDate: Long? = call.getLong("startDate")?.let { ImplementationHelper.getCalendarFromTimestamp(it).timeInMillis }
17
23
  val endDate: Long? = call.getLong("endDate")?.let { ImplementationHelper.getCalendarFromTimestamp(it).timeInMillis }
@@ -18,7 +18,7 @@ data class ModifyEventWithPromptInput(
18
18
 
19
19
  init {
20
20
  val idString = call.getString("id") ?: throw PluginError.MissingId
21
- id = idString.toLong()
21
+ id = idString.toLongOrNull() ?: throw PluginError.InvalidEventId
22
22
  val uri: Uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, id)
23
23
  intent.setData(uri)
24
24
  setEventUpdates()
@@ -19,7 +19,7 @@ data class GetDefaultCalendarResult(
19
19
  put("color", cal.color)
20
20
  put("colorName", null)
21
21
  put("isImmutable", null)
22
- put("allowsContentModifications", null)
22
+ put("allowsContentModifications", cal.allowsContentModifications)
23
23
  put("type", null)
24
24
  put("isSubscribed", null)
25
25
  put("source", null)
@@ -24,7 +24,7 @@ data class ListCalendarsResult(
24
24
  obj.put("color", calendar.color)
25
25
  obj.put("colorName", null)
26
26
  obj.put("isImmutable", null)
27
- obj.put("allowsContentModifications", null)
27
+ obj.put("allowsContentModifications", calendar.allowsContentModifications)
28
28
  obj.put("type", null)
29
29
  obj.put("isSubscribed", null)
30
30
  obj.put("source", null)