@octane-xplat/calendar 0.0.1 → 0.19.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 ADDED
@@ -0,0 +1,40 @@
1
+ # @octane-xplat/calendar
2
+
3
+ Read and edit the device calendar on iOS and Android. Web and desktop report
4
+ `supported: false`; this package does not generate calendar invitation files.
5
+
6
+ ## Read upcoming events
7
+
8
+ Install in an existing Xplat app:
9
+
10
+ ```sh
11
+ bun add @octane-xplat/calendar
12
+ ```
13
+
14
+ Configure the app's calendar privacy declarations before requesting access.
15
+ Call the following from an action such as **Show my upcoming events**. The
16
+ example uses actual dates and performs no writes:
17
+
18
+ ```ts
19
+ import { calendar } from '@octane-xplat/calendar'
20
+
21
+ export async function upcomingEvents() {
22
+ if (!calendar.supported || (await calendar.ensure()) !== 'granted') return []
23
+ const start = new Date()
24
+ const end = new Date(start.getTime() + 7 * 24 * 60 * 60 * 1000)
25
+ return calendar.getEvents(start, end)
26
+ }
27
+ ```
28
+
29
+ With access granted, the result contains events in the requested range.
30
+ An empty result may mean no events or, in this example, no access; use a
31
+ separate permission message in your screen when that distinction matters.
32
+ Event dates are JavaScript `Date` values. Catch and display operation failures
33
+ at the action boundary.
34
+
35
+ The package uses EventKit on iOS and CalendarContract on Android. Reminders,
36
+ attendees, and recurring-event edit modes are outside this API. Review the
37
+ [public calendar contract](https://github.com/octane-xplat/octane-xplat/blob/main/packages/calendar/src/types.ts)
38
+ before adding writes, and [permission handling](https://octane-xplat.goddardai.org/platform-services#the-shared-service-shape)
39
+ for the app's unavailable and denied states. Calendar privacy configuration
40
+ and device behavior still need verification in the app you ship.
package/package.json CHANGED
@@ -1,11 +1,87 @@
1
1
  {
2
2
  "name": "@octane-xplat/calendar",
3
- "version": "0.0.1",
4
- "description": "Placeholder release — no supported API. The first real version publishes via octane-xplat CI.",
3
+ "version": "0.19.0",
4
+ "description": "Device calendar for Octane xplat — event store reads and writes via EventKit/CalendarContract",
5
+ "keywords": [
6
+ "android",
7
+ "cross-platform",
8
+ "nativescript",
9
+ "octane",
10
+ "xplat",
11
+ "calendar",
12
+ "events"
13
+ ],
14
+ "homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/calendar",
5
15
  "license": "MIT",
6
16
  "repository": {
7
17
  "type": "git",
8
18
  "url": "https://github.com/octane-xplat/octane-xplat",
9
19
  "directory": "packages/calendar"
20
+ },
21
+ "files": [
22
+ "src",
23
+ "platforms",
24
+ "types",
25
+ "!src/**/*.test.*",
26
+ "!src/**/*.spec.*"
27
+ ],
28
+ "type": "module",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./types/generated/index.d.ts",
32
+ "macos": "./src/index.macos.ts",
33
+ "web": "./src/index.web.ts",
34
+ "native": "./src/index.ts",
35
+ "default": "./src/index.ts"
36
+ },
37
+ "./*": {
38
+ "types": "./types/generated/*.d.ts",
39
+ "default": "./src/*"
40
+ },
41
+ "./*.ts": {
42
+ "types": "./types/generated/*.d.ts",
43
+ "default": "./src/*.ts"
44
+ }
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "scripts": {
50
+ "typegen": "tsrx-typegen --target octane",
51
+ "prepack": "bun run typegen && tsrx-typegen --pack-check",
52
+ "pack:check": "tsrx-typegen --pack-check",
53
+ "typecheck": "node ../../scripts/typecheck.ts",
54
+ "test": "node --test tests/*.test.ts"
55
+ },
56
+ "devDependencies": {
57
+ "@nativescript-community/octane": "0.2.5",
58
+ "@nativescript/core": "9.1.3",
59
+ "@nativescript/types": "9.1.4",
60
+ "@tsrx/typescript-plugin": "0.4.11",
61
+ "octane": "0.16.3",
62
+ "tsrx-typegen": "0.5.0",
63
+ "typescript": "5.9.3"
64
+ },
65
+ "peerDependencies": {
66
+ "@nativescript-community/octane": ">=0.2.1 <1",
67
+ "@nativescript/core": ">=9.1.0 <10",
68
+ "octane": ">=0.6.3 <1"
69
+ },
70
+ "peerDependenciesMeta": {
71
+ "@nativescript-community/octane": {
72
+ "optional": true
73
+ },
74
+ "@nativescript/core": {
75
+ "optional": true
76
+ }
77
+ },
78
+ "nativescript": {
79
+ "platforms": {
80
+ "android": "9.1.0",
81
+ "ios": "9.1.0"
82
+ }
83
+ },
84
+ "dependencies": {
85
+ "@octane-xplat/platform": "0.19.0"
10
86
  }
11
87
  }
@@ -0,0 +1,4 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ <uses-permission android:name="android.permission.READ_CALENDAR" />
3
+ <uses-permission android:name="android.permission.WRITE_CALENDAR" />
4
+ </manifest>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>NSCalendarsUsageDescription</key>
6
+ <string>Calendar access is required to read and write events.</string>
7
+ <key>NSCalendarsWriteOnlyAccessUsageDescription</key>
8
+ <string>Calendar access is required to create events.</string>
9
+ </dict>
10
+ </plist>
@@ -0,0 +1 @@
1
+ export { calendar } from './calendar.web'
@@ -0,0 +1,244 @@
1
+ // Calendar — AppKit leaf over EventKit. EKEventStore is bridgeable through
2
+ // the JSC host; the API mirrors the iOS leaf's surface (same store class,
3
+ // same entity type). Metadata targets macOS 11, so the deprecated
4
+ // requestAccessToEntityTypeCompletion is the bound request entry point —
5
+ // on macOS 14+ it still performs the full-access events prompt.
6
+ import {
7
+ EKEntityType,
8
+ EKEvent,
9
+ EKEventStore,
10
+ EKSpan,
11
+ NSDate,
12
+ } from '@octane-xplat/platform/macos'
13
+ import type { Calendar, CalendarEvent, CalendarInfo, NewEvent, PermissionResult } from './types'
14
+
15
+ let store: any = null
16
+ const eventStore = () => {
17
+ if (store) {
18
+ return store
19
+ }
20
+
21
+ try {
22
+ if (typeof EKEventStore?.authorizationStatusForEntityType !== 'function') {
23
+ return null
24
+ }
25
+ return (store = EKEventStore.new())
26
+ } catch {
27
+ return null
28
+ }
29
+ }
30
+
31
+ // EKAuthorizationStatus: 0 notDetermined 1 restricted 2 denied
32
+ // 3 fullAccess/authorized 4 writeOnly
33
+ const rawStatus = () =>
34
+ Number(EKEventStore.authorizationStatusForEntityType(EKEntityType.Event ?? 0))
35
+
36
+ function ensureAccess(): Promise<PermissionResult> {
37
+ const s = eventStore()
38
+ if (!s) {
39
+ return Promise.resolve('unsupported')
40
+ }
41
+
42
+ const status = rawStatus()
43
+ if (status === 3 || status === 4) {
44
+ return Promise.resolve('granted')
45
+ }
46
+
47
+ if (status === 1 || status === 2) {
48
+ return Promise.resolve('denied')
49
+ }
50
+
51
+ return new Promise((resolve) => {
52
+ s.requestAccessToEntityTypeCompletion(EKEntityType.Event ?? 0, (granted: boolean) =>
53
+ resolve(granted ? 'granted' : 'denied'),
54
+ )
55
+ })
56
+ }
57
+
58
+ const permissionResponse = () => {
59
+ const s = eventStore()
60
+ if (!s) {
61
+ return { status: 'unsupported' as const, granted: false, canAskAgain: false }
62
+ }
63
+
64
+ const status = rawStatus()
65
+ return {
66
+ status: status === 3 || status === 4 ? ('granted' as const) : status === 0 ? ('undetermined' as const) : ('denied' as const),
67
+ granted: status === 3 || status === 4,
68
+ canAskAgain: status === 0 || status === 3 || status === 4,
69
+ }
70
+ }
71
+
72
+ function calendars(): CalendarInfo[] {
73
+ const s = eventStore()
74
+ const out: CalendarInfo[] = []
75
+ const list = s?.calendarsForEntityType?.(EKEntityType.Event ?? 0)
76
+ for (let i = 0; i < (list?.count ?? 0); i++) {
77
+ const c = list.objectAtIndex(i)
78
+ out.push({
79
+ id: String(c.calendarIdentifier ?? ''),
80
+ title: String(c.title ?? ''),
81
+ color: c.CGColor ? String(c.CGColor) : undefined,
82
+ source: c.source?.title ? String(c.source.title) : undefined,
83
+ allowsModifications: !!c.allowsContentModifications,
84
+ isPrimary: false,
85
+ })
86
+ }
87
+
88
+ return out
89
+ }
90
+
91
+ function asEvent(e: any): CalendarEvent {
92
+ return {
93
+ id: String(e.eventIdentifier ?? ''),
94
+ calendarId: e.calendar?.calendarIdentifier ? String(e.calendar.calendarIdentifier) : undefined,
95
+ title: String(e.title ?? ''),
96
+ startDate: new Date((e.startDate?.timeIntervalSince1970 ?? 0) * 1000),
97
+ endDate: new Date((e.endDate?.timeIntervalSince1970 ?? 0) * 1000),
98
+ allDay: !!e.allDay,
99
+ location: e.location ? String(e.location) : undefined,
100
+ notes: e.notes ? String(e.notes) : undefined,
101
+ timeZone: e.timeZone?.name ? String(e.timeZone.name) : undefined,
102
+ }
103
+ }
104
+
105
+ const toNSDate = (date: Date) => NSDate.dateWithTimeIntervalSince1970(date.getTime() / 1000)
106
+
107
+ /** Calendar service — EventKit/CalendarContract. */
108
+ export const calendar: Calendar = {
109
+ get supported() {
110
+ return eventStore() !== null
111
+ },
112
+
113
+ ensure(): Promise<PermissionResult> {
114
+ return ensureAccess()
115
+ },
116
+
117
+ async getCalendars(): Promise<CalendarInfo[]> {
118
+ return calendars()
119
+ },
120
+
121
+ async getDefaultCalendar(): Promise<CalendarInfo | null> {
122
+ const def = eventStore()?.defaultCalendarForNewEvents
123
+ return def
124
+ ? {
125
+ id: String(def.calendarIdentifier),
126
+ title: String(def.title ?? ''),
127
+ allowsModifications: !!def.allowsContentModifications,
128
+ }
129
+ : null
130
+ },
131
+
132
+ async getEvents(
133
+ startDate: Date,
134
+ endDate: Date,
135
+ calendarIds?: string[],
136
+ ): Promise<CalendarEvent[]> {
137
+ const s = eventStore()
138
+ const wanted =
139
+ calendarIds?.length
140
+ ? calendarIds
141
+ .map((id) => s?.calendarWithIdentifier?.(id))
142
+ .filter((c: any) => c != null)
143
+ : null
144
+ const predicate = s?.predicateForEventsWithStartDateEndDateCalendars(
145
+ toNSDate(startDate),
146
+ toNSDate(endDate),
147
+ wanted?.length ? wanted : null,
148
+ )
149
+ const list = s?.eventsMatchingPredicate?.(predicate)
150
+ const out: CalendarEvent[] = []
151
+ for (let i = 0; i < (list?.count ?? 0); i++) {
152
+ const e = asEvent(list.objectAtIndex(i))
153
+ if (!calendarIds?.length || calendarIds.includes(e.calendarId ?? '')) {
154
+ out.push(e)
155
+ }
156
+ }
157
+
158
+ return out
159
+ },
160
+
161
+ async getEvent(id: string): Promise<CalendarEvent | null> {
162
+ const e = eventStore()?.eventWithIdentifier?.(id)
163
+ return e ? asEvent(e) : null
164
+ },
165
+
166
+ async createEvent(calendarId: string | undefined, event: NewEvent): Promise<string> {
167
+ const s = eventStore()
168
+ if (!s) {
169
+ throw new Error('calendar.createEvent: EventKit is unavailable on this host')
170
+ }
171
+
172
+ const e = EKEvent.eventWithEventStore(s)
173
+ e.title = event.title
174
+ e.startDate = toNSDate(event.startDate)
175
+ e.endDate = toNSDate(event.endDate)
176
+ e.allDay = !!event.allDay
177
+ e.location = (event.location ?? null) as string
178
+ e.notes = (event.notes ?? null) as string
179
+ e.calendar = calendarId
180
+ ? (s.calendarWithIdentifier?.(calendarId) ?? s.defaultCalendarForNewEvents)
181
+ : s.defaultCalendarForNewEvents
182
+
183
+ if (!s.saveEventSpanError(e, EKSpan.ThisEvent ?? 0, null)) {
184
+ throw new Error('calendar.createEvent: EKEventStore.saveEvent failed')
185
+ }
186
+
187
+ return String(e.eventIdentifier ?? '')
188
+ },
189
+
190
+ async updateEvent(id: string, patch: Partial<NewEvent>): Promise<string> {
191
+ const s = eventStore()
192
+ const e = s?.eventWithIdentifier?.(id)
193
+ if (!e) {
194
+ throw new Error(`calendar.updateEvent: no event ${id}`)
195
+ }
196
+
197
+ if (patch.title !== undefined) {
198
+ e.title = patch.title
199
+ }
200
+
201
+ if (patch.startDate) {
202
+ e.startDate = toNSDate(patch.startDate)
203
+ }
204
+
205
+ if (patch.endDate) {
206
+ e.endDate = toNSDate(patch.endDate)
207
+ }
208
+
209
+ if (patch.allDay !== undefined) {
210
+ e.allDay = patch.allDay
211
+ }
212
+
213
+ if (patch.location !== undefined) {
214
+ e.location = patch.location
215
+ }
216
+
217
+ if (patch.notes !== undefined) {
218
+ e.notes = patch.notes
219
+ }
220
+
221
+ if (!s.saveEventSpanError(e, EKSpan.ThisEvent ?? 0, null)) {
222
+ throw new Error('calendar.updateEvent: EKEventStore.saveEvent failed')
223
+ }
224
+
225
+ return id
226
+ },
227
+
228
+ async deleteEvent(id: string): Promise<void> {
229
+ const s = eventStore()
230
+ const e = s?.eventWithIdentifier?.(id)
231
+ if (e && !s.removeEventSpanError(e, EKSpan.ThisEvent ?? 0, null)) {
232
+ throw new Error('calendar.deleteEvent: EKEventStore.removeEvent failed')
233
+ }
234
+ },
235
+ }
236
+
237
+ const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
238
+ permissionOwners.calendar = {
239
+ getPermissions: async () => permissionResponse(),
240
+ requestPermissions: async () => {
241
+ const result = await calendar.ensure()
242
+ return { status: result, granted: result === 'granted', canAskAgain: result === 'granted' }
243
+ },
244
+ }
@@ -0,0 +1,396 @@
1
+ // Calendar — native leaf, custom bridge (nativescript-calendar is stale).
2
+ // iOS: EventKit EKEventStore via declare-const bridging (auth-session
3
+ // pattern). Android: CalendarContract through the content provider.
4
+ import { Application, Utils, isIOS } from '@nativescript/core'
5
+ import { android } from '@octane-xplat/platform/android'
6
+ import type { Calendar, CalendarEvent, CalendarInfo, NewEvent, PermissionResult } from './types'
7
+
8
+ declare const EKEventStore: any
9
+ declare const EKEntityTypeEvent: number | undefined
10
+ declare const EKAuthorizationStatus: any
11
+ declare const EKEvent: any
12
+ declare const NSPredicate: any
13
+ declare const NSDate: any
14
+
15
+ let store: any = null
16
+ const eventStore = () => {
17
+ if (!store && typeof EKEventStore !== 'undefined') {
18
+ store = EKEventStore.new()
19
+ }
20
+ return store
21
+ }
22
+
23
+ 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
+ })
47
+ }
48
+
49
+ let calRequestCode = 7731
50
+
51
+ 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
+ }
90
+ }
91
+
92
+ 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
108
+ }
109
+
110
+ 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
+ }
122
+ }
123
+
124
+ const androidUtils = Utils.android as any
125
+ const resolver = () => androidUtils?.getApplicationContext?.()?.getContentResolver()
126
+
127
+ function androidQuery(
128
+ uri: any,
129
+ projection: string[],
130
+ selection: string,
131
+ args: string[],
132
+ sort: string,
133
+ ): 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
149
+ }
150
+
151
+ const CAL_URI = () => android.provider.CalendarContract.Calendars.CONTENT_URI
152
+ const EVENTS_URI = () => android.provider.CalendarContract.Events.CONTENT_URI
153
+ const INSTANCES_URI = () => android.provider.CalendarContract.Instances.CONTENT_URI
154
+
155
+ 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
+ }))
177
+ }
178
+
179
+ 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
+ }
190
+ }
191
+
192
+ /** Calendar service — EventKit/CalendarContract. */
193
+ 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
+ },
393
+ }
394
+
395
+ const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
396
+ permissionOwners.calendar = () => calendar.ensure()
@@ -0,0 +1,21 @@
1
+ // Calendar — no web calendar API exists; .ics generation is userland/library
2
+ // territory. Honest unsupported leaf.
3
+ import type { Calendar, PermissionResult } from './types'
4
+
5
+ const unsupported = () => Promise.reject(new Error('unsupported: no calendar API on web'))
6
+
7
+ /** Calendar service — EventKit/CalendarContract. */
8
+ export const calendar: Calendar = {
9
+ supported: false,
10
+ ensure: async (): Promise<PermissionResult> => 'unsupported',
11
+ getCalendars: unsupported,
12
+ getDefaultCalendar: unsupported,
13
+ getEvents: unsupported,
14
+ getEvent: unsupported,
15
+ createEvent: unsupported,
16
+ updateEvent: unsupported,
17
+ deleteEvent: unsupported,
18
+ }
19
+
20
+ const permissionOwners = ((globalThis as any).__xplatPermissionOwners ??= {})
21
+ permissionOwners.calendar = () => calendar.ensure()
@@ -0,0 +1,17 @@
1
+ import type { Calendar, PermissionResult } from './types'
2
+
3
+ const unsupported = () =>
4
+ Promise.reject(new Error('unsupported: calendar is not wired on this platform'))
5
+
6
+ /** Calendar service — EventKit/CalendarContract. */
7
+ export const calendar: Calendar = {
8
+ supported: false,
9
+ ensure: async (): Promise<PermissionResult> => 'unsupported',
10
+ getCalendars: unsupported,
11
+ getDefaultCalendar: unsupported,
12
+ getEvents: unsupported,
13
+ getEvent: unsupported,
14
+ createEvent: unsupported,
15
+ updateEvent: unsupported,
16
+ deleteEvent: unsupported,
17
+ }
@@ -0,0 +1,2 @@
1
+ export * from './types'
2
+ export { calendar } from './calendar.macos'
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './types'
2
+ export { calendar } from './calendar'
@@ -0,0 +1,2 @@
1
+ export * from './types'
2
+ export { calendar } from './calendar.web'
package/src/types.ts ADDED
@@ -0,0 +1,84 @@
1
+ /** Coarse permission answer; 'unsupported' means the platform has no
2
+ * calendar/permission path. */
3
+ export type PermissionResult = 'granted' | 'denied' | 'unsupported'
4
+
5
+ /** One calendar account-local store. */
6
+ export interface CalendarInfo {
7
+ /** Store-assigned id — pass to getEvents/createEvent. */
8
+ id: string
9
+ /** Display name. */
10
+ title: string
11
+ /** Display color (CSS color where the store exposes one). */
12
+ color?: string
13
+ /** Originating account/source name (iCloud, local, Exchange, …). */
14
+ source?: string
15
+ /** False for read-only stores (subscribed/Birthdays calendars). */
16
+ allowsModifications?: boolean
17
+ /** The store's default calendar. */
18
+ isPrimary?: boolean
19
+ }
20
+
21
+ /** A stored calendar event. */
22
+ export interface CalendarEvent {
23
+ /** Store-assigned id — pass to getEvent/updateEvent/deleteEvent. */
24
+ id: string
25
+ /** The owning CalendarInfo.id; absent where the store doesn't report it. */
26
+ calendarId?: string
27
+ /** Display title. */
28
+ title: string
29
+ /** Start instant. */
30
+ startDate: Date
31
+ /** End instant. */
32
+ endDate: Date
33
+ /** All-day flag; all-day dates are date-only values. */
34
+ allDay?: boolean
35
+ /** Free-text location. */
36
+ location?: string
37
+ /** Free-text notes. */
38
+ notes?: string
39
+ /** IANA time-zone name when the store records one. */
40
+ timeZone?: string
41
+ }
42
+
43
+ /** Event fields for createEvent/updateEvent — no store ids. */
44
+ export interface NewEvent {
45
+ /** Display title. */
46
+ title: string
47
+ /** Start instant. */
48
+ startDate: Date
49
+ /** End instant. */
50
+ endDate: Date
51
+ /** All-day flag. */
52
+ allDay?: boolean
53
+ /** Free-text location. */
54
+ location?: string
55
+ /** Free-text notes. */
56
+ notes?: string
57
+ /** IANA time-zone name. */
58
+ timeZone?: string
59
+ }
60
+
61
+ /** EventCalendar/EventKit calendar façade; false `supported` off mobile. */
62
+ export interface Calendar {
63
+ /** Synchronous fast check — a calendar backend exists. */
64
+ readonly supported: boolean
65
+ /** Ensure calendar permission, prompting if needed. */
66
+ ensure(): Promise<PermissionResult>
67
+ /** Every calendar store visible to the app. */
68
+ getCalendars(): Promise<CalendarInfo[]>
69
+ /** The store's default calendar, or null when none is writable. */
70
+ getDefaultCalendar(): Promise<CalendarInfo | null>
71
+ /** Events overlapping the range, optionally limited to calendar ids. */
72
+ getEvents(startDate: Date, endDate: Date, calendarIds?: string[]): Promise<CalendarEvent[]>
73
+ /** One event by id, or null. */
74
+ getEvent(id: string): Promise<CalendarEvent | null>
75
+ /** Insert an event; resolves the store-assigned event id. Pass
76
+ * undefined calendarId for the default calendar. */
77
+ createEvent(calendarId: string | undefined, event: NewEvent): Promise<string>
78
+ /** Patch fields of an existing event; resolves the event id. */
79
+ updateEvent(id: string, patch: Partial<NewEvent>): Promise<string>
80
+ /** Remove an event by id. */
81
+ deleteEvent(id: string): Promise<void>
82
+ }
83
+
84
+ export declare const calendar: Calendar
@@ -0,0 +1,14 @@
1
+ {
2
+ "version": 1,
3
+ "files": [
4
+ "calendar.d.ts",
5
+ "calendar.linux.d.ts",
6
+ "calendar.macos.d.ts",
7
+ "calendar.web.d.ts",
8
+ "calendar.windows.d.ts",
9
+ "index.d.ts",
10
+ "index.macos.d.ts",
11
+ "index.web.d.ts",
12
+ "types.d.ts"
13
+ ]
14
+ }
@@ -0,0 +1,3 @@
1
+ import type { Calendar } from './types.js';
2
+ /** Calendar service — EventKit/CalendarContract. */
3
+ export declare const calendar: Calendar;
@@ -0,0 +1 @@
1
+ export { calendar } from './calendar.web.js';
@@ -0,0 +1,3 @@
1
+ import type { Calendar } from './types.js';
2
+ /** Calendar service — EventKit/CalendarContract. */
3
+ export declare const calendar: Calendar;
@@ -0,0 +1,3 @@
1
+ import type { Calendar } from './types.js';
2
+ /** Calendar service — EventKit/CalendarContract. */
3
+ export declare const calendar: Calendar;
@@ -0,0 +1,3 @@
1
+ import type { Calendar } from './types.js';
2
+ /** Calendar service — EventKit/CalendarContract. */
3
+ export declare const calendar: Calendar;
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { calendar } from './calendar.js';
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { calendar } from './calendar.macos.js';
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export { calendar } from './calendar.web.js';
@@ -0,0 +1,79 @@
1
+ /** Coarse permission answer; 'unsupported' means the platform has no
2
+ * calendar/permission path. */
3
+ export type PermissionResult = 'granted' | 'denied' | 'unsupported';
4
+ /** One calendar account-local store. */
5
+ export interface CalendarInfo {
6
+ /** Store-assigned id — pass to getEvents/createEvent. */
7
+ id: string;
8
+ /** Display name. */
9
+ title: string;
10
+ /** Display color (CSS color where the store exposes one). */
11
+ color?: string;
12
+ /** Originating account/source name (iCloud, local, Exchange, …). */
13
+ source?: string;
14
+ /** False for read-only stores (subscribed/Birthdays calendars). */
15
+ allowsModifications?: boolean;
16
+ /** The store's default calendar. */
17
+ isPrimary?: boolean;
18
+ }
19
+ /** A stored calendar event. */
20
+ export interface CalendarEvent {
21
+ /** Store-assigned id — pass to getEvent/updateEvent/deleteEvent. */
22
+ id: string;
23
+ /** The owning CalendarInfo.id; absent where the store doesn't report it. */
24
+ calendarId?: string;
25
+ /** Display title. */
26
+ title: string;
27
+ /** Start instant. */
28
+ startDate: Date;
29
+ /** End instant. */
30
+ endDate: Date;
31
+ /** All-day flag; all-day dates are date-only values. */
32
+ allDay?: boolean;
33
+ /** Free-text location. */
34
+ location?: string;
35
+ /** Free-text notes. */
36
+ notes?: string;
37
+ /** IANA time-zone name when the store records one. */
38
+ timeZone?: string;
39
+ }
40
+ /** Event fields for createEvent/updateEvent — no store ids. */
41
+ export interface NewEvent {
42
+ /** Display title. */
43
+ title: string;
44
+ /** Start instant. */
45
+ startDate: Date;
46
+ /** End instant. */
47
+ endDate: Date;
48
+ /** All-day flag. */
49
+ allDay?: boolean;
50
+ /** Free-text location. */
51
+ location?: string;
52
+ /** Free-text notes. */
53
+ notes?: string;
54
+ /** IANA time-zone name. */
55
+ timeZone?: string;
56
+ }
57
+ /** EventCalendar/EventKit calendar façade; false `supported` off mobile. */
58
+ export interface Calendar {
59
+ /** Synchronous fast check — a calendar backend exists. */
60
+ readonly supported: boolean;
61
+ /** Ensure calendar permission, prompting if needed. */
62
+ ensure(): Promise<PermissionResult>;
63
+ /** Every calendar store visible to the app. */
64
+ getCalendars(): Promise<CalendarInfo[]>;
65
+ /** The store's default calendar, or null when none is writable. */
66
+ getDefaultCalendar(): Promise<CalendarInfo | null>;
67
+ /** Events overlapping the range, optionally limited to calendar ids. */
68
+ getEvents(startDate: Date, endDate: Date, calendarIds?: string[]): Promise<CalendarEvent[]>;
69
+ /** One event by id, or null. */
70
+ getEvent(id: string): Promise<CalendarEvent | null>;
71
+ /** Insert an event; resolves the store-assigned event id. Pass
72
+ * undefined calendarId for the default calendar. */
73
+ createEvent(calendarId: string | undefined, event: NewEvent): Promise<string>;
74
+ /** Patch fields of an existing event; resolves the event id. */
75
+ updateEvent(id: string, patch: Partial<NewEvent>): Promise<string>;
76
+ /** Remove an event by id. */
77
+ deleteEvent(id: string): Promise<void>;
78
+ }
79
+ export declare const calendar: Calendar;