# liveactivity-rs
Live Activities for Rust applications, behind one interface: iOS ActivityKit and
Android Live Updates by default, and any other backend in their place.
```rust
use liveactivity::{EndInput, LiveActivity, StartInput, UpdateInput};
let live = LiveActivity::builder().build()?;
let order = live
.start(StartInput::progress("order:42", "Order 42", "Preparing", 0.1))
.await?;
live.update(UpdateInput::progress(&order.id, 1, "Order 42", "On the way", 0.6))
.await?;
live.end(EndInput::new(&order.id)).await?;
```
## Install
```toml
[dependencies]
liveactivity = "0.2"
```
The crate uses [activitykit](https://github.com/1eedaegon/activitykit-rs) for
iOS and [liveupdate](https://github.com/1eedaegon/liveupdate-rs) for Android as
they are. It compiles on every target; where there is no platform, the default
is `UnsupportedBackend`: the queries answer that nothing is supported and every
other call answers `UNSUPPORTED_PLATFORM`.
| [docs/contract.md](docs/contract.md) | every command, its JSON, the limits, the errors, the events and the layout tree |
| [docs/ios-setup.md](docs/ios-setup.md) | the widget extension, the App Group and pushes of an iOS app |
| [docs/android-setup.md](docs/android-setup.md) | the AAR, the permissions and the files of an Android app |
| [docs/framework-integration.md](docs/framework-integration.md) | writing a framework plugin: commands, events, lifecycle, threads |
| [docs/design/engine-expressiveness](docs/design/engine-expressiveness/PRD.md) | what the engines are about to open, and what this crate does with each |
## The two sides
Everything goes through one builder. An application says where events go; a
framework plugin or a platform says which backend runs the commands. Whatever
is not said is the default.
```rust
let live = LiveActivity::builder()
.backend(backend) // the supporting side; without it, the platform's default
.event_sink(sink) // the using side; without it, events are dropped
.build()?;
```
| an application | `.event_sink(..)`, then the calls of [Features](#features) |
| a framework plugin | an `EventSink` that forwards events to the framework's own, commands that call `LiveActivity`, and a backend only when the framework has a native side of its own |
| a platform | a `LiveActivityBackend` |
## Features
Every snippet below is a function of
[`examples/features.rs`](crates/liveactivity/examples/features.rs), which
compiles and runs:
```sh
cargo run -p liveactivity --example features --features testkit
```
| [Connect](#connect) | `LiveActivity::builder()` | ✓ | ✓ |
| [Progress and timer](#progress-and-timer-activities) | `start`, `update`, `end`, `list` | ✓ | ✓ |
| [Layouts](#layout-activities) | `start_layout`, `update_layout`, `end_layout`, `list_layout` | ✓ | ✓ projected onto a notification |
| [Buttons and taps](#buttons-and-taps) | a `button` node, `Event::Action` | ✓ | ✓ up to three |
| [Alerts](#alerts-and-relevance) | `LayoutUpdateInput.alert` | ✓ | silent, reported as `alert` |
| [Relevance](#alerts-and-relevance) | `LayoutSnapshot.relevance` | ✓ | ignored |
| [Android content](#android-content) | `LayoutSnapshot.android` | ignored | ✓ |
| [Images](#images) | `write_image`, an `image` node | ✓ needs an App Group | ✓ as the large icon |
| [Pushes](#pushes) | `LayoutStartInput.push`, `push_tokens` | ✓ | none |
| [Events](#events) | the event sink, `sync_events` | ✓ as they come | ✓ on `sync_events` |
| [Capabilities and permissions](#capabilities-and-permissions) | `capabilities`, `permissions`, `request_permissions` | ✓ | ✓ the dialog is the host's |
| [Blocking calls](#blocking-calls) | `wait` | ✓ off the main thread | ✓ |
### Connect
```rust
let live = LiveActivity::builder()
.event_sink(|event: Event| match event {
Event::Action(tap) => println!("tapped {} on {}", tap.action_id, tap.activity_id),
Event::State(change) => println!("{} is {:?}", change.activity_id, change.state),
Event::PushToken(token) => println!("send {} to the server", token.token),
_ => {}
})
.build()?;
```
Without `.backend(..)` the backend is the platform's own. Keep the
`LiveActivity` in application state: it is cheap to clone and safe to use from
any thread. `liveactivity::connect_native(sink)` is the same in one call.
### Progress and timer activities
A title, a status, and a progress from 0 to 1 or a countdown. They need no
layout and look native on both platforms.
```rust
let order = live
.start(StartInput::progress("order:42", "Order 42", "Preparing", 0.1))
.await?;
// An update sends the whole content again, under a higher revision.
live.update(UpdateInput::progress(&order.id, 1, "Order 42", "On the way", 0.6))
.await?;
live.end(EndInput::new(&order.id)).await?;
// A countdown between two times, in epoch milliseconds.
let tea = live
.start(StartInput::timer("tea:1", "Tea", "Steeping", started_at, ends_at))
.await?;
live.end(EndInput::immediate(&tea.id)).await?;
```
`live.list()` answers the activities the backend knows.
### Layout activities
A tree of nodes, fixed at start, and values that updates replace. iOS draws
the tree on the Lock Screen and in the Dynamic Island; Android projects it
onto a notification and names what it left out in the record's
`degraded_features`. The nodes and styles are in
[docs/contract.md](docs/contract.md#the-layout-dsl).
```rust
use liveactivity::activitykit::{layout::{button, progress, text, vstack}, values};
let layout = Layout {
lock_screen: vstack([
text(Bound::key("title")),
progress("done"),
button("track", text("Track")),
]),
island: None,
background: None,
keyline: None,
};
let values = values! { "title" => "Delivery 7", "done" => 0.4 }.into_inner();
let start = LayoutStartInput::new("delivery:7", layout, LayoutSnapshot::new(0, values));
let record = live.start_layout(start).await?;
// Every value again, under a higher revision.
let values = values! { "title" => "Delivery 7", "done" => 0.9 }.into_inner();
live.update_layout(LayoutUpdateInput::new(&record.id, LayoutSnapshot::new(1, values)))
.await?;
live.end_layout(LayoutEndInput::new(&record.id)).await?;
```
A request that comes as JSON is read with
`parse_request("start_layout", json)?`, which checks it against the contract.
### Buttons and taps
A `button(action_id, label)` node is a button on iOS and a notification action
on Android. Its tap reaches the sink as `Event::Action`, with the `action_id`,
the activity's `id` and its key. The tap does not change the activity: send an
update if the button should change something.
### Alerts and relevance
```rust
let mut snapshot = LayoutSnapshot::new(2, values);
// Which of the application's activities the Dynamic Island shows first.
snapshot.relevance = Some(90.0);
let mut update = LayoutUpdateInput::new(id, snapshot);
update.alert = Some(Alert::new("Delivery arrived", "Your parcel is at the door"));
let record = live.update_layout(update).await?;
```
On iOS the device lights up, the Dynamic Island expands and an Apple Watch
shows the title and the body. Android updates silently and names `alert` in
`record.degraded_features`. An update without an alert is silent everywhere.
### Android content
What the notification shows, in Android's own terms, instead of the projection
of the layout: a progress with segments and points, metrics, the text of the
status chip, icons of actions. The types are liveupdate's, re-exported.
```rust
// Which notification styles the device draws.
let styles = live.capabilities().await?.android_presentation;
let segmented = styles.is_some_and(|styles| styles.progress_style);
let mut snapshot = LayoutSnapshot::new(3, values);
snapshot.android = Some(AndroidContent {
title: "Delivery 7".into(),
status: "2 stops away".into(),
presentation: AndroidPresentation {
style: PresentationStyle::Progress(ProgressStyle {
progress: 60,
indeterminate: false,
styled_by_progress: true,
segments: if segmented {
vec![segment(60, Some(0xFF00_7F00)), segment(40, None)]
} else {
vec![segment(100, None)]
},
points: Vec::new(),
start_icon: None,
end_icon: None,
tracker_icon: None,
}),
// The text of the status chip, at most 7 characters.
short_critical_text: Some("8 min".into()),
large_icon: None,
actions: Vec::new(),
samsung_now_bar: None,
},
});
live.update_layout(LayoutUpdateInput::new(id, snapshot)).await?;
```
Android keeps the style of an activity's first snapshot, and answers
`UNSUPPORTED_FEATURE` to a content of another style: give a content with every
snapshot of an activity or with none. The backend needs the app's
files for it: see [Platform setup](#platform-setup).
### Images
```rust
live.write_image("badge.png", &png).await?;
```
Write the PNG first, then show it with an `image` node whose source is
`{ "file": "badge.png" }`. On iOS it goes to the App Group that the widget
extension reads. On Android the first such image of a layout becomes the
notification's large icon.
### Pushes
iOS only. A start asks for a token, the sink gets it as `Event::PushToken`,
and a server updates the activity through APNs.
```rust
start.push = Some(LayoutPush::Token);
live.start_layout(start).await?;
// What came before the sink listened.
for held in live.push_tokens().await?.activities {
println!("{} has the token {}", held.activity_id, held.token);
}
```
The server builds its payloads with `activitykit::push::Payload`. Content that
a push delivered comes back as `Event::Updated`, and an activity that a push
started as `Event::Started`. The steps are in
[docs/ios-setup.md](docs/ios-setup.md#7-pushes).
### Events
| `Event::Action` | a button or an action was tapped | ✓ | ✓ |
| `Event::State` | an activity has a new lifecycle state | every change while the app runs | the person dismissed it |
| `Event::Started` | a layout activity began, also one a push started | ✓ | |
| `Event::Updated` | a push changed the content of a layout activity | ✓ | |
| `Event::PushToken` | an activity got a push token, or a new one | ✓ | |
| `Event::PushToStartToken` | the app got its push-to-start token | ✓ | |
| `Event::Permissions` | the person changed what the app may do | ✓ | |
The enum may grow: match with a `_` arm. iOS delivers events as they come, and
a tap keeps the app awake until the sink returns, for at most 4 seconds.
Android stores taps and dismissals, also while the application does not run:
```rust
// When the application starts and resumes. On iOS it does nothing.
live.sync_events().await?;
```
### Capabilities and permissions
```rust
let capabilities = live.capabilities().await?;
if capabilities.layout != FeatureSupport::Supported {
println!("{}: progress and timer activities only", capabilities.platform);
}
let permissions = live.permissions().await?;
if permissions.notifications == PermissionState::Prompt {
live.request_permissions(PermissionKind::Notifications).await?;
}
```
A feature is supported, unsupported or unknown, and unknown never counts as
supported. On iOS Live Activities are a switch in Settings and there is no
dialog. On Android the system's dialog needs an Activity, which the host
shows: see [Supporting it](#supporting-it).
### Blocking calls
Calls are futures for any executor. Where there is none:
```rust
let record = liveactivity::wait(live.start(input))?;
```
On iOS the answer comes from the main thread, so a blocking call there would
never end: `wait` answers `NATIVE_ERROR` at once on the main thread instead of
hanging. On Android a call runs on the thread that polls it.
### Rules of every backend
- **Key.** `activity_key` is the application's own name of the activity. A
repeated start with the same key and request answers the running activity; a
different request under that key is `CONFLICT`.
- **Handle.** Updates and ends take the `id` of the record, new for every start.
- **Revision.** A lower revision is `STALE_REVISION`. The same revision with
other content is `CONFLICT`; with the same content it changes nothing.
- **End.** Ending twice is fine. An ended activity takes no update (`NOT_FOUND`).
- **Errors.** Every failure is an `Error` with a `code` out of `ERROR_CODES`
and a message.
- **Validation.** Requests are checked against the contract before a backend
sees them, so an invalid one is `INVALID_INPUT` on every platform.
- **What a platform leaves out** is named in the record's `degraded_features`.
## Supporting it
### A framework plugin
A plugin carries the contract over its framework and adds nothing to it.
[`examples/adapter.rs`](crates/liveactivity/examples/adapter.rs) is a whole one
that compiles and runs, and
[docs/framework-integration.md](docs/framework-integration.md) walks through
it.
```sh
cargo run -p liveactivity --example adapter --features testkit
```
1. **Types.** Re-export `liveactivity::contract::*`: the requests and records
are serde types with camelCase JSON, ready for the framework's IPC.
2. **Commands.** Each command parses its input and awaits the call of the same
name:
```rust
"start" => answer(live.start(parse_request("start", input)?).await?),
```
3. **Events.** Give the builder a sink that forwards each event to the
framework's own event API:
```rust
LiveActivity::builder()
.event_sink(move |event: Event| match event {
Event::Action(tap) => framework.emit("live-activity://action", &tap),
_ => {}
})
.build()?;
```
4. **Lifecycle.** Build once at setup and keep the `LiveActivity` in the
framework's state. Call `sync_events()` at startup and on every resume.
5. **Backend.** Name one only when the framework has a native side of its own.
Wrap a default backend, answer what the framework does itself, and hand the
rest on:
```rust
let engine = LiveUpdateBackend::over_bridge(my_bridge) .with_files(AndroidFiles::new(app_name, files_dir));
impl LiveActivityBackend for MyBackend {
fn request_permissions(&self, kind: PermissionKind) -> BackendFuture<'_, Permissions> {
Box::pin(async move {
self.dialog.ask(kind)?;
self.engine.permissions().await
})
}
fn start(&self, input: StartInput) -> BackendFuture<'_, ActivityRecord> {
self.engine.start(input)
}
}
```
`liveupdate::Bridge` is one call that carries a command name and a JSON payload
to the Android side.
### A platform
Implement `LiveActivityBackend`, one method per call of the contract. Only
the template calls and the two queries are required; layouts, permission
requests and images default to `UNSUPPORTED_FEATURE`, a backend without pushes
holds no token, and `capabilities` says what the backend has. `attach` hands
the backend the sink once, before any command; the backend sends each event
with `events.emit(..)`.
The `testkit` feature checks that a backend keeps the rules above, which is what
makes it exchangeable with the default ones:
```rust
let live = LiveActivity::builder().backend(MyBackend::new()).build()?;
liveactivity::testkit::conformance(&live).await.assert_passed();
```
`testkit::MemoryBackend` is a backend without a platform, for an application's
own tests and as the smallest example of an implementation.
## Platform setup
**iOS.** activitykit builds its Swift bridge into the Rust library, which needs
Xcode. The app needs `NSSupportsLiveActivities`, a widget extension that links
activitykit's `ActivityKitWidget` product, and an App Group for images. Live
Activities need iOS 16.2, or iPadOS 17 on an iPad. The steps are in
[docs/ios-setup.md](docs/ios-setup.md).
**Android.** The app packages liveupdate's AAR and the Kotlin standard library,
and the host initializes `ndk-context`, which common Rust Android runtimes do.
The default backend draws progress and timer activities as it is. Given the
folder of the app's files, it also draws layouts, stores images and reports
taps on buttons:
```rust
let backend = LiveUpdateBackend::android()?
.with_files(AndroidFiles::new("My App", files_dir)); // Context.getFilesDir()
let live = LiveActivity::builder().backend(backend).build()?;
```
Only the system's permission dialog needs code of the host, because it needs an
Activity. The steps, and the 16 KB page-size note, are in
[docs/android-setup.md](docs/android-setup.md).
## Develop
```sh
cargo test --workspace --all-features
cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo llvm-cov --workspace --all-features --fail-under-lines 80
bash tools/check-requirements.sh
bash tests/release/run.sh
```
Requirements and their test evidence are in
[docs/requirements.md](docs/requirements.md), the structure in
[docs/design.md](docs/design.md).
## Releases
Releases follow the subjects of the commits on `main`, which are
[conventional commits](https://www.conventionalcommits.org/). After CI passes
on `main`, the release workflow bumps the version, writes the changelog, tags
the commit and publishes the crate:
| `feat:` | minor |
| `fix:`, `perf:`, `refactor:`, `docs:`, `revert:` | patch |
| `test:`, `ci:`, `chore:`, `build:`, `style:` | none |
| `!` or a `BREAKING CHANGE:` footer | major, only by running the "Auto release" workflow by hand with `major` checked |
## License
MIT or Apache-2.0, at your option.