Skip to content

Commit

Permalink
Fix a few device view bugs
Browse files Browse the repository at this point in the history
- It was possible to spam multiple connection requests at the same time
- Manually disconnected devices disappeared until device view is reloaded
  • Loading branch information
azymohliad committed Oct 31, 2023
1 parent 14995c6 commit 7e7beed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 1 addition & 4 deletions watchmate/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use infinitime::{bluer, bt};
use std::{sync::Arc, path::PathBuf};
use futures::{pin_mut, StreamExt};
use gtk::{gio, glib, prelude::{ApplicationExt, BoxExt, GtkWindowExt, SettingsExt, WidgetExt}};
use gtk::{gio, glib, prelude::{ApplicationExt, BoxExt, GtkWindowExt, WidgetExt}};
use relm4::{
adw, gtk, actions::{AccelsPlus, RelmAction, RelmActionGroup},
Component, ComponentController, ComponentParts,
Expand Down Expand Up @@ -204,9 +204,6 @@ impl Component for Model {
)));
global_group.register_for_widget(&widgets.main_window);

// Post-initialization
model.devices.emit(devices::Input::SetAutoReconnect(persistent_settings.boolean("auto-reconnect-enabled")));

ComponentParts { model, widgets }
}

Expand Down
21 changes: 19 additions & 2 deletions watchmate/src/ui/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum Input {
DeviceSelected(i32),
DeviceConnected(Arc<bluer::Device>),
DeviceDisconnected(Arc<bluer::Device>),
DeviceDisconnecting(Arc<bluer::Device>),
DeviceConnectionFailed,
DeviceConnectionLost(bluer::Address),
SetAutoReconnect(bool),
Expand All @@ -46,6 +47,7 @@ pub struct Model {
discovery_task: Option<JoinHandle<()>>,
auto_reconnect: bool,
auto_reconnect_address: Option<bluer::Address>,
disconnecting_address: Option<bluer::Address>,
}

impl Model {
Expand Down Expand Up @@ -188,6 +190,7 @@ impl Component for Model {
.forward(sender.input_sender(), |output| match output {
DeviceOutput::Connected(device) => Input::DeviceConnected(device),
DeviceOutput::Disconnected(device) => Input::DeviceDisconnected(device),
DeviceOutput::Disconnecting(device) => Input::DeviceDisconnecting(device),
DeviceOutput::ConnectionFailed => Input::DeviceConnectionFailed,
});
let model = Self {
Expand All @@ -197,6 +200,7 @@ impl Component for Model {
discovery_task: None,
auto_reconnect: false,
auto_reconnect_address: None,
disconnecting_address: None,
};

let factory_widget = model.devices.widget();
Expand Down Expand Up @@ -281,7 +285,11 @@ impl Component for Model {
log::debug!("Device selected: {}", index);
sender.input(Input::StopDiscovery);
self.auto_reconnect_address = None;
self.devices.send(index as usize, DeviceInput::Connect);
if let Some(device) = self.devices.get(index as usize) {
if device.state != DeviceState::Transitioning {
self.devices.send(index as usize, DeviceInput::Connect);
}
}
}

Input::DeviceConnected(device) => {
Expand All @@ -295,9 +303,16 @@ impl Component for Model {
if Some(device.address()) == self.auto_reconnect_address {
self.auto_reconnect_address = None;
}
self.disconnecting_address = None;
// Repopulate known devices
sender.input(Input::StopDiscovery);
sender.input(Input::StartDiscovery);
}

Input::DeviceDisconnecting(device) => {
self.disconnecting_address = Some(device.address());
}

Input::DeviceConnectionFailed => {
log::debug!("Device connection failed");
sender.input(Input::StartDiscovery);
Expand All @@ -311,7 +326,7 @@ impl Component for Model {
if let Some((idx, _)) = result {
devices.send(idx, DeviceInput::StateUpdated(DeviceState::Disconnected));
}
if self.auto_reconnect {
if self.auto_reconnect && Some(address) != self.disconnecting_address {
self.auto_reconnect_address = Some(address);
sender.input(Input::StartDiscovery);
}
Expand Down Expand Up @@ -439,6 +454,7 @@ pub enum DeviceInput {
pub enum DeviceOutput {
Connected(Arc<bluer::Device>),
Disconnected(Arc<bluer::Device>),
Disconnecting(Arc<bluer::Device>),
ConnectionFailed,
}

Expand Down Expand Up @@ -551,6 +567,7 @@ impl FactoryComponent for DeviceInfo {
DeviceInput::Disconnect => {
self.state = DeviceState::Transitioning;
let device = self.device.clone();
_ = sender.output(DeviceOutput::Disconnecting(device.clone()));
relm4::spawn(async move {
match device.disconnect().await {
Ok(()) => {
Expand Down

0 comments on commit 7e7beed

Please sign in to comment.