Skip to content

Commit 68dec69

Browse files
committed
feat: handling of notifications events
1 parent 060b316 commit 68dec69

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

cosmic-applet-notifications/src/main.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use cosmic::iced::{
77
window, Alignment, Application, Color, Command, Length, Subscription,
88
};
99
use cosmic::iced_core::image;
10+
use cosmic::iced_futures::subscription;
1011
use cosmic::iced_widget::button::StyleSheet;
1112
use cosmic_applet::{applet_button_theme, CosmicAppletHelper};
1213

@@ -191,7 +192,19 @@ impl Application for Notifications {
191192
Command::none()
192193
}
193194
Message::NotificationEvent(e) => {
194-
dbg!(e);
195+
match e {
196+
AppletEvent::Notification(n) => {
197+
self.notifications.push(n);
198+
}
199+
AppletEvent::Replace(n) => {
200+
if let Some(old) = self.notifications.iter_mut().find(|n| n.id == n.id) {
201+
*old = n;
202+
}
203+
}
204+
AppletEvent::Closed(id) => {
205+
self.notifications.retain(|n| n.id != id);
206+
}
207+
}
195208
Command::none()
196209
}
197210
Message::Ignore => Command::none(),

cosmic-applet-notifications/src/subscriptions/notifications.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,21 @@ pub fn notifications() -> Subscription<AppletEvent> {
117117
}
118118
State::WaitingForNotificationEvent(stream) => {
119119
info!("Waiting for notification event");
120-
let mut reader = BufReader::new(stream);
120+
let reader = BufReader::new(stream);
121121
// todo read messages
122-
let mut request_buf = String::with_capacity(1024);
123-
if let Err(err) = reader.read_line(&mut request_buf).await {
124-
error!("Failed to read line from stream {}", err);
125-
continue;
126-
}
127-
let Ok(event) = ron::de::from_str::<AppletEvent>(request_buf.as_str()) else {
128-
error!("Failed to deserialize event from notification stream");
129-
continue;
130-
};
131122

132-
if let Err(err) = output.send(event).await {
133-
error!("Error sending event: {}", err);
123+
let mut lines = reader.lines();
124+
while let Ok(Some(line)) = lines.next_line().await {
125+
if let Ok(event) = ron::de::from_str::<AppletEvent>(line.as_str()) {
126+
if let Err(_err) = output.send(event).await {
127+
error!("Error sending event");
128+
}
129+
} else {
130+
error!("Failed to deserialize event from notification stream");
131+
}
134132
}
133+
warn!("Notification stream closed");
134+
state = State::Finished;
135135
}
136136
State::Finished => {
137137
let () = futures::future::pending().await;

0 commit comments

Comments
 (0)