summaryrefslogtreecommitdiff
path: root/src-tauri/src/timer.rs
blob: 02ae285e1bc50bbbdecc17c869c79d84d086e1ae (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
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use serde::Serialize;
use tauri::{AppHandle, Emitter};

use crate::audio::{self, AudioEngine};
use crate::state::{TimerPhase, TimerState};
use crate::storage::{self, AppData};

fn persist_timer_state(data: &mut AppData, timer: &TimerState, data_dir: &std::path::PathBuf) {
    data.current_task_id = timer.current_task_id.clone();
    data.timer_snapshot = Some(timer.snapshot());
    let _ = storage::save(data_dir, data);
}

// LOCK ORDERING: When acquiring multiple locks, always take timer_state
// before app_data to prevent deadlock. Never acquire app_data first
// while timer_state is not held.

// ── Event payloads ──────────────────────────────────────────────────────────

#[derive(Clone, Serialize)]
pub struct TickPayload {
    pub phase: TimerPhase,
    pub remaining_secs: u64,
    pub total_secs: u64,
    pub session_count: u32,
    pub current_task_id: Option<String>,
}

#[derive(Clone, Serialize)]
pub struct PhaseChangedPayload {
    pub phase: TimerPhase,
    pub session_count: u32,
}

#[derive(Clone, Serialize)]
pub struct CompletedPayload {
    pub task_id: Option<String>,
}

// ── Timer thread ────────────────────────────────────────────────────────────

/// Spawns the background timer thread.  The thread runs for the lifetime of
/// the application; it sleeps while `running == false`.
pub fn spawn_timer_thread(
    app_handle: AppHandle,
    timer_arc: Arc<Mutex<TimerState>>,
    data_arc: Arc<Mutex<AppData>>,
    audio_arc: Arc<Mutex<Option<AudioEngine>>>,
    data_dir: std::path::PathBuf,
) {
    thread::spawn(move || {
        loop {
            thread::sleep(Duration::from_secs(1));

            let mut ts = timer_arc.lock().unwrap();

            if !ts.running {
                continue;
            }

            // Decrement
            if ts.remaining_secs > 0 {
                ts.remaining_secs -= 1;
            }

            // Emit tick
            let tick = TickPayload {
                phase: ts.phase,
                remaining_secs: ts.remaining_secs,
                total_secs: ts.total_secs,
                session_count: ts.session_count,
                current_task_id: ts.current_task_id.clone(),
            };

            {
                let mut data = data_arc.lock().unwrap();
                persist_timer_state(&mut data, &ts, &data_dir);
            }

            let _ = app_handle.emit("timer-tick", tick);

            // Check if phase has reached zero
            if ts.remaining_secs == 0 {
                ts.running = false;
                let mut emit_completed = false;
                let mut completed_task_id = None;

                match ts.phase {
                    TimerPhase::Work => {
                        // Decrement task remaining sessions
                        let task_id = ts.current_task_id.clone();
                        {
                            let mut data = data_arc.lock().unwrap();
                            if let Some(ref tid) = task_id {
                                if let Some(task) = data.tasks.iter_mut().find(|t| &t.id == tid) {
                                    if task.remaining_sessions > 0 {
                                        task.remaining_sessions -= 1;
                                    }
                                    if task.remaining_sessions == 0 {
                                        task.completed = true;
                                    }
                                }
                            }
                        }
                        emit_completed = true;
                        completed_task_id = task_id;

                        ts.session_count += 1;

                        // Determine next phase
                        let (next_phase, next_duration) = {
                            let data = data_arc.lock().unwrap();
                            let s = &data.settings;
                            if ts.session_count % s.sessions_before_long_break == 0 {
                                (TimerPhase::LongBreak, s.long_break_secs)
                            } else {
                                (TimerPhase::ShortBreak, s.short_break_secs)
                            }
                        };

                        ts.phase = next_phase;
                        ts.total_secs = next_duration;
                        ts.remaining_secs = next_duration;
                    }

                    TimerPhase::ShortBreak | TimerPhase::LongBreak => {
                        // Transition back to work
                        let work_secs = {
                            let data = data_arc.lock().unwrap();
                            data.settings.work_duration_secs
                        };
                        ts.phase = TimerPhase::Work;
                        ts.total_secs = work_secs;
                        ts.remaining_secs = work_secs;
                    }
                }

                {
                    let mut data = data_arc.lock().unwrap();
                    persist_timer_state(&mut data, &ts, &data_dir);
                }

                // Emit phase-changed after state is updated
                if let Some(engine) = audio_arc.lock().unwrap().as_mut() {
                    if audio::should_duck_for_phase(ts.phase) {
                        engine.duck();
                    } else {
                        engine.unduck();
                    }
                }

                if emit_completed {
                    let completed = CompletedPayload {
                        task_id: completed_task_id,
                    };
                    let _ = app_handle.emit("timer-completed", completed);
                }

                let phase_changed = PhaseChangedPayload {
                    phase: ts.phase,
                    session_count: ts.session_count,
                };
                let _ = app_handle.emit("timer-phase-changed", phase_changed);
            }
        }
    });
}