aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: af1eea7d5873f33c2acad810d0e907300d8d1efa (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
use std::{
    sync::{Arc, Mutex},
    time::Duration,
};

use pausable_clock::{PausableClock, PausableInstant};
use serde::{Serialize, Deserialize};
use zbus::{dbus_interface, ConnectionBuilder, Result};

use notify_rust::Notification;

#[derive(Serialize, Deserialize, Clone, Copy)]
struct PomdConfig {
    work_duration: f32,
    short_break_duration: f32,
    long_break_duration: f32,
    num_iterations: u8,
    notify: bool,
}

impl Default for PomdConfig {
    fn default() -> Self {
        Self {
            work_duration: 15.0 * 60.0,
            short_break_duration: 5.0 * 60.0,
            long_break_duration: 25.0 * 60.0,
            num_iterations: 4,
            notify: true,
        }
    }
}


struct Pomd {
    config: PomdConfig,
    duration: Duration,
    iteration: u8,
    on_break: bool,
    clock: PausableClock,
    start: PausableInstant
}

struct PomdInterface {
    data: Arc<Mutex<Pomd>>,
    config: PomdConfig
}

impl PomdInterface {
    fn new(config: PomdConfig) -> Self {
        Self { 
            data: Arc::new(Mutex::new(Pomd::new(config))),
            config,
        }
    }
}

#[dbus_interface(name = "dev.exvacuum.pomd")]
impl PomdInterface {
    async fn get_remaining(&self) -> Duration {
        let data = self.data.lock().unwrap();
        data.duration.checked_sub(data.start.elapsed(&data.clock)).unwrap_or_default()
    }

    async fn get_iteration(&self) -> u8 {
        self.data.lock().unwrap().iteration
    }

    async fn is_running(&self) -> bool {
        !self.data.lock().unwrap().clock.is_paused()
    }

    async fn is_on_break(&self) -> bool {
        self.data.lock().unwrap().on_break
    }

    async fn start(&self) {
        self.data.lock().unwrap().clock.resume();
    }

    async fn pause(&self) {
        self.data.lock().unwrap().clock.pause();
    }

    async fn stop(&self) {
        *self.data.lock().unwrap() = Pomd::new(self.config);
    }

    async fn skip(&self) {
        self.data.lock().unwrap().setup_next_iteration();
    }
}

impl  Pomd {
    fn new(config: PomdConfig) -> Self {
        let clock = PausableClock::new(Duration::ZERO, true);
        let start  = clock.now();
        Self {
            config,
            duration: Duration::from_secs_f32(config.work_duration),
            iteration: 0,
            on_break: false,
            clock,
            start,
        }
    }
}

impl Pomd {
    fn update(&mut self) {
        if self.duration < self.start.elapsed(&self.clock) {
                if self.config.notify {
                    self.notify();
                }
                self.setup_next_iteration();
        }
    }

    fn setup_next_iteration(&mut self) {
        self.clock.pause();
        self.start  = self.clock.now();
        self.on_break ^= true;
        self.duration = if self.on_break {
            if self.iteration == self.config.num_iterations - 1 {
                Duration::from_secs_f32(self.config.long_break_duration)
            } else {
                Duration::from_secs_f32(self.config.short_break_duration)
            }
        } else {
            self.iteration = (self.iteration + 1) % self.config.num_iterations;
            Duration::from_secs_f32(self.config.work_duration)
        }
    }

    fn notify(&self) {
        if self.on_break {
            Notification::new()
                .summary("Break Complete")
                .body("Click to dismiss")
                .show()
                .unwrap();
        } else {
            Notification::new()
                .summary(&format!(
                    "Pomodoro Complete ({}/{})",
                    self.iteration + 1,
                    self.config.num_iterations
                ))
                .body("Click to dismiss")
                .show()
                .unwrap();
        }
    }
}

#[async_std::main]
async fn main() -> Result<()> {
    let config: PomdConfig = confy::load("pomd", "config").expect("Failed to load config!");
    let pomd_interface = PomdInterface::new(config);
    let pomd = pomd_interface.data.clone();
    let _connection = ConnectionBuilder::session()?
        .name("dev.exvacuum.pomd")?
        .serve_at("/dev/exvacuum/pomd", pomd_interface)?
        .build()
        .await?;
    loop {
        pomd.lock().unwrap().update();
    }
}