liveactivity 0.1.0

Framework-neutral Live Activities: one interface over ActivityKit, Android Live Updates and other backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Request contract v2: a layout tree, a style table and a map of values.
//!
//! The node and style types and the validator are the `activitykit` crate's, re-exported here under the names of 0.3.
//! The requests and records are this crate's own, so that their values stay a `BTreeMap` and Android reads the same
//! JSON; each converts into activitykit's request to be checked.

use crate::{
    Error, Result,
    models::{ActivityState, Backend, EndPolicy, Fallback, PromotionState, Visibility},
    validation::Validate,
};
use activitykit::contract as wire;
use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned};
use std::collections::BTreeMap;

pub use activitykit::Value;
/// What a layout activity asks ActivityKit for on iOS: a push token of its own (`"token"`), reported as a
/// `live-activity://push-token` event once ActivityKit has it, or a broadcast channel the server made at APNs
/// (`{"channel": "<id>"}`, iOS 18). Android ignores it.
pub use activitykit::contract::PushWire as LayoutPush;
pub use activitykit::contract::{
    AndroidOptions, AndroidVisibility, Island, IslandExpanded, weight,
};
pub use activitykit::layout::{
    Alignment, Bindable, Bound, ButtonStyle, Color, ColorName, ContentMode, Dimension, Element,
    Font, FontDesign, FontWeight, Frame, HorizontalAlignment, ImageSource, IndicatorKind, Node,
    Padding, Style, SymbolRendering, VerticalAlignment,
};

/// The member `layout` of a start: one tree per region and the two tints. The layout is fixed when the activity
/// starts; only a bound `background` or `keyline` follows the values.
pub type Layout = wire::LayoutRegions;

/// Reads a member that may be absent but never `null`.
fn non_null<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
    deserializer: D,
) -> std::result::Result<Option<T>, D::Error> {
    T::deserialize(deserializer).map(Some)
}

fn none<T>(member: &Option<T>) -> bool {
    member.is_none()
}

/// The values a layout shows. An update replaces the whole map.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct LayoutSnapshot {
    /// The revision of this content: higher than the last accepted one.
    pub revision: u64,
    /// Every value the layout binds, by key. An update sends them all again.
    pub values: BTreeMap<String, Value>,
    /// When the system may show the activity as out of date, in epoch milliseconds.
    #[serde(default, deserialize_with = "non_null", skip_serializing_if = "none")]
    pub stale_at: Option<u64>,
    /// When the content was made, in epoch milliseconds. ActivityKit (iOS 17.2) drops content older than what it
    /// shows. Without it, iOS takes the time the content is sent.
    #[serde(default, deserialize_with = "non_null", skip_serializing_if = "none")]
    pub timestamp: Option<u64>,
}

/// The input of `start_layout`: a layout tree, its styles and its first values.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct LayoutStartInput {
    /// The application's own name of the activity: a running activity with the same key and request is the same activity.
    pub activity_key: String,
    /// The version of the request contract: 1 for a template, 2 for a layout.
    pub schema_version: u8,
    /// What to do when the platform cannot give the activity its native presentation.
    pub fallback: Fallback,
    /// One tree per region and the two tints, fixed at start.
    pub layout: Layout,
    /// Styles by token; nodes name them with `style`.
    #[serde(default, deserialize_with = "non_null", skip_serializing_if = "none")]
    pub styles: Option<BTreeMap<String, Style>>,
    /// The values, with their revision.
    pub snapshot: LayoutSnapshot,
    /// Options that only Android reads.
    #[serde(default, deserialize_with = "non_null", skip_serializing_if = "none")]
    pub android: Option<AndroidOptions>,
    /// Ignored on Android.
    #[serde(default, deserialize_with = "non_null", skip_serializing_if = "none")]
    pub push: Option<LayoutPush>,
}

/// The input of `update_layout`: every value again, under a higher revision.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct LayoutUpdateInput {
    /// The handle of the activity, new for every start.
    pub id: String,
    /// The values, with their revision.
    pub snapshot: LayoutSnapshot,
}

