diff options
Diffstat (limited to 'src-tauri/src/storage.rs')
| -rw-r--r-- | src-tauri/src/storage.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src-tauri/src/storage.rs b/src-tauri/src/storage.rs new file mode 100644 index 0000000..28b1f86 --- /dev/null +++ b/src-tauri/src/storage.rs @@ -0,0 +1,75 @@ +use std::fs; +use std::path::PathBuf; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Settings { + pub work_duration_secs: u64, + pub short_break_secs: u64, + pub long_break_secs: u64, + pub sessions_before_long_break: u32, +} + +impl Default for Settings { + fn default() -> Self { + Self { + work_duration_secs: 25 * 60, + short_break_secs: 5 * 60, + long_break_secs: 15 * 60, + sessions_before_long_break: 4, + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Task { + pub id: String, + pub name: String, + pub total_sessions: u32, + pub remaining_sessions: u32, + pub completed: bool, + pub created_at: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct AppData { + pub settings: Settings, + pub tasks: Vec<Task>, + pub current_task_id: Option<String>, +} + +impl Default for AppData { + fn default() -> Self { + Self { + settings: Settings::default(), + tasks: Vec::new(), + current_task_id: None, + } + } +} + +pub fn data_path(app_data_dir: &PathBuf) -> PathBuf { + app_data_dir.join("data.json") +} + +pub fn load(app_data_dir: &PathBuf) -> AppData { + let path = data_path(app_data_dir); + if !path.exists() { + return AppData::default(); + } + match fs::read_to_string(&path) { + Ok(contents) => serde_json::from_str(&contents).unwrap_or_default(), + Err(_) => AppData::default(), + } +} + +pub fn save(app_data_dir: &PathBuf, data: &AppData) -> Result<(), String> { + fs::create_dir_all(app_data_dir) + .map_err(|e| format!("Failed to create data directory: {}", e))?; + let path = data_path(app_data_dir); + let contents = serde_json::to_string_pretty(data) + .map_err(|e| format!("Failed to serialize data: {}", e))?; + fs::write(&path, contents) + .map_err(|e| format!("Failed to write data file: {}", e))?; + Ok(()) +} |
