tauri-plugin-live-activity 0.5.0 → 0.6.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.ko.md +61 -3
- package/README.md +61 -2
- package/dist-js/index.d.ts +28 -1
- package/dist-js/index.js +69 -0
- package/dist-js/react/index.d.ts +2 -1
- package/dist-js/react/index.js +26 -2
- package/dist-js/react/session.d.ts +16 -1
- package/dist-js/react/session.js +39 -10
- package/dist-js/types.d.ts +41 -0
- package/package.json +1 -1
package/README.ko.md
CHANGED
|
@@ -103,8 +103,7 @@ app.live_activity().update_layout(LayoutUpdateInput { id, snapshot: LayoutSnapsh
|
|
|
103
103
|
레이아웃은 `text`/`bound`, `path`(SVG path 데이터), `vstack`·`hstack`·`zstack`·`spacer`·`divider`와
|
|
104
104
|
아래 여섯 가지로 만듭니다. 그림은 모두 iOS 잠금화면입니다. 예시의 빌더는 `layout`에서 꺼내 씁니다
|
|
105
105
|
(`const { vstack, hstack, text, bound, symbol, image, gauge, progress, timerText, button } = layout;`).
|
|
106
|
-
같은 레이아웃을 TSX로 쓸 수 있고, React 훅으로 컴포넌트와 액티비티를 함께 움직일 수도 있습니다
|
|
107
|
-
([TSX and React](https://github.com/1eedaegon/tauri-plugin-live-activity/blob/main/docs/usage.md#tsx-and-react)).
|
|
106
|
+
같은 레이아웃을 TSX로 쓸 수 있고, React 훅으로 컴포넌트와 액티비티를 함께 움직일 수도 있습니다([TSX와 React](#tsx와-react)).
|
|
108
107
|
|
|
109
108
|
### 심볼
|
|
110
109
|
|
|
@@ -175,6 +174,19 @@ await onAction(async ({ actionId, activityId }) => {
|
|
|
175
174
|
|
|
176
175
|
탭은 `onAction`으로, 앱이 열려 있지 않을 때는 Rust의 `on_action`으로 전달됩니다. iOS 17 이상이며, Android는 최대 세 개를 알림 버튼으로 보여 줍니다.
|
|
177
176
|
|
|
177
|
+
### 푸시
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
await onPushToken(({ activityId, token }) => sendToServer(activityId, token));
|
|
181
|
+
await onUpdated(({ record }) => show(record.snapshot.values));
|
|
182
|
+
await startLayout({ ...input, push: 'token' });
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
iOS에서는 서버가 APNs로 액티비티를 갱신하고, 끝내고, 시작할 수 있습니다. 액티비티의 토큰은 `onPushToken`으로,
|
|
186
|
+
앱의 push-to-start 토큰(iOS 17.2, 플러그인 설정의 `pushToStart`)은 `onPushToStartToken`으로, 푸시가 전달한
|
|
187
|
+
내용은 `onUpdated`로 옵니다. 페이지가 구독할 때 플러그인이 이미 갖고 있던 토큰은 바로 전달됩니다(`pushTokens`).
|
|
188
|
+
페이로드는 서버가 activitykit의 `push::Payload`로 만듭니다. [docs/usage.md](docs/usage.md#pushes)를 보세요.
|
|
189
|
+
|
|
178
190
|
### Dynamic Island
|
|
179
191
|
|
|
180
192
|
<table>
|
|
@@ -205,6 +217,50 @@ keyline: '#A3D9C2', // 아일랜드 테두리 색
|
|
|
205
217
|
|
|
206
218
|
Android는 레이아웃을 알림으로 보여 줍니다. 첫 두 텍스트가 제목과 본문이 되고, 첫 진행률 막대와 타이머, 버튼 최대 세 개가 들어갑니다. 빠진 것은 `degradedFeatures`에 이름이 남습니다.
|
|
207
219
|
|
|
220
|
+
## TSX와 React
|
|
221
|
+
|
|
222
|
+
같은 레이아웃을 TSX로 쓸 수 있습니다. `tauri-plugin-live-activity/jsx`는 자체 JSX 소스라 `.tsx` 파일에 React가
|
|
223
|
+
필요 없습니다. `tsconfig.json`에 `"jsx": "react-jsx"`와 `"jsxImportSource": "tauri-plugin-live-activity/jsx"`를
|
|
224
|
+
두거나, 파일 맨 위에 `/** @jsxImportSource tauri-plugin-live-activity/jsx */`를 적습니다.
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
import { Button, HStack, SFSymbol, Text, VStack } from 'tauri-plugin-live-activity/jsx';
|
|
228
|
+
|
|
229
|
+
const lockScreen = (
|
|
230
|
+
<VStack alignment="leading" spacing={8}>
|
|
231
|
+
<HStack spacing={8}><SFSymbol name="cat.fill" /><Text bind="title" /></HStack>
|
|
232
|
+
<Text bind="status" />
|
|
233
|
+
<Button actionId="pet"><Text>Pet Mochi</Text></Button>
|
|
234
|
+
</VStack>
|
|
235
|
+
);
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
노드마다 컴포넌트가 하나씩 있고(`VStack`, `HStack`, `ZStack`, `Spacer`, `Divider`, `Text`, `SFSymbol`, `Image`,
|
|
239
|
+
`Path`, `Progress`, `Gauge`, `TimerText`, `Button`, `Custom`), 노드의 멤버가 그대로 prop입니다. `Text`는 자식을
|
|
240
|
+
글자로 보여 주고 `bind`를 주면 값을 보여 줍니다. 스택은 노드, 배열, `{late && <Text>Late</Text>}`를 받고, `Button`은
|
|
241
|
+
노드 하나를 감쌉니다. HTML 요소와 프래그먼트는 없습니다. 이 엔트리는 Tauri도 React도 불러오지 않아서, 서버에서 푸시
|
|
242
|
+
페이로드용으로 같은 레이아웃을 만들 수 있습니다.
|
|
243
|
+
|
|
244
|
+
`tauri-plugin-live-activity/react`는 액티비티 하나를 컴포넌트와 함께 움직입니다(React 18 이상, 선택적 peer
|
|
245
|
+
의존성).
|
|
246
|
+
|
|
247
|
+
```tsx
|
|
248
|
+
import { useLiveActivity } from 'tauri-plugin-live-activity/react';
|
|
249
|
+
|
|
250
|
+
const { status, record, error } = useLiveActivity({
|
|
251
|
+
activityKey: `delivery:${order.id}`,
|
|
252
|
+
layout: { lockScreen },
|
|
253
|
+
values: { title: order.title, status: order.status },
|
|
254
|
+
enabled: order.open,
|
|
255
|
+
onAction: ({ actionId }) => { if (actionId === 'done') closeOrder(order.id); },
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
마운트되면 시작하고, `values`가 바뀐 렌더마다 다음 revision으로 갱신 하나를 보내며(한 번에 요청 하나, 겹치면 마지막
|
|
260
|
+
값만), 언마운트나 `enabled: false`에 끝납니다. `layout`과 `styles`는 시작할 때만 읽으니 바꾸려면 `activityKey`를
|
|
261
|
+
바꿔 새 액티비티를 시작합니다. 거부된 요청은 `error`에 남고, `onAction`은 이 액티비티의 탭만 받습니다.
|
|
262
|
+
[더 보기](https://github.com/1eedaegon/tauri-plugin-live-activity/blob/main/docs/usage.md#tsx-and-react)
|
|
263
|
+
|
|
208
264
|
## 기본 부품 너머
|
|
209
265
|
|
|
210
266
|
대부분은 Swift 없이 바꿀 수 있습니다. 글꼴과 그림을 위젯 확장에 넣고 이름으로 부르면 됩니다.
|
|
@@ -257,7 +313,9 @@ Group을 Apple 계정에 한 번 등록해야 하는데, 자동 서명이 대신
|
|
|
257
313
|
- 버튼을 누르면 Rust의 `on_action` 핸들러가 끝날 때까지 최대 4초 기다립니다. JavaScript 리스너는 기다리지 않습니다.
|
|
258
314
|
- `app.live_activity()`의 Rust 메서드는 응답이 올 때까지 스레드를 멈추므로 메인 스레드가 아닌 곳에서 호출하세요. iOS에서
|
|
259
315
|
메인 스레드에서 호출하면 바로 `NATIVE_ERROR`를 돌려줍니다. JavaScript의 명령은 멈추지 않습니다.
|
|
260
|
-
-
|
|
316
|
+
- 푸시는 iOS에서만 됩니다. 토큰, 채널, push-to-start는 플러그인이 다루고 페이로드는 서버가 activitykit의
|
|
317
|
+
`push::Payload`로 만듭니다. Android는 진행 중 알림으로 보여 주고, 시스템이 허용하면 Live Update로 올리며, 푸시는
|
|
318
|
+
받지 않습니다.
|
|
261
319
|
|
|
262
320
|
## Samsung Now Bar
|
|
263
321
|
|
package/README.md
CHANGED
|
@@ -103,7 +103,7 @@ Layouts are built from `text`/`bound`, `path` (SVG path data) and `vstack`, `hst
|
|
|
103
103
|
`spacer`, `divider`, plus the six below. Each picture is the iOS Lock Screen. The snippets take
|
|
104
104
|
the builders from `layout` (`const { vstack, hstack, text, bound, symbol, image, gauge, progress, timerText, button } = layout;`).
|
|
105
105
|
The same layouts can be written in TSX, and a React hook keeps an activity in step with a component
|
|
106
|
-
([TSX and React](
|
|
106
|
+
([TSX and React](#tsx-and-react)).
|
|
107
107
|
|
|
108
108
|
### Symbols
|
|
109
109
|
|
|
@@ -174,6 +174,19 @@ await onAction(async ({ actionId, activityId }) => {
|
|
|
174
174
|
|
|
175
175
|
A tap reaches `onAction`, or `on_action` in Rust, which also runs when the app was not open. iOS 17+; Android shows up to three as notification actions.
|
|
176
176
|
|
|
177
|
+
### Pushes
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
await onPushToken(({ activityId, token }) => sendToServer(activityId, token));
|
|
181
|
+
await onUpdated(({ record }) => show(record.snapshot.values));
|
|
182
|
+
await startLayout({ ...input, push: 'token' });
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
On iOS a server updates, ends or starts activities through APNs: the activity's token comes to `onPushToken`, the
|
|
186
|
+
app's push-to-start token (iOS 17.2, `pushToStart` in the plugin configuration) to `onPushToStartToken`, and content a
|
|
187
|
+
push delivered to `onUpdated`. Tokens the plugin already holds when a page subscribes come at once (`pushTokens`).
|
|
188
|
+
The server builds the payloads with activitykit's `push::Payload`. See [docs/usage.md](docs/usage.md#pushes).
|
|
189
|
+
|
|
177
190
|
### Dynamic Island
|
|
178
191
|
|
|
179
192
|
<table>
|
|
@@ -204,6 +217,50 @@ Put `island` next to `lockScreen` in the layout. A part you leave out shows the
|
|
|
204
217
|
|
|
205
218
|
Android shows a layout as a notification: the first two texts as title and body, the first progress bar and timer, and up to three buttons. `degradedFeatures` names what it left out.
|
|
206
219
|
|
|
220
|
+
## TSX and React
|
|
221
|
+
|
|
222
|
+
The same layouts can be written in TSX. `tauri-plugin-live-activity/jsx` is a JSX import source of its own, so a
|
|
223
|
+
`.tsx` file needs no React: set `"jsx": "react-jsx"` and `"jsxImportSource": "tauri-plugin-live-activity/jsx"` in
|
|
224
|
+
`tsconfig.json`, or `/** @jsxImportSource tauri-plugin-live-activity/jsx */` at the top of the file.
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
import { Button, HStack, SFSymbol, Text, VStack } from 'tauri-plugin-live-activity/jsx';
|
|
228
|
+
|
|
229
|
+
const lockScreen = (
|
|
230
|
+
<VStack alignment="leading" spacing={8}>
|
|
231
|
+
<HStack spacing={8}><SFSymbol name="cat.fill" /><Text bind="title" /></HStack>
|
|
232
|
+
<Text bind="status" />
|
|
233
|
+
<Button actionId="pet"><Text>Pet Mochi</Text></Button>
|
|
234
|
+
</VStack>
|
|
235
|
+
);
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
One component per node (`VStack`, `HStack`, `ZStack`, `Spacer`, `Divider`, `Text`, `SFSymbol`, `Image`, `Path`,
|
|
239
|
+
`Progress`, `Gauge`, `TimerText`, `Button`, `Custom`), with the node's members as props. `Text` shows its
|
|
240
|
+
children or, with `bind`, a value; a stack takes nodes, arrays and `{late && <Text>Late</Text>}`; a `Button`
|
|
241
|
+
holds one node. There are no HTML elements and no fragments. The entry imports neither Tauri nor React, so a
|
|
242
|
+
server can build the same layout for a push payload.
|
|
243
|
+
|
|
244
|
+
`tauri-plugin-live-activity/react` keeps one activity in step with a component (React 18+, an optional peer
|
|
245
|
+
dependency):
|
|
246
|
+
|
|
247
|
+
```tsx
|
|
248
|
+
import { useLiveActivity } from 'tauri-plugin-live-activity/react';
|
|
249
|
+
|
|
250
|
+
const { status, record, error } = useLiveActivity({
|
|
251
|
+
activityKey: `delivery:${order.id}`,
|
|
252
|
+
layout: { lockScreen },
|
|
253
|
+
values: { title: order.title, status: order.status },
|
|
254
|
+
enabled: order.open,
|
|
255
|
+
onAction: ({ actionId }) => { if (actionId === 'done') closeOrder(order.id); },
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
The activity starts on mount, each render with other `values` sends one update at the next revision (one request
|
|
260
|
+
in flight at a time, the newest values last), and it ends on unmount or `enabled: false`. `layout` and `styles` are
|
|
261
|
+
read at start; change the `activityKey` to start a new activity. A refused request lands in `error`, and `onAction`
|
|
262
|
+
gets this activity's taps. [More](https://github.com/1eedaegon/tauri-plugin-live-activity/blob/main/docs/usage.md#tsx-and-react)
|
|
263
|
+
|
|
207
264
|
## Beyond the built-in parts
|
|
208
265
|
|
|
209
266
|
Most customizing needs no Swift. Bundle your fonts and pictures in the widget extension, and name them:
|
|
@@ -256,7 +313,9 @@ notification's large icon. Layout, styles and values still share 3584.
|
|
|
256
313
|
- A button's tap waits for your Rust `on_action` handlers, at most 4 seconds; JavaScript listeners are not awaited.
|
|
257
314
|
- The Rust methods of `app.live_activity()` block until the answer: call them off the main thread. On iOS a call on
|
|
258
315
|
the main thread answers `NATIVE_ERROR` at once. The commands from JavaScript never block.
|
|
259
|
-
-
|
|
316
|
+
- Pushes on iOS only: tokens, channels and push-to-start through the plugin, the payloads from activitykit's
|
|
317
|
+
`push::Payload` on the server. Android shows an ongoing notification, promoted to a Live Update where the system
|
|
318
|
+
allows it, and takes no pushes.
|
|
260
319
|
|
|
261
320
|
## Samsung Now Bar
|
|
262
321
|
|
package/dist-js/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type UnlistenFn } from '@tauri-apps/api/event';
|
|
2
|
-
import type { ActionEvent, ActivityRecord, Capabilities, EndInput, LayoutActivityRecord, LayoutEndInput, LayoutStartInput, LayoutUpdateInput, PermissionKind, Permissions, StartInput, UpdateInput } from './types.js';
|
|
2
|
+
import type { ActionEvent, ActivityRecord, Capabilities, EndInput, LayoutActivityRecord, LayoutEndInput, LayoutStartInput, LayoutUpdateInput, PermissionKind, Permissions, PushToStartTokenEvent, PushTokenEvent, PushTokens, StartInput, UpdateInput, UpdatedEvent } from './types.js';
|
|
3
3
|
export type * from './types.js';
|
|
4
4
|
/** Builders for the nodes of a layout tree. */
|
|
5
5
|
export * as layout from './layout.js';
|
|
@@ -42,3 +42,30 @@ export declare function writeImage(name: string, png: Uint8Array | ArrayBuffer):
|
|
|
42
42
|
* must not be lost.
|
|
43
43
|
*/
|
|
44
44
|
export declare function onAction(handler: (event: ActionEvent) => void): Promise<UnlistenFn>;
|
|
45
|
+
/**
|
|
46
|
+
* The push tokens the plugin holds since launch (iOS): the newest token of each of the last 32 activities that got
|
|
47
|
+
* one, and the app's push-to-start token. The events report each token as it comes, but an event reaches only the
|
|
48
|
+
* listeners there are when it goes out, and a token may come before the page listens, at launch above all:
|
|
49
|
+
* `onPushToken` and `onPushToStartToken` read this right after subscribing. Empty where there are no pushes. The
|
|
50
|
+
* read is in the plugin's `default` permission.
|
|
51
|
+
*/
|
|
52
|
+
export declare function pushTokens(): Promise<PushTokens>;
|
|
53
|
+
/**
|
|
54
|
+
* Receive the push token of each activity that asked for one (`push: 'token'`), when ActivityKit gives it and each
|
|
55
|
+
* time it changes (iOS), and first those the plugin already holds, each token once. Send it to the server, which
|
|
56
|
+
* pushes to it with activitykit's `push::Payload`. Rejects, subscribed to nothing, when the plugin cannot be asked
|
|
57
|
+
* for the tokens it holds.
|
|
58
|
+
*/
|
|
59
|
+
export declare function onPushToken(handler: (event: PushTokenEvent) => void): Promise<UnlistenFn>;
|
|
60
|
+
/**
|
|
61
|
+
* Receive the app's push-to-start token, with `pushToStart: true` in the plugin configuration (iOS 17.2), when the
|
|
62
|
+
* system gives it and each time it changes, and first the one the plugin already holds, each token once. The server
|
|
63
|
+
* starts activities with it. Rejects, subscribed to nothing, when the plugin cannot be asked for the token it holds.
|
|
64
|
+
*/
|
|
65
|
+
export declare function onPushToStartToken(handler: (event: PushToStartTokenEvent) => void): Promise<UnlistenFn>;
|
|
66
|
+
/**
|
|
67
|
+
* Receive the record of an activity whose content a push changed (iOS): content above every revision this app sent
|
|
68
|
+
* or accepted, so the app's own updates do not come this way. Content pushed before the page listened is in the
|
|
69
|
+
* activity's record, as `listLayout` gives it.
|
|
70
|
+
*/
|
|
71
|
+
export declare function onUpdated(handler: (event: UpdatedEvent) => void): Promise<UnlistenFn>;
|
package/dist-js/index.js
CHANGED
|
@@ -68,3 +68,72 @@ export function writeImage(name, png) {
|
|
|
68
68
|
export function onAction(handler) {
|
|
69
69
|
return listen('live-activity://action', event => handler(event.payload));
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* The push tokens the plugin holds since launch (iOS): the newest token of each of the last 32 activities that got
|
|
73
|
+
* one, and the app's push-to-start token. The events report each token as it comes, but an event reaches only the
|
|
74
|
+
* listeners there are when it goes out, and a token may come before the page listens, at launch above all:
|
|
75
|
+
* `onPushToken` and `onPushToStartToken` read this right after subscribing. Empty where there are no pushes. The
|
|
76
|
+
* read is in the plugin's `default` permission.
|
|
77
|
+
*/
|
|
78
|
+
export function pushTokens() {
|
|
79
|
+
return invoke('plugin:live-activity|push_tokens');
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Receive the push token of each activity that asked for one (`push: 'token'`), when ActivityKit gives it and each
|
|
83
|
+
* time it changes (iOS), and first those the plugin already holds, each token once. Send it to the server, which
|
|
84
|
+
* pushes to it with activitykit's `push::Payload`. Rejects, subscribed to nothing, when the plugin cannot be asked
|
|
85
|
+
* for the tokens it holds.
|
|
86
|
+
*/
|
|
87
|
+
export async function onPushToken(handler) {
|
|
88
|
+
const delivered = new Map();
|
|
89
|
+
const deliver = (event) => {
|
|
90
|
+
if (delivered.get(event.activityId) === event.token)
|
|
91
|
+
return;
|
|
92
|
+
delivered.set(event.activityId, event.token);
|
|
93
|
+
handler(event);
|
|
94
|
+
};
|
|
95
|
+
const unlisten = await listen('live-activity://push-token', event => deliver(event.payload));
|
|
96
|
+
const held = await heldTokens(unlisten);
|
|
97
|
+
// An event that came while the plugin was asked is newer than what it held for that activity.
|
|
98
|
+
for (const event of held.activities)
|
|
99
|
+
if (!delivered.has(event.activityId))
|
|
100
|
+
deliver(event);
|
|
101
|
+
return unlisten;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Receive the app's push-to-start token, with `pushToStart: true` in the plugin configuration (iOS 17.2), when the
|
|
105
|
+
* system gives it and each time it changes, and first the one the plugin already holds, each token once. The server
|
|
106
|
+
* starts activities with it. Rejects, subscribed to nothing, when the plugin cannot be asked for the token it holds.
|
|
107
|
+
*/
|
|
108
|
+
export async function onPushToStartToken(handler) {
|
|
109
|
+
let last;
|
|
110
|
+
const deliver = (event) => {
|
|
111
|
+
if (event.token === last)
|
|
112
|
+
return;
|
|
113
|
+
last = event.token;
|
|
114
|
+
handler(event);
|
|
115
|
+
};
|
|
116
|
+
const unlisten = await listen('live-activity://push-to-start-token', event => deliver(event.payload));
|
|
117
|
+
const held = await heldTokens(unlisten);
|
|
118
|
+
if (last === undefined && held.pushToStart !== undefined)
|
|
119
|
+
deliver({ token: held.pushToStart });
|
|
120
|
+
return unlisten;
|
|
121
|
+
}
|
|
122
|
+
/** What the plugin holds, or the subscription given up and the failure passed on. */
|
|
123
|
+
async function heldTokens(unlisten) {
|
|
124
|
+
try {
|
|
125
|
+
return await pushTokens();
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
unlisten();
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Receive the record of an activity whose content a push changed (iOS): content above every revision this app sent
|
|
134
|
+
* or accepted, so the app's own updates do not come this way. Content pushed before the page listened is in the
|
|
135
|
+
* activity's record, as `listLayout` gives it.
|
|
136
|
+
*/
|
|
137
|
+
export function onUpdated(handler) {
|
|
138
|
+
return listen('live-activity://updated', event => handler(event.payload));
|
|
139
|
+
}
|
package/dist-js/react/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface UseLiveActivityOptions extends SessionOptions {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Starts a layout activity on mount, sends `values` as an update whenever they change, and ends the activity on
|
|
10
|
-
* unmount or when `enabled` turns false.
|
|
10
|
+
* unmount or when `enabled` turns false. The state carries the activity's push token and takes content a push
|
|
11
|
+
* delivered (iOS).
|
|
11
12
|
*/
|
|
12
13
|
export declare function useLiveActivity(options: UseLiveActivityOptions): LiveActivityState;
|
package/dist-js/react/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// `useLiveActivity`: one layout activity kept in step with a React component. Only this entry imports React.
|
|
2
2
|
import { useEffect, useRef, useState } from 'react';
|
|
3
|
-
import { endLayout, onAction, startLayout, updateLayout } from '../index.js';
|
|
3
|
+
import { endLayout, onAction, onPushToken, onUpdated, startLayout, updateLayout } from '../index.js';
|
|
4
4
|
import { IDLE, Session } from './session.js';
|
|
5
5
|
const commands = { startLayout, updateLayout, endLayout };
|
|
6
6
|
/**
|
|
7
7
|
* Starts a layout activity on mount, sends `values` as an update whenever they change, and ends the activity on
|
|
8
|
-
* unmount or when `enabled` turns false.
|
|
8
|
+
* unmount or when `enabled` turns false. The state carries the activity's push token and takes content a push
|
|
9
|
+
* delivered (iOS).
|
|
9
10
|
*/
|
|
10
11
|
export function useLiveActivity(options) {
|
|
11
12
|
const [state, setState] = useState(IDLE);
|
|
@@ -15,6 +16,8 @@ export function useLiveActivity(options) {
|
|
|
15
16
|
const handler = useRef(options.onAction);
|
|
16
17
|
handler.current = options.onAction;
|
|
17
18
|
const listening = options.onAction !== undefined;
|
|
19
|
+
// Read at start, as `push` is: an activity that asks for no push gets no token and no pushed content.
|
|
20
|
+
const pushing = options.push !== undefined;
|
|
18
21
|
useEffect(() => {
|
|
19
22
|
session.current?.sync(options);
|
|
20
23
|
});
|
|
@@ -24,6 +27,25 @@ export function useLiveActivity(options) {
|
|
|
24
27
|
current?.keep();
|
|
25
28
|
return () => current?.release();
|
|
26
29
|
}, []);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!pushing)
|
|
32
|
+
return undefined;
|
|
33
|
+
let open = true;
|
|
34
|
+
const stops = [
|
|
35
|
+
onPushToken((event) => { if (open)
|
|
36
|
+
session.current?.token(event); }),
|
|
37
|
+
onUpdated((event) => { if (open)
|
|
38
|
+
session.current?.pushed(event); }),
|
|
39
|
+
];
|
|
40
|
+
for (const stop of stops)
|
|
41
|
+
stop.catch((error) => { if (open)
|
|
42
|
+
session.current?.failed(error); });
|
|
43
|
+
return () => {
|
|
44
|
+
open = false;
|
|
45
|
+
for (const stop of stops)
|
|
46
|
+
stop.then((unlisten) => unlisten(), () => { });
|
|
47
|
+
};
|
|
48
|
+
}, [pushing]);
|
|
27
49
|
useEffect(() => {
|
|
28
50
|
if (!listening)
|
|
29
51
|
return undefined;
|
|
@@ -33,6 +55,8 @@ export function useLiveActivity(options) {
|
|
|
33
55
|
if (open && running !== undefined && event.activityId === running)
|
|
34
56
|
handler.current?.(event);
|
|
35
57
|
});
|
|
58
|
+
stop.catch((error) => { if (open)
|
|
59
|
+
session.current?.failed(error); });
|
|
36
60
|
return () => {
|
|
37
61
|
open = false;
|
|
38
62
|
stop.then((unlisten) => unlisten(), () => { });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EndPolicy, Fallback, Layout, LayoutActivityRecord, LayoutAndroidOptions, LayoutEndInput, LayoutStartInput, LayoutStyle, LayoutUpdateInput, LayoutValues, LiveActivityError } from '../types.js';
|
|
1
|
+
import type { EndPolicy, Fallback, Layout, LayoutActivityRecord, LayoutAndroidOptions, LayoutEndInput, LayoutPush, LayoutStartInput, LayoutStyle, LayoutUpdateInput, LayoutValues, LiveActivityError, PushTokenEvent, UpdatedEvent } from '../types.js';
|
|
2
2
|
export interface SessionOptions {
|
|
3
3
|
/** The app's key of the activity; another key ends the running activity and starts one for it. */
|
|
4
4
|
readonly activityKey: string;
|
|
@@ -14,6 +14,8 @@ export interface SessionOptions {
|
|
|
14
14
|
readonly fallback?: Fallback;
|
|
15
15
|
/** Read at start. */
|
|
16
16
|
readonly android?: LayoutAndroidOptions;
|
|
17
|
+
/** Read at start: what the activity asks ActivityKit for, a push token or a channel (iOS). */
|
|
18
|
+
readonly push?: LayoutPush;
|
|
17
19
|
/** False ends the activity, and true starts it again. Defaults to true. */
|
|
18
20
|
readonly enabled?: boolean;
|
|
19
21
|
/** How the activity ends on unmount or when `enabled` turns false. Defaults to `{ kind: 'immediate' }`. */
|
|
@@ -26,6 +28,8 @@ export interface LiveActivityState {
|
|
|
26
28
|
readonly record: LayoutActivityRecord | undefined;
|
|
27
29
|
/** The last refusal; an accepted request clears it. */
|
|
28
30
|
readonly error: LiveActivityError | undefined;
|
|
31
|
+
/** The running activity's latest push token (iOS, with `push: 'token'`), until the activity ends. */
|
|
32
|
+
readonly pushToken: string | undefined;
|
|
29
33
|
}
|
|
30
34
|
/** The commands the session sends. */
|
|
31
35
|
export interface SessionCommands {
|
|
@@ -46,11 +50,22 @@ export declare class Session {
|
|
|
46
50
|
private refused;
|
|
47
51
|
/** The key whose start was refused: not started again until `enabled` turns off or the key changes. */
|
|
48
52
|
private failedKey;
|
|
53
|
+
/** The latest push token of each activity seen, by id: one may come before the start it belongs to has answered. */
|
|
54
|
+
private readonly tokens;
|
|
49
55
|
private busy;
|
|
50
56
|
private mounts;
|
|
51
57
|
constructor(commands: SessionCommands, changed: (state: LiveActivityState) => void);
|
|
52
58
|
/** The id of the running activity. */
|
|
53
59
|
get activityId(): string | undefined;
|
|
60
|
+
/** A push token: the running activity's goes into the state; one that comes before its start answered waits. */
|
|
61
|
+
token(event: PushTokenEvent): void;
|
|
62
|
+
/**
|
|
63
|
+
* Content a push delivered: the running activity's record takes it. The component's values stand as last sent, so
|
|
64
|
+
* the pushed content stays until they change, and that change goes above the pushed revision.
|
|
65
|
+
*/
|
|
66
|
+
pushed(event: UpdatedEvent): void;
|
|
67
|
+
/** A failure outside a request, as a subscription the plugin refused: it lands in `error`. */
|
|
68
|
+
failed(error: unknown): void;
|
|
54
69
|
/** The options of a render. */
|
|
55
70
|
sync(options: SessionOptions): void;
|
|
56
71
|
/** The component is gone: the activity ends, now or once the request in flight has answered. */
|
package/dist-js/react/session.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const IDLE = Object.freeze({ status: 'idle', record: undefined, error: undefined });
|
|
1
|
+
export const IDLE = Object.freeze({ status: 'idle', record: undefined, error: undefined, pushToken: undefined });
|
|
2
2
|
const IMMEDIATE = Object.freeze({ kind: 'immediate' });
|
|
3
3
|
export class Session {
|
|
4
4
|
constructor(commands, changed) {
|
|
@@ -6,6 +6,8 @@ export class Session {
|
|
|
6
6
|
this.changed = changed;
|
|
7
7
|
this.state = IDLE;
|
|
8
8
|
this.endPolicy = IMMEDIATE;
|
|
9
|
+
/** The latest push token of each activity seen, by id: one may come before the start it belongs to has answered. */
|
|
10
|
+
this.tokens = new Map();
|
|
9
11
|
this.busy = false;
|
|
10
12
|
this.mounts = 0;
|
|
11
13
|
}
|
|
@@ -13,6 +15,28 @@ export class Session {
|
|
|
13
15
|
get activityId() {
|
|
14
16
|
return this.running?.record.id;
|
|
15
17
|
}
|
|
18
|
+
/** A push token: the running activity's goes into the state; one that comes before its start answered waits. */
|
|
19
|
+
token(event) {
|
|
20
|
+
this.tokens.set(event.activityId, event.token);
|
|
21
|
+
if (this.running?.record.id === event.activityId)
|
|
22
|
+
this.set({ ...this.state, pushToken: event.token });
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Content a push delivered: the running activity's record takes it. The component's values stand as last sent, so
|
|
26
|
+
* the pushed content stays until they change, and that change goes above the pushed revision.
|
|
27
|
+
*/
|
|
28
|
+
pushed(event) {
|
|
29
|
+
const { record } = event;
|
|
30
|
+
if (!this.running || record.id !== this.running.record.id)
|
|
31
|
+
return;
|
|
32
|
+
const revision = Math.max(this.running.revision, record.snapshot.revision);
|
|
33
|
+
this.running = { ...this.running, record, revision };
|
|
34
|
+
this.set({ ...this.state, record });
|
|
35
|
+
}
|
|
36
|
+
/** A failure outside a request, as a subscription the plugin refused: it lands in `error`. */
|
|
37
|
+
failed(error) {
|
|
38
|
+
this.set({ ...this.state, error: asError(error) });
|
|
39
|
+
}
|
|
16
40
|
/** The options of a render. */
|
|
17
41
|
sync(options) {
|
|
18
42
|
this.endPolicy = options.endPolicy ?? IMMEDIATE;
|
|
@@ -71,18 +95,20 @@ export class Session {
|
|
|
71
95
|
snapshot: { revision: 0, values },
|
|
72
96
|
...(options.styles === undefined ? {} : { styles: options.styles }),
|
|
73
97
|
...(options.android === undefined ? {} : { android: options.android }),
|
|
98
|
+
...(options.push === undefined ? {} : { push: options.push }),
|
|
74
99
|
};
|
|
75
100
|
this.failedKey = undefined;
|
|
76
101
|
this.refused = undefined;
|
|
77
|
-
this.set({ status: 'starting', record: undefined, error: undefined });
|
|
102
|
+
this.set({ status: 'starting', record: undefined, error: undefined, pushToken: undefined });
|
|
78
103
|
this.send(this.commands.startLayout(input), (record) => {
|
|
79
|
-
|
|
80
|
-
this.
|
|
104
|
+
// A repeated keyed start, after a relaunch, answers the running activity at its own revision.
|
|
105
|
+
this.running = { record, values, revision: record.snapshot.revision };
|
|
106
|
+
this.set({ status: 'active', record, error: undefined, pushToken: this.tokens.get(record.id) });
|
|
81
107
|
}, (error) => {
|
|
82
108
|
// Blocks only the key still wanted: `enabled` off meanwhile, or another key, starts again.
|
|
83
109
|
if (this.wanted?.activityKey === options.activityKey)
|
|
84
110
|
this.failedKey = options.activityKey;
|
|
85
|
-
this.set({ status: 'failed', record: undefined, error });
|
|
111
|
+
this.set({ status: 'failed', record: undefined, error, pushToken: undefined });
|
|
86
112
|
});
|
|
87
113
|
}
|
|
88
114
|
update(running, values) {
|
|
@@ -91,22 +117,25 @@ export class Session {
|
|
|
91
117
|
this.running = { ...running, revision };
|
|
92
118
|
this.send(this.commands.updateLayout({ id: running.record.id, snapshot: { revision, values } }), (record) => {
|
|
93
119
|
this.refused = undefined;
|
|
94
|
-
|
|
95
|
-
|
|
120
|
+
// A push may have moved the activity on meanwhile: the next change goes above it.
|
|
121
|
+
const taken = Math.max(revision, record.snapshot.revision, this.running?.revision ?? 0);
|
|
122
|
+
this.running = { record, values, revision: taken };
|
|
123
|
+
this.set({ status: 'active', record, error: undefined, pushToken: this.state.pushToken });
|
|
96
124
|
}, (error) => {
|
|
97
125
|
this.refused = values;
|
|
98
126
|
this.set({ ...this.state, error });
|
|
99
127
|
});
|
|
100
128
|
}
|
|
101
129
|
end(record) {
|
|
102
|
-
this.set({ status: 'ending', record, error: this.state.error });
|
|
130
|
+
this.set({ status: 'ending', record, error: this.state.error, pushToken: this.state.pushToken });
|
|
131
|
+
this.tokens.delete(record.id);
|
|
103
132
|
this.send(this.commands.endLayout({ id: record.id, policy: this.endPolicy }), (ended) => {
|
|
104
133
|
this.running = undefined;
|
|
105
|
-
this.set({ status: 'ended', record: ended, error: undefined });
|
|
134
|
+
this.set({ status: 'ended', record: ended, error: undefined, pushToken: undefined });
|
|
106
135
|
}, (error) => {
|
|
107
136
|
// Not tried again: the activity is left to the system.
|
|
108
137
|
this.running = undefined;
|
|
109
|
-
this.set({ status: 'failed', record, error });
|
|
138
|
+
this.set({ status: 'failed', record, error, pushToken: undefined });
|
|
110
139
|
});
|
|
111
140
|
}
|
|
112
141
|
send(request, accepted, refused) {
|
package/dist-js/types.d.ts
CHANGED
|
@@ -17,12 +17,16 @@ export interface Capabilities {
|
|
|
17
17
|
readonly nativeLiveActivity: FeatureSupport;
|
|
18
18
|
/** Layout activities of request contract v2. */
|
|
19
19
|
readonly layout: FeatureSupport;
|
|
20
|
+
/** Pushes: tokens, channels and push-to-start (iOS). */
|
|
21
|
+
readonly push: FeatureSupport;
|
|
20
22
|
readonly maxPayloadBytes: number;
|
|
21
23
|
}
|
|
22
24
|
export interface Permissions {
|
|
23
25
|
readonly notifications: PermissionState;
|
|
24
26
|
readonly liveActivities: PermissionState;
|
|
25
27
|
readonly promotion: PermissionState;
|
|
28
|
+
/** Frequent pushes, a switch in Settings on iOS (`granted` or `denied`); `notApplicable` where there are no pushes. */
|
|
29
|
+
readonly frequentPushes: PermissionState;
|
|
26
30
|
}
|
|
27
31
|
interface SnapshotFields {
|
|
28
32
|
/** Nonnegative JavaScript safe integer; strictly increases when content changes. */
|
|
@@ -368,6 +372,13 @@ export interface LayoutAndroidOptions {
|
|
|
368
372
|
/** Lock screen visibility of the notification. */
|
|
369
373
|
readonly visibility: 'public' | 'private' | 'secret';
|
|
370
374
|
}
|
|
375
|
+
/**
|
|
376
|
+
* What a layout activity asks ActivityKit for on iOS: a push token of its own, which `onPushToken` reports, or a
|
|
377
|
+
* broadcast channel the server made at APNs (iOS 18; `UNSUPPORTED_FEATURE` before). Android ignores it.
|
|
378
|
+
*/
|
|
379
|
+
export type LayoutPush = 'token' | {
|
|
380
|
+
readonly channel: string;
|
|
381
|
+
};
|
|
371
382
|
export interface LayoutStartInput {
|
|
372
383
|
/** Stable app-owned key; do not put credentials or private data in this key. */
|
|
373
384
|
readonly activityKey: string;
|
|
@@ -383,6 +394,8 @@ export interface LayoutStartInput {
|
|
|
383
394
|
readonly snapshot: LayoutSnapshot;
|
|
384
395
|
/** Ignored on iOS. */
|
|
385
396
|
readonly android?: LayoutAndroidOptions;
|
|
397
|
+
/** Ignored on Android. */
|
|
398
|
+
readonly push?: LayoutPush;
|
|
386
399
|
}
|
|
387
400
|
export interface LayoutUpdateInput {
|
|
388
401
|
readonly id: string;
|
|
@@ -412,6 +425,34 @@ export interface LayoutActivityRecord {
|
|
|
412
425
|
readonly promotionRequested: boolean;
|
|
413
426
|
readonly promotionState: PromotionState;
|
|
414
427
|
}
|
|
428
|
+
/** A push token of an activity, new or changed: what the server sends to. */
|
|
429
|
+
export interface PushTokenEvent {
|
|
430
|
+
readonly activityId: string;
|
|
431
|
+
/** The key of that activity, when the plugin knows it. */
|
|
432
|
+
readonly activityKey?: string;
|
|
433
|
+
/** Lowercase hex, as APNs takes it. */
|
|
434
|
+
readonly token: string;
|
|
435
|
+
}
|
|
436
|
+
/** The app's push-to-start token, new or changed (`pushToStart` in the plugin configuration, iOS 17.2). */
|
|
437
|
+
export interface PushToStartTokenEvent {
|
|
438
|
+
readonly token: string;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Content a push delivered: the activity's record with it, above every revision the app sent or accepted. The record
|
|
442
|
+
* is the activity's state when the plugin reads the event: two pushes close together may both report the later one.
|
|
443
|
+
*/
|
|
444
|
+
export interface UpdatedEvent {
|
|
445
|
+
readonly record: LayoutActivityRecord;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* The push tokens the plugin holds since launch (iOS): the newest token of each of the last 32 activities that got
|
|
449
|
+
* one, and the app's push-to-start token. Empty where there are no pushes.
|
|
450
|
+
*/
|
|
451
|
+
export interface PushTokens {
|
|
452
|
+
readonly pushToStart?: string;
|
|
453
|
+
/** In the order the activities' first tokens came. */
|
|
454
|
+
readonly activities: readonly PushTokenEvent[];
|
|
455
|
+
}
|
|
415
456
|
/** A tap on a button node. */
|
|
416
457
|
export interface ActionEvent {
|
|
417
458
|
readonly actionId: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tauri-plugin-live-activity",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Tauri 2 plugin for iOS Live Activities (ActivityKit and Dynamic Island) and Android Live Updates with ongoing notification fallback",
|
|
5
5
|
"author": "1eedaegon",
|
|
6
6
|
"type": "module",
|