/// The input of `end_layout`: the policy, and optionally the final content, which is checked as an update. Without
/// it the activity ends with its last accepted values.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct LayoutEndInput {
    /// The handle of the activity, new for every start.
    pub id: String,
    /// When the ended activity leaves the screen.
    pub policy: EndPolicy,
    /// The final content, checked as an update. Without it the activity ends with its last accepted values.
    #[serde(default, deserialize_with = "non_null", skip_serializing_if = "none")]
    pub snapshot: Option<LayoutSnapshot>,
}

/// A layout activity as the native side reports it. It does not repeat the layout.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LayoutActivityRecord {
    /// The handle of the activity, new for every start.
    pub id: String,
    /// The application's own name of the activity: a running activity with the same key and request is the same activity.
    pub activity_key: String,
    /// The version of the request contract: 1 for a template, 2 for a layout.
    pub schema_version: u8,
    /// The values, with their revision.
    pub snapshot: LayoutSnapshot,
    /// Which backend shows the activity.
    pub backend: Backend,
    /// The lifecycle state the platform reports.
    pub state: ActivityState,
    /// What is known of the activity being on screen.
    pub visibility: Visibility,
    /// What the platform left out of the request, such as `nativeLiveActivity` or `layout.<node>`.
    pub degraded_features: Vec<String>,
    /// Whether promotion to a Live Update was requested (Android).
    pub promotion_requested: bool,
    /// What Android did with a request to promote the notification.
    pub promotion_state: PromotionState,
}

/// A tap on a button node.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActionEvent {
    /// The id of the tapped button, as its node declares it.
    pub action_id: String,
    /// Handle of the activity that showed the button.
    pub activity_id: String,
    /// Absent when the backend no longer knows that activity.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub activity_key: Option<String>,
}

impl LayoutSnapshot {
    fn wire(&self) -> wire::LayoutSnapshot {
        wire::LayoutSnapshot {
            revision: self.revision,
            values: self.values.clone().into(),
            stale_at: self.stale_at,
            timestamp: self.timestamp,
        }
    }
}

impl LayoutStartInput {
    /// The same request in activitykit's types.
    pub(crate) fn wire(&self) -> wire::LayoutStartInput {
        wire::LayoutStartInput {
            activity_key: self.activity_key.clone(),
            schema_version: self.schema_version,
            fallback: match self.fallback {
                Fallback::None => wire::Fallback::None,
                Fallback::Notification => wire::Fallback::Notification,
            },
            layout: self.layout.clone(),
            styles: self.styles.clone(),
            snapshot: self.snapshot.wire(),
            android: self.android.clone(),
            push: self.push.clone(),
        }
    }

    /// Checks `update` against this start as the native layer does: the same keys and value types, the layout's
    /// references (bound style members included) and the weight of the layout, the styles and the new values.
    pub fn validate_update(&self, update: &LayoutUpdateInput) -> Result<()> {
        Ok(self.wire().validate_update(&update.wire())?)
    }

    /// Checks `end` against this start: its final content, when it has one, as an update.
    pub fn validate_end(&self, end: &LayoutEndInput) -> Result<()> {
        Ok(self.wire().validate_end(&end.wire())?)
    }
}

impl LayoutUpdateInput {
    fn wire(&self) -> wire::LayoutUpdateInput {
        wire::LayoutUpdateInput {
            id: self.id.clone(),
            snapshot: self.snapshot.wire(),
        }
    }
}

impl LayoutEndInput {
    fn wire(&self) -> wire::EndInput {
        wire::EndInput {
            id: self.id.clone(),
            policy: match self.policy {
                EndPolicy::Immediate {} => wire::EndPolicy::Immediate {},
                EndPolicy::Default {} => wire::EndPolicy::Default {},
                EndPolicy::After { at } => wire::EndPolicy::After { at },
            },
            snapshot: self.snapshot.as_ref().map(LayoutSnapshot::wire),
        }
    }
}

