Download packages/tauri-plugins/plugins/geolocation/src/mobile.rs from echodict/readest: direct link, hf CLI and curl.
- Browser
- Download file 3.57 kB
-
https://huggingface.co/datasets/echodict/readest/resolve/main/packages/tauri-plugins/plugins/geolocation/src/mobile.rs
- Command line
-
hf download hf://datasets/echodict/readest/packages/tauri-plugins/plugins/geolocation/src/mobile.rs
-
curl -L -o mobile.rs https://huggingface.co/datasets/echodict/readest/resolve/main/packages/tauri-plugins/plugins/geolocation/src/mobile.rs
3.57 kB
| // 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, InvokeResponseBody}, | |
| plugin::{PluginApi, PluginHandle}, | |
| AppHandle, Runtime, | |
| }; | |
| use crate::models::*; | |
| const PLUGIN_IDENTIFIER: &str = "app.tauri.geolocation"; | |
| 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>> { | |
| let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "GeolocationPlugin")?; | |
| 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 { | |
| InvokeResponseBody::Json(payload) => serde_json::from_str::<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, | |
| 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", | |
| serde_json::json!({ "permissions": permissions }), | |
| ) | |
| .map_err(Into::into) | |
| } | |
| } | |
| struct WatchPayload { | |
| options: PositionOptions, | |
| channel: Channel, | |
| } | |
| struct ClearWatchPayload { | |
| channel_id: u32, | |
| } | |