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