refactor: factor out notification sending

This commit is contained in:
Moritz Böhme 2023-07-29 17:14:41 +02:00
parent 82d94650a5
commit b53689584a
Signed by: moritz
GPG key ID: 970C6E89EB0547A9
4 changed files with 24 additions and 32 deletions

View file

@ -1,7 +1,7 @@
use crate::notification::send_notifictation;
use crate::pomodoro::Pomodoro;
pub use crate::timer::Timer;
use anyhow::Context;
use notify_rust::Notification;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::{
@ -100,21 +100,14 @@ impl Daemon {
}
if self.notify {
match Notification::new()
.summary("󰀠 Timers")
.body(
send_notifictation(
format!(
"Started timer {} for {}",
&name,
humantime::format_duration(duration)
)
.as_str(),
)
.show()
{
Ok(_) => println!("Sent notification sucessfully."),
Err(_) => println!("Failed to send notification."),
};
);
}
let timer = Timer::new(name, duration);
@ -135,14 +128,7 @@ impl Daemon {
long_pause,
pauses_till_long,
} => {
match Notification::new()
.summary("󰀠 Timers")
.body("Started pomodoro.")
.show()
{
Ok(_) => println!("Sent notification sucessfully."),
Err(_) => println!("Failed to send notification."),
};
send_notifictation("Started pomodoro.");
self.pomodoro = Some(Pomodoro::new(work, pause, long_pause, pauses_till_long));
Ok(Answer::Ok)
}

View file

@ -1,7 +1,8 @@
pub mod cli;
pub mod daemon;
pub mod pomodoro;
pub mod timer;
mod cli;
mod daemon;
mod pomodoro;
mod timer;
mod notification;
use crate::cli::{send_command, Cli, Command as CliCommand};
use crate::daemon::{Command as DaemonCommand, Daemon};

8
src/notification.rs Normal file
View file

@ -0,0 +1,8 @@
use notify_rust::Notification;
pub fn send_notifictation(msg: &str) {
match Notification::new().summary("󰀠 Timers").body(msg).show() {
Ok(_) => println!("Sent notification sucessfully."),
Err(_) => println!("Failed to send notification."),
};
}

View file

@ -1,4 +1,4 @@
use notify_rust::Notification;
use crate::notification::send_notifictation;
use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Formatter},
@ -74,10 +74,7 @@ impl Timer {
let msg = format!("Timer {} has expired!", self.name);
println!("{}", &msg);
if notify {
match Notification::new().summary("󰀠 Timers").body(&msg).show() {
Ok(_) => println!("Sent notification sucessfully."),
Err(_) => println!("Failed to send notification."),
}
send_notifictation(msg.as_str());
}
}
}