From 68dec69d4c4d3d8cd60907e67b04ec5da7c0ef29 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 3 Jul 2023 19:48:08 -0400 Subject: [PATCH] feat: handling of notifications events --- cosmic-applet-notifications/src/main.rs | 15 +++++++++++- .../src/subscriptions/notifications.rs | 24 +++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/cosmic-applet-notifications/src/main.rs b/cosmic-applet-notifications/src/main.rs index f0030a92..88fbdee2 100644 --- a/cosmic-applet-notifications/src/main.rs +++ b/cosmic-applet-notifications/src/main.rs @@ -7,6 +7,7 @@ use cosmic::iced::{ window, Alignment, Application, Color, Command, Length, Subscription, }; use cosmic::iced_core::image; +use cosmic::iced_futures::subscription; use cosmic::iced_widget::button::StyleSheet; use cosmic_applet::{applet_button_theme, CosmicAppletHelper}; @@ -191,7 +192,19 @@ impl Application for Notifications { Command::none() } Message::NotificationEvent(e) => { - dbg!(e); + match e { + AppletEvent::Notification(n) => { + self.notifications.push(n); + } + AppletEvent::Replace(n) => { + if let Some(old) = self.notifications.iter_mut().find(|n| n.id == n.id) { + *old = n; + } + } + AppletEvent::Closed(id) => { + self.notifications.retain(|n| n.id != id); + } + } Command::none() } Message::Ignore => Command::none(), diff --git a/cosmic-applet-notifications/src/subscriptions/notifications.rs b/cosmic-applet-notifications/src/subscriptions/notifications.rs index 85781601..12633c79 100644 --- a/cosmic-applet-notifications/src/subscriptions/notifications.rs +++ b/cosmic-applet-notifications/src/subscriptions/notifications.rs @@ -117,21 +117,21 @@ pub fn notifications() -> Subscription { } State::WaitingForNotificationEvent(stream) => { info!("Waiting for notification event"); - let mut reader = BufReader::new(stream); + let reader = BufReader::new(stream); // todo read messages - let mut request_buf = String::with_capacity(1024); - if let Err(err) = reader.read_line(&mut request_buf).await { - error!("Failed to read line from stream {}", err); - continue; - } - let Ok(event) = ron::de::from_str::(request_buf.as_str()) else { - error!("Failed to deserialize event from notification stream"); - continue; - }; - if let Err(err) = output.send(event).await { - error!("Error sending event: {}", err); + let mut lines = reader.lines(); + while let Ok(Some(line)) = lines.next_line().await { + if let Ok(event) = ron::de::from_str::(line.as_str()) { + if let Err(_err) = output.send(event).await { + error!("Error sending event"); + } + } else { + error!("Failed to deserialize event from notification stream"); + } } + warn!("Notification stream closed"); + state = State::Finished; } State::Finished => { let () = futures::future::pending().await;