use std::{collections::HashMap, fmt::Debug, path::PathBuf};
use async_trait::async_trait;
use crate::{Error, xdg_category::XdgNotificationCategory};
#[derive(Debug, Default)]
pub struct NotificationBuilder {
pub(crate) body: Option<String>,
pub(crate) title: Option<String>,
pub(crate) subtitle: Option<String>,
pub(crate) image: Option<std::path::PathBuf>,
pub(crate) icon: Option<std::path::PathBuf>,
pub(crate) icon_round_crop: bool,
pub(crate) thread_id: Option<String>,
pub(crate) category_id: Option<String>,
pub(crate) xdg_category: Option<XdgNotificationCategory>,
pub(crate) xdg_app_name: Option<String>,
pub(crate) user_info: Option<HashMap<String, String>>,
}
impl NotificationBuilder
where
Self: Sized,
{
pub fn new() -> Self {
NotificationBuilder {
..Default::default()
}
}
pub fn body(mut self, body: &str) -> Self {
self.body = Some(body.to_owned());
self
}
pub fn title(mut self, title: &str) -> Self {
self.title = Some(title.to_owned());
self
}
pub fn subtitle(mut self, subtitle: &str) -> Self {
self.subtitle = Some(subtitle.to_owned());
self
}
pub fn set_image(mut self, path: PathBuf) -> Self {
self.image = Some(path);
self
}
pub fn set_icon(mut self, path: PathBuf) -> Self {
self.icon = Some(path);
self
}
pub fn set_icon_round_crop(mut self, icon_round_crop: bool) -> Self {
self.icon_round_crop = icon_round_crop;
self
}
pub fn set_thread_id(mut self, thread_id: &str) -> Self {
self.thread_id = Some(thread_id.to_owned());
self
}
pub fn set_category_id(mut self, category_id: &str) -> Self {
self.category_id = Some(category_id.to_owned());
self
}
pub fn set_xdg_category(mut self, category: XdgNotificationCategory) -> Self {
self.xdg_category = Some(category);
self
}
pub fn set_xdg_app_name(mut self, name: String) -> Self {
self.xdg_app_name = Some(name);
self
}
pub fn set_user_info(mut self, user_info: HashMap<String, String>) -> Self {
self.user_info = Some(user_info);
self
}
}
pub trait NotificationHandle
where
Self: Send + Sync + Debug,
{
fn close(&self) -> Result<(), Error>;
fn get_id(&self) -> String;
fn get_user_info(&self) -> &HashMap<String, String>;
}
#[async_trait]
pub trait NotificationManager
where
Self: Send + Sync + Debug,
{
async fn get_notification_permission_state(&self) -> Result<bool, crate::Error>;
async fn first_time_ask_for_notification_permission(&self) -> Result<bool, Error>;
fn register(
&self,
handler_callback: Box<dyn Fn(crate::NotificationResponse) + Send + Sync + 'static>,
categories: Vec<NotificationCategory>,
) -> Result<(), Error>;
fn remove_all_delivered_notifications(&self) -> Result<(), Error>;
fn remove_delivered_notifications(&self, ids: Vec<&str>) -> Result<(), Error>;
async fn get_active_notifications(&self) -> Result<Vec<Box<dyn NotificationHandle>>, Error>;
async fn send_notification(
&self,
builder: NotificationBuilder,
) -> Result<Box<dyn NotificationHandle>, Error>;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct NotificationResponse {
pub notification_id: String,
pub action: NotificationResponseAction,
pub user_text: Option<String>,
pub user_info: HashMap<String, String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum NotificationResponseAction {
Default,
Dismiss,
Other(String),
}
#[derive(Debug, Clone)]
pub struct NotificationCategory {
pub identifier: String,
pub actions: Vec<NotificationCategoryAction>,
}
#[derive(Debug, Clone)]
pub enum NotificationCategoryAction {
Action {
identifier: String,
title: String,
},
TextInputAction {
identifier: String,
title: String,
input_button_title: String,
input_placeholder: String,
},
}