Set up the welcome screen and open the database #125

Merged
savanni merged 7 commits from fitnesstrax/welcome into main 2023-12-19 22:02:41 +00:00
1 changed files with 24 additions and 36 deletions
Showing only changes of commit 87994012fa - Show all commits

View File

@ -25,7 +25,7 @@ use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
use std::{ use std::{
cell::RefCell, cell::RefCell,
env, env,
path::PathBuf, path::{Path, PathBuf},
rc::Rc, rc::Rc,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
@ -47,14 +47,32 @@ enum Events {
/// The real, headless application. This is where all of the logic will reside. /// The real, headless application. This is where all of the logic will reside.
#[derive(Clone)] #[derive(Clone)]
struct App { struct App {
settings: gio::Settings,
database: Arc<RwLock<Option<Series<TraxRecord>>>>, database: Arc<RwLock<Option<Series<TraxRecord>>>>,
} }
impl App { impl App {
pub fn new() -> Self { pub fn new(settings: gio::Settings) -> Self {
Self { let s = Self {
settings,
database: Arc::new(RwLock::new(None)), 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 // If the file does not exist, create a new one. Again, show the user an error if
// some kind of error occurs. // some kind of error occurs.
if path.exists() { app.open_db(&path);
let db = Series::open(&path);
match db {
Ok(db) => {
*app.database.write().unwrap() = Some(db);
s.change_view(HistoricalView::new().upcast()); 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"
),
}
}
}) })
}) })
.upcast() .upcast()
@ -352,7 +340,7 @@ fn main() {
println!("database path: {}", settings.string("series-path")); println!("database path: {}", settings.string("series-path"));
let app = App::new(); let app = App::new(settings);
/* /*
let runtime = tokio::runtime::Builder::new_multi_thread() let runtime = tokio::runtime::Builder::new_multi_thread()