cosmic-applets/cosmic-applet-network/src/network_manager/active_conns.rs
Ashley Wulber 3ad64df5f3
Refactor icons jammy (#108)
* chore: add icons and installation

* refactor: use the new battery icons

* refactor: use audio applet icons

* chore dependency cleanup

* chore: remove icons included in cosmic-icons package

* refactor: use icons for signal strength and fix handling of new connections

* fix: improve some logic for the networking applet

* chore: add cosmic-icons to Depends

* feat: use notifications icons

* chore: set bluetooth icon depending on state

* fix: set default font and text size

* feat (network): add airplane-mode message

* feat (battery): add display icons to the battery applet

* fix (battery): logic for selecting the battery icon & use new battery icons
2023-08-08 16:18:12 -04:00

58 lines
1.7 KiB
Rust

use super::{NetworkManagerEvent, NetworkManagerState};
use cosmic::iced::{self, subscription};
use cosmic_dbus_networkmanager::nm::NetworkManager;
use futures::{SinkExt, StreamExt};
use log::error;
use std::fmt::Debug;
use std::hash::Hash;
use zbus::Connection;
pub fn active_conns_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
conn: Connection,
) -> iced::Subscription<NetworkManagerEvent> {
let initial = State::Continue(conn.clone());
subscription::channel(id, 50, move |mut output| {
let mut state = initial.clone();
async move {
loop {
state = start_listening(state, &mut output).await;
_ = tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
}
}
})
}
#[derive(Debug, Clone)]
pub enum State {
Continue(Connection),
Error,
}
async fn start_listening(
state: State,
output: &mut futures::channel::mpsc::Sender<NetworkManagerEvent>,
) -> State {
let conn = match state {
State::Continue(conn) => conn,
State::Error => iced::futures::future::pending().await,
};
let network_manager = match NetworkManager::new(&conn).await {
Ok(n) => n,
Err(e) => {
error!("Failed to connect to NetworkManager: {}", e);
return State::Error;
}
};
let mut active_conns_changed = network_manager.receive_active_connections_changed().await;
active_conns_changed.next().await;
let new_state = NetworkManagerState::new(&conn).await.unwrap_or_default();
_ = output
.send(NetworkManagerEvent::ActiveConns(new_state))
.await;
State::Continue(conn)
}