use serde::{Deserialize, Deserializer, Serialize};
fn non_null<'de, D: Deserializer<'de>, T: Deserialize<'de>>(
d: D,
) -> std::result::Result<Option<T>, D::Error> {
T::deserialize(d).map(Some)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Timer {
pub started_at: u64,
pub ends_at: u64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Snapshot {
pub revision: u64,
pub title: String,
pub status: String,
#[serde(
default,
deserialize_with = "non_null",
skip_serializing_if = "Option::is_none"
)]
pub progress: Option<f64>,
#[serde(
default,
deserialize_with = "non_null",
skip_serializing_if = "Option::is_none"
)]
pub timer: Option<Timer>,
#[serde(
default,
deserialize_with = "non_null",
skip_serializing_if = "Option::is_none"
)]
pub stale_at: Option<u64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Template {
Progress,
Timer,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Fallback {
None,
Notification,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct StartInput {
pub activity_key: String,
pub schema_version: u8,
pub template: Template,
pub fallback: Fallback,
pub snapshot: Snapshot,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UpdateInput {
pub id: String,
pub snapshot: Snapshot,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", deny_unknown_fields)]
pub enum EndPolicy {
Immediate {},
Default {},
After {
at: u64,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct EndInput {
pub id: String,
pub policy: EndPolicy,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PermissionKind {
Notifications,
LiveActivities,
Promotion,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum FeatureSupport {
Supported,
Unsupported,
Unknown,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Backend {
ActivityKit,
AndroidNotification,
Unsupported,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Capabilities {
pub platform: String,
pub backend: Backend,
pub start: FeatureSupport,
pub update: FeatureSupport,
pub end: FeatureSupport,
pub progress: FeatureSupport,
pub timer: FeatureSupport,
pub actions: FeatureSupport,
pub state_observation: FeatureSupport,
pub silent_update: FeatureSupport,
pub native_live_activity: FeatureSupport,
pub max_payload_bytes: u64,
#[serde(default = "unsupported")]
pub layout: FeatureSupport,
#[serde(default = "unsupported")]
pub push: FeatureSupport,
}
fn unsupported() -> FeatureSupport {
FeatureSupport::Unsupported
}
fn not_applicable() -> PermissionState {
PermissionState::NotApplicable
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PermissionState {
Granted,
Denied,
Prompt,
SettingsRequired,
NotApplicable,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Permissions {
pub notifications: PermissionState,
pub live_activities: PermissionState,
pub promotion: PermissionState,
#[serde(default = "not_applicable")]
pub frequent_pushes: PermissionState,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum ActivityState {
Pending,
Active,
Stale,
Ended,
Dismissed,
Unknown,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Visibility {
Unknown,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PromotionState {
Promoted,
NotPromoted,
Unknown,
NotApplicable,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActivityRecord {
pub id: String,
pub activity_key: String,
pub schema_version: u8,
pub template: Template,
pub snapshot: Snapshot,
pub backend: Backend,
pub state: ActivityState,
pub visibility: Visibility,
pub degraded_features: Vec<String>,
pub promotion_requested: bool,
pub promotion_state: PromotionState,
}
impl Snapshot {
#[must_use]
pub fn stale_at(self, at: u64) -> Self {
Self {
stale_at: Some(at),
..self
}
}
fn progress(revision: u64, title: &str, status: &str, progress: f64) -> Self {
Self {
revision,
title: title.into(),
status: status.into(),
progress: Some(progress),
timer: None,
stale_at: None,
}
}
fn timer(revision: u64, title: &str, status: &str, started_at: u64, ends_at: u64) -> Self {
Self {
revision,
title: title.into(),
status: status.into(),
progress: None,
timer: Some(Timer {
started_at,
ends_at,
}),
stale_at: None,
}
}
}
impl StartInput {
#[must_use]
pub fn stale(self, at: u64) -> Self {
Self {
snapshot: self.snapshot.stale_at(at),
..self
}
}
fn of(activity_key: &str, template: Template, snapshot: Snapshot) -> Self {
Self {
activity_key: activity_key.into(),
schema_version: 1,
template,
fallback: Fallback::Notification,
snapshot,
}
}
pub fn progress(activity_key: &str, title: &str, status: &str, progress: f64) -> Self {
Self::of(
activity_key,
Template::Progress,
Snapshot::progress(0, title, status, progress),
)
}
pub fn timer(
activity_key: &str,
title: &str,
status: &str,
started_at: u64,
ends_at: u64,
) -> Self {
Self::of(
activity_key,
Template::Timer,
Snapshot::timer(0, title, status, started_at, ends_at),
)
}
#[must_use]
pub fn without_fallback(self) -> Self {
Self {
fallback: Fallback::None,
..self
}
}
}
impl UpdateInput {
pub fn progress(id: &str, revision: u64, title: &str, status: &str, progress: f64) -> Self {
Self {
id: id.into(),
snapshot: Snapshot::progress(revision, title, status, progress),
}
}
pub fn timer(
id: &str,
revision: u64,
title: &str,
status: &str,
started_at: u64,
ends_at: u64,
) -> Self {
Self {
id: id.into(),
snapshot: Snapshot::timer(revision, title, status, started_at, ends_at),
}
}
}
impl EndInput {
pub fn new(id: &str) -> Self {
Self {
id: id.into(),
policy: EndPolicy::Default {},
}
}
pub fn immediate(id: &str) -> Self {
Self {
id: id.into(),
policy: EndPolicy::Immediate {},
}
}
}