@octane-xplat/calendar 0.18.0 → 0.20.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/src/calendar.ts CHANGED
@@ -14,129 +14,154 @@ declare const NSDate: any
14
14
 
15
15
  let store: any = null
16
16
  const eventStore = () => {
17
- if (!store && typeof EKEventStore !== 'undefined') {
18
- store = EKEventStore.new()
19
- }
20
- return store
17
+ if (!store && typeof EKEventStore !== 'undefined') {
18
+ store = EKEventStore.new()
19
+ }
20
+
21
+ return store
21
22
  }
22
23
 
23
24
  function iosEnsure(): Promise<PermissionResult> {
24
- const s = eventStore()
25
- if (!s || typeof EKEntityTypeEvent === 'undefined') {
26
- return Promise.resolve('unsupported')
27
- }
28
- const status = EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent)
29
- // 0 notDetermined 1 restricted 2 denied 3 fullAccess/authorized 4 writeOnly
30
- if (status === 3 || status === 4) {
31
- return Promise.resolve('granted')
32
- }
33
- if (status === 1 || status === 2) {
34
- return Promise.resolve('denied')
35
- }
36
- return new Promise((resolve) => {
37
- if (typeof s.requestFullAccessToEventsWithCompletion === 'function') {
38
- s.requestFullAccessToEventsWithCompletion((granted: boolean) =>
39
- resolve(granted ? 'granted' : 'denied'),
40
- )
41
- } else {
42
- s.requestAccessToEntityTypeCompletion(EKEntityTypeEvent, (granted: boolean) =>
43
- resolve(granted ? 'granted' : 'denied'),
44
- )
45
- }
46
- })
25
+ const s = eventStore()
26
+
27
+ if (!s || typeof EKEntityTypeEvent === 'undefined') {
28
+ return Promise.resolve('unsupported')
29
+ }
30
+
31
+ const status = EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent)
32
+
33
+ // 0 notDetermined 1 restricted 2 denied 3 fullAccess/authorized 4 writeOnly
34
+ if (status === 3 || status === 4) {
35
+ return Promise.resolve('granted')
36
+ }
37
+
38
+ if (status === 1 || status === 2) {
39
+ return Promise.resolve('denied')
40
+ }
41
+
42
+ return new Promise((resolve) => {
43
+ if (typeof s.requestFullAccessToEventsWithCompletion === 'function') {
44
+ s.requestFullAccessToEventsWithCompletion((granted: boolean) =>
45
+ resolve(granted ? 'granted' : 'denied'),
46
+ )
47
+ } else {
48
+ s.requestAccessToEntityTypeCompletion(EKEntityTypeEvent, (granted: boolean) =>
49
+ resolve(granted ? 'granted' : 'denied'),
50
+ )
51
+ }
52
+ })
47
53
  }
48
54
 
49
55
  let calRequestCode = 7731
50
56
 
