feat: Add geolocation and haptics plugins (#1599)

* init geolocation plugin

* ios impl (w/o js api)

* generate ts api

* use newer tauri commit

* add temporary postinstall

* include src in files

* guest-js

* just ship dist-js for now

* fix watcher

* fix android compile error

* fix android build for real

* fix heading type

* initial getCurrentPosition android impl (wip)

* prevent panics if errors (strings) are sent over the channel

* Add android watchPosition implementation

* init haptics plugin (android)

* ios and new apis (ANDROID IS LIKELY BROKEN - MAY NOT EVEN COMPILE)

* use tauri-specta that accounts for raw fn arg idents

* add complete android support (it's not working great due to random soft-/hardware support)

* fix(haptics): Fix the NotificationFeedbackType::Success and Version (#1)

* Fix success feedback and version

* Apply suggestions from code review

* Update package.json

---------

Co-authored-by: Fabian-Lars <118197967+FabianLars-crabnebula@users.noreply.github.com>

* android: improve permission callback handling

* keep track of ongoing perms requests

* rebuild

* license headers

* rm sqlite feat

* fmt

* what diff u talkin bout?

* ignore dist-js again

* fix audits

* dedupe api.js

* clippy

* changefiles

* readmes

* clean up todos

* rm dsstore

* rm wrong feats

* mirror

* covector

* rebuild

* ios requires the wry feature

* lint

* update lock

---------

Co-authored-by: fabianlars <fabianlars@fabianlars.de>
Co-authored-by: Brendan Allan <brendonovich@outlook.com>
Co-authored-by: Naman Garg <155433377+naman-crabnebula@users.noreply.github.com>
Co-authored-by: Lucas Nogueira <lucas@crabnebula.dev>
This commit is contained in:
Fabian-Lars
2024-08-02 15:45:47 +02:00
committed by GitHub
parent 34df132fb1
commit 9606089b2a
102 changed files with 4896 additions and 52 deletions
+47
View File
@@ -0,0 +1,47 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use tauri::{command, ipc::Channel, AppHandle, Runtime};
use crate::{GeolocationExt, PermissionStatus, PermissionType, Position, PositionOptions, Result};
#[command]
#[specta::specta]
pub(crate) async fn get_current_position<R: Runtime>(
app: AppHandle<R>,
options: Option<PositionOptions>,
) -> Result<Position> {
app.geolocation().get_current_position(options)
}
#[command]
#[specta::specta]
pub(crate) async fn watch_position<R: Runtime>(
app: AppHandle<R>,
options: PositionOptions,
channel: Channel,
) -> Result<()> {
app.geolocation().watch_position_inner(options, channel)
}
#[command]
#[specta::specta]
pub(crate) async fn clear_watch<R: Runtime>(app: AppHandle<R>, channel_id: u32) -> Result<()> {
app.geolocation().clear_watch(channel_id)
}
#[command]
#[specta::specta]
pub(crate) async fn check_permissions<R: Runtime>(app: AppHandle<R>) -> Result<PermissionStatus> {
app.geolocation().check_permissions()
}
#[command]
#[specta::specta]
pub(crate) async fn request_permissions<R: Runtime>(
app: AppHandle<R>,
permissions: Option<Vec<PermissionType>>,
) -> Result<PermissionStatus> {
app.geolocation().request_permissions(permissions)
}
+95
View File
@@ -0,0 +1,95 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::{de::DeserializeOwned, Serialize};
use tauri::{
ipc::{Channel, InvokeBody},
plugin::PluginApi,
AppHandle, Runtime,
};
use crate::models::*;
pub fn init<R: Runtime, C: DeserializeOwned>(
app: &AppHandle<R>,
_api: PluginApi<R, C>,
) -> crate::Result<Geolocation<R>> {
Ok(Geolocation(app.clone()))
}
/// Access to the geolocation APIs.
pub struct Geolocation<R: Runtime>(AppHandle<R>);
impl<R: Runtime> Geolocation<R> {
pub fn get_current_position(
&self,
_options: Option<PositionOptions>,
) -> crate::Result<Position> {
Ok(Position::default())
}
pub fn watch_position<F: Fn(WatchEvent) + Send + Sync + 'static>(
&self,
options: PositionOptions,
callback: F,
) -> crate::Result<u32> {
let channel = Channel::new(move |event| {
let payload = match event {
InvokeBody::Json(payload) => serde_json::from_value::<WatchEvent>(payload)
.unwrap_or_else(|error| {
WatchEvent::Error(format!(
"Couldn't deserialize watch event payload: `{error}`"
))
}),
_ => WatchEvent::Error("Unexpected watch event payload.".to_string()),
};
callback(payload);
Ok(())
});
let id = channel.id();
self.watch_position_inner(options, channel)?;
Ok(id)
}
pub(crate) fn watch_position_inner(
&self,
_options: PositionOptions,
_callback_channel: Channel,
) -> crate::Result<()> {
Ok(())
}
pub fn clear_watch(&self, _channel_id: u32) -> crate::Result<()> {
Ok(())
}
pub fn check_permissions(&self) -> crate::Result<PermissionStatus> {
Ok(PermissionStatus::default())
}
pub fn request_permissions(
&self,
_permissions: Option<Vec<PermissionType>>,
) -> crate::Result<PermissionStatus> {
Ok(PermissionStatus::default())
}
}
#[derive(Serialize)]
#[allow(unused)] // TODO:
struct WatchPayload {
options: PositionOptions,
channel: Channel,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
#[allow(unused)] // TODO:
struct ClearWatchPayload {
channel_id: u32,
}
+30
View File
@@ -0,0 +1,30 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::{ser::Serializer, Serialize};
use specta::Type;
pub type Result<T> = std::result::Result<T, Error>;
// TODO: Improve Error handling (different typed errors instead of one (stringified) PluginInvokeError for all mobile errors)
#[derive(Debug, thiserror::Error, Type)]
pub enum Error {
#[cfg(mobile)]
#[error(transparent)]
PluginInvoke(
#[serde(skip)]
#[from]
tauri::plugin::mobile::PluginInvokeError,
),
}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
+100
View File
@@ -0,0 +1,100 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
//use tauri_specta::*;
pub use models::*;
#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;
mod commands;
mod error;
mod models;
pub use error::{Error, Result};
#[cfg(desktop)]
use desktop::Geolocation;
#[cfg(mobile)]
use mobile::Geolocation;
/* macro_rules! specta_builder {
() => {
ts::builder()
.commands(collect_commands![
commands::get_current_position,
commands::watch_position,
commands::clear_watch,
commands::check_permissions,
commands::request_permissions
])
.header("// @ts-nocheck")
.config(
specta::ts::ExportConfig::default()
.bigint(specta::ts::BigIntExportBehavior::Number),
)
};
} */
/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the geolocation APIs.
pub trait GeolocationExt<R: Runtime> {
fn geolocation(&self) -> &Geolocation<R>;
}
impl<R: Runtime, T: Manager<R>> crate::GeolocationExt<R> for T {
fn geolocation(&self) -> &Geolocation<R> {
self.state::<Geolocation<R>>().inner()
}
}
/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
/* let (invoke_handler, register_events) =
specta_builder!().build_plugin_utils("geolocation").unwrap(); */
Builder::new("geolocation")
.invoke_handler(tauri::generate_handler![
commands::get_current_position,
commands::watch_position,
commands::clear_watch,
commands::check_permissions,
commands::request_permissions
])
.setup(|app, api| {
#[cfg(mobile)]
let geolocation = mobile::init(app, api)?;
#[cfg(desktop)]
let geolocation = desktop::init(app, api)?;
app.manage(geolocation);
Ok(())
})
.build()
}
/* #[cfg(test)]
mod test {
use super::*;
#[test]
fn export_types() {
specta_builder!()
.path("./guest-js/bindings.ts")
.config(
specta::ts::ExportConfig::default()
.formatter(specta::ts::formatter::prettier)
.bigint(specta::ts::BigIntExportBehavior::Number),
)
.export_for_plugin("geolocation")
.expect("failed to export specta types");
}
}
*/
+116
View File
@@ -0,0 +1,116 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::{de::DeserializeOwned, Serialize};
use tauri::{
ipc::{Channel, InvokeBody},
plugin::{PluginApi, PluginHandle},
AppHandle, Runtime,
};
use crate::models::*;
#[cfg(target_os = "android")]
const PLUGIN_IDENTIFIER: &str = "app.tauri.geolocation";
#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_geolocation);
// initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>(
_app: &AppHandle<R>,
api: PluginApi<R, C>,
) -> crate::Result<Geolocation<R>> {
#[cfg(target_os = "android")]
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "GeolocationPlugin")?;
#[cfg(target_os = "ios")]
let handle = api.register_ios_plugin(init_plugin_geolocation)?;
Ok(Geolocation(handle))
}
/// Access to the geolocation APIs.
pub struct Geolocation<R: Runtime>(PluginHandle<R>);
impl<R: Runtime> Geolocation<R> {
pub fn get_current_position(
&self,
options: Option<PositionOptions>,
) -> crate::Result<Position> {
// TODO: We may have to send over None if that's better on Android
self.0
.run_mobile_plugin("getCurrentPosition", options.unwrap_or_default())
.map_err(Into::into)
}
/// Register a position watcher. This method returns an id to use in `clear_watch`.
pub fn watch_position<F: Fn(WatchEvent) + Send + Sync + 'static>(
&self,
options: PositionOptions,
callback: F,
) -> crate::Result<u32> {
let channel = Channel::new(move |event| {
let payload = match event {
InvokeBody::Json(payload) => serde_json::from_value::<WatchEvent>(dbg!(payload))
.unwrap_or_else(|error| {
WatchEvent::Error(format!(
"Couldn't deserialize watch event payload: `{error}`"
))
}),
_ => WatchEvent::Error("Unexpected watch event payload.".to_string()),
};
callback(payload);
Ok(())
});
let id = channel.id();
self.watch_position_inner(options, channel)?;
Ok(id)
}
pub(crate) fn watch_position_inner(
&self,
options: PositionOptions,
channel: Channel,
) -> crate::Result<()> {
self.0
.run_mobile_plugin("watchPosition", WatchPayload { options, channel })
.map_err(Into::into)
}
pub fn clear_watch(&self, channel_id: u32) -> crate::Result<()> {
self.0
.run_mobile_plugin("clearWatch", ClearWatchPayload { channel_id })
.map_err(Into::into)
}
pub fn check_permissions(&self) -> crate::Result<PermissionStatus> {
self.0
.run_mobile_plugin("checkPermissions", ())
.map_err(Into::into)
}
pub fn request_permissions(
&self,
permissions: Option<Vec<PermissionType>>,
) -> crate::Result<PermissionStatus> {
self.0
.run_mobile_plugin("requestPermissions", permissions)
.map_err(Into::into)
}
}
#[derive(Serialize)]
struct WatchPayload {
options: PositionOptions,
channel: Channel,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ClearWatchPayload {
channel_id: u32,
}
+103
View File
@@ -0,0 +1,103 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use serde::{Deserialize, Serialize};
use specta::Type;
#[derive(Debug, Clone, Default, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct PermissionStatus {
/// Permission state for the location alias.
///
/// On Android it requests/checks both ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions.
///
/// On iOS it requests/checks location permissions.
pub location: PermissionState,
/// Permissions state for the coarseLoaction alias.
///
/// On Android it requests/checks ACCESS_COARSE_LOCATION.
///
/// On Android 12+, users can choose between Approximate location (ACCESS_COARSE_LOCATION) and Precise location (ACCESS_FINE_LOCATION).
///
/// On iOS it will have the same value as the `location` alias.
pub coarse_location: PermissionState,
}
/// Permission state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum PermissionState {
/// Permission access has been granted.
Granted,
/// Permission access has been denied.
Denied,
/// The end user should be prompted for permission.
#[default]
Prompt,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct PositionOptions {
/// High accuracy mode (such as GPS, if available)
/// Will be ignored on Android 12+ if users didn't grant the ACCESS_FINE_LOCATION permission.
pub enable_high_accuracy: bool,
/// The maximum wait time in milliseconds for location updates.
/// Default: 10000
/// On Android the timeout gets ignored for getCurrentPosition.
/// Ignored on iOS.
// TODO: Handle Infinity and default to it.
// TODO: Should be u64+ but specta doesn't like that?
pub timeout: u32,
/// The maximum age in milliseconds of a possible cached position that is acceptable to return.
/// Default: 0
/// Ignored on iOS.
// TODO: Handle Infinity.
// TODO: Should be u64+ but specta doesn't like that?
pub maximum_age: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum PermissionType {
Location,
CoarseLocation,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct Coordinates {
/// Latitude in decimal degrees.
pub latitude: f64,
/// Longitude in decimal degrees.
pub longitude: f64,
/// Accuracy level of the latitude and longitude coordinates in meters.
pub accuracy: f64,
/// Accuracy level of the altitude coordinate in meters, if available.
/// Available on all iOS versions and on Android 8 and above.
pub altitude_accuracy: Option<f64>,
/// The altitude the user is at, if available.
pub altitude: Option<f64>,
// The speed the user is traveling, if available.
pub speed: Option<f64>,
/// The heading the user is facing, if available.
pub heading: Option<f64>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub struct Position {
/// Creation time for these coordinates.
// TODO: Check if we're actually losing precision.
pub timestamp: u64,
/// The GPS coordinates along with the accuracy of the data.
pub coords: Coordinates,
}
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
#[serde(untagged)]
pub enum WatchEvent {
Position(Position),
Error(String),
}