@ebarooni/capacitor-calendar 8.2.0 → 8.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.
Files changed (32) hide show
  1. package/README.md +40 -22
  2. package/android/src/main/java/dev/barooni/capacitor/calendar/PluginError.kt +4 -0
  3. package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CapacitorCalendar.kt +29 -6
  4. package/android/src/main/java/dev/barooni/capacitor/calendar/models/data/EventDeleteInfo.kt +8 -0
  5. package/android/src/main/java/dev/barooni/capacitor/calendar/models/enums/EventSpan.kt +13 -0
  6. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/DeleteEventInput.kt +4 -1
  7. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/DeleteEventWithPromptInput.kt +4 -1
  8. package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/DeleteEventsByIdInput.kt +2 -0
  9. package/android/src/main/java/dev/barooni/capacitor/calendar/utils/ImplementationHelper.kt +272 -4
  10. package/dist/docs.json +177 -37
  11. package/dist/esm/schemas/enums/event-span.d.ts +4 -0
  12. package/dist/esm/schemas/enums/event-span.js +4 -0
  13. package/dist/esm/schemas/enums/event-span.js.map +1 -1
  14. package/dist/esm/schemas/interfaces/delete-event-options.d.ts +28 -3
  15. package/dist/esm/schemas/interfaces/delete-event-options.js.map +1 -1
  16. package/dist/esm/schemas/interfaces/delete-event-with-prompt-options.d.ts +38 -13
  17. package/dist/esm/schemas/interfaces/delete-event-with-prompt-options.js.map +1 -1
  18. package/dist/esm/schemas/interfaces/delete-events-by-id-options.d.ts +15 -3
  19. package/dist/esm/schemas/interfaces/delete-events-by-id-options.js.map +1 -1
  20. package/dist/esm/sub-definitions/calendar-operations.d.ts +1 -1
  21. package/dist/esm/sub-definitions/calendar-operations.js.map +1 -1
  22. package/dist/plugin.cjs.js +4 -0
  23. package/dist/plugin.cjs.js.map +1 -1
  24. package/dist/plugin.js +4 -0
  25. package/dist/plugin.js.map +1 -1
  26. package/ios/Plugin/Implementation/CapacitorCalendar.swift +3 -3
  27. package/ios/Plugin/Models/Enums/EventSpan.swift +8 -0
  28. package/ios/Plugin/Models/Inputs/DeleteEventInput.swift +9 -4
  29. package/ios/Plugin/Models/Inputs/DeleteEventWithPromptInput.swift +9 -4
  30. package/ios/Plugin/Models/Inputs/DeleteEventsByIdInput.swift +9 -4
  31. package/ios/Plugin/Utils/ImplementationHelper.swift +7 -2
  32. package/package.json +1 -1
package/README.md CHANGED
@@ -14,6 +14,9 @@
14
14
  <a href="https://capacitorjs.com/">
15
15
  <img src="https://img.shields.io/badge/Capacitor-8.x-119EFF.svg?style=flat-square" />
16
16
  </a>
17
+ <a href="mcp/README.md">
18
+ <img src="https://img.shields.io/badge/MCP-161b22?style=flat&logo=modelcontextprotocol&logoColor=5FB8AF" />
19
+ </a>
17
20
  </p>
18
21
 
19
22
  ![capacitor-calendar-logo](assets/images/text-logo.png)
@@ -43,6 +46,16 @@ Full-featured Capacitor plugin for native calendar and reminders access. Manage
43
46
  - **Fast support.** Questions, bug reports, and integration help are handled promptly.
44
47
  - **Reduced vendor risk.** Relying on a single plugin provider for all your needs is a liability. Choosing specialized, independent maintainers keeps your stack resilient.
45
48
 
