diff --git a/code/src/app.rs b/code/src/app.rs index 0dbf1d5..8d49f34 100644 --- a/code/src/app.rs +++ b/code/src/app.rs @@ -1,6 +1,9 @@ -use egui_dock::{DockArea, Style}; +use std::sync::Arc; -use crate::database::DBOperator; +use egui::mutex::RwLock; +use egui_dock::{DockArea, Style}; +use sqlx::{Connection, MySqlConnection, MySqlPool}; +use crate::{database::DBOperator, models::{self, Position}}; static TABS_CAN_BE_WINDOWS: bool = false; @@ -20,7 +23,7 @@ struct Tab{ title: String, } pub struct App{ - tree: egui_dock::DockState, + pub tree: egui_dock::DockState, } impl Default for App{ @@ -54,19 +57,26 @@ impl eframe::App for App{ fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame){ DockArea::new(&mut self.tree) .style(Style::from_egui(ui.style().as_ref())) - .show_inside(ui, &mut MainTabViewer { - equipment: vec![ - crate::models::Equipment::default(), - crate::models::Equipment::default(), - crate::models::Equipment::default(), - crate::models::Equipment::default(), - ] - }); + .show_inside(ui, &mut MainTabViewer::default()); } } struct MainTabViewer { + positions: Arc>>>, equipment: std::vec::Vec, + db_oper: DBOperator, +} +impl Default for MainTabViewer{ + + fn default() -> Self { + let db_oper = DBOperator::new(); + let rt = tokio::runtime::Runtime::new().unwrap(); + Self{ + positions: Arc::new(RwLock::new(Option::Some(rt.block_on(async{db_oper.get_position().await.unwrap()})))), + equipment: vec![models::Equipment::default()], + db_oper + } + } } impl MainTabViewer{ fn show_equipment(&mut self, ui: &mut egui::Ui){ @@ -108,13 +118,10 @@ impl MainTabViewer{ fn show_worker(&mut self, ui: &mut egui::Ui){ let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async{ - let dbd = DBOperator::new(); - let positions = dbd.get_position().await.unwrap(); - for eq in positions{ - println!("{}",&eq.name); + for eq in self.positions.read().clone().unwrap().iter(){ ui.push_id(&eq.name, |ui|{ egui::CollapsingHeader::new(&eq.name) - .default_open(false) + .default_open(false) .show(ui, |ui|{ ui.label(eq.wage.to_string()); }) @@ -163,4 +170,4 @@ impl egui_dock::TabViewer for MainTabViewer{ fn is_closeable(&self, _tab: &Self::Tab) -> bool { false } -} \ No newline at end of file +} diff --git a/code/src/database.rs b/code/src/database.rs index 414ecf0..ef6738b 100644 --- a/code/src/database.rs +++ b/code/src/database.rs @@ -1,7 +1,6 @@ -use sqlx::MySqlConnection; use sqlx::mysql::MySqlPoolOptions; use sqlx::mysql::MySqlPool; -use sqlx::Connection; + use dotenv::dotenv; // use crate::schema::equipment::dsl::*; diff --git a/code/src/main.rs b/code/src/main.rs index 9a2852d..58cb86c 100644 --- a/code/src/main.rs +++ b/code/src/main.rs @@ -1,11 +1,15 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use egui::mutex::RwLock; +use sqlx::MySqlPool; + use crate::database::DBOperator; mod app; mod models; mod database; // mod schema; -fn main() -> eframe::Result{ +fn main() +{ database::estabilish_connection(); let native_opts = eframe::NativeOptions { viewport: egui::ViewportBuilder::default() @@ -14,6 +18,6 @@ fn main() -> eframe::Result{ ..Default::default() }; - eframe::run_native("BCS", native_opts, Box::new(|cc| Ok(Box::new(app::App::new(cc)))),) + eframe::run_native("BCS", native_opts, Box::new(|cc| Ok(Box::new(app::App::new(cc)))),); }