Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Feb 13, 2025
1 parent 479dbaa commit bb28e7e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/bluetooth/bluetooth_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn create_button<'a>(
Icon::Bluetooth,
),
BluetoothButtonVariant::Disconnect => (
BluetoothMsg::RemoveBluetoothDevice(value.path.clone()),
BluetoothMsg::DisconnectFromBluetoothDevice(value.path.clone()),
Icon::BluetoothConnected,
),
};
Expand All @@ -80,7 +80,11 @@ fn create_button<'a>(
.spacing(10),
ButtonVariant::Primary,
)
.on_press(ReSetMessage::SubMsgBluetooth(msg))
.on_press_maybe(if value.conect_in_progress {
None
} else {
Some(ReSetMessage::SubMsgBluetooth(msg))
})
.style(move |theme, state| {
let at = if length == 1 {
RowAt::Only
Expand Down
28 changes: 23 additions & 5 deletions src/bluetooth/bluetooth_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
icons::{icon_widget, Icon},
loading_spinner::Circular,
},
utils::ignore,
utils::{ignore, TToError},
ReSetMessage,
};

Expand Down Expand Up @@ -47,6 +47,8 @@ pub enum BluetoothMsg {
GetDefaultBluetoothAdapter,
StartBluetoothListener,
StopBluetoothListener,
StartBluetoothScan,
StopBluetoothScan,
SetBluetoothAdapter(zbus::zvariant::OwnedObjectPath),
SetBluetoothAdapterEnabled(zbus::zvariant::OwnedObjectPath, bool),
SetBluetoothAdapterDiscoverability(zbus::zvariant::OwnedObjectPath, bool),
Expand Down Expand Up @@ -134,15 +136,13 @@ impl<'a> BluetoothModel<'a> {
// is async in order to not block
self.proxy.start_bluetooth_listener().await?;
let func = async || -> ReSetMessage {
//sleep(Duration::from_millis(10_000)).await;
thread::sleep(Duration::from_millis(10_000));
wrap(BluetoothMsg::StopBluetoothListener)
wrap(BluetoothMsg::StopBluetoothScan)
};
Task::future(func())
}
BluetoothMsg::StopBluetoothListener => {
self.proxy.stop_bluetooth_listener().await?;
println!("pingpang");
self.is_scanning = false;
Task::none()
}
Expand Down Expand Up @@ -174,10 +174,18 @@ impl<'a> BluetoothModel<'a> {
Task::none()
}
BluetoothMsg::ConnectToBluetoothDevice(device) => {
self.devices
.get_mut(&device)
.to_zbus_error()?
.conect_in_progress = true;
self.proxy.connect_to_bluetooth_device(device).await?;
Task::none()
}
BluetoothMsg::DisconnectFromBluetoothDevice(device) => {
self.devices
.get_mut(&device)
.to_zbus_error()?
.conect_in_progress = true;
self.proxy.disconnect_from_bluetooth_device(device).await?;
Task::none()
}
Expand All @@ -198,6 +206,16 @@ impl<'a> BluetoothModel<'a> {
self.page_id = page_id;
Task::none()
}
BluetoothMsg::StartBluetoothScan => {
self.proxy.start_bluetooth_scan().await?;
self.is_scanning = true;
Task::none()
}
BluetoothMsg::StopBluetoothScan => {
self.proxy.stop_bluetooth_scan().await?;
self.is_scanning = false;
Task::none()
}
};
Ok(task)
}
Expand Down Expand Up @@ -237,7 +255,7 @@ impl<'a> BluetoothModel<'a> {
.on_press_maybe(if self.is_scanning {
None
} else {
Some(wrap(BluetoothMsg::StartBluetoothListener))
Some(wrap(BluetoothMsg::StartBluetoothScan))
})
.width(Length::Fill),
bluetooth_device_buttons(
Expand Down
7 changes: 7 additions & 0 deletions src/bluetooth/dbus_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub trait TPath {
}

#[derive(Debug, Clone, Default, Deserialize, Serialize, Type)]
#[zvariant(signature = "(onssobbbbbss)")]
pub struct BluetoothDevice {
pub path: OwnedObjectPath,
pub rssi: i16,
Expand All @@ -22,6 +23,11 @@ pub struct BluetoothDevice {
pub connected: bool,
pub icon: String,
pub address: String,
// Internal state, not sent or received
// Always set to false
#[zvariant(signature = "")]
#[serde(skip_serializing, default)]
pub conect_in_progress: bool,
}

impl TPath for BluetoothDevice {
Expand Down Expand Up @@ -53,6 +59,7 @@ impl TPath for BluetoothAdapter {
)]
pub trait BluetoothDbus {
fn start_bluetooth_scan(&self) -> zbus::Result<()>;
fn stop_bluetooth_scan(&self) -> zbus::Result<()>;
fn start_bluetooth_listener(&self) -> zbus::Result<()>;
fn stop_bluetooth_listener(&self) -> zbus::Result<()>;
fn get_bluetooth_adapters(&self) -> zbus::Result<Vec<BluetoothAdapter>>;
Expand Down
14 changes: 14 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ pub fn rounded_card(theme: &Theme) -> Style {
..Style::default()
}
}

pub trait TToError<T> {
fn to_zbus_error(self) -> Result<T, zbus::Error>;
}

impl<T> TToError<T> for Option<T> {
fn to_zbus_error(self) -> Result<T, zbus::Error> {
if let Some(value) = self {
Ok(value)
} else {
Err(zbus::Error::Failure("Error".to_string()))
}
}
}

0 comments on commit bb28e7e

Please sign in to comment.