Skip to content

Commit

Permalink
Use is_busy for job update empty tables
Browse files Browse the repository at this point in the history
  • Loading branch information
TaQuangKhoi committed Aug 15, 2024
1 parent dde5cea commit f0a33db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
15 changes: 11 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct TwoDBApp {

pub is_busy_old: bool, // This field is for Spinner

pub is_busy: Arc<Mutex<bool>>,
pub is_busy: Arc<Mutex<bool>>, // for synchronize thread
pub field: Arc<Mutex<i128>>,
}

Expand All @@ -41,7 +41,14 @@ impl TwoDBApp {
// Note that you must enable the `persistence` feature for this to work.
if let Some(storage) = cc.storage {
let mut app: TwoDBApp = eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();

Check failure on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Check

variable does not need to be mutable

Check failure on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

variable does not need to be mutable

Check failure on line 43 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable
app.is_busy_old = false;

{
println!("App loaded");
/// Reset is_busy to false

Check failure on line 47 in src/app.rs

View workflow job for this annotation

GitHub Actions / Check

unused doc comment

Check failure on line 47 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused doc comment

Check failure on line 47 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused doc comment
let is_busy = app.is_busy.clone();
*is_busy.lock().unwrap() = false;
}

return app;
}

Expand Down Expand Up @@ -78,7 +85,7 @@ impl eframe::App for TwoDBApp {
self.render_get_empty_tables_button(ui);
});

if self.is_busy_old.eq(&true) {
if self.is_busy.lock().unwrap().clone() {
ui.add(egui::Spinner::new());
}

Expand Down Expand Up @@ -126,8 +133,8 @@ impl eframe::App for TwoDBApp {
}

/// Called by the framework to save state before shutdown.
/// Notes by Keios: this fn can be call despite the app is not closing
fn save(&mut self, storage: &mut dyn eframe::Storage) {
self.is_busy_old = false;
eframe::set_value(storage, eframe::APP_KEY, self);
}
}
Expand Down
28 changes: 15 additions & 13 deletions src/widgets/update_empty_tables_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ impl TwoDBApp {
}

fn get_empty_tables_event(&mut self) {
let field = self.field.clone();
let is_busy = self.is_busy.clone();
*is_busy.lock().unwrap() = true;
thread::spawn(move || {
*field.lock().unwrap() += 1;
let mut thread_toasts = Toasts::new()
.anchor(Align2::RIGHT_BOTTOM, (-10.0, -10.0)) // 10 units from the bottom right corner
.direction(egui::Direction::BottomUp);
// let mut thread_toasts = Toasts::new()
// .anchor(Align2::RIGHT_BOTTOM, (-10.0, -10.0)) // 10 units from the bottom right corner
// .direction(egui::Direction::BottomUp);

let database_name_source = var("POSTGRES_DB_SOURCE").unwrap_or(String::from(""));
update_empty_tables(&database_name_source);
Expand All @@ -30,14 +30,16 @@ impl TwoDBApp {
update_empty_tables(&database_name_target);

let text = format!("Done Get **Empty** Tables for {} and {}", database_name_source, database_name_target);
thread_toasts.add(Toast {
text: text.into(),
kind: ToastKind::Success,
options: ToastOptions::default()
.duration_in_seconds(5.0)
.show_progress(true),
..Default::default()
});
// thread_toasts.add(Toast {
// text: text.clone().into(),
// kind: ToastKind::Success,
// options: ToastOptions::default()
// .duration_in_seconds(5.0)
// .show_progress(true),
// ..Default::default()
// });
println!("{}", text);
*is_busy.lock().unwrap() = false;
});
}
}
2 changes: 1 addition & 1 deletion src/working_database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env::var;
use std::time::SystemTime;
use postgres::error::SqlState;
use rusqlite::{Connection, params};
use rusqlite::{Connection};
use crate::core::get_tables;
use crate::database::connect;
use crate::table::{build_base_simple_table, create_tables_table, insert_new_table, Table};
Expand Down

0 comments on commit f0a33db

Please sign in to comment.