impl Validate for LayoutSnapshot {
    fn validate(&self) -> Result<()> {
        Ok(wire::Validate::validate(&self.wire())?)
    }
}

/// The value key that marks contract v1's template activities on iOS: a layout request may not use it, or its activity
/// would pass for a template and leave the layout commands.
pub(crate) const TEMPLATE_MARKER: &str = "liveActivityTemplate";

/// This crate's rule beyond activitykit's contract: no value under [`TEMPLATE_MARKER`].
fn unmarked(snapshot: &LayoutSnapshot) -> Result<()> {
    if snapshot.values.contains_key(TEMPLATE_MARKER) {
        Err(Error::invalid())
    } else {
        Ok(())
    }
}

impl LayoutStartInput {
    /// activitykit's rules alone, for the starts that the backend makes of contract v1's templates.
    pub(crate) fn validate_contract(&self) -> Result<()> {
        Ok(wire::Validate::validate(&self.wire())?)
    }
}

impl Validate for LayoutStartInput {
    fn validate(&self) -> Result<()> {
        self.validate_contract()?;
        unmarked(&self.snapshot)
    }
}

impl Validate for LayoutUpdateInput {
    fn validate(&self) -> Result<()> {
        wire::Validate::validate(&self.wire())?;
        unmarked(&self.snapshot)
    }
}

impl Validate for LayoutEndInput {
    fn validate(&self) -> Result<()> {
        wire::Validate::validate(&self.wire())?;
        self.snapshot.as_ref().map_or(Ok(()), unmarked)
    }
}

