summaryrefslogtreecommitdiff
path: root/src-tauri/src/storage.rs
blob: 2e726a3ad9bcfaf874a7ebe834cd9e39fb938e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::fs;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};

use crate::state::TimerPhase;

#[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 TimerSnapshot {
    pub phase: TimerPhase,
    pub remaining_secs: u64,
    pub total_secs: u64,
    pub running: bool,
    pub session_count: u32,
    pub current_task_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppData {
    pub settings: Settings,
    pub tasks: Vec<Task>,
    pub current_task_id: Option<String>,
    #[serde(default)]
    pub timer_snapshot: Option<TimerSnapshot>,
}

impl Default for AppData {
    fn default() -> Self {
        Self {
            settings: Settings::default(),
            tasks: Vec::new(),
            current_task_id: None,
            timer_snapshot: 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();
    }
    let mut data = match fs::read_to_string(&path) {
        Ok(contents) => serde_json::from_str(&contents).unwrap_or_default(),
        Err(_) => AppData::default(),
    };
    // Guard against a corrupt/zero value that would cause division by zero
    if data.settings.sessions_before_long_break == 0 {
        data.settings.sessions_before_long_break = Settings::default().sessions_before_long_break;
    }
    data
}

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))?;
    let tmp_path = app_data_dir.join("data.json.tmp");
    fs::write(&tmp_path, contents)
        .map_err(|e| format!("Failed to write temp data file: {}", e))?;

    if cfg!(windows) {
        if path.exists() {
            fs::remove_file(&path)
                .map_err(|e| format!("Failed to remove existing data file: {}", e))?;
        }
    }

    fs::rename(&tmp_path, &path)
        .map_err(|e| format!("Failed to finalize data file: {}", e))?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{load, save, AppData, Settings, Task, TimerSnapshot};
    use crate::state::TimerPhase;
    use std::fs;
    use std::time::{SystemTime, UNIX_EPOCH};

    fn temp_dir(label: &str) -> std::path::PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        std::env::temp_dir().join(format!("solstice-storage-{label}-{unique}"))
    }

    #[test]
    fn load_preserves_persisted_timer_snapshot() {
        let dir = temp_dir("load-timer-snapshot");
        fs::create_dir_all(&dir).unwrap();

        let json = serde_json::json!({
            "settings": {
                "work_duration_secs": 1500,
                "short_break_secs": 300,
                "long_break_secs": 900,
                "sessions_before_long_break": 4
            },
            "tasks": [],
            "current_task_id": "task-1",
            "timer_snapshot": {
                "phase": "short_break",
                "remaining_secs": 120,
                "total_secs": 300,
                "running": false,
                "session_count": 2,
                "current_task_id": "task-1"
            }
        })
        .to_string();
        fs::write(dir.join("data.json"), json).unwrap();

        let data = load(&dir);

        let snapshot = data.timer_snapshot.expect("expected timer snapshot");
        assert_eq!(snapshot.phase, TimerPhase::ShortBreak);
        assert_eq!(snapshot.remaining_secs, 120);
        assert_eq!(snapshot.total_secs, 300);
        assert_eq!(snapshot.session_count, 2);
        assert_eq!(snapshot.current_task_id.as_deref(), Some("task-1"));
    }

    #[test]
    fn save_persists_timer_snapshot() {
        let dir = temp_dir("save-timer-snapshot");
        let data = AppData {
            settings: Settings::default(),
            tasks: vec![Task {
                id: "task-1".to_string(),
                name: "Deep work".to_string(),
                total_sessions: 4,
                remaining_sessions: 3,
                completed: false,
                created_at: "2026-06-09T00:00:00Z".to_string(),
            }],
            current_task_id: Some("task-1".to_string()),
            timer_snapshot: Some(TimerSnapshot {
                phase: TimerPhase::Work,
                remaining_secs: 600,
                total_secs: 1500,
                running: true,
                session_count: 1,
                current_task_id: Some("task-1".to_string()),
            }),
        };

        save(&dir, &data).unwrap();
        let reloaded = load(&dir);

        let snapshot = reloaded.timer_snapshot.expect("expected timer snapshot");
        assert!(snapshot.running);
        assert_eq!(snapshot.remaining_secs, 600);
        assert_eq!(snapshot.total_secs, 1500);
    }
}