From 804985981641d2fad9e0016a43c79f5379529167 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 28 Dec 2023 21:45:55 -0500 Subject: [PATCH] Clean up showing the welcome and historical screens Swapping is now done in dedicated functions instead of a big pattern match. After selecting a database, the app window will apply the configuration by opening the database, saving the path to configuration, and switching to the historical view. --- fitnesstrax/app/src/app_window.rs | 44 ++++++++++++++--------- fitnesstrax/app/src/views/welcome_view.rs | 23 ++++-------- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/fitnesstrax/app/src/app_window.rs b/fitnesstrax/app/src/app_window.rs index 7a51efc..a9ce905 100644 --- a/fitnesstrax/app/src/app_window.rs +++ b/fitnesstrax/app/src/app_window.rs @@ -22,13 +22,14 @@ use adw::prelude::*; use chrono::{Duration, Local}; use gio::resources_lookup_data; use gtk::STYLE_PROVIDER_PRIORITY_USER; -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, path::PathBuf, rc::Rc}; /// The application window, or the main window, is the main user interface for the app. Almost /// everything occurs here. #[derive(Clone)] pub struct AppWindow { app: App, + app_id: String, layout: gtk::Box, current_view: Rc>, settings: gio::Settings, @@ -99,6 +100,7 @@ impl AppWindow { let s = Self { app: ft_app, + app_id: app_id.to_owned(), layout, current_view: Rc::new(RefCell::new(initial_view)), settings: gio::Settings::new(app_id), @@ -111,8 +113,8 @@ impl AppWindow { let end = Local::now().date_naive(); let start = end - Duration::days(7); match s.app.records(start, end).await { - Ok(_) => s.change_view(ViewName::Historical), - Err(_) => s.change_view(ViewName::Welcome), + Ok(_) => s.show_historical_view(), + Err(_) => s.show_welcome_view(), } } }); @@ -120,8 +122,17 @@ impl AppWindow { s } - pub fn change_view(&self, view: ViewName) { - self.swap_main(self.construct_view(view)); + fn show_welcome_view(&self) { + let view = View::Welcome(WelcomeView::new({ + let s = self.clone(); + move |path| s.on_apply_config(path) + })); + self.swap_main(view); + } + + fn show_historical_view(&self) { + let view = View::Historical(HistoricalView::new(vec![])); + self.swap_main(view); } // Switch views. @@ -136,16 +147,17 @@ impl AppWindow { self.layout.append(¤t_widget.widget()); } - fn construct_view(&self, view: ViewName) -> View { - match view { - ViewName::Welcome => View::Welcome( - WelcomeView::new(self.app.clone(), { - let s = self.clone(); - move || s.change_view(ViewName::Historical) - }) - .upcast(), - ), - ViewName::Historical => View::Historical(HistoricalView::new(vec![]).upcast()), - } + fn on_apply_config(&self, path: PathBuf) { + println!("saving configuration"); + glib::spawn_future_local({ + let s = self.clone(); + async move { + if s.app.open_db(path.clone()).await.is_ok() { + let settings = gio::Settings::new(&s.app_id); + let _ = settings.set("series-path", path.to_str().unwrap()); + s.show_historical_view(); + } + } + }); } } diff --git a/fitnesstrax/app/src/views/welcome_view.rs b/fitnesstrax/app/src/views/welcome_view.rs index 7febb64..682bde7 100644 --- a/fitnesstrax/app/src/views/welcome_view.rs +++ b/fitnesstrax/app/src/views/welcome_view.rs @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Fit use crate::{app::App, components::FileChooserRow}; use glib::Object; use gtk::{prelude::*, subclass::prelude::*}; -use std::rc::Rc; +use std::path::PathBuf; /// This is the view to show if the application has not yet been configured. It will walk the user /// through the most critical setup steps so that we can move on to the other views in the app. @@ -43,9 +43,9 @@ glib::wrapper! { } impl WelcomeView { - pub fn new(app: App, on_save: OnSave) -> Self + pub fn new(on_save: OnSave) -> Self where - OnSave: Fn() + 'static, + OnSave: Fn(PathBuf) + 'static, { let s: Self = Object::builder().build(); s.set_orientation(gtk::Orientation::Vertical); @@ -81,22 +81,11 @@ impl WelcomeView { content.append(&db_row); save_button.connect_clicked({ - let on_save = Rc::new(on_save); - let app = app.clone(); let db_row = db_row.clone(); move |_| { - println!("save button clicked. Do something"); - let app = app.clone(); - let db_row = db_row.clone(); - let on_save = on_save.clone(); - glib::spawn_future_local(async move { - println!("{:?}", db_row.path()); - if let Some(path) = db_row.path() { - if app.open_db(path).await.is_ok() { - on_save(); - } - } - }); + if let Some(path) = db_row.path() { + on_save(path); + } } });