Skip to content

Commit

Permalink
refresh toolbox list every 10s #5
Browse files Browse the repository at this point in the history
  • Loading branch information
13hannes11 committed Jun 19, 2022
1 parent b81a5d5 commit 7a201d1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/ui/app/messages.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use relm4::factory::DynamicIndex;

use crate::toolbx::ToolbxContainer;

use super::model::ToolbxEntry;

pub enum AppMsg {
ShowToolboxSettingsRequest,
ShowToolboxAppsRequest,
ToolbxListUpdate(Vec<ToolbxContainer>),
ToolbxContainerToggleStartStop(DynamicIndex),
OpenToolbxTerminal(DynamicIndex),
ToolbxContainerChanged(DynamicIndex, ToolbxEntry),
Expand Down
4 changes: 4 additions & 0 deletions src/ui/app/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl AppUpdate for AppModel {
toolbx_container.update_entry(container);
}
}
AppMsg::ToolbxListUpdate(tbx_vec) => {
println!("Updating Toolbox List");
self.update_toolbxes(tbx_vec.into_iter());
}

AppMsg::OpenToolbxTerminal(index) => {
if let Some(toolbx_container) = self.toolboxes.get_mut(index.current_index()) {
Expand Down
18 changes: 17 additions & 1 deletion src/ui/app/workers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::time::Duration;

use relm4::factory::DynamicIndex;
use relm4::{send, MessageHandler, Sender};
use tokio::runtime::{Builder, Runtime};
use tokio::sync::mpsc::{channel, Sender as TokioSender};

use crate::toolbx::ToolbxContainer;

use super::{
messages::AppMsg,
model::{AppModel, ToolbxEntry},
Expand All @@ -18,6 +22,7 @@ pub struct AsyncHandler {
pub enum AsyncHandlerMsg {
StopToolbx(DynamicIndex, ToolbxEntry),
StartToolbx(DynamicIndex, ToolbxEntry),
UpdateToolbxes,
}

impl MessageHandler<AppModel> for AsyncHandler {
Expand All @@ -37,8 +42,11 @@ impl MessageHandler<AppModel> for AsyncHandler {
while let Some(msg) = rx.recv().await {
let parent_sender = parent_sender.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
match msg {
AsyncHandlerMsg::UpdateToolbxes => {
let toolboxes = ToolbxContainer::get_toolboxes();
send! {parent_sender, AppMsg::ToolbxListUpdate(toolboxes)};
}
AsyncHandlerMsg::StopToolbx(index, mut tbx) => {
tbx.toolbx_container.stop();
tbx.changing_status = false;
Expand All @@ -54,6 +62,14 @@ impl MessageHandler<AppModel> for AsyncHandler {
}
});

let _sender = sender.clone();
rt.spawn(async move {
loop {
tokio::time::sleep(Duration::from_secs(10)).await;
_sender.send(AsyncHandlerMsg::UpdateToolbxes).await;
}
});

AsyncHandler { _rt: rt, sender }
}

Expand Down

0 comments on commit 7a201d1

Please sign in to comment.