@pmate/bridge 0.1.5 → 0.1.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/bridge",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "files": [
5
5
  "src",
6
6
  "global.d.ts"
@@ -2,6 +2,8 @@ import { PropsWithChildren, ReactNode, useEffect, useMemo, useState } from "reac
2
2
  import { PmateBridgeContext } from "./context"
3
3
  import {
4
4
  getFallbackSafeInsets,
5
+ getNotificationStatus,
6
+ getPushToken,
5
7
  pickFileBridge,
6
8
  readDeviceInfo,
7
9
  readIsPmate,
@@ -84,6 +86,8 @@ export function PmateBridgeProvider({
84
86
  safeInsetsState,
85
87
  deviceInfoState,
86
88
  requestPermissions: requestPermissionsBridge,
89
+ getNotificationStatus,
90
+ getPushToken,
87
91
  pickFile: pickFileBridge,
88
92
  }),
89
93
  [deviceInfoState, isPmate, safeInsetsState]
package/src/context.tsx CHANGED
@@ -1,8 +1,11 @@
1
1
  import { createContext, useContext } from "react"
2
2
  import type {
3
3
  DeviceInfoState,
4
+ NotificationStatus,
4
5
  PermissionResult,
5
6
  PickedFile,
7
+ PushTokenInfo,
8
+ PushTokenOptions,
6
9
  SafeInsetsState,
7
10
  } from "./types"
8
11
 
@@ -11,6 +14,8 @@ export interface PmateBridgeContextValue {
11
14
  safeInsetsState: SafeInsetsState
12
15
  deviceInfoState: DeviceInfoState
13
16
  requestPermissions: (permissions: string[]) => Promise<PermissionResult>
17
+ getNotificationStatus: () => Promise<NotificationStatus>
18
+ getPushToken: (options?: PushTokenOptions) => Promise<PushTokenInfo | undefined>
14
19
  pickFile: () => Promise<PickedFile[]>
15
20
  }
16
21
 
package/src/hooks.ts CHANGED
@@ -2,13 +2,15 @@ import { useCallback, useEffect, useState } from "react"
2
2
  import { useOptionalPmateBridgeContext } from "./context"
3
3
  import {
4
4
  getFallbackSafeInsets,
5
+ getNotificationStatus,
6
+ getPushToken,
5
7
  pickFileBridge,
6
8
  readDeviceInfo,
7
9
  readIsPmate,
8
10
  readSafeInsets,
9
11
  requestPermissionsBridge,
10
12
  } from "./internal"
11
- import type { DeviceInfoState, SafeInsetsState } from "./types"
13
+ import type { DeviceInfoState, PushTokenOptions, SafeInsetsState } from "./types"
12
14
 
13
15
  export function useIsPmate() {
14
16
  return useOptionalPmateBridgeContext()?.isPmate ?? readIsPmate()
@@ -93,3 +95,19 @@ export function useRequestPermissions() {
93
95
  [context]
94
96
  )
95
97
  }
98
+
99
+ export function useNotificationStatus() {
100
+ const context = useOptionalPmateBridgeContext()
101
+ return useCallback(
102
+ () => context?.getNotificationStatus() ?? getNotificationStatus(),
103
+ [context]
104
+ )
105
+ }
106
+
107
+ export function useGetPushToken() {
108
+ const context = useOptionalPmateBridgeContext()
109
+ return useCallback(
110
+ (options?: PushTokenOptions) => context?.getPushToken(options) ?? getPushToken(options),
111
+ [context]
112
+ )
113
+ }
package/src/index.ts CHANGED
@@ -2,15 +2,22 @@ export * from "./PmateBridgeProvider"
2
2
  export * from "./SafeAreaVarsProvider"
3
3
  export * from "./hooks"
4
4
  export {
5
+ cancelAlarm,
6
+ cancelAllAlarms,
5
7
  connectBluetoothDevice,
6
8
  disconnectBluetoothDevice,
9
+ getAlarm,
10
+ getAlarmCapabilities,
7
11
  getBluetoothCapabilities,
8
12
  getConnectedBluetoothDevices,
9
13
  getNotificationStatus,
10
14
  getPushToken,
15
+ listAlarms,
16
+ onAlarmEvent,
11
17
  onBluetoothEvent,
12
18
  readBluetooth,
13
19
  requestBluetoothPermission,
20
+ scheduleAlarm,
14
21
  scanBluetoothDevices,
15
22
  stopBluetoothScan,
16
23
  subscribeBluetooth,
package/src/internal.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import type {
2
+ Alarm,
3
+ AlarmCapabilities,
4
+ AlarmEventName,
2
5
  BluetoothCapabilities,
3
6
  BluetoothConnectOptions,
4
7
  BluetoothConnection,
@@ -18,6 +21,7 @@ import type {
18
21
  PushTokenInfo,
19
22
  PushTokenOptions,
20
23
  SafeInsets,
24
+ ScheduleAlarmInput,
21
25
  } from "./types"
22
26
 
23
27
  type PmateWindow = Window & {
@@ -112,7 +116,10 @@ export async function getNotificationStatus(): Promise<NotificationStatus> {
112
116
  export async function getPushToken(
113
117
  options?: PushTokenOptions
114
118
  ): Promise<PushTokenInfo | undefined> {
115
- return callBridge<PushTokenInfo>("notification.getPushToken", options ?? null)
119
+ if (!hasWindow() || !(window as PmateWindow).PmateBridge?.call) {
120
+ return undefined
121
+ }
122
+ return requireBridge<PushTokenInfo>("notification.getPushToken", options ?? null)
116
123
  }
117
124
 
118
125
  export async function pickFileBridge(): Promise<PickedFile[]> {
@@ -120,6 +127,50 @@ export async function pickFileBridge(): Promise<PickedFile[]> {
120
127
  return files ?? []
121
128
  }
122
129
 
130
+ export async function getAlarmCapabilities(): Promise<AlarmCapabilities> {
131
+ const capabilities = await callBridge<AlarmCapabilities>("alarm.getCapabilities")
132
+ return (
133
+ capabilities ?? {
134
+ supported: false,
135
+ systemAlarm: false,
136
+ appAlarm: false,
137
+ exact: false,
138
+ repeating: false,
139
+ notificationPermission: "unsupported",
140
+ }
141
+ )
142
+ }
143
+
144
+ export function scheduleAlarm(input: ScheduleAlarmInput): Promise<Alarm> {
145
+ return requireBridge<Alarm>("alarm.schedule", input)
146
+ }
147
+
148
+ export async function getAlarm(id: string): Promise<Alarm | undefined> {
149
+ return callBridge<Alarm>("alarm.get", { id })
150
+ }
151
+
152
+ export function listAlarms(): Promise<{ alarms: Alarm[] }> {
153
+ return requireBridge<{ alarms: Alarm[] }>("alarm.list")
154
+ }
155
+
156
+ export function cancelAlarm(id: string): Promise<{ cancelled: true }> {
157
+ return requireBridge<{ cancelled: true }>("alarm.cancel", { id })
158
+ }
159
+
160
+ export function cancelAllAlarms(): Promise<{ cancelled: true }> {
161
+ return requireBridge<{ cancelled: true }>("alarm.cancelAll")
162
+ }
163
+
164
+ export function onAlarmEvent(
165
+ eventName: AlarmEventName,
166
+ handler: (payload: unknown) => void
167
+ ): () => void {
168
+ if (!hasWindow() || !(window as PmateWindow).PmateBridge?.on) {
169
+ throw new Error("Pmate bridge event subscription is unavailable")
170
+ }
171
+ return (window as PmateWindow).PmateBridge!.on!(`alarm.${eventName}`, handler)
172
+ }
173
+
123
174
  export function getBluetoothCapabilities(): Promise<BluetoothCapabilities> {
124
175
  return requireBridge<BluetoothCapabilities>("bluetooth.getCapabilities")
125
176
  }
package/src/types.ts CHANGED
@@ -61,6 +61,59 @@ export interface PushTokenOptions {
61
61
  providers?: PushProvider[]
62
62
  }
63
63
 
64
+ export type AlarmDeliveryMode = "systemAlarm" | "appAlarm"
65
+ export type AlarmDeliveryPreference =
66
+ | "preferSystemAlarm"
67
+ | "preferAppAlarm"
68
+ | "systemAlarmOnly"
69
+ export type AlarmPermissionState =
70
+ | "granted"
71
+ | "denied"
72
+ | "notDetermined"
73
+ | "unsupported"
74
+ export type AlarmRepeatRule =
75
+ | { type: "none" }
76
+ | { type: "interval"; intervalMs: number }
77
+ | { type: "daily"; timeOfDay: string; timezone?: string }
78
+ export type AlarmState = "scheduled" | "triggered" | "cancelled"
79
+ export type AlarmEventName = "triggered" | "cancelled" | "error"
80
+
81
+ export interface AlarmCapabilities {
82
+ supported: boolean
83
+ systemAlarm: boolean
84
+ appAlarm: boolean
85
+ exact: boolean
86
+ repeating: boolean
87
+ notificationPermission: AlarmPermissionState
88
+ minimumIntervalMs?: number
89
+ maximumScheduledCount?: number
90
+ }
91
+
92
+ export interface Alarm {
93
+ id: string
94
+ title?: string
95
+ body?: string
96
+ deliveryMode: AlarmDeliveryMode
97
+ scheduledAt: number
98
+ repeat: AlarmRepeatRule
99
+ payload?: Record<string, unknown>
100
+ state: AlarmState
101
+ createdAt: number
102
+ updatedAt: number
103
+ lastTriggeredAt?: number
104
+ nextTriggerAt?: number
105
+ }
106
+
107
+ export interface ScheduleAlarmInput {
108
+ id?: string
109
+ title?: string
110
+ body?: string
111
+ scheduledAt: number
112
+ repeat?: AlarmRepeatRule
113
+ deliveryPreference?: AlarmDeliveryPreference
114
+ payload?: Record<string, unknown>
115
+ }
116
+
64
117
  export type BluetoothTransport = "ble" | "classic"
65
118
  export type BluetoothBinaryEncoding = "base64" | "hex" | "utf-8"
66
119
  export type BluetoothEventName =