mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-16 16:47:21 +02:00
feat(store)!: fully rework and add auto save (#1550)
* Add auto save to store plugin * Put jsdoc at constructor instead of class level * Clippy * Use enum instead of bool * Some(AutoSaveMessage::Cancel) | None * from_millis * u64 * Add change file * Rename to emit_on_change * should use Duration in `with_store` * Add breaking change notice to change file * Emit change event for inserts by reset * Update readme example * Update example * Remove extra line * Make description clear it only works with managed * Fix links in docstring * Fix doc string closing * get_mut * Proof of concept * fmt * Load store on create * cargo fmt * Fix merge conflits * Format * small cleanup * update docs, use `impl Into<JsonValue>` * fix doctests, further simplification of api * add store options --------- Co-authored-by: Tillmann <28728469+tweidinger@users.noreply.github.com> Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
@@ -21,9 +21,8 @@ impl AppSettings {
|
||||
|
||||
let theme = store
|
||||
.get("appSettings.theme")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| "dark".to_string());
|
||||
.and_then(|v| v.as_str().map(String::from))
|
||||
.unwrap_or_else(|| "dark".to_owned());
|
||||
|
||||
Ok(AppSettings {
|
||||
launch_at_login,
|
||||
|
||||
@@ -5,17 +5,24 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use tauri_plugin_store::StoreBuilder;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde_json::json;
|
||||
use tauri_plugin_store::StoreExt;
|
||||
|
||||
mod app;
|
||||
use app::settings::AppSettings;
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_store::Builder::default().build())
|
||||
.plugin(tauri_plugin_store::Builder::new().build())
|
||||
.setup(|app| {
|
||||
// Init store and load it from disk
|
||||
let mut store = StoreBuilder::new("settings.json").build(app.handle().clone());
|
||||
let store = app
|
||||
.handle()
|
||||
.store_builder("settings.json")
|
||||
.auto_save(Duration::from_millis(100))
|
||||
.build();
|
||||
|
||||
// If there are no saved settings yet, this will return an error so we ignore the return value.
|
||||
let _ = store.load();
|
||||
@@ -27,17 +34,20 @@ fn main() {
|
||||
let theme = app_settings.theme;
|
||||
let launch_at_login = app_settings.launch_at_login;
|
||||
|
||||
println!("theme {}", theme);
|
||||
println!("launch_at_login {}", launch_at_login);
|
||||
|
||||
Ok(())
|
||||
println!("theme {theme}");
|
||||
println!("launch_at_login {launch_at_login}");
|
||||
store.set(
|
||||
"appSettings",
|
||||
json!({ "theme": theme, "launchAtLogin": launch_at_login }),
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error loading settings: {}", err);
|
||||
eprintln!("Error loading settings: {err}");
|
||||
// Handle the error case if needed
|
||||
Err(err) // Convert the error to a Box<dyn Error> and return Err(err) here
|
||||
return Err(err); // Convert the error to a Box<dyn Error> and return Err(err) here
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user