51
57
  function androidEnsure(): Promise<PermissionResult> {
52
- const perm = 'android.permission.READ_CALENDAR'
53
- const activity = Application.android.foregroundActivity ?? Application.android.startActivity
54
- try {
55
- if (
56
- activity?.checkSelfPermission?.(perm) === android.content.pm.PackageManager.PERMISSION_GRANTED
57
- ) {
58
- return Promise.resolve('granted')
59
- }
60
- if (!activity) {
61
- return Promise.resolve('denied')
62
- }
63
- return new Promise<PermissionResult>((resolve) => {
64
- const requestCode = calRequestCode++
65
- const event = Application.android.activityRequestPermissionsEvent
66
- const onResult = (args: any) => {
67
- if (args.requestCode !== requestCode) {
68
- return
69
- }
70
- Application.android.off(event, onResult)
71
- resolve(
72
- args.grantResults?.[0] === android.content.pm.PackageManager.PERMISSION_GRANTED
73
- ? 'granted'
74
- : 'denied',
75
- )
76
- }
77
- Application.android.on(event, onResult)
78
- try {
79
- activity.requestPermissions([perm, 'android.permission.WRITE_CALENDAR'], requestCode)
80
- } catch {
81
- Application.android.off(event, onResult)
82
- resolve('denied')
83
- }
84
- })
85
- } catch {
86
- return Promise.resolve('denied')
87
- }
58
+ const perm = 'android.permission.READ_CALENDAR'
59
+ const activity = Application.android.foregroundActivity ?? Application.android.startActivity
60
+
61
+ try {
62
+ if (
63
+ activity?.checkSelfPermission?.(perm) === android.content.pm.PackageManager.PERMISSION_GRANTED
64
+ ) {
65
+ return Promise.resolve('granted')
66
+ }
67
+
68
+ if (!activity) {
69
+ return Promise.resolve('denied')
70
+ }
71
+
72
+ return new Promise<PermissionResult>((resolve) => {
73
+ const requestCode = calRequestCode++
74
+ const event = Application.android.activityRequestPermissionsEvent
75
+ const onResult = (args: any) => {
76
+ if (args.requestCode !== requestCode) {
77
+ return
78
+ }
79
+
80
+ Application.android.off(event, onResult)
81
+ resolve(
82
+ args.grantResults?.[0] === android.content.pm.PackageManager.PERMISSION_GRANTED
83
+ ? 'granted'
84
+ : 'denied',
85
+ )
86
+ }
87
+
88
+ Application.android.on(event, onResult)
89
+ try {
90
+ activity.requestPermissions([perm, 'android.permission.WRITE_CALENDAR'], requestCode)
91
+ } catch {
92
+ Application.android.off(event, onResult)
93
+ resolve('denied')
94
+ }
95
+ })
96
+ } catch {
97
+ return Promise.resolve('denied')
98
+ }
88
99
  }
89
100
 
90
101
  function iosCalendars(): CalendarInfo[] {
91
- const s = eventStore()
92
- const out: CalendarInfo[] = []
93
- const list = s?.calendarsForEntityType?.(EKEntityTypeEvent ?? 0)
94
- for (let i = 0; i < (list?.count ?? 0); i++) {
95
- const c = list.objectAtIndex(i)
96
- out.push({
97
- id: String(c.calendarIdentifier ?? ''),
98
- title: String(c.title ?? ''),
99
- color: c.CGColor ? String(c.CGColor) : undefined,
100
- source: c.source?.title ? String(c.source.title) : undefined,
101
- allowsModifications: !!c.allowsContentModifications,
102
- isPrimary: false,
103
- })
104
- }
105
- return out
102
+ const s = eventStore()
103
+ const out: CalendarInfo[] = []
104
+ const list = s?.calendarsForEntityType?.(EKEntityTypeEvent ?? 0)
105
+
106
+ for (let i = 0; i < (list?.count ?? 0); i++) {
107
+ const c = list.objectAtIndex(i)
108
+
109
+ out.push({
110
+ id: String(c.calendarIdentifier ?? ''),
111
+ title: String(c.title ?? ''),
112
+ color: c.CGColor ? String(c.CGColor) : undefined,
113
+ source: c.source?.title ? String(c.source.title) : undefined,
114
+ allowsModifications: !!c.allowsContentModifications,
115
+ isPrimary: false,
116
+ })
117
+ }
118
+
119
+ return out
106
120
  }
107
121
 
108
122
  function iosEvent(e: any): CalendarEvent {
109
- return {
110
- id: String(e.eventIdentifier ?? ''),
111
- calendarId: e.calendar?.calendarIdentifier ? String(e.calendar.calendarIdentifier) : undefined,
112
- title: String(e.title ?? ''),
113
- startDate: new Date((e.startDate?.timeIntervalSince1970 ?? 0) * 1000),
114
- endDate: new Date((e.endDate?.timeIntervalSince1970 ?? 0) * 1000),
115
- allDay: !!e.allDay,
116
- location: e.location ? String(e.location) : undefined,
117
- notes: e.notes ? String(e.notes) : undefined,
118
- timeZone: e.timeZone?.name ? String(e.timeZone.name) : undefined,
119
- }
123
+ return {
124
+ id: String(e.eventIdentifier ?? ''),
125
+ calendarId: e.calendar?.calendarIdentifier ? String(e.calendar.calendarIdentifier) : undefined,
126
+ title: String(e.title ?? ''),
127
+ startDate: new Date((e.startDate?.timeIntervalSince1970 ?? 0) * 1000),
128
+ endDate: new Date((e.endDate?.timeIntervalSince1970 ?? 0) * 1000),
129
+ allDay: !!e.allDay,
130
+ location: e.location ? String(e.location) : undefined,
131
+ notes: e.notes ? String(e.notes) : undefined,
132
+ timeZone: e.timeZone?.name ? String(e.timeZone.name) : undefined,
133
+ }
120
134
  }
