[update/improve] Improve the notification cereation mecarthm

This commit is contained in:
Anas Elgarhy 2023-04-22 08:49:52 +02:00
parent 31946b66ca
commit 10543a2cc1
No known key found for this signature in database
GPG Key ID: 0501802A1D496528
2 changed files with 67 additions and 98 deletions

View File

@ -1,8 +1,8 @@
use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle};
use crate::{process_template_placeholders, settings};
use crate::cmus::{Track, TrackStatus};
use crate::cmus::player_settings::{AAAMode, PlayerSettings, Shuffle};
use crate::notification::Action;
use crate::settings::Settings;
use crate::{process_template_placeholders, settings};
#[derive(PartialEq)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
@ -20,61 +20,36 @@ impl CmusEvent {
pub fn build_notification(
&self,
settings: &Settings,
_notification: &mut notify_rust::Notification,
) -> Action {
macro_rules! build_the_notification {
($body_template: expr, $summary_template: expr, $track: expr, $player_settings: expr) => {
_notification.body(&process_template_placeholders(
$body_template,
$track,
$player_settings,
))
.summary(&process_template_placeholders(
$summary_template,
$track,
$player_settings,
));
};
}
use CmusEvent::*;
match self {
StatusChanged(track, player_settings) => {
build_the_notification!(settings.status_notification_body(), settings.status_notification_summary(), track, player_settings);
Action::Show
}
TrackChanged(track, player_settings) => {
build_the_notification!(settings.body(), settings.summary(), track, player_settings);
Action::Show
}
VolumeChanged(track, player_settings) if settings.show_player_notifications => {
build_the_notification!(settings.volume_notification_body(), settings.volume_notification_summary(), track, player_settings);
Action::Show
}
PositionChanged(_track, _player_settings) if settings.show_player_notifications => {
//build_the_notification!(settings.volume_notification_body(), settings.volume_notification_summary(), track, player_settings);
//TODO: Implement this
Action::None
}
ShuffleChanged(track, player_settings) if settings.show_player_notifications => {
build_the_notification!(settings.shuffle_notification_body(), settings.shuffle_notification_summary(), track, player_settings);
Action::Show
}
RepeatChanged(track, player_settings) if settings.show_player_notifications => {
build_the_notification!(settings.repeat_notification_body(), settings.repeat_notification_summary(), track, player_settings);
Action::Show
}
AAAModeChanged(track, player_settings) if settings.show_player_notifications => {
build_the_notification!(settings.aaa_mode_notification_body(), settings.aaa_mode_notification_summary(), track, player_settings);
Action::Show
}
_ => Action::None,
let (body_template, summary_template, track, player_settings) = match self {
StatusChanged(track, player_settings) =>
(settings.status_notification_body(), settings.status_notification_summary(), track, player_settings),
TrackChanged(track, player_settings) =>
(settings.body(), settings.summary(), track, player_settings),
VolumeChanged(track, player_settings) if settings.show_player_notifications =>
(settings.volume_notification_body(), settings.volume_notification_summary(), track, player_settings),
ShuffleChanged(track, player_settings) if settings.show_player_notifications =>
(settings.shuffle_notification_body(), settings.shuffle_notification_summary(), track, player_settings),
RepeatChanged(track, player_settings) if settings.show_player_notifications =>
(settings.repeat_notification_body(), settings.repeat_notification_summary(), track, player_settings),
AAAModeChanged(track, player_settings) if settings.show_player_notifications =>
(settings.aaa_mode_notification_body(), settings.aaa_mode_notification_summary(), track, player_settings),
_ => { return Action::None },
};
Action::Show {
notification_body: process_template_placeholders(
body_template,
track,
player_settings,
),
notification_summary: process_template_placeholders(
summary_template,
track,
player_settings,
),
save: false
}
}
}

View File

@ -1,30 +1,21 @@
use crate::cmus::events::CmusEvent;
use crate::cmus::player_settings::PlayerSettings;
use crate::cmus::query::CmusQueryResponse;
use crate::cmus::{TemplateProcessor, Track, TrackStatus};
use crate::settings::Settings;
use crate::{process_template_placeholders, settings, track_cover, TrackCover};
#[cfg(feature = "debug")]
use log::{debug, info};
use notify_rust::Notification;
pub enum Action {
Show,
Update,
None,
}
use crate::{process_template_placeholders, settings, track_cover, TrackCover};
use crate::cmus::{TemplateProcessor, Track, TrackStatus};
use crate::cmus::events::CmusEvent;
use crate::cmus::player_settings::PlayerSettings;
use crate::cmus::query::CmusQueryResponse;
use crate::settings::Settings;
impl Action {
pub fn max(self, other: Self) -> Self {
match (self, other) {
(Action::Show, Action::Show) => Action::Show,
(Action::Show, Action::Update) | (Action::Update, Action::Show | Action::Update) => {
Action::Update
}
(Action::None, _) => Action::None,
(_, Action::None) => Action::None,
}
}
pub enum Action {
Show {
notification_body: String,
notification_summary: String,
save: bool,
},
None,
}
pub struct NotificationsHandler {
@ -39,7 +30,7 @@ impl NotificationsHandler {
Self {
cover_set: false,
notification: Notification::new(),
handlers: Vec::new(),
handlers: Vec::with_capacity(2),
settings,
}
}
@ -50,28 +41,31 @@ impl NotificationsHandler {
events: Vec<CmusEvent>,
response: &CmusQueryResponse,
) -> Result<(), notify_rust::error::Error> {
// Setup the notification cover
if self.settings.show_track_cover {
self.update_cover(&events[0], response);
} else if self.settings.notification_static_cover.is_some() && !self.cover_set {
self.setup_the_notification();
self.notification
.image_path(self.settings.notification_static_cover.as_ref().unwrap());
self.cover_set = true;
}
for event in events {
self.setup_notification_timeout(&event);
#[cfg(feature = "debug")]
info!("event: {:?}", event);
let action = event.build_notification(&mut self.settings, &mut self.notification);
match event.build_notification(&self.settings) {
Action::Show { notification_body, notification_summary, save } => {
// Setup the notification cover
if self.settings.show_track_cover {
self.update_cover(&event, response);
} else if self.settings.notification_static_cover.is_some() && !self.cover_set {
self.setup_the_notification();
self.notification
.image_path(self.settings.notification_static_cover.as_ref().unwrap());
self.cover_set = true;
}
match action {
Action::Show => {
let _ = self.notification.show()?;
self.notification.summary(&notification_summary).body(&notification_body);
// Show the notification
let handle = self.notification.show()?;
if save {
self.handlers.push(handle);
}
}
Action::Update => todo!("Update notification"),
Action::None => {}
};
}
@ -80,10 +74,12 @@ impl NotificationsHandler {
}
#[inline(always)]
fn update_cover(&mut self, first_event: &CmusEvent, response: &CmusQueryResponse) {
fn update_cover(&mut self, event: &CmusEvent, response: &CmusQueryResponse) {
// If the track is changed, we need to update the cover.
match first_event {
match event {
CmusEvent::TrackChanged(track, _) => {
// Reset the notification
self.setup_the_notification();
self.set_cover(track);
}
_ => {
@ -99,8 +95,6 @@ impl NotificationsHandler {
#[inline]
fn set_cover(&mut self, track: &Track) {
// Reset the notification
self.setup_the_notification();
let path = match &self.settings.cover_path_template {
Some(template) => track.process(template.clone()),
None => track.path.clone(),
@ -142,7 +136,7 @@ impl NotificationsHandler {
match event {
TrackChanged(_, _) => self.settings.timeout(),
StatusChanged(_, _) => self.settings.status_notification_timeout(),
AAAMode(_, _) => self.settings.aaa_mode_notification_timeout(),
AAAModeChanged(_, _) => self.settings.aaa_mode_notification_timeout(),
VolumeChanged(_, _) => self.settings.volume_notification_timeout(),
RepeatChanged(_, _) => self.settings.repeat_notification_timeout(),
ShuffleChanged(_, _) => self.settings.shuffle_notification_timeout(),