use sync_async::sync_async;
use crate::*;
use super::*;
#[sync_async]
pub struct Picker<'a, R: tauri::Runtime> {
#[cfg(target_os = "android")]
pub(crate) handle: &'a tauri::plugin::PluginHandle<R>,
#[cfg(not(target_os = "android"))]
#[allow(unused)]
pub(crate) handle: &'a std::marker::PhantomData<fn() -> R>,
}
#[cfg(target_os = "android")]
#[sync_async(
use(if_sync) impls::SyncImpls as Impls;
use(if_async) impls::AsyncImpls as Impls;
)]
impl<'a, R: tauri::Runtime> Picker<'a, R> {
#[always_sync]
fn impls(&self) -> Impls<'_, R> {
Impls { handle: &self.handle }
}
}
#[sync_async(
use(if_async) api_async::{AndroidFs, Opener, PrivateStorage, PublicStorage};
use(if_sync) api_sync::{AndroidFs, Opener, PrivateStorage, PublicStorage};
)]
impl<'a, R: tauri::Runtime> Picker<'a, R> {
#[maybe_async]
pub fn pick_files(
&self,
initial_location: Option<&FsUri>,
mime_types: &[&str],
local_only: bool,
) -> Result<Vec<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_pick_file_dialog(initial_location, mime_types, true, local_only).await
}
}
#[maybe_async]
pub fn pick_file(
&self,
initial_location: Option<&FsUri>,
mime_types: &[&str],
local_only: bool
) -> Result<Option<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_pick_file_dialog(initial_location, mime_types, false, local_only)
.await
.map(|mut i| i.pop())
}
}
#[maybe_async]
pub fn pick_visual_medias(
&self,
target: VisualMediaTarget<'_>,
local_only: bool
) -> Result<Vec<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_pick_visual_media_dialog(target, true, local_only).await
}
}
#[maybe_async]
pub fn pick_visual_media(
&self,
target: VisualMediaTarget<'_>,
local_only: bool
) -> Result<Option<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_pick_visual_media_dialog(target, false, local_only)
.await
.map(|mut i| i.pop())
}
}
#[maybe_async]
#[deprecated = "This may not support operations other than opening files."]
pub fn pick_contents(
&self,
mime_types: &[&str],
) -> Result<Vec<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_pick_content_dialog(mime_types, true).await
}
}
#[maybe_async]
#[deprecated = "This may not support operations other than opening files."]
pub fn pick_content(
&self,
mime_types: &[&str],
) -> Result<Option<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_pick_content_dialog(mime_types, false)
.await
.map(|mut i| i.pop())
}
}
#[maybe_async]
pub fn pick_dir(
&self,
initial_location: Option<&FsUri>,
local_only: bool
) -> Result<Option<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_pick_dir_dialog(initial_location, local_only).await
}
}
#[maybe_async]
pub fn save_file(
&self,
initial_location: Option<&FsUri>,
initial_file_name: impl AsRef<str>,
mime_type: Option<&str>,
local_only: bool
) -> Result<Option<FsUri>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().show_save_file_dialog(initial_location, initial_file_name, mime_type, local_only).await
}
}
#[maybe_async]
pub fn resolve_public_storage_initial_location(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<std::path::Path>,
create_dir_all: bool
) -> Result<FsUri> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().resolve_initial_location_in_public_storage(volume_id, base_dir, relative_path, create_dir_all).await
}
}
#[maybe_async]
pub fn resolve_initial_location(&self, volume_id: Option<&StorageVolumeId>) -> Result<FsUri> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().resolve_root_initial_location(volume_id).await
}
}
#[maybe_async]
pub fn is_visual_media_picker_available(&self) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().is_visual_media_picker_available().await
}
}
#[maybe_async]
pub fn check_uri_permission(
&self,
uri: &FsUri,
permission: UriPermission
) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().check_picker_uri_permission(uri, permission).await
}
}
#[maybe_async]
pub fn persist_uri_permission(&self, uri: &FsUri) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().persist_picker_uri_permission(uri).await
}
}
#[maybe_async]
pub fn check_persisted_uri_permission(
&self,
uri: &FsUri,
permission: UriPermission
) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().check_persisted_picker_uri_permission(uri, permission).await
}
}
#[maybe_async]
pub fn get_all_persisted_uri_permissions(&self) -> Result<Vec<PersistedUriPermissionState>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls()
.get_all_persisted_picker_uri_permissions().await
.map(|v| v.collect())
}
}
#[maybe_async]
pub fn release_persisted_uri_permission(&self, uri: &FsUri) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().release_persisted_picker_uri_permission(uri).await
}
}
#[maybe_async]
pub fn release_all_persisted_uri_permissions(&self) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().release_all_persisted_picker_uri_permissions().await
}
}
}