Compare commits

..

No commits in common. "87994012fa5f97b77938148ff0c848fd19211a98" and "db188ea75ab19516345e5d83afae07c08b172bb0" have entirely different histories.

3 changed files with 39 additions and 164 deletions

View File

@ -1,21 +0,0 @@
.welcome {
margin: 64px;
}
.welcome-title {
font-size: larger;
padding: 8px;
}
.welcome-content {
padding: 8px;
}
.welcome-footer {
}
.dialog-row {
margin: 8px 0px 8px 0px;
padding: 8px;
}

View File

@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Fit
mod ui; mod ui;
use adw::prelude::*; use adw::prelude::*;
use emseries::{EmseriesReadError, Series}; use emseries::Series;
use ft_core::TraxRecord; use ft_core::TraxRecord;
use gio::resources_lookup_data; use gio::resources_lookup_data;
use glib::Object; use glib::Object;
@ -25,7 +25,7 @@ use gtk::{subclass::prelude::*, STYLE_PROVIDER_PRIORITY_USER};
use std::{ use std::{
cell::RefCell, cell::RefCell,
env, env,
path::{Path, PathBuf}, path::PathBuf,
rc::Rc, rc::Rc,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
@ -36,71 +36,17 @@ const APP_ID_PROD: &str = "com.luminescent-dreams.fitnesstrax";
const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/fitnesstrax/"; const RESOURCE_BASE_PATH: &str = "/com/luminescent-dreams/fitnesstrax/";
/// A set of events that can occur at the global application level. These events should represent
/// significant state changes that should go through a central dispatcher.
enum Events {
DatabaseChanged(Series<TraxRecord>),
}
// Note that I have not yet figured out the communication channel or how the central dispatcher
// should work. There's a dance between the App and the AppWindow that I haven't figured out yet.
/// 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(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();
}
}
pub struct PlaceholderViewPrivate {}
#[glib::object_subclass]
impl ObjectSubclass for PlaceholderViewPrivate {
const NAME: &'static str = "PlaceholderView";
type Type = PlaceholderView;
type ParentType = gtk::Box;
fn new() -> Self {
Self {}
}
}
impl ObjectImpl for PlaceholderViewPrivate {}
impl WidgetImpl for PlaceholderViewPrivate {}
impl BoxImpl for PlaceholderViewPrivate {}
glib::wrapper! {
pub struct PlaceholderView(ObjectSubclass<PlaceholderViewPrivate>) @extends gtk::Box, gtk::Widget;
}
impl PlaceholderView {
pub fn new() -> Self { pub fn new() -> Self {
let s: Self = Object::builder().build(); Self {
s database: Arc::new(RwLock::new(None)),
}
} }
} }
@ -128,19 +74,19 @@ glib::wrapper! {
} }
impl WelcomeView { impl WelcomeView {
pub fn new<F>(on_save: Box<F>) -> Self pub fn new<F>(on_save: F) -> Self
where where
F: Fn(PathBuf) + 'static, F: Fn(PathBuf) + 'static,
{ {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical); s.set_orientation(gtk::Orientation::Vertical);
s.set_css_classes(&["welcome"]); s.set_css_classes(&["modal"]);
// Replace this with the welcome screen that we set up in the fitnesstrax/unconfigured-page // Replace this with the welcome screen that we set up in the fitnesstrax/unconfigured-page
// branch. // branch.
let title = gtk::Label::builder() let title = gtk::Label::builder()
.label("Welcome to FitnessTrax") .label("Welcome to FitnessTrax")
.css_classes(["welcome-title"]) .css_classes(["modal-title"])
.build(); .build();
let content = gtk::Box::builder() let content = gtk::Box::builder()
@ -165,7 +111,7 @@ impl WelcomeView {
content.append(&gtk::Label::new(Some("Welcome to FitnessTrax. The application has not yet been configured, so I will walk you through that. Let's start out by selecting your database."))); content.append(&gtk::Label::new(Some("Welcome to FitnessTrax. The application has not yet been configured, so I will walk you through that. Let's start out by selecting your database.")));
content.append(&db_row); content.append(&db_row);
let on_save = on_save; let on_save = Box::new(on_save);
save_button.connect_clicked({ save_button.connect_clicked({
move |_| { move |_| {
if let Some(path) = db_row.path() { if let Some(path) = db_row.path() {
@ -220,12 +166,11 @@ impl HistoricalView {
/// The application window, or the main window, is the main user interface for the app. Almost /// The application window, or the main window, is the main user interface for the app. Almost
/// everything occurs here. /// everything occurs here.
#[derive(Clone)]
struct AppWindow { struct AppWindow {
app: App, app: App,
window: adw::ApplicationWindow, window: adw::ApplicationWindow,
layout: gtk::Box, layout: gtk::Box,
current_view: Rc<RefCell<gtk::Widget>>, current_view: RefCell<gtk::Widget>,
} }
impl AppWindow { impl AppWindow {
@ -242,6 +187,12 @@ impl AppWindow {
.height_request(600) .height_request(600)
.build(); .build();
let current_view = if app.database.read().unwrap().is_none() {
WelcomeView::new(&|_| {}).upcast()
} else {
HistoricalView::new().upcast()
};
let stylesheet = String::from_utf8( let stylesheet = String::from_utf8(
resources_lookup_data( resources_lookup_data(
&format!("{}style.css", RESOURCE_BASE_PATH), &format!("{}style.css", RESOURCE_BASE_PATH),
@ -265,47 +216,18 @@ impl AppWindow {
let layout = gtk::Box::builder() let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical) .orientation(gtk::Orientation::Vertical)
.build(); .build();
let initial_view = PlaceholderView::new();
layout.append(&header); layout.append(&header);
layout.append(&initial_view); layout.append(&current_view);
window.set_content(Some(&layout)); window.set_content(Some(&layout));
window.present(); window.present();
let s = Self { let s = Self {
app: app.clone(), app,
window, window,
layout, layout,
current_view: Rc::new(RefCell::new(initial_view.upcast())), current_view: RefCell::new(current_view),
}; };
let initial_view = if app.database.read().unwrap().is_none() {
WelcomeView::new({
let app = app.clone();
let s = s.clone();
Box::new(move |path: PathBuf| {
// The user has selected a path. Perhaps the path is new, perhaps it already
// exists.
//
// If the file exists already, attempt to read it. Fail if that doesn't work.
// A should show to the user something that indicates that the file exists but is
// not already a database.
//
// If the file does not exist, create a new one. Again, show the user an error if
// some kind of error occurs.
app.open_db(&path);
s.change_view(HistoricalView::new().upcast());
})
})
.upcast()
} else {
HistoricalView::new().upcast()
};
s.change_view(initial_view);
s s
} }
@ -340,7 +262,7 @@ fn main() {
println!("database path: {}", settings.string("series-path")); println!("database path: {}", settings.string("series-path"));
let app = App::new(settings); let app = App::new();
/* /*
let runtime = tokio::runtime::Builder::new_multi_thread() let runtime = tokio::runtime::Builder::new_multi_thread()

View File

@ -56,71 +56,45 @@ impl FileChooserRow {
{ {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_css_classes(&["dialog-row", "card"]);
s.set_orientation(gtk::Orientation::Horizontal); s.set_orientation(gtk::Orientation::Horizontal);
s.set_spacing(8);
// The database selection row should be a box that shows a default database path, along with a // The database selection row should be a box that shows a default database path, along with a
// button that triggers a file chooser dialog. Once the dialog returns, the box should be // button that triggers a file chooser dialog. Once the dialog returns, the box should be
// updated to reflect the chosen path. // updated to reflect the chosen path.
s.imp().label.set_text("No database selected"); s.imp().label.set_text("No database selected");
let db_file_chooser_button = gtk::Button::builder().label("Select Database").build();
let on_selected = Rc::new(Box::new(on_selected)); let on_selected = Rc::new(Box::new(on_selected));
db_file_chooser_button.connect_clicked({
let import_button = gtk::Button::builder().label("Import a Database").build();
let handle_file_selection = Rc::new(Box::new({
let s = s.clone(); let s = s.clone();
let on_selected = on_selected.clone();
move |file_id: Result<gio::File, glib::Error>| match file_id {
Ok(file_id) => match file_id.path() {
Some(path) => {
s.imp().label.set_text(path.to_str().unwrap());
on_selected(path.clone());
*s.imp().path.borrow_mut() = Some(path);
}
None => {
*s.imp().path.borrow_mut() = None;
s.imp().label.set_text("No database selected");
}
},
Err(err) => println!("file opening failed: {}", err),
}
}));
import_button.connect_clicked({
let handle_file_selection = handle_file_selection.clone();
move |_| { move |_| {
let no_window: Option<&gtk::Window> = None; let no_window: Option<&gtk::Window> = None;
let not_cancellable: Option<&gio::Cancellable> = None; let not_cancellable: Option<&gio::Cancellable> = None;
let handle_file_selection = handle_file_selection.clone(); let s = s.clone();
gtk::FileDialog::builder().build().open( let on_selected = on_selected.clone();
no_window,
not_cancellable,
move |file_id| handle_file_selection(file_id),
);
}
});
let new_button = gtk::Button::builder().label("Create Database").build();
new_button.connect_clicked({
let handle_file_selection = handle_file_selection.clone();
move |_| {
let no_window: Option<&gtk::Window> = None;
let not_cancellable: Option<&gio::Cancellable> = None;
let handle_file_selection = handle_file_selection.clone();
gtk::FileDialog::builder().build().save( gtk::FileDialog::builder().build().save(
no_window, no_window,
not_cancellable, not_cancellable,
move |file_id| handle_file_selection(file_id), move |file_id| match file_id {
Ok(file_id) => match file_id.path() {
Some(path) => {
s.imp().label.set_text(path.to_str().unwrap());
on_selected(path.clone());
*s.imp().path.borrow_mut() = Some(path);
}
None => {
*s.imp().path.borrow_mut() = None;
s.imp().label.set_text("No database selected");
}
},
Err(err) => println!("file opening failed: {}", err),
},
); );
} }
}); });
s.imp().label.set_halign(gtk::Align::Start);
s.append(&s.imp().label); s.append(&s.imp().label);
s.append(&import_button); s.append(&db_file_chooser_button);
s.append(&new_button);
s s
} }