Skip to content

Commit

Permalink
feat: use gui_status for various gui states
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Oct 15, 2022
1 parent 90e26c3 commit 9e9d515
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 229 deletions.
4 changes: 1 addition & 3 deletions src/app_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ pub struct AppData {
args: CliArgs,
error: Option<AppError>,
logs_parsed: bool,
pub containers: StatefulList<ContainerItem>,
// pub init: bool,
// pub show_error: bool,
sorted_by: Option<(Header, SortedOrder)>,
pub containers: StatefulList<ContainerItem>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down
87 changes: 49 additions & 38 deletions src/docker_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bollard::{
service::ContainerSummary,
Docker,
};
use futures_util::StreamExt;
use futures_util::{Future, StreamExt};
use parking_lot::Mutex;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -318,6 +318,7 @@ impl DockerData {

// Initialize docker container data, before any messages are received
async fn initialise_container_data(&mut self) {
self.gui_state.lock().status_push(Status::Init);
let loading_uuid = Uuid::new_v4();
let loading_spin = self.loading_spin(loading_uuid).await;

Expand All @@ -336,8 +337,7 @@ impl DockerData {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
self.initialised = self.app_data.lock().initialised(&all_ids);
}
// self.app_data.lock().init = true;
self.gui_state.lock().set_status(Status::Normal);
self.gui_state.lock().status_del(Status::Init);
self.stop_loading_spin(&loading_spin, loading_uuid);
}

Expand All @@ -346,57 +346,68 @@ impl DockerData {
self.app_data
.lock()
.set_error(AppError::DockerCommand(error));
self.gui_state.lock().set_status(Status::Error);
self.gui_state.lock().status_push(Status::Error);
}

/// Execute docker commands, will start and stop the loading spinner
async fn exec_docker(
&mut self,
docker_fn: impl Future<Output = Result<(), bollard::errors::Error>>,
uuid: Uuid,
control: DockerControls,
) {
let loading_spin = self.loading_spin(uuid).await;
if docker_fn.await.is_err() {
self.set_error(control);
};
self.stop_loading_spin(&loading_spin, uuid);
}

/// Handle incoming messages, container controls & all container information update
async fn message_handler(&mut self) {
while let Some(message) = self.receiver.recv().await {
let docker = Arc::clone(&self.docker);
let loading_uuid = Uuid::new_v4();
let docker = Arc::clone(&self.docker);
match message {
DockerMessage::Pause(id) => {
let loading_spin = self.loading_spin(loading_uuid).await;
if docker.pause_container(id.get()).await.is_err() {
self.set_error(DockerControls::Pause);
};
self.stop_loading_spin(&loading_spin, loading_uuid);
self.exec_docker(
docker.pause_container(id.get()),
loading_uuid,
DockerControls::Pause,
)
.await;
}
DockerMessage::Restart(id) => {
// DEBUG
// self.set_error(DockerControls::Restart);
// DEBUG

let loading_spin = self.loading_spin(loading_uuid).await;
if docker.restart_container(id.get(), None).await.is_err() {
self.set_error(DockerControls::Restart);
};
self.stop_loading_spin(&loading_spin, loading_uuid);
self.exec_docker(
docker.restart_container(id.get(), None),
loading_uuid,
DockerControls::Restart,
)
.await;
}
DockerMessage::Start(id) => {
let loading_spin = self.loading_spin(loading_uuid).await;
if docker
.start_container(id.get(), None::<StartContainerOptions<String>>)
.await
.is_err()
{
self.set_error(DockerControls::Start);
};
self.stop_loading_spin(&loading_spin, loading_uuid);
self.exec_docker(
docker.start_container(id.get(), None::<StartContainerOptions<String>>),
loading_uuid,
DockerControls::Start,
)
.await;
}
DockerMessage::Stop(id) => {
let loading_spin = self.loading_spin(loading_uuid).await;
if docker.stop_container(id.get(), None).await.is_err() {
self.set_error(DockerControls::Stop);
};
self.stop_loading_spin(&loading_spin, loading_uuid);
self.exec_docker(
docker.stop_container(id.get(), None),
loading_uuid,
DockerControls::Stop,
)
.await;
}
DockerMessage::Unpause(id) => {
let loading_spin = self.loading_spin(loading_uuid).await;
if docker.unpause_container(id.get()).await.is_err() {
self.set_error(DockerControls::Unpause);
};
self.stop_loading_spin(&loading_spin, loading_uuid);
self.exec_docker(
docker.unpause_container(id.get()),
loading_uuid,
DockerControls::Unpause,
)
.await;
self.update_everything().await;
}
DockerMessage::Update => self.update_everything().await,
Expand Down
Loading

0 comments on commit 9e9d515

Please sign in to comment.