use std::sync::Arc;
use serde_json::{Map, Value};
use tauri::{Runtime, State, Webview};
use super::{Action, Clear, Request};
use crate::commands::caller;
use crate::error::{Error, Result};
use crate::Shared;
#[allow(clippy::too_many_arguments)]
#[tauri::command(rename_all = "snake_case")]
pub async fn notify_show<R: Runtime>(
webview: Webview<R>,
state: State<'_, Arc<Shared>>,
id: String,
title: String,
body: String,
kind: String,
conversation_id: Option<String>,
seq: Option<i64>,
data: Option<Map<String, Value>>,
sound: Option<bool>,
actions: Option<Vec<Action>>,
group: Option<String>,
) -> Result<()> {
caller(&state, &webview)?;
let req = Request { id, title, body, kind, conversation_id, seq, data, sound: sound.unwrap_or(true), actions, group };
state.notify.show(&req).await
}
#[tauri::command(rename_all = "snake_case")]
pub async fn notify_clear<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, id: Option<String>, conversation_id: Option<String>, all: Option<bool>) -> Result<()> {
caller(&state, &webview)?;
let target = match (id, conversation_id, all.unwrap_or(false)) {
(Some(id), None, false) => Clear::Id(id),
(None, Some(conversation_id), false) => Clear::Conversation(conversation_id),
(None, None, true) => Clear::All,
_ => return Err(Error::validation("invalid_target", "notify_clear 只能给出 id、conversation_id、all 其中之一")),
};
state.notify.clear(target).await
}
#[tauri::command(rename_all = "snake_case")]
pub async fn notify_permission<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>) -> Result<&'static str> {
caller(&state, &webview)?;
Ok(state.notify.permission().await)
}
#[tauri::command(rename_all = "snake_case")]
pub async fn notify_request_permission<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>) -> Result<&'static str> {
caller(&state, &webview)?;
Ok(state.notify.request_permission().await)
}
#[tauri::command(rename_all = "snake_case")]
pub fn notify_claim<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, click_id: String) -> Result<bool> {
caller(&state, &webview)?;
Ok(state.notify.claim(&click_id))
}