refactor(network): update the icon name for disconnected state, and improve subscriptions

This commit is contained in:
Ashley Wulber 2023-11-01 14:45:48 -04:00 committed by Ashley Wulber
parent 2c9470bdd7
commit a9b82222c4
5 changed files with 56 additions and 27 deletions

View file

@ -9,16 +9,16 @@ use zbus::Connection;
pub fn devices_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
has_popup: bool,
conn: Connection,
) -> iced::Subscription<NetworkManagerEvent> {
let initial = State::Continue(conn.clone());
subscription::channel(id, 50, move |mut output| {
subscription::channel((id, has_popup), 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;
state = start_listening(state, has_popup, &mut output).await;
}
}
})
@ -32,6 +32,7 @@ pub enum State {
async fn start_listening(
state: State,
has_popup: bool,
output: &mut futures::channel::mpsc::Sender<NetworkManagerEvent>,
) -> State {
let conn = match state {
@ -47,11 +48,17 @@ async fn start_listening(
};
let mut devices_changed = network_manager.receive_devices_changed().await;
devices_changed.next().await;
let new_state = NetworkManagerState::new(&conn).await.unwrap_or_default();
_ = output
.send(NetworkManagerEvent::WirelessAccessPoints(new_state))
.await;
let secs = if has_popup { 4 } else { 60 };
while let (Some(_change), _) = tokio::join!(
devices_changed.next(),
tokio::time::sleep(tokio::time::Duration::from_secs(secs))
) {
let new_state = NetworkManagerState::new(&conn).await.unwrap_or_default();
_ = output
.send(NetworkManagerEvent::WirelessAccessPoints(new_state))
.await;
}
State::Continue(conn)
}