feat(notification): add plugin (#326)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-02 09:09:50 -07:00
committed by GitHub
parent 864b9d790f
commit e9bbe94181
34 changed files with 1536 additions and 8 deletions
+66
View File
@@ -0,0 +1,66 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::{de::DeserializeOwned, Deserialize};
use tauri::{
plugin::{PluginApi, PluginHandle},
AppHandle, Runtime,
};
use crate::models::*;
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.notification";
#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_notification);
// initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>(
_app: &AppHandle<R>,
api: PluginApi<R, C>,
) -> crate::Result<Notification<R>> {
#[cfg(target_os = "android")]
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "NotificationPlugin")?;
#[cfg(target_os = "ios")]
let handle = api.register_ios_plugin(init_plugin_notification)?;
Ok(Notification(handle))
}
impl<R: Runtime> crate::NotificationBuilder<R> {
pub fn show(self) -> crate::Result<()> {
self.handle
.run_mobile_plugin("notify", self.data)
.map_err(Into::into)
}
}
/// Access to the notification APIs.
pub struct Notification<R: Runtime>(PluginHandle<R>);
impl<R: Runtime> Notification<R> {
pub fn builder(&self) -> crate::NotificationBuilder<R> {
crate::NotificationBuilder::new(self.0.clone())
}
pub fn request_permission(&self) -> crate::Result<PermissionState> {
self.0
.run_mobile_plugin::<PermissionResponse>("requestPermission", ())
.map(|r| r.permission_state)
.map_err(Into::into)
}
pub fn permission_state(&self) -> crate::Result<PermissionState> {
self.0
.run_mobile_plugin::<PermissionResponse>("permissionState", ())
.map(|r| r.permission_state)
.map_err(Into::into)
}
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct PermissionResponse {
permission_state: PermissionState,
}