This should maybe work?

This commit is contained in:
Lucy 2022-02-02 16:54:08 -05:00
parent 7941c2f6a3
commit f9bea05bbb
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1

View file

@ -1,22 +1,58 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
use crate::task;
use crate::{task, widgets::SettingsEntry};
use cosmic_dbus_networkmanager::{
device::{wireless::WirelessDevice, SpecificDevice},
nm::NetworkManager,
};
use futures_util::StreamExt;
use gtk4::{
glib::{source::PRIORITY_DEFAULT, MainContext, Sender},
glib::{self, clone, source::PRIORITY_DEFAULT, MainContext, Sender},
prelude::*,
Align, Image,
};
use tokio::sync::mpsc::UnboundedSender;
use std::{cell::RefCell, rc::Rc};
use zbus::Connection;
pub fn add_available_wifi(target: &gtk4::Box) {
let ap_entries = Rc::<RefCell<Vec<SettingsEntry>>>::default();
let (tx, rx) = MainContext::channel::<Vec<AccessPoint>>(PRIORITY_DEFAULT);
task::spawn(scan_for_wifi(tx));
rx.attach(None, |aps| Continue(true));
task::spawn(async move {
if let Err(err) = scan_for_wifi(tx).await {
eprintln!("scan_for_wifi failed: {}", err);
}
});
rx.attach(
None,
clone!(@strong ap_entries, @weak target => @default-return Continue(true), move |aps| {
build_aps_list(ap_entries.clone(), target, dbg!(aps));
Continue(true)
}),
);
}
fn build_aps_list(
ap_entries: Rc<RefCell<Vec<SettingsEntry>>>,
target: gtk4::Box,
aps: Vec<AccessPoint>,
) {
let mut ap_entries = ap_entries.borrow_mut();
for old_ap_box in ap_entries.drain(..) {
target.remove(&old_ap_box);
}
for ap in aps {
view! {
ap_entry = SettingsEntry {
align_child: Align::Start,
set_title: &ap.ssid,
set_child: ap_icon = &Image {
set_icon_name: Some("network-wireless-symbolic"),
}
}
}
target.append(&ap_entry);
ap_entries.push(ap_entry);
}
}
async fn scan_for_wifi(tx: Sender<Vec<AccessPoint>>) -> zbus::Result<()> {
@ -45,6 +81,7 @@ async fn handle_wireless_device(
let mut scan_changed = device.receive_last_scan_changed().await;
if let Some(t) = scan_changed.next().await {
if let Ok(-1) = t.get().await {
eprintln!("scan errored");
return Ok(());
}
}
@ -56,9 +93,11 @@ async fn handle_wireless_device(
strength: ap.strength().await?,
});
}
tx.send(aps).expect("failed to send back to main thread");
Ok(())
}
#[derive(Debug)]
struct AccessPoint {
ssid: String,
strength: u8,