Skip to content

Commit

Permalink
Watch net status change.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Apr 27, 2023
1 parent 95e9188 commit dd9c096
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
5 changes: 5 additions & 0 deletions tunet-cui/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Event {
}

pub fn start(&self) {
self.spawn_watch_status();
self.spawn_timer();
self.spawn_online();
self.spawn_details();
Expand Down Expand Up @@ -79,6 +80,10 @@ impl Event {
});
}

fn spawn_watch_status(&self) {
self.model.queue(Action::WatchStatus);
}

fn spawn_timer(&self) {
self.model.queue(Action::Timer);
}
Expand Down
6 changes: 3 additions & 3 deletions tunet-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ async fn main() -> Result<()> {
if let Ok(cred) = settings_reader.read_with_password() {
model.queue(Action::Credential(Arc::new(cred)));
}
model.queue(Action::WatchStatus);
model.queue(Action::Timer);

home_model.set_status(model.status.to_string().into());
Expand Down Expand Up @@ -335,11 +336,10 @@ fn update(
UpdateMsg::Status => {
model.queue(Action::State(None));

let status = model.status.clone();
let status = model.status.to_string();
weak_app
.upgrade_in_event_loop(move |app| {
app.global::<HomeModel>()
.set_status(status.to_string().into());
app.global::<HomeModel>().set_status(status.into());
})
.unwrap();
}
Expand Down
32 changes: 25 additions & 7 deletions tunet-model/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![forbid(unsafe_code)]

use drop_guard::guard;
use futures_util::{pin_mut, TryStreamExt};
use futures_util::{pin_mut, StreamExt, TryStreamExt};
use itertools::Itertools;
use mac_address::*;
use netstatus::*;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Model {
let http = self.http.clone();
let status = self.status.clone();
tokio::spawn(async move {
let state = suggest::suggest_with_status(&http, status).await;
let state = suggest::suggest_with_status(&http, &status).await;
tx.send(Action::State(Some(state))).await.ok()
});
}
Expand All @@ -133,6 +133,16 @@ impl Model {
}
};
}
Action::WatchStatus => {
self.spawn_watch_status();
}
Action::Status => {
let status = NetStatus::current();
if status != self.status {
self.status = status;
self.update(UpdateMsg::Status);
}
}
Action::Timer => {
self.spawn_timer();
}
Expand All @@ -154,11 +164,6 @@ impl Model {
self.spawn_logout();
}
Action::Flux => {
let status = NetStatus::current();
if status != self.status {
self.status = status;
self.update(UpdateMsg::Status);
}
self.spawn_flux();
}
Action::LoginDone(s) | Action::LogoutDone(s) => {
Expand Down Expand Up @@ -224,6 +229,17 @@ impl Model {
}
}

fn spawn_watch_status(&self) {
let tx = self.tx.clone();
tokio::spawn(async move {
let mut events = NetStatus::watch();
while let Some(()) = events.next().await {
tx.send(Action::Status).await?;
}
Ok::<_, anyhow::Error>(())
});
}

fn spawn_timer(&self) {
let tx = self.tx.clone();
tokio::spawn(async move {
Expand Down Expand Up @@ -363,6 +379,8 @@ pub enum Action {
Credential(Arc<NetCredential>),
UpdateCredential(String, String),
State(Option<NetState>),
WatchStatus,
Status,
Timer,
Tick,
Login,
Expand Down
4 changes: 2 additions & 2 deletions tunet-suggest/src/ssid_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ static SUGGEST_SSID_MAP: Lazy<BTreeMap<&'static str, NetState>> = Lazy::new(|| {
});

pub async fn suggest(client: &HttpClient) -> NetState {
suggest_with_status(client, NetStatus::current()).await
suggest_with_status(client, &NetStatus::current()).await
}

pub async fn suggest_with_status(client: &HttpClient, s: NetStatus) -> NetState {
pub async fn suggest_with_status(client: &HttpClient, s: &NetStatus) -> NetState {
let state = match s {
NetStatus::Unknown => None,
NetStatus::Wwan => Some(NetState::Unknown),
Expand Down

0 comments on commit dd9c096

Please sign in to comment.