/// A JSON request of `command` (`start_layout`, `update_layout` or `end_layout`) as activitykit checks it, strictly
/// (unknown members, `null`, a request that does not read back as itself), and then in this crate's type, with the
/// crate's rules.
pub(crate) fn parse<T: DeserializeOwned + Validate>(
    command: &str,
    input: serde_json::Value,
) -> Result<T> {
    wire::validate_request(command, input.clone())?;
    let typed: T = serde_json::from_value(input).map_err(|_| Error::invalid())?;
    typed.validate()?;
    Ok(typed)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn start() -> serde_json::Value {
        json!({"activityKey":"demo:1","schemaVersion":2,"fallback":"notification",
            "layout":{"lockScreen":{"type":"text","bind":"status","style":"s"},"keyline":{"bind":"tone"}},
            "styles":{"s":{"color":{"bind":"tone"},"font":"headline"}},
            "snapshot":{"revision":0,"values":{"status":"On time","tone":"green"},"staleAt":5,"timestamp":7},
            "android":{"visibility":"private"}})
    }
    #[test]
    fn a_request_reads_into_the_contracts_types_and_writes_back_the_same_json() {
        let input: LayoutStartInput = parse("start_layout", start()).unwrap();
        assert_eq!(serde_json::to_value(&input).unwrap(), start());
        assert_eq!(input.snapshot.values["tone"], Value::Text("green".into()));
        assert_eq!(input.snapshot.timestamp, Some(7));
        assert_eq!(serde_json::to_value(input.wire()).unwrap(), start());
        let end = json!({"id":"a","policy":{"kind":"after","at":3},"snapshot":{"revision":1,"values":{}}});
        let typed: LayoutEndInput = parse("end_layout", end.clone()).unwrap();
        assert_eq!(serde_json::to_value(&typed).unwrap(), end);
        assert_eq!(serde_json::to_value(typed.wire()).unwrap(), end);
        let plain: LayoutEndInput =
            parse("end_layout", json!({"id":"a","policy":{"kind":"default"}})).unwrap();
        assert_eq!(plain.snapshot, None);
        assert_eq!(
            serde_json::to_value(plain.wire()).unwrap(),
            json!({"id":"a","policy":{"kind":"default"}})
        );
        let immediate = LayoutEndInput {
            id: "a".into(),
            policy: EndPolicy::Immediate {},
            snapshot: None,
        };
        assert_eq!(
            serde_json::to_value(immediate.wire()).unwrap()["policy"]["kind"],
            "immediate"
        );
    }
    #[test]
    fn parse_is_activitykits_strict_check_and_the_types_validate_the_same() {
        let mut nulled = start();
        nulled["snapshot"]["timestamp"] = json!(null);
        assert_eq!(
            parse::<LayoutStartInput>("start_layout", nulled),
            Err(Error::invalid())
        );
        let mut unknown = start();
        unknown["extra"] = json!(1);
        assert_eq!(
            parse::<LayoutStartInput>("start_layout", unknown),
            Err(Error::invalid())
        );
        // Valid for activitykit, but not this type.
        assert_eq!(
            parse::<LayoutStartInput>("end_layout", json!({"id":"a","policy":{"kind":"default"}})),
            Err(Error::invalid())
        );
        let input: LayoutStartInput = serde_json::from_value(start()).unwrap();
        assert_eq!(input.validate(), Ok(()));
        assert_eq!(input.snapshot.validate(), Ok(()));
        let mut late = input.snapshot.clone();
        late.timestamp = Some(8_640_000_000_000_001);
        assert_eq!(late.validate(), Err(Error::invalid()));
        let update = LayoutUpdateInput {
            id: "a".into(),
            snapshot: input.snapshot.clone(),
        };
        assert_eq!(
            (update.validate(), input.validate_update(&update)),
            (Ok(()), Ok(()))
        );
        let mut crimson = update.clone();
        crimson
            .snapshot
            .values
            .insert("tone".into(), Value::Text("crimson".into()));
        assert_eq!(input.validate_update(&crimson), Err(Error::invalid()));
        let end = LayoutEndInput {
            id: "a".into(),
            policy: EndPolicy::Default {},
            snapshot: Some(crimson.snapshot),
        };
        assert_eq!(
            (end.validate(), input.validate_end(&end)),
            (Ok(()), Err(Error::invalid()))
        );
        let bare = LayoutEndInput {
            snapshot: None,
            ..end
        };
        assert_eq!(input.validate_end(&bare), Ok(()));
    }
    #[test]
    fn the_template_marker_is_no_value_key_of_a_layout_request() {
        let code = |result: Result<()>| result.map_err(|error| error.code);
        let invalid = Err("INVALID_INPUT".to_owned());
        let mut marked = start();
        marked["snapshot"]["values"]["liveActivityTemplate"] = json!("progress");
        // The commands' check, and the Rust API's.
        assert_eq!(
            code(crate::validate_request("start_layout", marked.clone())),
            invalid
        );
        let typed: LayoutStartInput = serde_json::from_value(marked).unwrap();
        assert_eq!(code(typed.validate()), invalid);
        let update =
            json!({"id":"a","snapshot":{"revision":1,"values":{"liveActivityTemplate":"timer"}}});
        assert_eq!(
            code(crate::validate_request("update_layout", update.clone())),
            invalid
        );
        let typed: LayoutUpdateInput = serde_json::from_value(update).unwrap();
        assert_eq!(code(typed.validate()), invalid);
        let end = json!({"id":"a","policy":{"kind":"immediate"},
            "snapshot":{"revision":1,"values":{"liveActivityTemplate":"timer"}}});
        assert_eq!(
            code(crate::validate_request("end_layout", end.clone())),
            invalid
        );
        let typed: LayoutEndInput = serde_json::from_value(end).unwrap();
        assert_eq!(code(typed.validate()), invalid);
        // Only that key: a longer one is a value like any other, and an end without content has none.
        let mut similar = start();
        similar["snapshot"]["values"]["liveActivityTemplates"] = json!("progress");
        assert_eq!(
            code(crate::validate_request("start_layout", similar)),
            Ok(())
        );
        let bare = json!({"id":"a","policy":{"kind":"immediate"}});
        assert_eq!(code(crate::validate_request("end_layout", bare)), Ok(()));
    }
}