summaryrefslogtreecommitdiff
path: root/src-tauri/src/state.rs
blob: c6b4d035df10405d7e538653c67269730a3c3bd5 (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
use std::sync::{Arc, Mutex};
use serde::{Deserialize, Serialize};
use crate::storage::{AppData, TimerSnapshot};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TimerPhase {
    Work,
    ShortBreak,
    LongBreak,
}

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

/// Wrapper held in Tauri managed state — contains the Arc so commands can clone it.
pub struct TimerStateWrapper(pub Arc<Mutex<TimerState>>);

/// Wrapper for AppData — contains the Arc.
pub struct AppDataWrapper {
    pub data: Arc<Mutex<AppData>>,
}

impl TimerState {
    pub fn new(work_duration_secs: u64) -> Self {
        Self {
            phase: TimerPhase::Work,
            remaining_secs: work_duration_secs,
            total_secs: work_duration_secs,
            running: false,
            session_count: 0,
            current_task_id: None,
        }
    }

    pub fn from_snapshot(snapshot: TimerSnapshot) -> Self {
        Self {
            phase: snapshot.phase,
            remaining_secs: snapshot.remaining_secs,
            total_secs: snapshot.total_secs,
            running: snapshot.running,
            session_count: snapshot.session_count,
            current_task_id: snapshot.current_task_id,
        }
    }

    pub fn snapshot(&self) -> TimerSnapshot {
        TimerSnapshot {
            phase: self.phase,
            remaining_secs: self.remaining_secs,
            total_secs: self.total_secs,
            running: self.running,
            session_count: self.session_count,
            current_task_id: self.current_task_id.clone(),
        }
    }
}