49
+ ## MCP Server
50
+
51
+ `@ebarooni/capacitor-calendar` ships an official [MCP](https://modelcontextprotocol.io) server so AI coding assistants can work with the plugin accurately.
52
+
53
+ ```bash
54
+ docker run --rm -d --name capacitor-calendar-mcp -p 8080:8080 ghcr.io/ebarooni/capacitor-calendar-mcp:1.0.0
55
+ ```
56
+
57
+ See [`mcp/README.md`](mcp/README.md) for client configuration and full details.
58
+
46
59
  ## Table of Contents
47
60
 
48
61
  - [Installation](#installation)
@@ -65,7 +78,7 @@ npx cap sync
65
78
 
66
79
  ## Demo
67
80
 
68
- | iOS 26 | Android 16 |
81
+ | iOS 26 | Android 17 |
69
82
  | :-----------------------------: | :---------------------------------: |
70
83
  | ![](./assets/demo/ios-demo.gif) | ![](./assets/demo/android-demo.gif) |
71
84
 
@@ -584,7 +597,7 @@ Retrieves the events within a date range.
584
597
  commit() => Promise<void>
585
598
  ```
586
599
 
587
- Save the changes to the calendar.
600
+ Saves pending calendar changes.
588
601
 
589
602
  **Since:** 7.1.0
590
603
 
@@ -1131,30 +1144,35 @@ Update a reminders list with options.
1131
1144
 
1132
1145
  #### DeleteEventsByIdOptions
1133
1146
 
1134
- | Prop | Type | Description | Default | Since | Platform |
1135
- | ---------- | ----------------------------------------------- | --------------------- | --------------------------------- | ----- | -------- |
1136
- | **`ids`** | <code>string[]</code> | | | 7.1.0 | |
1137
- | **`span`** | <code><a href="#eventspan">EventSpan</a></code> | The span of deletion. | <code>EventSpan.THIS_EVENT</code> | | iOS |
1147
+ | Prop | Type | Description | Default | Since | Platform |
1148
+ | ------------ | ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | ----- | ------------ |
1149
+ | **`commit`** | <code>boolean</code> | Whether to save the deletion to the event store immediately. Pass `false` to batch multiple changes and commit them together using `CapacitorCalendar.commit()`, which is more efficient than committing each save individually. | <code>true</code> | 8.3.0 | iOS |
1150
+ | **`ids`** | <code>string[]</code> | | | 7.1.0 | |
1151
+ | **`span`** | <code><a href="#eventspan">EventSpan</a></code> | How much of a recurring series to delete. On Android, `THIS_EVENT` cannot target a single recurring occurrence here — use `deleteEvent` with `instanceDate`. Those ids are reported in `failed`. | <code>EventSpan.THIS_EVENT</code> | 7.1.0 | Android, iOS |
1138
1152
 
1139
1153
 
1140
1154
  #### DeleteEventOptions
1141
1155
 
1142
- | Prop | Type | Description | Default | Since | Platform |
1143
- | ---------- | ----------------------------------------------- | --------------------- | --------------------------------- | ----- | -------- |
1144
- | **`id`** | <code>string</code> | | | 7.1.0 | |
1145
- | **`span`** | <code><a href="#eventspan">EventSpan</a></code> | The span of deletion. | <code>EventSpan.THIS_EVENT</code> | | iOS |
1156
+ | Prop | Type | Description | Default | Since | Platform |
1157
+ | ------------------ | ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | ----- | ------------ |
1158
+ | **`commit`** | <code>boolean</code> | Whether to save the deletion to the event store immediately. Pass `false` to batch multiple changes and commit them together using `CapacitorCalendar.commit()`, which is more efficient than committing each save individually. | <code>true</code> | 8.3.0 | iOS |
1159
+ | **`id`** | <code>string</code> | The ID of the event to delete. | | 7.1.0 | Android, iOS |
1160
+ | **`instanceDate`** | <code>number</code> | The start time of the occurrence to delete, in milliseconds since the epoch. Use `startDate` from `listEventsInRange`. On Android, required for `THIS_EVENT` on recurring events. If omitted with `THIS_AND_FUTURE_EVENTS`, the whole series is deleted. | | 8.3.0 | Android |
1161
+ | **`span`** | <code><a href="#eventspan">EventSpan</a></code> | How much of a recurring series to delete. | <code>EventSpan.THIS_EVENT</code> | 7.1.0 | Android, iOS |
1146
1162
 
1147
1163
 
1148
1164
  #### DeleteEventWithPromptOptions
1149
1165
 
1150
- | Prop | Type | Description | Default | Since | Platform |
1151
- | ----------------------- | ----------------------------------------------- | ----------------------------------- | --------------------------------- | ----- | ------------ |
1152
- | **`id`** | <code>string</code> | | | 7.1.0 | |
1153
- | **`span`** | <code><a href="#eventspan">EventSpan</a></code> | The span of deletion. | <code>EventSpan.THIS_EVENT</code> | | iOS |
1154
- | **`title`** | <code>string</code> | Title of the dialog. | | 7.1.0 | Android, iOS |
1155
- | **`message`** | <code>string</code> | Message of the dialog. | | 7.1.0 | Android, iOS |
1156
- | **`confirmButtonText`** | <code>string</code> | Text to show on the confirm button. | <code>'Delete'</code> | 7.1.0 | Android, iOS |
1157
- | **`cancelButtonText`** | <code>string</code> | Text to show on the cancel button. | <code>'Cancel'</code> | 7.1.0 | Android, iOS |
1166
+ | Prop | Type | Description | Default | Since | Platform |
1167
+ | ----------------------- | ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | ----- | ------------ |
1168
+ | **`cancelButtonText`** | <code>string</code> | Text to show on the cancel button. | <code>'Cancel'</code> | 7.1.0 | Android, iOS |
1169
+ | **`commit`** | <code>boolean</code> | Whether to save the deletion to the event store immediately. Pass `false` to batch multiple changes and commit them together using `CapacitorCalendar.commit()`, which is more efficient than committing each save individually. | <code>true</code> | 8.3.0 | iOS |
1170
+ | **`confirmButtonText`** | <code>string</code> | Text to show on the confirm button. | <code>'Delete'</code> | 7.1.0 | Android, iOS |
1171
+ | **`id`** | <code>string</code> | The ID of the event to delete. | | 7.1.0 | Android, iOS |
1172
+ | **`instanceDate`** | <code>number</code> | The start time of the occurrence to delete, in milliseconds since the epoch. Use `startDate` from `listEventsInRange`. On Android, required for `THIS_EVENT` on recurring events. If omitted with `THIS_AND_FUTURE_EVENTS`, the whole series is deleted. | | 8.3.0 | Android |
1173
+ | **`message`** | <code>string</code> | Message of the dialog. | | 7.1.0 | Android, iOS |
1174
+ | **`span`** | <code><a href="#eventspan">EventSpan</a></code> | How much of a recurring series to delete. | <code>EventSpan.THIS_EVENT</code> | 7.1.0 | Android, iOS |
1175
+ | **`title`** | <code>string</code> | Title of the dialog. | | 7.1.0 | Android, iOS |
1158
1176
 
1159
1177
 
1160
1178
  #### CalendarEvent
@@ -1483,10 +1501,10 @@ Construct a type with a set of properties K of type T
1483
1501
 
1484
1502
  #### EventSpan
1485
1503
 
1486
- | Members | Since |
1487
- | ---------------------------- | ----- |
1488
- | **`THIS_EVENT`** | 7.1.0 |
1489
- | **`THIS_AND_FUTURE_EVENTS`** | 7.1.0 |
1504
+ | Members | Description | Since |
1505
+ | ---------------------------- | --------------------------------------------------------------- | ----- |
1506
+ | **`THIS_EVENT`** | Only the identified event or occurrence. | 7.1.0 |
1507
+ | **`THIS_AND_FUTURE_EVENTS`** | The identified occurrence and future occurrences in the series. | 7.1.0 |
1490
1508
 
1491
1509
 
1492
1510
  #### EventStatus
@@ -13,6 +13,10 @@ sealed class PluginError(
13
13
 
14
14
  data object MissingId : PluginError("Event ID must be provided.")
15
15
 
16
+ data object InstanceDateMissing : PluginError(
17
+ "Instance date must be provided for this span on recurring events.",
18
+ )
19
+
16
20
  data object TitleMissing : PluginError("Title must be provided.")
17
21
 
18
22
  data object FailedToRetrieveEventId : PluginError("Failed to retrieve event ID.")
@@ -201,10 +201,18 @@ class CapacitorCalendar(
201
201
  val cr = plugin.context.contentResolver
202
202
  val result = DeleteEventsByIdResult()
203
203
  input.ids.forEach { id ->
204
- val deleted = ImplementationHelper.deleteEvent(cr, id)
205
- if (deleted) {
206
- result.deleted(id)
207
- } else {
204
+ try {
205
+ if (ImplementationHelper.requiresInstanceDateForDelete(cr, id, input.span)) {
206
+ result.failed(id)
207
+ return@forEach
208
+ }
209
+ val deleted = ImplementationHelper.deleteEvent(cr, id, input.span, null)
210
+ if (deleted) {
211
+ result.deleted(id)
212
+ } else {
213
+ result.failed(id)
214
+ }
215
+ } catch (_: Exception) {
208
216
  result.failed(id)
209
217
  }
210
218
  }
@@ -213,7 +221,13 @@ class CapacitorCalendar(
213
221
 
214
222
  fun deleteEvent(input: DeleteEventInput) {
215
223
  val cr = plugin.context.contentResolver
216
- val deleted = ImplementationHelper.deleteEvent(cr, input.id)
224
+ ImplementationHelper.ensureInstanceDatePresentIfRequired(
225
+ cr,
226
+ input.id,
227
+ input.span,
228
+ input.instanceDate,
229
+ )
230
+ val deleted = ImplementationHelper.deleteEvent(cr, input.id, input.span, input.instanceDate)
217
231
  if (!deleted) {
218
232
  throw PluginError.FailedToDelete
219
233
  }
@@ -224,6 +238,12 @@ class CapacitorCalendar(
224
238
  onComplete: (DeleteEventWithPromptResult) -> Unit,
225
239
  ) {
226
240
  val cr = plugin.context.contentResolver
241
+ ImplementationHelper.ensureInstanceDatePresentIfRequired(
242
+ cr,
243
+ input.id,
244
+ input.span,
245
+ input.instanceDate,
246
+ )
227
247
  val builder =
228
248
  AlertDialog
229
249
  .Builder(plugin.context)
@@ -234,7 +254,10 @@ class CapacitorCalendar(
234
254
  val result = DeleteEventWithPromptResult(false)
235
255
  onComplete(result)
236
256
  }.setPositiveButton(input.confirmButtonText) { _, _ ->
237
- val result = DeleteEventWithPromptResult(ImplementationHelper.deleteEvent(cr, input.id))
257
+ val result =
258
+ DeleteEventWithPromptResult(
259
+ ImplementationHelper.deleteEvent(cr, input.id, input.span, input.instanceDate),
260
+ )
238
261
  onComplete(result)
239
262
  }
240
263
 
@@ -0,0 +1,8 @@
1
+ package dev.barooni.capacitor.calendar.models.data
2
+
3
+ data class EventDeleteInfo(
4
+ val dtStart: Long?,
5
+ val originalId: Long?,
6
+ val originalInstanceTime: Long?,
7
+ val rrule: String?,
8
+ )
@@ -0,0 +1,13 @@
1
+ package dev.barooni.capacitor.calendar.models.enums
2
+
3
+ enum class EventSpan(
4
+ val value: Int,
5
+ ) {
6
+ THIS_EVENT(0),
7
+ THIS_AND_FUTURE_EVENTS(1),
8
+ ;
9
+
10
+ companion object {
11
+ fun fromInt(value: Int): EventSpan? = entries.find { it.value == value }
12
+ }
13
+ }
@@ -2,9 +2,12 @@ package dev.barooni.capacitor.calendar.models.inputs
2
2
 
3
3
  import com.getcapacitor.PluginCall
4
4
  import dev.barooni.capacitor.calendar.PluginError
5
+ import dev.barooni.capacitor.calendar.models.enums.EventSpan
5
6
 
6
7
  data class DeleteEventInput(
7
8
  private val call: PluginCall,
8
9
  ) {
9
- val id = call.getString("id")?.toLong() ?: throw PluginError.MissingId
10
+ val id = call.getString("id")?.toLongOrNull() ?: throw PluginError.MissingId
11
+ val instanceDate: Long? = call.getLong("instanceDate")
12
+ val span: EventSpan = call.getInt("span")?.let { EventSpan.fromInt(it) } ?: EventSpan.THIS_EVENT
10
13
  }
@@ -2,13 +2,16 @@ package dev.barooni.capacitor.calendar.models.inputs
2
2
 
3
3
  import com.getcapacitor.PluginCall
4
4
  import dev.barooni.capacitor.calendar.PluginError
5
+ import dev.barooni.capacitor.calendar.models.enums.EventSpan
5
6
 
6
7
  data class DeleteEventWithPromptInput(
7
8
  private val call: PluginCall,
8
9
  ) {
9
- val id: Long = call.getString("id")?.toLong() ?: throw PluginError.MissingId
10
+ val id: Long = call.getString("id")?.toLongOrNull() ?: throw PluginError.MissingId
10
11
  val title: String = call.getString("title") ?: throw PluginError.TitleMissing
11
12
  val message: String = call.getString("message") ?: throw PluginError.MessageMissing
12
13
  val confirmButtonText: String = call.getString("confirmButtonText") ?: "Delete"
13
14
  val cancelButtonText: String = call.getString("cancelButtonText") ?: "Cancel"
15
+ val instanceDate: Long? = call.getLong("instanceDate")
16
+ val span: EventSpan = call.getInt("span")?.let { EventSpan.fromInt(it) } ?: EventSpan.THIS_EVENT
14
17
  }
@@ -2,10 +2,12 @@ package dev.barooni.capacitor.calendar.models.inputs
2
2
 
3
3
  import com.getcapacitor.PluginCall
4
4
  import dev.barooni.capacitor.calendar.PluginError
5
+ import dev.barooni.capacitor.calendar.models.enums.EventSpan
5
6
  import dev.barooni.capacitor.calendar.utils.ImplementationHelper
6
7
 
7
8
  data class DeleteEventsByIdInput(
8
9
  private val call: PluginCall,
9
10
  ) {
10
11
  var ids = call.getArray("ids")?.let { ImplementationHelper.jsArrayToLongArray(it) } ?: throw PluginError.MissingId
12
+ val span: EventSpan = call.getInt("span")?.let { EventSpan.fromInt(it) } ?: EventSpan.THIS_EVENT
11
13
  }
@@ -8,9 +8,13 @@ import com.getcapacitor.JSArray
8
8
  import com.getcapacitor.PluginCall
9
9
  import dev.barooni.capacitor.calendar.PluginError
10
10
  import dev.barooni.capacitor.calendar.models.data.CalendarInfo
11
+ import dev.barooni.capacitor.calendar.models.data.EventDeleteInfo
11
12
  import dev.barooni.capacitor.calendar.models.data.EventGuest
13
+ import dev.barooni.capacitor.calendar.models.enums.EventSpan
12
14
  import org.json.JSONObject
13
15
  import java.util.Calendar
16
+ import java.util.Locale
17
+ import java.util.TimeZone
14
18
 
15
19
  class ImplementationHelper {
16
20
  companion object {
@@ -36,7 +40,7 @@ class ImplementationHelper {
36
40
 
37
41
  fun jsArrayToLongArray(array: JSArray): List<Long> {
38
42
  val list = array.toList<String>()
39
- return list.map { it.toLong() }
43
+ return list.map { it.toLongOrNull() ?: throw PluginError.MissingId }
40
44
  }
41
45
 
42
46
  fun hexToColorInt(hex: String?): Int? =
@@ -272,13 +276,277 @@ class ImplementationHelper {
272
276
  return calendars
273
277
  }
274
278
 
279
+ fun requiresInstanceDateForDelete(
280
+ cr: ContentResolver,
281
+ eventId: Long,
282
+ span: EventSpan,
283
+ ): Boolean = needsInstanceDate(queryEventDeleteInfo(cr, eventId), span)
284
+
285
+ fun ensureInstanceDatePresentIfRequired(
286
+ cr: ContentResolver,
287
+ eventId: Long,
288
+ span: EventSpan,
289
+ instanceDate: Long?,
290
+ ) {
291
+ if (requiresInstanceDateForDelete(cr, eventId, span) && instanceDate == null) {
292
+ throw PluginError.InstanceDateMissing
293
+ }
294
+ }
295
+
275
296
  fun deleteEvent(
276
297
  cr: ContentResolver,
277
298
  eventId: Long,
299
+ span: EventSpan,
300
+ instanceDate: Long? = null,
301
+ ): Boolean {
302
+ val info = queryEventDeleteInfo(cr, eventId)
303
+ val masterId = info.originalId ?: eventId
304
+ val isException = info.originalId != null
305
+ val isRecurringMaster = !info.rrule.isNullOrBlank() && !isException
306
+
307
+ return when (span) {
308
+ EventSpan.THIS_EVENT -> {
309
+ when {
310
+ isException -> {
311
+ deleteEventRow(cr, eventId)
312
+ }
313
+
314
+ isRecurringMaster -> {
315
+ val date = instanceDate ?: throw PluginError.InstanceDateMissing
316
+ insertCanceledException(cr, masterId, date)
317
+ }
318
+
319
+ else -> {
320
+ deleteEventRow(cr, eventId)
321
+ }
322
+ }
323
+ }
324
+
325
+ EventSpan.THIS_AND_FUTURE_EVENTS -> {
326
+ when {
327
+ isRecurringMaster -> {
328
+ // No instanceDate → treat as deleting from series start (whole series).
329
+ if (instanceDate == null) {
330
+ deleteEventRow(cr, masterId)
331
+ } else {
332
+ deleteThisAndFuture(cr, masterId, instanceDate)
333
+ }
334
+ }
335
+
336
+ isException -> {
337
+ val date =
338
+ instanceDate
339
+ ?: info.originalInstanceTime
340
+ ?: throw PluginError.InstanceDateMissing
341
+ deleteThisAndFuture(cr, masterId, date)
342
+ }
343
+
344
+ else -> {
345
+ deleteEventRow(cr, eventId)
346
+ }
347
+ }
348
+ }
349
+ }
350
+ }
351
+
352
+ private fun needsInstanceDate(
353
+ info: EventDeleteInfo,
354
+ span: EventSpan,
355
+ ): Boolean {
356
+ val isException = info.originalId != null
357
+ val isRecurringMaster = !info.rrule.isNullOrBlank() && !isException
358
+ return when (span) {
359
+ // Only occurrence cancel on a series master needs an explicit instance time.
360
+ EventSpan.THIS_EVENT -> isRecurringMaster
361
+
362
+ // Masters fall back to whole-series delete; exceptions fall back to ORIGINAL_INSTANCE_TIME.
363
+ EventSpan.THIS_AND_FUTURE_EVENTS -> isException && info.originalInstanceTime == null
364
+ }
365
+ }
366
+
367
+ private fun deleteEventRow(
368
+ cr: ContentResolver,
369
+ eventId: Long,
370
+ ): Boolean {
371
+ val uri =
372
+ ContentUris.withAppendedId(
373
+ CalendarContract.Events.CONTENT_URI,
374
+ eventId,
375
+ )
376
+ return cr.delete(uri, null, null) > 0
377
+ }
378
+
379
+ private fun insertCanceledException(
380
+ cr: ContentResolver,
381
+ masterId: Long,
382
+ instanceDate: Long,
383
+ ): Boolean {
384
+ val values =
385
+ ContentValues().apply {
386
+ put(CalendarContract.Events.ORIGINAL_INSTANCE_TIME, instanceDate)
387
+ put(CalendarContract.Events.STATUS, CalendarContract.Events.STATUS_CANCELED)
388
+ }
389
+ val uri =
390
+ ContentUris.withAppendedId(
391
+ CalendarContract.Events.CONTENT_EXCEPTION_URI,
392
+ masterId,
393
+ )
394
+ return cr.insert(uri, values) != null
395
+ }
396
+
397
+ private fun deleteThisAndFuture(
398
+ cr: ContentResolver,
399
+ masterId: Long,
400
+ instanceDate: Long,
278
401
  ): Boolean {
279
- val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId)
280
- val rowsDeleted = cr.delete(uri, null, null)
281
- return rowsDeleted > 0
402
+ val info = queryEventDeleteInfo(cr, masterId)
403
+ val rrule = info.rrule
404
+ if (rrule.isNullOrBlank()) {
405
+ return false
406
+ }
407
+
408
+ // Targeting the first occurrence (or earlier) removes the whole series.
409
+ val seriesStart = info.dtStart
410
+ if (seriesStart != null && instanceDate <= seriesStart) {
411
+ return deleteEventRow(cr, masterId)
412
+ }
413
+
414
+ val values =
415
+ ContentValues().apply {
416
+ put(CalendarContract.Events.RRULE, rruleEndingBefore(rrule, instanceDate))
417
+ }
418
+ val uri =
419
+ ContentUris.withAppendedId(
420
+ CalendarContract.Events.CONTENT_URI,
421
+ masterId,
422
+ )
423
+ return cr.update(uri, values, null, null) > 0
424
+ }
425
+
426
+ private fun rruleEndingBefore(
427
+ rrule: String,
428
+ instanceDateMs: Long,
429
+ ): String {
430
+ // UNTIL is inclusive; end the series one second before this occurrence.
431
+ val untilMs = instanceDateMs - 1000L
432
+ val untilPart = "UNTIL=${formatUntilUtc(untilMs)}"
433
+ val parts =
434
+ rrule
435
+ .split(";")
436
+ .filter { part ->
437
+ val key = part.substringBefore("=").uppercase()
438
+ key.isNotEmpty() && key != "UNTIL" && key != "COUNT"
439
+ }.toMutableList()
440
+ parts.add(untilPart)
441
+ return parts.joinToString(";")
442
+ }
443
+
444
+ private fun formatUntilUtc(untilMs: Long): String {
445
+ val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
446
+ calendar.timeInMillis = untilMs
447
+
448
+ val year = calendar.get(Calendar.YEAR)
449
+ val month = calendar.get(Calendar.MONTH) + 1
450
+ val day = calendar.get(Calendar.DAY_OF_MONTH)
451
+ val hour = calendar.get(Calendar.HOUR_OF_DAY)
452
+ val minute = calendar.get(Calendar.MINUTE)
453
+ val second = calendar.get(Calendar.SECOND)
454
+
455
+ return String.format(
456
+ Locale.US,
457
+ "%04d%02d%02dT%02d%02d%02dZ",
458
+ year,
459
+ month,
460
+ day,
461
+ hour,
462
+ minute,
463
+ second,
464
+ )
465
+ }
466
+
467
+ private fun queryEventDeleteInfo(
468
+ cr: ContentResolver,
469
+ eventId: Long,
470
+ ): EventDeleteInfo {
471
+ val uri =
472
+ ContentUris.withAppendedId(
473
+ CalendarContract.Events.CONTENT_URI,
474
+ eventId,
475
+ )
476
+
477
+ val projection =
478
+ arrayOf(
479
+ CalendarContract.Events.DTSTART,
480
+ CalendarContract.Events.ORIGINAL_ID,
481
+ CalendarContract.Events.ORIGINAL_INSTANCE_TIME,
482
+ CalendarContract.Events.RRULE,
483
+ )
484
+
485
+ cr
486
+ .query(
487
+ uri,
488
+ projection,
489
+ null,
490
+ null,
491
+ null,
492
+ )?.use { cursor ->
493
+ if (!cursor.moveToFirst()) {
494
+ return EventDeleteInfo(
495
+ dtStart = null,
496
+ originalId = null,
497
+ originalInstanceTime = null,
498
+ rrule = null,
499
+ )
500
+ }
501
+
502
+ val dtStartIndex = cursor.getColumnIndex(CalendarContract.Events.DTSTART)
503
+ val originalIdIndex = cursor.getColumnIndex(CalendarContract.Events.ORIGINAL_ID)
504
+ val originalInstanceTimeIndex =
505
+ cursor.getColumnIndex(CalendarContract.Events.ORIGINAL_INSTANCE_TIME)
506
+ val rruleIndex = cursor.getColumnIndex(CalendarContract.Events.RRULE)
507
+
508
+ val dtStart =
509
+ if (dtStartIndex >= 0 && !cursor.isNull(dtStartIndex)) {
510
+ cursor.getLong(dtStartIndex)
511
+ } else {
512
+ null
513
+ }
514
+
515
+ val originalId =
516
+ if (originalIdIndex >= 0 && !cursor.isNull(originalIdIndex)) {
517
+ cursor.getLong(originalIdIndex)
518
+ } else {
519
+ null
520
+ }
521
+
522
+ val originalInstanceTime =
523
+ if (originalInstanceTimeIndex >= 0 && !cursor.isNull(originalInstanceTimeIndex)) {
524
+ cursor.getLong(originalInstanceTimeIndex)
525
+ } else {
526
+ null
527
+ }
528
+
529
+ val rrule =
530
+ if (rruleIndex >= 0 && !cursor.isNull(rruleIndex)) {
531
+ cursor.getString(rruleIndex)
532
+ } else {
533
+ null
534
+ }
535
+
536
+ return EventDeleteInfo(
537
+ dtStart = dtStart,
538
+ originalId = originalId,
539
+ originalInstanceTime = originalInstanceTime,
540
+ rrule = rrule,
541
+ )
542
+ }
543
+
544
+ return EventDeleteInfo(
545
+ dtStart = null,
546
+ originalId = null,
547
+ originalInstanceTime = null,
548
+ rrule = null,
549
+ )
282
550
  }
283
551
 
284
552
  fun getEventAlerts(