mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
The Android and iOS support introduced on #1011 is not really supported - the Tauri path API correctly resolves the cache directory on mobile, and we can access those directly using Rust code. This is a breaking change because we no longer uses the same directory to store the files - app_cache_dir returns a different location
This commit is contained in:
committed by
GitHub
parent
0d5e7e2892
commit
0c040bcc9a
@@ -1,49 +0,0 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use crate::Error;
|
||||
use crate::Runtime;
|
||||
use crate::Store;
|
||||
use std::fs::create_dir_all;
|
||||
use std::fs::read;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use tauri::Manager;
|
||||
|
||||
#[cfg(desktop)]
|
||||
impl<R: Runtime> Store<R> {
|
||||
pub fn save(&self) -> Result<(), Error> {
|
||||
let app_dir = self
|
||||
.app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.expect("failed to resolve app dir");
|
||||
let store_path = app_dir.join(&self.path);
|
||||
|
||||
create_dir_all(store_path.parent().expect("invalid store path"))?;
|
||||
|
||||
let bytes = (self.serialize)(&self.cache).map_err(Error::Serialize)?;
|
||||
let mut f = File::create(&store_path)?;
|
||||
f.write_all(&bytes)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Update the store from the on-disk state
|
||||
pub fn load(&mut self) -> Result<(), Error> {
|
||||
let app_dir = self
|
||||
.app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.expect("failed to resolve app dir");
|
||||
let store_path = app_dir.join(&self.path);
|
||||
|
||||
let bytes = read(store_path)?;
|
||||
|
||||
self.cache
|
||||
.extend((self.deserialize)(&bytes).map_err(Error::Deserialize)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,6 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
#[cfg(mobile)]
|
||||
#[error(transparent)]
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
/// Mobile plugin handled is not initialized, Probably [`StoreBuilder::mobile_plugin_handle`] was not called.
|
||||
#[cfg(mobile)]
|
||||
#[error("Mobile plugin handled is not initialized, Perhaps you forgot to call StoreBuilder::mobile_plugin_handle")]
|
||||
MobilePluginHandleUnInitialized,
|
||||
#[error("Failed to serialize store. {0}")]
|
||||
Serialize(Box<dyn std::error::Error + Send + Sync>),
|
||||
#[error("Failed to deserialize store. {0}")]
|
||||
|
||||
@@ -29,18 +29,6 @@ use tauri::{
|
||||
mod error;
|
||||
mod store;
|
||||
|
||||
#[cfg(mobile)]
|
||||
mod mobile;
|
||||
#[cfg(mobile)]
|
||||
use crate::plugin::PluginHandle;
|
||||
#[cfg(target_os = "android")]
|
||||
const PLUGIN_IDENTIFIER: &str = "app.tauri.store";
|
||||
#[cfg(target_os = "ios")]
|
||||
tauri::ios_plugin_binding!(init_plugin_store);
|
||||
|
||||
#[cfg(desktop)]
|
||||
mod desktop;
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
struct ChangePayload<'a> {
|
||||
path: &'a Path,
|
||||
@@ -51,9 +39,6 @@ struct ChangePayload<'a> {
|
||||
pub struct StoreCollection<R: Runtime> {
|
||||
stores: Mutex<HashMap<PathBuf, Store<R>>>,
|
||||
frozen: bool,
|
||||
|
||||
#[cfg(mobile)]
|
||||
mobile_plugin_handle: PluginHandle<R>,
|
||||
}
|
||||
|
||||
pub fn with_store<R: Runtime, T, F: FnOnce(&mut Store<R>) -> Result<T>>(
|
||||
@@ -73,11 +58,6 @@ pub fn with_store<R: Runtime, T, F: FnOnce(&mut Store<R>) -> Result<T>>(
|
||||
#[allow(unused_mut)]
|
||||
let mut builder = StoreBuilder::new(path);
|
||||
|
||||
#[cfg(mobile)]
|
||||
{
|
||||
builder = builder.mobile_plugin_handle(collection.mobile_plugin_handle.clone());
|
||||
}
|
||||
|
||||
let mut store = builder.build(app);
|
||||
|
||||
// ignore loading errors, just use the default
|
||||
@@ -329,17 +309,9 @@ impl<R: Runtime> Builder<R> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
let handle = _api.register_android_plugin(PLUGIN_IDENTIFIER, "StorePlugin")?;
|
||||
#[cfg(target_os = "ios")]
|
||||
let handle = _api.register_ios_plugin(init_plugin_store)?;
|
||||
|
||||
app_handle.manage(StoreCollection {
|
||||
stores: Mutex::new(self.stores),
|
||||
frozen: self.frozen,
|
||||
|
||||
#[cfg(mobile)]
|
||||
mobile_plugin_handle: handle,
|
||||
});
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use tauri::Runtime;
|
||||
|
||||
use crate::error::Result;
|
||||
use crate::Store;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LoadStore {
|
||||
pub cache: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SaveStore {
|
||||
pub store: String,
|
||||
pub cache: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
#[cfg(mobile)]
|
||||
impl<R: Runtime> Store<R> {
|
||||
pub fn save(&self) -> Result<()> {
|
||||
self.mobile_plugin_handle
|
||||
.as_ref()
|
||||
.ok_or_else(|| crate::error::Error::MobilePluginHandleUnInitialized)?
|
||||
.run_mobile_plugin(
|
||||
"save",
|
||||
SaveStore {
|
||||
store: self.path.to_string_lossy().to_string(),
|
||||
cache: self.cache.clone(),
|
||||
},
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn load(&mut self) -> Result<()> {
|
||||
let result: Value = self
|
||||
.mobile_plugin_handle
|
||||
.as_ref()
|
||||
.ok_or_else(|| crate::error::Error::MobilePluginHandleUnInitialized)?
|
||||
.run_mobile_plugin("load", self.path.to_string_lossy().to_string())?;
|
||||
|
||||
let map = serde_json::from_value::<HashMap<String, Value>>(result)?;
|
||||
self.cache.extend(map);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
+40
-27
@@ -2,15 +2,15 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#[cfg(mobile)]
|
||||
use crate::plugin::PluginHandle;
|
||||
use crate::{ChangePayload, Error};
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{create_dir_all, read, File},
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use tauri::{AppHandle, Emitter, Runtime};
|
||||
use tauri::{AppHandle, Emitter, Manager, Runtime};
|
||||
|
||||
type SerializeFn =
|
||||
fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
|
||||
@@ -30,20 +30,15 @@ fn default_deserialize(
|
||||
}
|
||||
|
||||
/// Builds a [`Store`]
|
||||
pub struct StoreBuilder<R: Runtime> {
|
||||
pub struct StoreBuilder {
|
||||
path: PathBuf,
|
||||
defaults: Option<HashMap<String, JsonValue>>,
|
||||
cache: HashMap<String, JsonValue>,
|
||||
serialize: SerializeFn,
|
||||
deserialize: DeserializeFn,
|
||||
|
||||
#[cfg(mobile)]
|
||||
mobile_plugin_handle: Option<PluginHandle<R>>,
|
||||
#[cfg(not(mobile))]
|
||||
_marker: std::marker::PhantomData<R>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> StoreBuilder<R> {
|
||||
impl StoreBuilder {
|
||||
/// Creates a new [`StoreBuilder`].
|
||||
///
|
||||
/// # Examples
|
||||
@@ -64,19 +59,9 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
cache: Default::default(),
|
||||
serialize: default_serialize,
|
||||
deserialize: default_deserialize,
|
||||
#[cfg(mobile)]
|
||||
mobile_plugin_handle: None,
|
||||
#[cfg(not(mobile))]
|
||||
_marker: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(mobile)]
|
||||
pub fn mobile_plugin_handle(mut self, handle: PluginHandle<R>) -> Self {
|
||||
self.mobile_plugin_handle = Some(handle);
|
||||
self
|
||||
}
|
||||
|
||||
/// Inserts a default key-value pair.
|
||||
///
|
||||
/// # Examples
|
||||
@@ -164,7 +149,7 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// ```
|
||||
pub fn build(self, app: AppHandle<R>) -> Store<R> {
|
||||
pub fn build<R: Runtime>(self, app: AppHandle<R>) -> Store<R> {
|
||||
Store {
|
||||
app,
|
||||
path: self.path,
|
||||
@@ -172,9 +157,6 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
cache: self.cache,
|
||||
serialize: self.serialize,
|
||||
deserialize: self.deserialize,
|
||||
|
||||
#[cfg(mobile)]
|
||||
mobile_plugin_handle: self.mobile_plugin_handle,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,12 +169,43 @@ pub struct Store<R: Runtime> {
|
||||
pub(crate) cache: HashMap<String, JsonValue>,
|
||||
pub(crate) serialize: SerializeFn,
|
||||
pub(crate) deserialize: DeserializeFn,
|
||||
|
||||
#[cfg(mobile)]
|
||||
pub(crate) mobile_plugin_handle: Option<PluginHandle<R>>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Store<R> {
|
||||
pub fn save(&self) -> Result<(), Error> {
|
||||
let app_dir = self
|
||||
.app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.expect("failed to resolve app dir");
|
||||
let store_path = app_dir.join(&self.path);
|
||||
|
||||
create_dir_all(store_path.parent().expect("invalid store path"))?;
|
||||
|
||||
let bytes = (self.serialize)(&self.cache).map_err(Error::Serialize)?;
|
||||
let mut f = File::create(&store_path)?;
|
||||
f.write_all(&bytes)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Update the store from the on-disk state
|
||||
pub fn load(&mut self) -> Result<(), Error> {
|
||||
let app_dir = self
|
||||
.app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.expect("failed to resolve app dir");
|
||||
let store_path = app_dir.join(&self.path);
|
||||
|
||||
let bytes = read(store_path)?;
|
||||
|
||||
self.cache
|
||||
.extend((self.deserialize)(&bytes).map_err(Error::Deserialize)?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, key: String, value: JsonValue) -> Result<(), Error> {
|
||||
self.cache.insert(key.clone(), value.clone());
|
||||
self.app.emit(
|
||||
|
||||
Reference in New Issue
Block a user