@ebarooni/capacitor-calendar 8.11.0 → 8.12.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 +279 -103
- package/android/src/main/java/dev/barooni/capacitor/calendar/CapacitorCalendarPlugin.kt +37 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CalendarChangeObserver.kt +181 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CapacitorCalendar.kt +58 -8
- package/android/src/main/java/dev/barooni/capacitor/calendar/models/data/EventDeleteInfo.kt +6 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/StartListeningInput.kt +20 -0
- package/android/src/main/java/dev/barooni/capacitor/calendar/utils/ImplementationHelper.kt +88 -67
- package/dist/docs.json +1383 -976
- package/dist/esm/definitions.d.ts +2 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +12 -2
- package/dist/esm/index.js +4 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/schemas/enums/capacitor-calendar-error-code.d.ts +2 -2
- package/dist/esm/schemas/enums/capacitor-calendar-error-code.js +2 -2
- package/dist/esm/schemas/enums/capacitor-calendar-error-code.js.map +1 -1
- package/dist/esm/schemas/interfaces/calendar-changed-event.d.ts +52 -0
- package/dist/esm/schemas/interfaces/calendar-changed-event.js +2 -0
- package/dist/esm/schemas/interfaces/calendar-changed-event.js.map +1 -0
- package/dist/esm/schemas/interfaces/calendar-links.d.ts +32 -0
- package/dist/esm/schemas/interfaces/calendar-links.js +2 -0
- package/dist/esm/schemas/interfaces/calendar-links.js.map +1 -0
- package/dist/esm/schemas/interfaces/create-event-options.d.ts +18 -0
- package/dist/esm/schemas/interfaces/create-event-options.js.map +1 -1
- package/dist/esm/schemas/interfaces/create-event-with-prompt-options.d.ts +9 -0
- package/dist/esm/schemas/interfaces/create-event-with-prompt-options.js.map +1 -1
- package/dist/esm/schemas/interfaces/create-ics-file-options.d.ts +20 -0
- package/dist/esm/schemas/interfaces/create-ics-file-options.js +2 -0
- package/dist/esm/schemas/interfaces/create-ics-file-options.js.map +1 -0
- package/dist/esm/schemas/interfaces/open-calendar-options.d.ts +9 -0
- package/dist/esm/schemas/interfaces/open-calendar-options.js.map +1 -1
- package/dist/esm/schemas/interfaces/share-ics-file-options.d.ts +17 -0
- package/dist/esm/schemas/interfaces/share-ics-file-options.js +2 -0
- package/dist/esm/schemas/interfaces/share-ics-file-options.js.map +1 -0
- package/dist/esm/schemas/interfaces/start-listening-options.d.ts +46 -0
- package/dist/esm/schemas/interfaces/start-listening-options.js +2 -0
- package/dist/esm/schemas/interfaces/start-listening-options.js.map +1 -0
- package/dist/esm/schemas/types/calendar-day-view-provider.d.ts +7 -0
- package/dist/esm/schemas/types/calendar-day-view-provider.js +2 -0
- package/dist/esm/schemas/types/calendar-day-view-provider.js.map +1 -0
- package/dist/esm/schemas/types/calendar-link-provider.d.ts +6 -0
- package/dist/esm/schemas/types/calendar-link-provider.js +2 -0
- package/dist/esm/schemas/types/calendar-link-provider.js.map +1 -0
- package/dist/esm/sub-definitions/calendar-listening.d.ts +49 -0
- package/dist/esm/sub-definitions/calendar-listening.js +2 -0
- package/dist/esm/sub-definitions/calendar-listening.js.map +1 -0
- package/dist/esm/sub-definitions/calendar-operations.d.ts +2 -1
- package/dist/esm/sub-definitions/calendar-operations.js.map +1 -1
- package/dist/esm/web/calendar-links.d.ts +26 -0
- package/dist/esm/web/calendar-links.js +114 -0
- package/dist/esm/web/calendar-links.js.map +1 -0
- package/dist/esm/web/create-ics-file.d.ts +21 -0
- package/dist/esm/web/create-ics-file.js +30 -0
- package/dist/esm/web/create-ics-file.js.map +1 -0
- package/dist/esm/web/ics.d.ts +11 -0
- package/dist/esm/web/ics.js +48 -32
- package/dist/esm/web/ics.js.map +1 -1
- package/dist/esm/web/share-ics-file.d.ts +21 -0
- package/dist/esm/web/share-ics-file.js +46 -0
- package/dist/esm/web/share-ics-file.js.map +1 -0
- package/dist/esm/web.d.ts +4 -1
- package/dist/esm/web.js +18 -4
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +292 -72
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +292 -72
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/CapacitorCalendarPlugin.swift +19 -0
- package/ios/Plugin/Implementation/CalendarChangeObserver.swift +259 -0
- package/ios/Plugin/Implementation/CapacitorCalendar.swift +15 -1
- package/ios/Plugin/Models/Inputs/DeleteReminderInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/DeleteReminderWithPromptInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/DeleteRemindersListInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/GetReminderByIdInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/ModifyReminderInput.swift +1 -1
- package/ios/Plugin/Models/Inputs/StartListeningInput.swift +26 -0
- package/ios/Plugin/Models/Inputs/UpdateRemindersListInput.swift +1 -1
- package/ios/Plugin/PluginConfig.swift +2 -0
- package/ios/Plugin/PluginError.swift +25 -3
- package/ios/Plugin/Utils/ImplementationHelper.swift +16 -30
- package/ios/Plugin/Utils/PluginCallRejecter.swift +13 -0
- package/package.json +1 -1
|
@@ -2,6 +2,7 @@ package dev.barooni.capacitor.calendar
|
|
|
2
2
|
|
|
3
3
|
import android.Manifest
|
|
4
4
|
import androidx.activity.result.ActivityResult
|
|
5
|
+
import com.getcapacitor.JSObject
|
|
5
6
|
import com.getcapacitor.Plugin
|
|
6
7
|
import com.getcapacitor.PluginCall
|
|
7
8
|
import com.getcapacitor.PluginMethod
|
|
@@ -28,6 +29,7 @@ import dev.barooni.capacitor.calendar.models.inputs.ModifyEventWithPromptInput
|
|
|
28
29
|
import dev.barooni.capacitor.calendar.models.inputs.OpenCalendarInput
|
|
29
30
|
import dev.barooni.capacitor.calendar.models.inputs.RequestAllPermissionsInput
|
|
30
31
|
import dev.barooni.capacitor.calendar.models.inputs.RequestPermissionInput
|
|
32
|
+
import dev.barooni.capacitor.calendar.models.inputs.StartListeningInput
|
|
31
33
|
import dev.barooni.capacitor.calendar.models.results.CreateEventWithPromptResult
|
|
32
34
|
import dev.barooni.capacitor.calendar.models.results.ModifyEventWithPromptResult
|
|
33
35
|
import dev.barooni.capacitor.calendar.models.results.RequestAllPermissionsResult
|
|
@@ -492,6 +494,41 @@ class CapacitorCalendarPlugin : Plugin() {
|
|
|
492
494
|
}
|
|
493
495
|
}
|
|
494
496
|
|
|
497
|
+
@PluginMethod
|
|
498
|
+
fun startListening(call: PluginCall) {
|
|
499
|
+
try {
|
|
500
|
+
declarationCheck.ensureManifestPermissions(Manifest.permission.READ_CALENDAR)
|
|
501
|
+
val input = StartListeningInput(call)
|
|
502
|
+
implementation.startListening(input)
|
|
503
|
+
resolveCall(call)
|
|
504
|
+
} catch (error: Exception) {
|
|
505
|
+
rejectCall(call, error)
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
@PluginMethod
|
|
510
|
+
fun stopListening(call: PluginCall) {
|
|
511
|
+
implementation.stopListening()
|
|
512
|
+
resolveCall(call)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
fun hasCalendarChangedListeners(): Boolean = hasListeners("calendarChanged")
|
|
516
|
+
|
|
517
|
+
@PluginMethod
|
|
518
|
+
override fun removeAllListeners(call: PluginCall) {
|
|
519
|
+
implementation.stopListening()
|
|
520
|
+
super.removeAllListeners(call)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
fun emitCalendarChanged(payload: JSObject) {
|
|
524
|
+
notifyListeners("calendarChanged", payload)
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
override fun handleOnDestroy() {
|
|
528
|
+
implementation.stopListening()
|
|
529
|
+
super.handleOnDestroy()
|
|
530
|
+
}
|
|
531
|
+
|
|
495
532
|
@PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
|
|
496
533
|
fun getEventById(call: PluginCall) {
|
|
497
534
|
try {
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
package dev.barooni.capacitor.calendar.implementation
|
|
2
|
+
|
|
3
|
+
import android.content.ContentResolver
|
|
4
|
+
import android.database.ContentObserver
|
|
5
|
+
import android.provider.CalendarContract
|
|
6
|
+
import com.getcapacitor.JSArray
|
|
7
|
+
import com.getcapacitor.JSObject
|
|
8
|
+
import dev.barooni.capacitor.calendar.models.data.CalendarEvent
|
|
9
|
+
import dev.barooni.capacitor.calendar.models.results.toJSObject
|
|
10
|
+
import dev.barooni.capacitor.calendar.utils.ImplementationHelper
|
|
11
|
+
import java.util.concurrent.Executors
|
|
12
|
+
import java.util.concurrent.ScheduledExecutorService
|
|
13
|
+
import java.util.concurrent.ScheduledFuture
|
|
14
|
+
import java.util.concurrent.TimeUnit
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Watches the calendar provider. After each change it waits for the changes to settle,
|
|
18
|
+
* reads the watched window again and compares it with the last snapshot.
|
|
19
|
+
* Android does not say what changed, so the comparison finds it.
|
|
20
|
+
*/
|
|
21
|
+
class CalendarChangeObserver(
|
|
22
|
+
private val contentResolver: ContentResolver,
|
|
23
|
+
private val queryEvents: (from: Long, to: Long, calendarIds: List<Long>?) -> List<CalendarEvent>,
|
|
24
|
+
private val hasListeners: () -> Boolean,
|
|
25
|
+
private val emit: (JSObject) -> Unit,
|
|
26
|
+
) {
|
|
27
|
+
private data class Entry(
|
|
28
|
+
val id: String,
|
|
29
|
+
val fingerprint: Int,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
private var executor: ScheduledExecutorService? = null
|
|
33
|
+
private var pending: ScheduledFuture<*>? = null
|
|
34
|
+
private var contentObserver: ContentObserver? = null
|
|
35
|
+
private var from = 0L
|
|
36
|
+
private var to = 0L
|
|
37
|
+
private var calendarIds: List<Long>? = null
|
|
38
|
+
private var includeEvents = true
|
|
39
|
+
private var eventSnapshot: Map<String, Entry> = emptyMap()
|
|
40
|
+
private var calendarSnapshot: Map<String, Int> = emptyMap()
|
|
41
|
+
private var listenerSeen = false
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Stops a running watch, reads the baseline and starts to observe.
|
|
45
|
+
* Throws when the baseline cannot be read. Then nothing is observed.
|
|
46
|
+
*/
|
|
47
|
+
@Synchronized
|
|
48
|
+
fun start(
|
|
49
|
+
from: Long,
|
|
50
|
+
to: Long,
|
|
51
|
+
calendarIds: List<Long>?,
|
|
52
|
+
includeEvents: Boolean,
|
|
53
|
+
) {
|
|
54
|
+
stop()
|
|
55
|
+
this.from = from
|
|
56
|
+
this.to = to
|
|
57
|
+
this.calendarIds = calendarIds
|
|
58
|
+
this.includeEvents = includeEvents
|
|
59
|
+
eventSnapshot = toEntries(queryEvents(from, to, calendarIds))
|
|
60
|
+
calendarSnapshot = readCalendars()
|
|
61
|
+
executor = Executors.newSingleThreadScheduledExecutor()
|
|
62
|
+
val observer =
|
|
63
|
+
// No handler: onChange runs on a binder thread, so a running check never blocks the main thread.
|
|
64
|
+
object : ContentObserver(null) {
|
|
65
|
+
override fun onChange(selfChange: Boolean) {
|
|
66
|
+
scheduleCheck()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
contentResolver.registerContentObserver(CalendarContract.Events.CONTENT_URI, true, observer)
|
|
70
|
+
contentResolver.registerContentObserver(CalendarContract.Calendars.CONTENT_URI, true, observer)
|
|
71
|
+
contentObserver = observer
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@Synchronized
|
|
75
|
+
fun stop() {
|
|
76
|
+
contentObserver?.let { contentResolver.unregisterContentObserver(it) }
|
|
77
|
+
contentObserver = null
|
|
78
|
+
pending?.cancel(false)
|
|
79
|
+
pending = null
|
|
80
|
+
executor?.shutdownNow()
|
|
81
|
+
executor = null
|
|
82
|
+
eventSnapshot = emptyMap()
|
|
83
|
+
calendarSnapshot = emptyMap()
|
|
84
|
+
listenerSeen = false
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@Synchronized
|
|
88
|
+
private fun scheduleCheck() {
|
|
89
|
+
val currentExecutor = executor ?: return
|
|
90
|
+
pending?.cancel(false)
|
|
91
|
+
pending = currentExecutor.schedule({ check() }, DEBOUNCE_MS, TimeUnit.MILLISECONDS)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Synchronized
|
|
95
|
+
private fun check() {
|
|
96
|
+
if (executor == null) {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
// After `removeAllListeners` nobody listens. Then the watch ends by itself.
|
|
100
|
+
if (!hasListeners()) {
|
|
101
|
+
if (listenerSeen) {
|
|
102
|
+
stop()
|
|
103
|
+
}
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
listenerSeen = true
|
|
107
|
+
try {
|
|
108
|
+
val freshEvents = queryEvents(from, to, calendarIds)
|
|
109
|
+
val freshEntries = toEntries(freshEvents)
|
|
110
|
+
val freshCalendars = readCalendars()
|
|
111
|
+
|
|
112
|
+
val oldCounts = eventSnapshot.values.groupingBy { it.id }.eachCount()
|
|
113
|
+
val freshCounts = freshEntries.values.groupingBy { it.id }.eachCount()
|
|
114
|
+
// A new start time for a known id is a moved event or occurrence, not a new event.
|
|
115
|
+
val created = freshEvents.filter { key(it) !in eventSnapshot && it.id !in oldCounts }
|
|
116
|
+
val updated =
|
|
117
|
+
freshEvents.filter { event ->
|
|
118
|
+
val old = eventSnapshot[key(event)]
|
|
119
|
+
if (old == null) {
|
|
120
|
+
event.id in oldCounts
|
|
121
|
+
} else {
|
|
122
|
+
old.fingerprint != freshEntries[key(event)]?.fingerprint
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
val deletedIds =
|
|
126
|
+
eventSnapshot
|
|
127
|
+
.filterKeys { it !in freshEntries }
|
|
128
|
+
.values
|
|
129
|
+
.map { it.id }
|
|
130
|
+
.filter { it !in freshCounts }
|
|
131
|
+
.distinct()
|
|
132
|
+
// An occurrence left the window while its series stays. The lists cannot express it.
|
|
133
|
+
val occurrenceRemoved = freshCounts.any { (id, count) -> count < (oldCounts[id] ?: 0) }
|
|
134
|
+
val calendarsChanged = freshCalendars != calendarSnapshot
|
|
135
|
+
|
|
136
|
+
eventSnapshot = freshEntries
|
|
137
|
+
calendarSnapshot = freshCalendars
|
|
138
|
+
|
|
139
|
+
if (created.isEmpty() && updated.isEmpty() && deletedIds.isEmpty() && !occurrenceRemoved && !calendarsChanged) {
|
|
140
|
+
return
|
|
141
|
+
}
|
|
142
|
+
val payload = JSObject()
|
|
143
|
+
if (includeEvents) {
|
|
144
|
+
if (created.isNotEmpty()) {
|
|
145
|
+
payload.put("created", toJSArray(created))
|
|
146
|
+
}
|
|
147
|
+
if (updated.isNotEmpty()) {
|
|
148
|
+
payload.put("updated", toJSArray(updated))
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (deletedIds.isNotEmpty()) {
|
|
152
|
+
payload.put("deletedIds", JSArray(deletedIds))
|
|
153
|
+
}
|
|
154
|
+
if (calendarsChanged) {
|
|
155
|
+
payload.put("calendarsChanged", true)
|
|
156
|
+
}
|
|
157
|
+
payload.put("precise", !occurrenceRemoved && (includeEvents || (created.isEmpty() && updated.isEmpty())))
|
|
158
|
+
emit(payload)
|
|
159
|
+
} catch (error: Exception) {
|
|
160
|
+
// The snapshot stays as it is. The next check compares with it again.
|
|
161
|
+
emit(JSObject().put("precise", false))
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private fun readCalendars(): Map<String, Int> = ImplementationHelper.listCalendars(contentResolver).associate { it.id to it.hashCode() }
|
|
166
|
+
|
|
167
|
+
private fun toEntries(events: List<CalendarEvent>): Map<String, Entry> = events.associate { key(it) to Entry(it.id, it.hashCode()) }
|
|
168
|
+
|
|
169
|
+
// One event id has one instance for each occurrence of a series.
|
|
170
|
+
private fun key(event: CalendarEvent): String = "${event.id}|${event.startDate}"
|
|
171
|
+
|
|
172
|
+
private fun toJSArray(events: List<CalendarEvent>): JSArray {
|
|
173
|
+
val array = JSArray()
|
|
174
|
+
events.forEach { array.put(it.toJSObject()) }
|
|
175
|
+
return array
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private companion object {
|
|
179
|
+
const val DEBOUNCE_MS = 400L
|
|
180
|
+
}
|
|
181
|
+
}
|
package/android/src/main/java/dev/barooni/capacitor/calendar/implementation/CapacitorCalendar.kt
CHANGED
|
@@ -29,6 +29,7 @@ import dev.barooni.capacitor.calendar.models.inputs.ModifyEventWithPromptInput
|
|
|
29
29
|
import dev.barooni.capacitor.calendar.models.inputs.OpenCalendarInput
|
|
30
30
|
import dev.barooni.capacitor.calendar.models.inputs.RequestAllPermissionsInput
|
|
31
31
|
import dev.barooni.capacitor.calendar.models.inputs.RequestPermissionInput
|
|
32
|
+
import dev.barooni.capacitor.calendar.models.inputs.StartListeningInput
|
|
32
33
|
import dev.barooni.capacitor.calendar.models.results.CheckAllPermissionsResult
|
|
33
34
|
import dev.barooni.capacitor.calendar.models.results.CheckPermissionResult
|
|
34
35
|
import dev.barooni.capacitor.calendar.models.results.CreateCalendarResult
|
|
@@ -97,6 +98,16 @@ class CapacitorCalendar(
|
|
|
97
98
|
Pair(input.startDate, input.endDate)
|
|
98
99
|
}
|
|
99
100
|
val timezoneId = if (isAllDay) "UTC" else input.timezoneId
|
|
101
|
+
// The provider needs DURATION and no DTEND on a repeating event.
|
|
102
|
+
// Without DURATION it cannot add exceptions, so deleting or editing one occurrence fails.
|
|
103
|
+
val isRecurring = input.recurrence != null
|
|
104
|
+
val duration =
|
|
105
|
+
input.duration
|
|
106
|
+
?: if (isRecurring && startDate != null && endDate != null) {
|
|
107
|
+
ImplementationHelper.buildDurationFromBeginEnd(startDate, endDate, isAllDay)
|
|
108
|
+
} else {
|
|
109
|
+
null
|
|
110
|
+
}
|
|
100
111
|
|
|
101
112
|
val values =
|
|
102
113
|
ContentValues().apply {
|
|
@@ -106,11 +117,13 @@ class CapacitorCalendar(
|
|
|
106
117
|
input.isAllDay?.let { put(CalendarContract.Events.ALL_DAY, it) }
|
|
107
118
|
input.location?.let { put(CalendarContract.Events.EVENT_LOCATION, it) }
|
|
108
119
|
startDate?.let { put(CalendarContract.Events.DTSTART, it) }
|
|
109
|
-
|
|
120
|
+
if (!isRecurring || duration == null) {
|
|
121
|
+
endDate?.let { put(CalendarContract.Events.DTEND, it) }
|
|
122
|
+
}
|
|
110
123
|
input.description?.let { put(CalendarContract.Events.DESCRIPTION, it) }
|
|
111
124
|
input.availability?.let { put(CalendarContract.Events.AVAILABILITY, it) }
|
|
112
125
|
input.organizer?.let { put(CalendarContract.Events.ORGANIZER, it) }
|
|
113
|
-
|
|
126
|
+
duration?.let { put(CalendarContract.Events.DURATION, it) }
|
|
114
127
|
input.color?.let { put(CalendarContract.Events.EVENT_COLOR, it) }
|
|
115
128
|
input.recurrence?.let { put(CalendarContract.Events.RRULE, it.toRRule()) }
|
|
116
129
|
}
|
|
@@ -358,7 +371,45 @@ class CapacitorCalendar(
|
|
|
358
371
|
dialog.show()
|
|
359
372
|
}
|
|
360
373
|
|
|
361
|
-
|
|
374
|
+
private val calendarChangeObserver: CalendarChangeObserver by lazy {
|
|
375
|
+
CalendarChangeObserver(
|
|
376
|
+
plugin.context.contentResolver,
|
|
377
|
+
::queryEventsInRange,
|
|
378
|
+
plugin::hasCalendarChangedListeners,
|
|
379
|
+
plugin::emitCalendarChanged,
|
|
380
|
+
)
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
fun startListening(input: StartListeningInput) {
|
|
384
|
+
input.calendarIds?.forEach { calendarId ->
|
|
385
|
+
val exists =
|
|
386
|
+
plugin.context.contentResolver
|
|
387
|
+
.query(
|
|
388
|
+
ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, calendarId),
|
|
389
|
+
arrayOf(CalendarContract.Calendars._ID),
|
|
390
|
+
null,
|
|
391
|
+
null,
|
|
392
|
+
null,
|
|
393
|
+
)?.use { it.moveToFirst() } ?: false
|
|
394
|
+
if (!exists) {
|
|
395
|
+
throw PluginError.CalendarNotFound
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
calendarChangeObserver.start(input.from, input.to, input.calendarIds, input.includeEvents)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
fun stopListening() {
|
|
402
|
+
calendarChangeObserver.stop()
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
fun listEventsInRange(input: ListEventsInRangeInput): ListEventsInRangeResult =
|
|
406
|
+
ListEventsInRangeResult(queryEventsInRange(input.from, input.to, input.calendarIds))
|
|
407
|
+
|
|
408
|
+
fun queryEventsInRange(
|
|
409
|
+
from: Long,
|
|
410
|
+
to: Long,
|
|
411
|
+
calendarIds: List<Long>?,
|
|
412
|
+
): List<CalendarEvent> {
|
|
362
413
|
val cr = plugin.context.contentResolver
|
|
363
414
|
val events = mutableListOf<CalendarEvent>()
|
|
364
415
|
// Cache master Events.DTSTART by master id for this call only.
|
|
@@ -391,8 +442,8 @@ class CapacitorCalendar(
|
|
|
391
442
|
}
|
|
392
443
|
|
|
393
444
|
val builder: Uri.Builder = CalendarContract.Instances.CONTENT_URI.buildUpon()
|
|
394
|
-
ContentUris.appendId(builder,
|
|
395
|
-
ContentUris.appendId(builder,
|
|
445
|
+
ContentUris.appendId(builder, from)
|
|
446
|
+
ContentUris.appendId(builder, to)
|
|
396
447
|
val uri: Uri = builder.build()
|
|
397
448
|
|
|
398
449
|
val projection =
|
|
@@ -416,7 +467,6 @@ class CapacitorCalendar(
|
|
|
416
467
|
CalendarContract.Instances.DTSTART,
|
|
417
468
|
)
|
|
418
469
|
|
|
419
|
-
val calendarIds = input.calendarIds
|
|
420
470
|
calendarIds?.forEach { calendarId ->
|
|
421
471
|
val exists =
|
|
422
472
|
cr
|
|
@@ -439,7 +489,7 @@ class CapacitorCalendar(
|
|
|
439
489
|
"(${CalendarContract.Instances.BEGIN} < ?) AND " +
|
|
440
490
|
"(${CalendarContract.Instances.END} > ?)" + calendarFilter
|
|
441
491
|
val selectionArgs =
|
|
442
|
-
arrayOf(
|
|
492
|
+
arrayOf(to.toString(), from.toString()) +
|
|
443
493
|
(calendarIds?.map { it.toString() } ?: emptyList())
|
|
444
494
|
|
|
445
495
|
val sortOrder = "${CalendarContract.Instances.BEGIN} ASC"
|
|
@@ -592,7 +642,7 @@ class CapacitorCalendar(
|
|
|
592
642
|
}
|
|
593
643
|
}
|
|
594
644
|
cursor?.close()
|
|
595
|
-
return
|
|
645
|
+
return events
|
|
596
646
|
}
|
|
597
647
|
|
|
598
648
|
fun getEventById(
|
|
@@ -5,4 +5,10 @@ data class EventDeleteInfo(
|
|
|
5
5
|
val originalId: Long?,
|
|
6
6
|
val originalInstanceTime: Long?,
|
|
7
7
|
val rrule: String?,
|
|
8
|
+
val dtEnd: Long? = null,
|
|
9
|
+
val duration: String? = null,
|
|
10
|
+
val exdate: String? = null,
|
|
11
|
+
val timezone: String? = null,
|
|
12
|
+
val isAllDay: Boolean = false,
|
|
13
|
+
val syncId: String? = null,
|
|
8
14
|
)
|
package/android/src/main/java/dev/barooni/capacitor/calendar/models/inputs/StartListeningInput.kt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package dev.barooni.capacitor.calendar.models.inputs
|
|
2
|
+
|
|
3
|
+
import com.getcapacitor.PluginCall
|
|
4
|
+
import dev.barooni.capacitor.calendar.PluginError
|
|
5
|
+
import dev.barooni.capacitor.calendar.utils.ImplementationHelper
|
|
6
|
+
|
|
7
|
+
data class StartListeningInput(
|
|
8
|
+
private var call: PluginCall,
|
|
9
|
+
) {
|
|
10
|
+
val from: Long =
|
|
11
|
+
call.getLong("from")?.let { ImplementationHelper.getCalendarFromTimestamp(it).timeInMillis } ?: throw PluginError.FromDateMissing
|
|
12
|
+
val to: Long =
|
|
13
|
+
call.getLong("to")?.let { ImplementationHelper.getCalendarFromTimestamp(it).timeInMillis } ?: throw PluginError.ToDateMissing
|
|
14
|
+
val calendarIds: List<Long>? =
|
|
15
|
+
call
|
|
16
|
+
.getArray("calendarIds")
|
|
17
|
+
?.let { ImplementationHelper.jsArrayToLongArray(it, PluginError.InvalidCalendarId) }
|
|
18
|
+
?.takeIf { it.isNotEmpty() }
|
|
19
|
+
val includeEvents: Boolean = call.getBoolean("includeEvents", true) ?: true
|
|
20
|
+
}
|
|
@@ -473,7 +473,13 @@ class ImplementationHelper {
|
|
|
473
473
|
|
|
474
474
|
isRecurringMaster -> {
|
|
475
475
|
val date = instanceDate ?: throw PluginError.InstanceDateMissing
|
|
476
|
-
|
|
476
|
+
// The provider links exceptions to a series by its sync id.
|
|
477
|
+
// A series without a sync id loses all its instances when an exception is added.
|
|
478
|
+
if (info.syncId.isNullOrEmpty()) {
|
|
479
|
+
excludeOccurrence(cr, masterId, info, date)
|
|
480
|
+
} else {
|
|
481
|
+
insertCanceledException(cr, masterId, date)
|
|
482
|
+
}
|
|
477
483
|
}
|
|
478
484
|
|
|
479
485
|
else -> {
|
|
@@ -572,7 +578,7 @@ class ImplementationHelper {
|
|
|
572
578
|
}
|
|
573
579
|
|
|
574
580
|
val values =
|
|
575
|
-
|
|
581
|
+
recurrenceValues(info).apply {
|
|
576
582
|
put(CalendarContract.Events.RRULE, rruleEndingBefore(rrule, instanceDate))
|
|
577
583
|
}
|
|
578
584
|
val uri =
|
|
@@ -583,6 +589,58 @@ class ImplementationHelper {
|
|
|
583
589
|
return cr.update(uri, values, null, null) > 0
|
|
584
590
|
}
|
|
585
591
|
|
|
592
|
+
/**
|
|
593
|
+
* Adds the occurrence that starts at [instanceDate] to the EXDATE list of the series.
|
|
594
|
+
*/
|
|
595
|
+
private fun excludeOccurrence(
|
|
596
|
+
cr: ContentResolver,
|
|
597
|
+
masterId: Long,
|
|
598
|
+
info: EventDeleteInfo,
|
|
599
|
+
instanceDate: Long,
|
|
600
|
+
): Boolean {
|
|
601
|
+
val excluded = formatExdate(instanceDate, info.isAllDay)
|
|
602
|
+
val values =
|
|
603
|
+
recurrenceValues(info).apply {
|
|
604
|
+
put(
|
|
605
|
+
CalendarContract.Events.EXDATE,
|
|
606
|
+
listOfNotNull(info.exdate?.takeIf { it.isNotBlank() }, excluded).joinToString(","),
|
|
607
|
+
)
|
|
608
|
+
}
|
|
609
|
+
val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, masterId)
|
|
610
|
+
return cr.update(uri, values, null, null) > 0
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* The provider expands the instances of a series only from the fields in the update.
|
|
615
|
+
* Fields that are not in the update are ignored. An update of the rule must send all of them.
|
|
616
|
+
* A series must have DURATION and no DTEND.
|
|
617
|
+
*/
|
|
618
|
+
private fun recurrenceValues(info: EventDeleteInfo): ContentValues =
|
|
619
|
+
ContentValues().apply {
|
|
620
|
+
info.dtStart?.let { put(CalendarContract.Events.DTSTART, it) }
|
|
621
|
+
info.rrule?.let { put(CalendarContract.Events.RRULE, it) }
|
|
622
|
+
info.exdate?.let { put(CalendarContract.Events.EXDATE, it) }
|
|
623
|
+
info.timezone?.let { put(CalendarContract.Events.EVENT_TIMEZONE, it) }
|
|
624
|
+
put(CalendarContract.Events.ALL_DAY, if (info.isAllDay) 1 else 0)
|
|
625
|
+
val duration =
|
|
626
|
+
info.duration
|
|
627
|
+
?: info.dtStart?.let { start ->
|
|
628
|
+
info.dtEnd?.let { end -> buildDurationFromBeginEnd(start, end, info.isAllDay) }
|
|
629
|
+
}
|
|
630
|
+
if (duration != null) {
|
|
631
|
+
put(CalendarContract.Events.DURATION, duration)
|
|
632
|
+
putNull(CalendarContract.Events.DTEND)
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
private fun formatExdate(
|
|
637
|
+
instanceDate: Long,
|
|
638
|
+
isAllDay: Boolean,
|
|
639
|
+
): String {
|
|
640
|
+
val utc = formatUntilUtc(instanceDate)
|
|
641
|
+
return if (isAllDay) utc.substringBefore("T") else utc
|
|
642
|
+
}
|
|
643
|
+
|
|
586
644
|
private fun rruleEndingBefore(
|
|
587
645
|
rrule: String,
|
|
588
646
|
instanceDateMs: Long,
|
|
@@ -633,80 +691,43 @@ class ImplementationHelper {
|
|
|
633
691
|
CalendarContract.Events.CONTENT_URI,
|
|
634
692
|
eventId,
|
|
635
693
|
)
|
|
636
|
-
|
|
637
694
|
val projection =
|
|
638
695
|
arrayOf(
|
|
639
696
|
CalendarContract.Events.DTSTART,
|
|
640
697
|
CalendarContract.Events.ORIGINAL_ID,
|
|
641
698
|
CalendarContract.Events.ORIGINAL_INSTANCE_TIME,
|
|
642
699
|
CalendarContract.Events.RRULE,
|
|
700
|
+
CalendarContract.Events.DTEND,
|
|
701
|
+
CalendarContract.Events.DURATION,
|
|
702
|
+
CalendarContract.Events.EXDATE,
|
|
703
|
+
CalendarContract.Events.EVENT_TIMEZONE,
|
|
704
|
+
CalendarContract.Events.ALL_DAY,
|
|
705
|
+
CalendarContract.Events._SYNC_ID,
|
|
643
706
|
)
|
|
644
|
-
|
|
645
|
-
cr
|
|
646
|
-
.
|
|
647
|
-
|
|
648
|
-
projection,
|
|
649
|
-
null,
|
|
650
|
-
null,
|
|
651
|
-
null,
|
|
652
|
-
)?.use { cursor ->
|
|
653
|
-
if (!cursor.moveToFirst()) {
|
|
654
|
-
return EventDeleteInfo(
|
|
655
|
-
dtStart = null,
|
|
656
|
-
originalId = null,
|
|
657
|
-
originalInstanceTime = null,
|
|
658
|
-
rrule = null,
|
|
659
|
-
)
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
val dtStartIndex = cursor.getColumnIndex(CalendarContract.Events.DTSTART)
|
|
663
|
-
val originalIdIndex = cursor.getColumnIndex(CalendarContract.Events.ORIGINAL_ID)
|
|
664
|
-
val originalInstanceTimeIndex =
|
|
665
|
-
cursor.getColumnIndex(CalendarContract.Events.ORIGINAL_INSTANCE_TIME)
|
|
666
|
-
val rruleIndex = cursor.getColumnIndex(CalendarContract.Events.RRULE)
|
|
667
|
-
|
|
668
|
-
val dtStart =
|
|
669
|
-
if (dtStartIndex >= 0 && !cursor.isNull(dtStartIndex)) {
|
|
670
|
-
cursor.getLong(dtStartIndex)
|
|
671
|
-
} else {
|
|
672
|
-
null
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
val originalId =
|
|
676
|
-
if (originalIdIndex >= 0 && !cursor.isNull(originalIdIndex)) {
|
|
677
|
-
cursor.getLong(originalIdIndex)
|
|
678
|
-
} else {
|
|
679
|
-
null
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
val originalInstanceTime =
|
|
683
|
-
if (originalInstanceTimeIndex >= 0 && !cursor.isNull(originalInstanceTimeIndex)) {
|
|
684
|
-
cursor.getLong(originalInstanceTimeIndex)
|
|
685
|
-
} else {
|
|
686
|
-
null
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
val rrule =
|
|
690
|
-
if (rruleIndex >= 0 && !cursor.isNull(rruleIndex)) {
|
|
691
|
-
cursor.getString(rruleIndex)
|
|
692
|
-
} else {
|
|
693
|
-
null
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
return EventDeleteInfo(
|
|
697
|
-
dtStart = dtStart,
|
|
698
|
-
originalId = originalId,
|
|
699
|
-
originalInstanceTime = originalInstanceTime,
|
|
700
|
-
rrule = rrule,
|
|
701
|
-
)
|
|
707
|
+
val empty = EventDeleteInfo(dtStart = null, originalId = null, originalInstanceTime = null, rrule = null)
|
|
708
|
+
return cr.query(uri, projection, null, null, null)?.use { cursor ->
|
|
709
|
+
if (!cursor.moveToFirst()) {
|
|
710
|
+
return@use empty
|
|
702
711
|
}
|
|
703
712
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
713
|
+
fun column(name: String): Int? = cursor.getColumnIndex(name).takeIf { it >= 0 && !cursor.isNull(it) }
|
|
714
|
+
|
|
715
|
+
fun long(name: String): Long? = column(name)?.let { cursor.getLong(it) }
|
|
716
|
+
|
|
717
|
+
fun string(name: String): String? = column(name)?.let { cursor.getString(it) }
|
|
718
|
+
EventDeleteInfo(
|
|
719
|
+
dtStart = long(CalendarContract.Events.DTSTART),
|
|
720
|
+
originalId = long(CalendarContract.Events.ORIGINAL_ID),
|
|
721
|
+
originalInstanceTime = long(CalendarContract.Events.ORIGINAL_INSTANCE_TIME),
|
|
722
|
+
rrule = string(CalendarContract.Events.RRULE),
|
|
723
|
+
dtEnd = long(CalendarContract.Events.DTEND),
|
|
724
|
+
duration = string(CalendarContract.Events.DURATION),
|
|
725
|
+
exdate = string(CalendarContract.Events.EXDATE),
|
|
726
|
+
timezone = string(CalendarContract.Events.EVENT_TIMEZONE),
|
|
727
|
+
isAllDay = long(CalendarContract.Events.ALL_DAY) == 1L,
|
|
728
|
+
syncId = string(CalendarContract.Events._SYNC_ID),
|
|
729
|
+
)
|
|
730
|
+
} ?: empty
|
|
710
731
|
}
|
|
711
732
|
|
|
712
733
|
fun getEventAlerts(
|