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.
This commit is contained in:
Savanni D'Gerinel 2023-12-28 21:45:55 -05:00
parent ac343a2af6
commit 8049859816
2 changed files with 34 additions and 33 deletions

View File

@ -22,13 +22,14 @@ use adw::prelude::*;
use chrono::{Duration, Local}; use chrono::{Duration, Local};
use gio::resources_lookup_data; use gio::resources_lookup_data;
use gtk::STYLE_PROVIDER_PRIORITY_USER; 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 /// The application window, or the main window, is the main user interface for the app. Almost
/// everything occurs here. /// everything occurs here.
#[derive(Clone)] #[derive(Clone)]
pub struct AppWindow { pub struct AppWindow {
app: App, app: App,
app_id: String,
layout: gtk::Box, layout: gtk::Box,
current_view: Rc<RefCell<View>>, current_view: Rc<RefCell<View>>,
settings: gio::Settings, settings: gio::Settings,
@ -99,6 +100,7 @@ impl AppWindow {
let s = Self { let s = Self {
app: ft_app, app: ft_app,
app_id: app_id.to_owned(),
layout, layout,
current_view: Rc::new(RefCell::new(initial_view)), current_view: Rc::new(RefCell::new(initial_view)),
settings: gio::Settings::new(app_id), settings: gio::Settings::new(app_id),
@ -111,8 +113,8 @@ impl AppWindow {
let end = Local::now().date_naive(); let end = Local::now().date_naive();
let start = end - Duration::days(7); let start = end - Duration::days(7);
match s.app.records(start, end).await { match s.app.records(start, end).await {
Ok(_) => s.change_view(ViewName::Historical), Ok(_) => s.show_historical_view(),
Err(_) => s.change_view(ViewName::Welcome), Err(_) => s.show_welcome_view(),
} }
} }
}); });
@ -120,8 +122,17 @@ impl AppWindow {
s s
} }
pub fn change_view(&self, view: ViewName) { fn show_welcome_view(&self) {
self.swap_main(self.construct_view(view)); 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. // Switch views.
@ -136,16 +147,17 @@ impl AppWindow {
self.layout.append(&current_widget.widget()); self.layout.append(&current_widget.widget());
} }
fn construct_view(&self, view: ViewName) -> View { fn on_apply_config(&self, path: PathBuf) {
match view { println!("saving configuration");
ViewName::Welcome => View::Welcome( glib::spawn_future_local({
WelcomeView::new(self.app.clone(), { let s = self.clone();
let s = self.clone(); async move {
move || s.change_view(ViewName::Historical) if s.app.open_db(path.clone()).await.is_ok() {
}) let settings = gio::Settings::new(&s.app_id);
.upcast(), let _ = settings.set("series-path", path.to_str().unwrap());
), s.show_historical_view();
ViewName::Historical => View::Historical(HistoricalView::new(vec![]).upcast()), }
} }
});
} }
} }

View File

@ -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 crate::{app::App, components::FileChooserRow};
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; 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 /// 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. /// 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 { impl WelcomeView {
pub fn new<OnSave>(app: App, on_save: OnSave) -> Self pub fn new<OnSave>(on_save: OnSave) -> Self
where where
OnSave: Fn() + 'static, OnSave: 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);
@ -81,22 +81,11 @@ impl WelcomeView {
content.append(&db_row); content.append(&db_row);
save_button.connect_clicked({ save_button.connect_clicked({
let on_save = Rc::new(on_save);
let app = app.clone();
let db_row = db_row.clone(); let db_row = db_row.clone();
move |_| { move |_| {
println!("save button clicked. Do something"); if let Some(path) = db_row.path() {
let app = app.clone(); on_save(path);
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();
}
}
});
} }
}); });