Skip to content

Commit

Permalink
feat: setting store in file
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuRanYa committed Oct 25, 2024
1 parent a81e28e commit 27c2f4b
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 64 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

8 changes: 7 additions & 1 deletion src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/**" }]
}
]
}
22 changes: 15 additions & 7 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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"]),
Expand Down
2 changes: 2 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CONFIG_FILE = 'standup_settings.json';

131 changes: 75 additions & 56 deletions src/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLSelectElement>) => {
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<HTMLSelectElement>) => {
setNotificationMethod(event.target.value as 'desktop' | 'sound' | 'screen_lock');
saveSettings();
};

return (
<main className="max-w-2xl mx-auto">
<h1 className="text-3xl font-bold text-center text-gray-800 mb-8">设置</h1>
return (
<main className="max-w-2xl mx-auto">
<h1 className="text-3xl font-bold text-center text-gray-800 mb-8">设置</h1>

<div className="mb-8">
<label className="flex items-center justify-between mb-3">
<span className="text-gray-700 font-medium">开机自启动</span>
<input
type="checkbox"
checked={isAutoStartEnabled}
onChange={toggleAutoStart}
className="form-checkbox h-5 w-5 text-blue-600"
/>
</label>
</div>
<div className="mb-8">
<label className="flex items-center justify-between mb-3">
<span className="text-gray-700 font-medium">开机自启动</span>
<input
type="checkbox"
checked={isAutoStartEnabled}
onChange={toggleAutoStart}
className="form-checkbox h-5 w-5 text-blue-600"
/>
</label>
</div>

<div className="mb-8">
<label className="block text-gray-700 font-medium mb-2">
提醒方式
</label>
<select
value={notificationMethod}
onChange={handleNotificationMethodChange}
className="select select-sm select-bordered w-full"
>
<option value="desktop">桌面通知</option>
<option value="sound">声音提醒</option>
<option value="screen_lock">短暂锁屏</option>
</select>
</div>
<div className="mb-8">
<label className="block text-gray-700 font-medium mb-2">
提醒方式
</label>
<select
value={notificationMethod}
onChange={handleNotificationMethodChange}
className="select select-sm select-bordered w-full"
>
<option value="desktop">桌面通知</option>
<option value="sound">声音提醒</option>
<option value="screen_lock">短暂锁屏</option>
</select>
</div>

<footer className="mt-12 text-center">
<Link to="/" className="text-blue-500 hover:underline">
返回主页
</Link>
</footer>
</main>
);
<footer className="mt-12 text-center">
<Link to="/" className="text-blue-500 hover:underline">
返回主页
</Link>
</footer>
</main>
);
}

0 comments on commit 27c2f4b

Please sign in to comment.