refactor(battery): use channel subscription

This commit is contained in:
Ashley Wulber 2023-07-11 14:54:44 -04:00 committed by Jeremy Soller
parent ff6e9e3483
commit 8d9bb40b1b
5 changed files with 127 additions and 174 deletions

View file

@ -3,8 +3,7 @@
//! This code was generated by `zbus-xmlgen` `2.0.1` from DBus introspection data.
//! Source: `Interface '/org/freedesktop/UPower/KbdBacklight' from service 'org.freedesktop.UPower' on system bus`.
use cosmic::iced;
use iced::subscription;
use cosmic::iced::{self, futures::SinkExt, subscription};
use std::{fmt::Debug, hash::Hash};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use zbus::dbus_proxy;
@ -35,21 +34,14 @@ trait KbdBacklight {
pub fn kbd_backlight_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
) -> iced::Subscription<(I, KeyboardBacklightUpdate)> {
subscription::unfold(id, State::Ready, move |state| start_listening_loop(id, state))
}
) -> iced::Subscription<KeyboardBacklightUpdate> {
subscription::channel(id, 50, move |mut output| async move {
let mut state = State::Ready;
async fn start_listening_loop<I: Copy + Debug>(
id: I,
mut state: State,
) -> ((I, KeyboardBacklightUpdate), State) {
loop {
let (update, new_state) = start_listening(id, state).await;
state = new_state;
if let Some(update) = update {
return (update, state);
loop {
state = start_listening(state, &mut output).await;
}
}
})
}
#[derive(Debug)]
@ -62,38 +54,35 @@ pub enum State {
Finished,
}
async fn start_listening<I: Copy>(
id: I,
async fn start_listening(
state: State,
) -> (Option<(I, KeyboardBacklightUpdate)>, State) {
output: &mut futures::channel::mpsc::Sender<KeyboardBacklightUpdate>,
) -> State {
match state {
State::Ready => {
let conn = match zbus::Connection::system().await {
Ok(conn) => conn,
Err(_) => return (None, State::Finished),
Err(_) => return State::Finished,
};
let kbd_proxy = match KbdBacklightProxy::builder(&conn).build().await {
Ok(p) => p,
Err(_) => return (None, State::Finished),
Err(_) => return State::Finished,
};
let (tx, rx) = unbounded_channel();
let b = kbd_proxy.get_brightness().await.unwrap_or_default() as f64
/ kbd_proxy.get_max_brightness().await.unwrap_or(1) as f64;
(
Some((id, KeyboardBacklightUpdate::Init(tx, b))),
State::Waiting(kbd_proxy, rx),
)
_ = output.send(KeyboardBacklightUpdate::Init(tx, b)).await;
State::Waiting(kbd_proxy, rx)
}
State::Waiting(proxy, mut rx) => match rx.recv().await {
Some(req) => match req {
KeyboardBacklightRequest::Get => {
let b = proxy.get_brightness().await.unwrap_or_default() as f64
/ proxy.get_max_brightness().await.unwrap_or(1) as f64;
(
Some((id, KeyboardBacklightUpdate::Update(b))),
State::Waiting(proxy, rx),
)
_ = output.send(KeyboardBacklightUpdate::Update(b)).await;
State::Waiting(proxy, rx)
}
KeyboardBacklightRequest::Set(value) => {
if let Ok(max_brightness) = proxy.get_max_brightness().await {
@ -102,10 +91,10 @@ async fn start_listening<I: Copy>(
let _ = proxy.set_brightness(value).await;
}
(None, State::Waiting(proxy, rx))
State::Waiting(proxy, rx)
}
},
None => (None, State::Finished),
None => State::Finished,
},
State::Finished => iced::futures::future::pending().await,
}