use crate::{
ActivityRecord, BackendContext, Capabilities, EndInput, EventSink, LayoutActivityRecord,
LayoutEndInput, LayoutStartInput, LayoutUpdateInput, LiveActivityBackend, PermissionKind,
Permissions, PushTokens, Result, StartInput, UpdateInput, Validate, check_image,
};
use std::sync::Arc;
#[derive(Default)]
#[must_use = "a builder connects nothing until `build` is called"]
pub struct Builder {
backend: Option<Arc<dyn LiveActivityBackend>>,
sink: Option<BackendContext>,
}
impl Builder {
pub fn backend<B: LiveActivityBackend>(self, backend: B) -> Self {
Self {
backend: Some(Arc::new(backend)),
..self
}
}
pub fn event_sink<S: EventSink>(self, sink: S) -> Self {
Self {
sink: Some(BackendContext::new(sink)),
..self
}
}
pub fn build(self) -> Result<LiveActivity> {
let backend = match self.backend {
Some(backend) => backend,
None => crate::native::default_backend()?,
};
let sink = self
.sink
.unwrap_or_else(|| BackendContext::new(|_: crate::Event| {}));
backend.attach(sink)?;
Ok(LiveActivity { backend })
}
}
#[derive(Clone)]
pub struct LiveActivity {
backend: Arc<dyn LiveActivityBackend>,
}
impl LiveActivity {
pub fn builder() -> Builder {
Builder::default()
}
pub async fn capabilities(&self) -> Result<Capabilities> {
self.backend.capabilities().await
}
pub async fn permissions(&self) -> Result<Permissions> {
self.backend.permissions().await
}
pub async fn request_permissions(&self, kind: PermissionKind) -> Result<Permissions> {
self.backend.request_permissions(kind).await
}
pub async fn start(&self, input: StartInput) -> Result<ActivityRecord> {
input.validate()?;
self.backend.start(input).await
}
pub async fn update(&self, input: UpdateInput) -> Result<ActivityRecord> {
input.validate()?;
self.backend.update(input).await
}
pub async fn end(&self, input: EndInput) -> Result<ActivityRecord> {
input.validate()?;
self.backend.end(input).await
}
pub async fn list(&self) -> Result<Vec<ActivityRecord>> {
self.backend.list().await
}
pub async fn start_layout(&self, input: LayoutStartInput) -> Result<LayoutActivityRecord> {
input.validate()?;
self.backend.start_layout(input).await
}
pub async fn update_layout(&self, input: LayoutUpdateInput) -> Result<LayoutActivityRecord> {
input.validate()?;
self.backend.update_layout(input).await
}
pub async fn end_layout(&self, input: LayoutEndInput) -> Result<LayoutActivityRecord> {
input.validate()?;
self.backend.end_layout(input).await
}
pub async fn list_layout(&self) -> Result<Vec<LayoutActivityRecord>> {
self.backend.list_layout().await
}
pub async fn write_image(&self, name: &str, png: &[u8]) -> Result<()> {
check_image(name, png)?;
self.backend
.write_image(name.to_owned(), png.to_vec())
.await
}
pub async fn push_tokens(&self) -> Result<PushTokens> {
self.backend.push_tokens().await
}
pub async fn sync_events(&self) -> Result<()> {
self.backend.sync_events().await
}
pub async fn open_promotion_settings(&self) -> Result<()> {
self.backend.open_promotion_settings().await
}
}