From 27c2f4b9f0ead161e27490b74ab4bf372729e0cd Mon Sep 17 00:00:00 2001 From: JiuRanYa <1084350607@qq.com> Date: Fri, 25 Oct 2024 18:11:52 +0800 Subject: [PATCH] feat: setting store in file --- package.json | 1 + pnpm-lock.yaml | 10 +++ src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/capabilities/default.json | 8 +- src-tauri/src/lib.rs | 22 +++-- src/config/index.ts | 2 + src/pages/settings.tsx | 131 ++++++++++++++++------------ 8 files changed, 112 insertions(+), 64 deletions(-) create mode 100644 src/config/index.ts diff --git a/package.json b/package.json index e93112c..78c05af 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "dependencies": { "@tauri-apps/api": "^2", "@tauri-apps/plugin-autostart": "^2.0.0", + "@tauri-apps/plugin-fs": "^2.0.1", "@tauri-apps/plugin-notification": "^2.0.0", "@tauri-apps/plugin-shell": "^2", "core": "link:@tauri-apps/api/core", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 55cd067..b0e441c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@tauri-apps/plugin-autostart': specifier: ^2.0.0 version: 2.0.0 + '@tauri-apps/plugin-fs': + specifier: ^2.0.1 + version: 2.0.1 '@tauri-apps/plugin-notification': specifier: ^2.0.0 version: 2.0.0 @@ -656,6 +659,9 @@ packages: '@tauri-apps/plugin-autostart@2.0.0': resolution: {integrity: sha512-NEwOQWVasZ8RczXkMLNJokRDujneuMH/UFA5t84DLkbNZUmiD3G7HZWhgSd1YQ0BFU9h9w+h2B/py3y6bzWg4Q==} + '@tauri-apps/plugin-fs@2.0.1': + resolution: {integrity: sha512-PkeZG2WAob9Xpmr66aPvj+McDVgFjV2a7YBzYVZjiCvbGeMs6Yk09tlXhCe3EyZdT/pwWMSi8lXUace+hlsjsw==} + '@tauri-apps/plugin-notification@2.0.0': resolution: {integrity: sha512-6qEDYJS7mgXZWLXA0EFL+DVCJh8sJlzSoyw6B50pxhLPVFjc5Vr5DVzl5W3mUHaYhod5wsC984eQnlCCGqxYDA==} @@ -1860,6 +1866,10 @@ snapshots: dependencies: '@tauri-apps/api': 2.0.3 + '@tauri-apps/plugin-fs@2.0.1': + dependencies: + '@tauri-apps/api': 2.0.3 + '@tauri-apps/plugin-notification@2.0.0': dependencies: '@tauri-apps/api': 2.0.3 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 377de29..84236cb 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -3674,6 +3674,7 @@ dependencies = [ "tauri-build", "tauri-plugin-autostart", "tauri-plugin-dialog", + "tauri-plugin-fs", "tauri-plugin-notification", "tauri-plugin-shell", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 704f19d..aea52ea 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,4 +25,5 @@ serde_json = "1" tauri-plugin-dialog = "2.0.3" tauri-plugin-notification = "2.0.1" tauri-plugin-autostart = "2.0.1" +tauri-plugin-fs = "2" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index bac770f..f9d0ff6 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -10,6 +10,12 @@ "core:window:allow-hide", "shell:allow-open", "core:window:default", - "core:window:allow-start-dragging" + "core:window:allow-start-dragging", + "fs:default", + "fs:write-files", + { + "identifier": "fs:scope", + "allow": [{ "path": "$CONFIG" }, { "path": "$CONFIG/**" }] + } ] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 44ec0d8..3495256 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -31,12 +31,16 @@ fn pause_timer(window: WebviewWindow) { #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() + .plugin(tauri_plugin_fs::init()) .setup(|app| { let quit_i = MenuItem::with_id(app, "exit", "退出", true, None::<&str>)?; let settings_i = MenuItem::with_id(app, "settings", "设置", true, None::<&str>)?; - let start_timer_i = MenuItem::with_id(app, "start_timer", "开始计时", true, None::<&str>)?; - let pause_timer_i = MenuItem::with_id(app, "pause_timer", "暂停计时", true, None::<&str>)?; - let menu = Menu::with_items(app, &[&start_timer_i, &pause_timer_i, &settings_i, &quit_i])?; + let start_timer_i = + MenuItem::with_id(app, "start_timer", "开始计时", true, None::<&str>)?; + let pause_timer_i = + MenuItem::with_id(app, "pause_timer", "暂停计时", true, None::<&str>)?; + let menu = + Menu::with_items(app, &[&start_timer_i, &pause_timer_i, &settings_i, &quit_i])?; let _tray = TrayIconBuilder::new() .icon(app.default_window_icon().unwrap().clone()) @@ -48,20 +52,24 @@ pub fn run() { "exit" => app.exit(0), "settings" => { let _show = window.show(); - }, + } "start_timer" => { start_timer(window); - }, + } "pause_timer" => { pause_timer(window); - }, + } _ => {} } }) .build(app)?; Ok(()) }) - .invoke_handler(tauri::generate_handler![lock_screen, start_timer, pause_timer]) + .invoke_handler(tauri::generate_handler![ + lock_screen, + start_timer, + pause_timer + ]) .plugin(tauri_plugin_autostart::init( MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]), diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 0000000..40e1d21 --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,2 @@ +export const CONFIG_FILE = 'standup_settings.json'; + diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx index 71c9470..d14d205 100644 --- a/src/pages/settings.tsx +++ b/src/pages/settings.tsx @@ -2,71 +2,90 @@ import React, { useState, useEffect } from "react"; import { Link } from 'react-router-dom'; import { enable, disable, isEnabled } from '@tauri-apps/plugin-autostart'; import { useTimerStore } from '../store/timer'; +import { readTextFile, writeTextFile, BaseDirectory, exists, create } from '@tauri-apps/plugin-fs'; +import { CONFIG_FILE } from "../config"; export default function Settings() { - const [isAutoStartEnabled, setIsAutoStartEnabled] = useState(false); - const { notificationMethod, setNotificationMethod } = useTimerStore(); + const [isAutoStartEnabled, setIsAutoStartEnabled] = useState(false); + const { notificationMethod, setNotificationMethod } = useTimerStore(); - useEffect(() => { - checkAutoStartStatus(); - // 这里可以添加加载通知方式设置的逻辑 - }, []); + useEffect(() => { + checkAutoStartStatus(); + loadSettings(); + }, []); - const checkAutoStartStatus = async () => { - const enabled = await isEnabled(); - setIsAutoStartEnabled(enabled); - }; + const checkAutoStartStatus = async () => { + const enabled = await isEnabled(); + setIsAutoStartEnabled(enabled); + }; - const toggleAutoStart = async () => { - if (isAutoStartEnabled) { - await disable(); - } else { - await enable(); - } - await checkAutoStartStatus(); - }; + const loadSettings = async () => { + const exist = await exists(CONFIG_FILE, { baseDir: BaseDirectory.Config }); + if (exist) { + const settings = await readTextFile(CONFIG_FILE, { baseDir: BaseDirectory.Config }); + const parsedSettings = JSON.parse(settings); + setNotificationMethod(parsedSettings.notificationMethod); + } + }; - const handleNotificationMethodChange = (event: React.ChangeEvent) => { - setNotificationMethod(event.target.value as 'desktop' | 'sound' | 'screen_lock'); - // 这里可以添加保存通知方式设置的逻辑 + const saveSettings = async () => { + const settings = { + notificationMethod, }; + await writeTextFile(CONFIG_FILE, JSON.stringify(settings), { baseDir: BaseDirectory.Config }); + }; + + const toggleAutoStart = async () => { + if (isAutoStartEnabled) { + await disable(); + } else { + await enable(); + } + await checkAutoStartStatus(); + await saveSettings(); + }; + + const handleNotificationMethodChange = (event: React.ChangeEvent) => { + setNotificationMethod(event.target.value as 'desktop' | 'sound' | 'screen_lock'); + saveSettings(); + }; - return ( -
-

设置

+ return ( +
+

设置

-
- -
+
+ +
-
- - -
+
+ + +
-
- - 返回主页 - -
-
- ); + +
+ ); }