121
135
 
122
136
  const androidUtils = Utils.android as any
123
137
  const resolver = () => androidUtils?.getApplicationContext?.()?.getContentResolver()
124
138
 
125
- function androidQuery(uri: any, projection: string[], selection: string, args: string[], sort: string): any[] {
126
- const cursor = resolver()?.query(uri, projection, selection, args, sort)
127
- const out: any[] = []
128
- if (!cursor) {
129
- return out
130
- }
131
- while (cursor.moveToNext()) {
132
- const row: Record<string, any> = {}
133
- for (let i = 0; i < cursor.getColumnCount(); i++) {
134
- row[cursor.getColumnName(i)] = cursor.getString(i)
135
- }
136
- out.push(row)
137
- }
138
- cursor.close()
139
- return out
139
+ function androidQuery(
140
+ uri: any,
141
+ projection: string[],
142
+ selection: string,
143
+ args: string[],
144
+ sort: string,
145
+ ): any[] {
146
+ const cursor = resolver()?.query(uri, projection, selection, args, sort)
147
+ const out: any[] = []
148
+
149
+ if (!cursor) {
150
+ return out
151
+ }
152
+
153
+ while (cursor.moveToNext()) {
154
+ const row: Record<string, any> = {}
155
+
156
+ for (let i = 0; i < cursor.getColumnCount(); i++) {
157
+ row[cursor.getColumnName(i)] = cursor.getString(i)
158
+ }
159
+
160
+ out.push(row)
161
+ }
162
+
163
+ cursor.close()
164
+ return out
140
165
  }
141
166
 
142
167
  const CAL_URI = () => android.provider.CalendarContract.Calendars.CONTENT_URI
@@ -144,167 +169,273 @@ const EVENTS_URI = () => android.provider.CalendarContract.Events.CONTENT_URI
144
169
  const INSTANCES_URI = () => android.provider.CalendarContract.Instances.CONTENT_URI
145
170
 
146
171
  function androidCalendars(): CalendarInfo[] {
147
- return androidQuery(CAL_URI(), ['_id', 'calendar_displayName', 'calendar_color', 'account_name', 'calendar_access_level', 'isPrimary'], '', [], 'calendar_displayName ASC').map((row) => ({
148
- id: String(row._id),
149
- title: String(row.calendar_displayName ?? ''),
150
- color: row.calendar_color,
151
- source: row.account_name,
152
- allowsModifications: Number(row.calendar_access_level) >= 500,
153
- isPrimary: row.isPrimary === '1' || row.isPrimary === 'true',
154
- }))
172
+ return androidQuery(
173
+ CAL_URI(),
174
+ [
175
+ '_id',
176
+ 'calendar_displayName',
177
+ 'calendar_color',
178
+ 'account_name',
179
+ 'calendar_access_level',
180
+ 'isPrimary',
181
+ ],
182
+ '',
183
+ [],
184
+ 'calendar_displayName ASC',
185
+ ).map((row) => ({
186
+ id: String(row._id),
187
+ title: String(row.calendar_displayName ?? ''),
188
+ color: row.calendar_color,
189
+ source: row.account_name,
190
+ allowsModifications: Number(row.calendar_access_level) >= 500,
191
+ isPrimary: row.isPrimary === '1' || row.isPrimary === 'true',
192
+ }))
155
193
  }
156
194
 
157
195
  function androidEvent(row: any): CalendarEvent {
158
- return {
159
- id: String(row._id ?? row.event_id ?? ''),
160
- calendarId: row.calendar_id ? String(row.calendar_id) : undefined,
161
- title: String(row.title ?? ''),
162
- startDate: new Date(Number(row.dtstart ?? row.begin ?? 0)),
163
- endDate: new Date(Number(row.dtend ?? row.end ?? 0)),
164
- allDay: row.allDay === '1' || row.allDay === 1,
165
- location: row.eventLocation,
166
- notes: row.description,
167
- }
196
+ return {
197
+ id: String(row._id ?? row.event_id ?? ''),
198
+ calendarId: row.calendar_id ? String(row.calendar_id) : undefined,
199
+ title: String(row.title ?? ''),
200
+ startDate: new Date(Number(row.dtstart ?? row.begin ?? 0)),
201
+ endDate: new Date(Number(row.dtend ?? row.end ?? 0)),
202
+ allDay: row.allDay === '1' || row.allDay === 1,
203
+ location: row.eventLocation,
204
+ notes: row.description,
205
+ }
168
206
  }
169
207
 
208
+ /** Calendar service — EventKit/CalendarContract. */
170
209
  export const calendar: Calendar = {
171
- supported: isIOS ? typeof EKEventStore !== 'undefined' : true,
172
-
173
- ensure(): Promise<PermissionResult> {
174
- return isIOS ? iosEnsure() : androidEnsure()
175
- },
176
-
177
- async getCalendars(): Promise<CalendarInfo[]> {
178
- return isIOS ? iosCalendars() : androidCalendars()
179
- },
180
-
181
- async getDefaultCalendar(): Promise<CalendarInfo | null> {
182
- if (isIOS) {
183
- const s = eventStore()
184
- const def = s?.defaultCalendarForNewEvents
185
- return def ? { id: String(def.calendarIdentifier), title: String(def.title ?? ''), allowsModifications: !!def.allowsContentModifications } : null
186
- }
187
- return androidCalendars().find((c) => c.isPrimary) ?? androidCalendars()[0] ?? null
188
- },
189
-
190
- async getEvents(startDate: Date, endDate: Date, calendarIds?: string[]): Promise<CalendarEvent[]> {
191
- if (isIOS) {
192
- const s = eventStore()
193
- const begin = NSDate.dateWithTimeIntervalSince1970(startDate.getTime() / 1000)
194
- const end = NSDate.dateWithTimeIntervalSince1970(endDate.getTime() / 1000)
195
- const predicate = s.predicateForEventsWithStartDateEndDateCalendars(begin, end, null)
196
- const list = s.eventsMatchingPredicate(predicate)
197
- const out: CalendarEvent[] = []
198
- for (let i = 0; i < (list?.count ?? 0); i++) {
199
- const e = iosEvent(list.objectAtIndex(i))
200
- if (!calendarIds?.length || calendarIds.includes(e.calendarId ?? '')) {
201
- out.push(e)
202
- }
203
- }
204
- return out
205
- }
206
- const selection = calendarIds?.length
207
- ? `calendar_id IN (${calendarIds.map(() => '?').join(',')})`
208
- : ''
209
- const args = calendarIds ?? []
210
- return androidQuery(
211
- INSTANCES_URI(),
212
- ['event_id', 'calendar_id', 'title', 'begin', 'end', 'allDay', 'eventLocation', 'description'],
213
- selection,
214
- args,
215
- 'begin ASC',
216
- )
217
- .map((row) => androidEvent(row))
218
- .filter((e) => e.startDate <= endDate && e.endDate >= startDate)
219
- },
220
-
221
- async getEvent(id: string): Promise<CalendarEvent | null> {
222
- if (isIOS) {
223
- const e = eventStore()?.eventWithIdentifier?.(id)
224
- return e ? iosEvent(e) : null
225
- }
226
- const rows = androidQuery(
227
- (android.net.Uri as any).withAppendedId(EVENTS_URI(), Number(id)),
228
- ['_id', 'calendar_id', 'title', 'dtstart', 'dtend', 'allDay', 'eventLocation', 'description'],
229
- '', [], '',
230
- )
231
- return rows.length ? androidEvent(rows[0]) : null
232
- },
233
-
234
- async createEvent(calendarId: string | undefined, event: NewEvent): Promise<string> {
235
- if (isIOS) {
236
- const s = eventStore()
237
- const e = EKEvent.eventWithEventStore(s)
238
- e.title = event.title
239
- e.startDate = NSDate.dateWithTimeIntervalSince1970(event.startDate.getTime() / 1000)
240
- e.endDate = NSDate.dateWithTimeIntervalSince1970(event.endDate.getTime() / 1000)
241
- e.allDay = !!event.allDay
242
- e.location = event.location
243
- e.notes = event.notes
244
- e.calendar = calendarId
245
- ? s.calendarWithIdentifier?.(calendarId) ?? s.defaultCalendarForNewEvents
246
- : s.defaultCalendarForNewEvents
247
- if (!s.saveEventSpanError(e, 0, null)) {
248
- throw new Error('calendar.createEvent: EKEventStore.saveEvent failed')
249
- }
250
- return String(e.eventIdentifier ?? '')
251
- }
252
- const values = new android.content.ContentValues()
253
- const def = await calendar.getDefaultCalendar()
254
- values.put('calendar_id', java.lang.Long.valueOf(calendarId ?? def?.id ?? '0'))
255
- values.put('title', event.title)
256
- values.put('dtstart', java.lang.Long.valueOf(event.startDate.getTime()))
257
- values.put('dtend', java.lang.Long.valueOf(event.endDate.getTime()))
258
- values.put('allDay', java.lang.Integer.valueOf(event.allDay ? 1 : 0))
259
- if (event.location) {
260
- values.put('eventLocation', event.location)
261
- }
262
- if (event.notes) {
263
- values.put('description', event.notes)
264
- }
265
- values.put('eventTimezone', event.timeZone ?? 'UTC')
266
- const uri = resolver()?.insert(EVENTS_URI(), values)
267
- return uri ? String(uri.getLastPathSegment()) : ''
268
- },
269
-
270
- async updateEvent(id: string, patch: Partial<NewEvent>): Promise<string> {
271
- if (isIOS) {
272
- const s = eventStore()
273
- const e = s.eventWithIdentifier?.(id)
274
- if (!e) {
275
- throw new Error(`calendar.updateEvent: no event ${id}`)
276
- }
277
- if (patch.title !== undefined) {e.title = patch.title}
278
- if (patch.startDate) {e.startDate = NSDate.dateWithTimeIntervalSince1970(patch.startDate.getTime() / 1000)}
279
- if (patch.endDate) {e.endDate = NSDate.dateWithTimeIntervalSince1970(patch.endDate.getTime() / 1000)}
280
- if (patch.allDay !== undefined) {e.allDay = patch.allDay}
281
- if (patch.location !== undefined) {e.location = patch.location}
282
- if (patch.notes !== undefined) {e.notes = patch.notes}
283
- s.saveEventSpanError(e, 0, null)
284
- return id
285
- }
286
- const values = new android.content.ContentValues()
287
- if (patch.title !== undefined) {values.put('title', patch.title)}
288
- if (patch.startDate) {values.put('dtstart', java.lang.Long.valueOf(patch.startDate.getTime()))}
289
- if (patch.endDate) {values.put('dtend', java.lang.Long.valueOf(patch.endDate.getTime()))}
290
- if (patch.location !== undefined) {values.put('eventLocation', patch.location)}
291
- if (patch.notes !== undefined) {values.put('description', patch.notes)}
292
- resolver()?.update((android.net.Uri as any).withAppendedId(EVENTS_URI(), Number(id)), values, null, null)
293
- return id
294
- },
295
-
296
- async deleteEvent(id: string): Promise<void> {
297
- if (isIOS) {
298
- const s = eventStore()
299
- const e = s.eventWithIdentifier?.(id)
300
- if (e) {
301
- s.removeEventSpanError(e, 0, null)
302
- }
303
- return
304
- }
305
- resolver()?.delete((android.net.Uri as any).withAppendedId(EVENTS_URI(), Number(id)), null, null)
306
- },
210
+ supported: isIOS ? typeof EKEventStore !== 'undefined' : true,
211
+
212
+ ensure(): Promise<PermissionResult> {
213
+ return isIOS ? iosEnsure() : androidEnsure()
214
+ },
215
+
216
+ async getCalendars(): Promise<CalendarInfo[]> {
217
+ return isIOS ? iosCalendars() : androidCalendars()
218
+ },
219
+
220
+ async getDefaultCalendar(): Promise<CalendarInfo | null> {
221
+ if (isIOS) {
222
+ const s = eventStore()
223
+ const def = s?.defaultCalendarForNewEvents
224
+
225
+ return def
226
+ ? {
227
+ id: String(def.calendarIdentifier),
228
+ title: String(def.title ?? ''),
229
+ allowsModifications: !!def.allowsContentModifications,
230
+ }
231
+ : null
232
+ }
233
+
234
+ return androidCalendars().find((c) => c.isPrimary) ?? androidCalendars()[0] ?? null
235
+ },
236
+
237
+ async getEvents(
238
+ startDate: Date,
239
+ endDate: Date,
240
+ calendarIds?: string[],
241
+ ): Promise<CalendarEvent[]> {
242
+ if (isIOS) {
243
+ const s = eventStore()
244
+ const begin = NSDate.dateWithTimeIntervalSince1970(startDate.getTime() / 1000)
245
+ const end = NSDate.dateWithTimeIntervalSince1970(endDate.getTime() / 1000)
246
+ const predicate = s.predicateForEventsWithStartDateEndDateCalendars(begin, end, null)
247
+ const list = s.eventsMatchingPredicate(predicate)
248
+ const out: CalendarEvent[] = []
249
+
250
+ for (let i = 0; i < (list?.count ?? 0); i++) {
251
+ const e = iosEvent(list.objectAtIndex(i))
252
+
253
+ if (!calendarIds?.length || calendarIds.includes(e.calendarId ?? '')) {
254
+ out.push(e)
255
+ }
256
+ }
257
+
258
+ return out
259
+ }
260
+
261
+ const selection = calendarIds?.length
262
+ ? `calendar_id IN (${calendarIds.map(() => '?').join(',')})`
263
+ : ''
264
+
265
+ const args = calendarIds ?? []
266
+
267
+ return androidQuery(
268
+ INSTANCES_URI(),
269
+ [
270
+ 'event_id',
271
+ 'calendar_id',
272
+ 'title',
273
+ 'begin',
274
+ 'end',
275
+ 'allDay',
276
+ 'eventLocation',
277
+ 'description',
278
+ ],
279
+ selection,
280
+ args,
281
+ 'begin ASC',
282
+ )
283
+ .map((row) => androidEvent(row))
284
+ .filter((e) => e.startDate <= endDate && e.endDate >= startDate)
285
+ },
286
+
287
+ async getEvent(id: string): Promise<CalendarEvent | null> {
288
+ if (isIOS) {
289
+ const e = eventStore()?.eventWithIdentifier?.(id)
290
+
291
+ return e ? iosEvent(e) : null
292
+ }
293
+
294
+ const rows = androidQuery(
295
+ (android.net.Uri as any).withAppendedId(EVENTS_URI(), Number(id)),
296
+ ['_id', 'calendar_id', 'title', 'dtstart', 'dtend', 'allDay', 'eventLocation', 'description'],
297
+ '',
298
+ [],
299
+ '',
300
+ )
301
+
302
+ return rows.length ? androidEvent(rows[0]) : null
303
+ },
304
+
305
+ async createEvent(calendarId: string | undefined, event: NewEvent): Promise<string> {
306
+ if (isIOS) {
307
+ const s = eventStore()
308
+ const e = EKEvent.eventWithEventStore(s)
309
+
310
+ e.title = event.title
311
+ e.startDate = NSDate.dateWithTimeIntervalSince1970(event.startDate.getTime() / 1000)
312
+ e.endDate = NSDate.dateWithTimeIntervalSince1970(event.endDate.getTime() / 1000)
313
+ e.allDay = !!event.allDay
314
+ e.location = event.location
315
+ e.notes = event.notes
316
+ e.calendar = calendarId
317
+ ? (s.calendarWithIdentifier?.(calendarId) ?? s.defaultCalendarForNewEvents)
318
+ : s.defaultCalendarForNewEvents
319
+
320
+ if (!s.saveEventSpanError(e, 0, null)) {
321
+ throw new Error('calendar.createEvent: EKEventStore.saveEvent failed')
322
+ }
323
+
324
+ return String(e.eventIdentifier ?? '')
325
+ }
326
+
327
+ const values = new android.content.ContentValues()
328
+ const def = await calendar.getDefaultCalendar()
329
+
330
+ values.put('calendar_id', java.lang.Long.valueOf(calendarId ?? def?.id ?? '0'))
331
+ values.put('title', event.title)
332
+ values.put('dtstart', java.lang.Long.valueOf(event.startDate.getTime()))
333
+ values.put('dtend', java.lang.Long.valueOf(event.endDate.getTime()))
334
+ values.put('allDay', java.lang.Integer.valueOf(event.allDay ? 1 : 0))
335
+ if (event.location) {
336
+ values.put('eventLocation', event.location)
337
+ }
338
+
339
+ if (event.notes) {
340
+ values.put('description', event.notes)
341
+ }
342
+
343
+ values.put('eventTimezone', event.timeZone ?? 'UTC')
344
+
345
+ const uri = resolver()?.insert(EVENTS_URI(), values)
346
+
347
+ return uri ? String(uri.getLastPathSegment()) : ''
348
+ },
349
+
350
+ async updateEvent(id: string, patch: Partial<NewEvent>): Promise<string> {
351
+ if (isIOS) {
352
+ const s = eventStore()
353
+ const e = s.eventWithIdentifier?.(id)
354
+
355
+ if (!e) {
356
+ throw new Error(`calendar.updateEvent: no event ${id}`)
357
+ }
358
+
359
+ if (patch.title !== undefined) {
360
+ e.title = patch.title
361
+ }
362
+
363
+ if (patch.startDate) {
364
+ e.startDate = NSDate.dateWithTimeIntervalSince1970(patch.startDate.getTime() / 1000)
365
+ }
366
+
367
+ if (patch.endDate) {
368
+ e.endDate = NSDate.dateWithTimeIntervalSince1970(patch.endDate.getTime() / 1000)
369
+ }
370
+
371
+ if (patch.allDay !== undefined) {
372
+ e.allDay = patch.allDay
373
+ }
374
+
375
+ if (patch.location !== undefined) {
376
+ e.location = patch.location
377
+ }
378
+
379
+ if (patch.notes !== undefined) {
380
+ e.notes = patch.notes
381
+ }
382
+
383
+ s.saveEventSpanError(e, 0, null)
384
+ return id
385
+ }
386
+
387
+ const values = new android.content.ContentValues()
388
+
389
+ if (patch.title !== undefined) {
390
+ values.put('title', patch.title)
391
+ }
392
+
393
+ if (patch.startDate) {
394
+ values.put('dtstart', java.lang.Long.valueOf(patch.startDate.getTime()))
395
+ }
396
+
397
+ if (patch.endDate) {
398
+ values.put('dtend', java.lang.Long.valueOf(patch.endDate.getTime()))
399
+ }
400
+
401
+ if (patch.location !== undefined) {
402
+ values.put('eventLocation', patch.location)
403
+ }
404
+
405
+ if (patch.notes !== undefined) {
406
+ values.put('description', patch.notes)
407
+ }
408
+
409
+ resolver()?.update(
410
+ (android.net.Uri as any).withAppendedId(EVENTS_URI(), Number(id)),
411
+ values,
412
+ null,
413
+ null,
414
+ )
415
+
416
+ return id
417
+ },
418
+
419
+ async deleteEvent(id: string): Promise<void> {
420
+ if (isIOS) {
421
+ const s = eventStore()
422
+ const e = s.eventWithIdentifier?.(id)
423
+
424
+ if (e) {
425
+ s.removeEventSpanError(e, 0, null)
426
+ }
427
+
428
+ return
429
+ }
430
+
431
+ resolver()?.delete(
432
+ (android.net.Uri as any).withAppendedId(EVENTS_URI(), Number(id)),
433
+ null,
434
+ null,
435
+ )
436
+ },
307
437
  }
308
438
 
309
439
  const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
440
+
310
441
  permissionOwners.calendar = () => calendar.ensure()