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

View file

@ -1,7 +1,8 @@
pub mod cli; mod cli;
pub mod daemon; mod daemon;
pub mod pomodoro; mod pomodoro;
pub mod timer; mod timer;
mod notification;
use crate::cli::{send_command, Cli, Command as CliCommand}; use crate::cli::{send_command, Cli, Command as CliCommand};
use crate::daemon::{Command as DaemonCommand, Daemon}; 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 serde::{Deserialize, Serialize};
use std::{ use std::{
fmt::{Display, Formatter}, fmt::{Display, Formatter},
@ -74,10 +74,7 @@ impl Timer {
let msg = format!("Timer {} has expired!", self.name); let msg = format!("Timer {} has expired!", self.name);
println!("{}", &msg); println!("{}", &msg);
if notify { if notify {
match Notification::new().summary("󰀠 Timers").body(&msg).show() { send_notifictation(msg.as_str());
Ok(_) => println!("Sent notification sucessfully."),
Err(_) => println!("Failed to send notification."),
}
} }
} }
} }