diff --git a/fitnesstrax/app/src/main.rs b/fitnesstrax/app/src/main.rs index a3111a7..1eb412e 100644 --- a/fitnesstrax/app/src/main.rs +++ b/fitnesstrax/app/src/main.rs @@ -25,7 +25,7 @@ use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER}; use std::{ cell::RefCell, env, - path::PathBuf, + path::{Path, PathBuf}, rc::Rc, sync::{Arc, RwLock}, }; @@ -47,14 +47,32 @@ enum Events { /// The real, headless application. This is where all of the logic will reside. #[derive(Clone)] struct App { + settings: gio::Settings, database: Arc>>>, } impl App { - pub fn new() -> Self { - Self { + pub fn new(settings: gio::Settings) -> Self { + let s = Self { + settings, database: Arc::new(RwLock::new(None)), + }; + + if !s.settings.string("series-path").is_empty() { + let path = PathBuf::from(s.settings.string("series-path")); + let db = Series::open(path).unwrap(); + *s.database.write().unwrap() = Some(db); } + + s + } + + pub fn open_db(&self, path: &Path) { + let db = Series::open(path).unwrap(); + *self.database.write().unwrap() = Some(db); + self.settings + .set_string("series-path", path.to_str().unwrap()) + .unwrap(); } } @@ -277,38 +295,8 @@ impl AppWindow { // // If the file does not exist, create a new one. Again, show the user an error if // some kind of error occurs. - if path.exists() { - let db = Series::open(&path); - match db { - Ok(db) => { - *app.database.write().unwrap() = Some(db); - s.change_view(HistoricalView::new().upcast()); - } - Err(EmseriesReadError::UUIDParseError(_)) => { - println!("Invalid UUID detected in the file") - } - Err(EmseriesReadError::JSONParseError(_)) => { - println!("The file cannot be parsed and may not be a database") - } - Err(EmseriesReadError::IOError(err)) => { - println!("The file cannot be read: {}", err) - } - } - } else { - let db = Series::open(&path); - match db { - Ok(db) => { - *app.database.write().unwrap() = Some(db); - s.change_view(HistoricalView::new().upcast()); - } - Err(EmseriesReadError::IOError(err)) => { - println!("The file cannot be read: {}", err) - } - _ => unreachable!( - "other error types should not be possible when creating a new DB" - ), - } - } + app.open_db(&path); + s.change_view(HistoricalView::new().upcast()); }) }) .upcast() @@ -352,7 +340,7 @@ fn main() { println!("database path: {}", settings.string("series-path")); - let app = App::new(); + let app = App::new(settings); /* let runtime = tokio::runtime::Builder::new_multi_thread()