use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use tokio::sync::mpsc::Sender;
use crate::types::{Activity, IPCCommand};
#[derive(Debug, Clone)]
pub struct PresenceClient {
pub(crate) tx: Sender<IPCCommand>,
pub(crate) client_id: String,
pub(crate) running: Arc<AtomicBool>,
}
impl PresenceClient {
pub fn client_id(&self) -> String {
self.client_id.clone()
}
pub fn is_running(&self) -> bool {
self.running.load(Ordering::SeqCst)
}
pub async fn set_activity(&self, activity: Activity) -> Result<(), anyhow::Error> {
if !self.is_running() {
anyhow::bail!("Call .run() before .set_activity() execution.");
}
self.tx.send(IPCCommand::SetActivity { activity }).await?;
Ok(())
}
pub async fn clear_activity(&self) -> Result<(), anyhow::Error> {
if self.is_running() {
self.tx.send(IPCCommand::ClearActivity).await?;
}
Ok(())
}
pub async fn close(&self) -> Result<(), anyhow::Error> {
if self.is_running() {
let _ = self.tx.send(IPCCommand::Close).await;
}
Ok(())
}
}