2023-12-22 19:54:38 +00:00
|
|
|
/*
|
2024-01-15 20:53:01 +00:00
|
|
|
Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
2023-12-22 19:54:38 +00:00
|
|
|
|
|
|
|
This file is part of FitnessTrax.
|
|
|
|
|
|
|
|
FitnessTrax is free software: you can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
General Public License as published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
FitnessTrax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use crate::{
|
2023-12-29 00:09:12 +00:00
|
|
|
app::App,
|
2024-01-20 16:16:31 +00:00
|
|
|
view_models::DayDetailViewModel,
|
|
|
|
views::{DayDetailView, HistoricalView, PlaceholderView, View, WelcomeView},
|
2023-12-22 19:54:38 +00:00
|
|
|
};
|
|
|
|
use adw::prelude::*;
|
2023-12-29 00:09:12 +00:00
|
|
|
use chrono::{Duration, Local};
|
2023-12-25 05:36:13 +00:00
|
|
|
use emseries::Record;
|
|
|
|
use ft_core::TraxRecord;
|
2023-12-22 19:54:38 +00:00
|
|
|
use gio::resources_lookup_data;
|
|
|
|
use gtk::STYLE_PROVIDER_PRIORITY_USER;
|
2023-12-29 02:45:55 +00:00
|
|
|
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
2023-12-22 19:54:38 +00:00
|
|
|
|
|
|
|
/// The application window, or the main window, is the main user interface for the app. Almost
|
|
|
|
/// everything occurs here.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct AppWindow {
|
2023-12-29 00:09:12 +00:00
|
|
|
app: App,
|
2023-12-22 19:54:38 +00:00
|
|
|
layout: gtk::Box,
|
|
|
|
current_view: Rc<RefCell<View>>,
|
|
|
|
settings: gio::Settings,
|
2023-12-25 06:09:31 +00:00
|
|
|
navigation: adw::NavigationView,
|
2023-12-22 19:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppWindow {
|
|
|
|
/// Construct a new App Window.
|
|
|
|
///
|
|
|
|
/// adw_app is an Adwaita application. Application windows need to have access to this, but
|
|
|
|
/// otherwise we don't use this.
|
|
|
|
///
|
2023-12-24 17:00:12 +00:00
|
|
|
/// app is a core [crate::app::App] object which encapsulates all of the basic logic.
|
2023-12-22 19:54:38 +00:00
|
|
|
pub fn new(
|
|
|
|
app_id: &str,
|
|
|
|
resource_path: &str,
|
|
|
|
adw_app: &adw::Application,
|
2023-12-29 00:09:12 +00:00
|
|
|
ft_app: App,
|
2023-12-22 19:54:38 +00:00
|
|
|
) -> AppWindow {
|
|
|
|
let window = adw::ApplicationWindow::builder()
|
|
|
|
.application(adw_app)
|
|
|
|
.width_request(800)
|
|
|
|
.height_request(600)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let stylesheet = String::from_utf8(
|
|
|
|
resources_lookup_data(
|
|
|
|
&format!("{}style.css", resource_path),
|
|
|
|
gio::ResourceLookupFlags::NONE,
|
|
|
|
)
|
|
|
|
.expect("stylesheet must be available in the resources")
|
|
|
|
.to_vec(),
|
|
|
|
)
|
|
|
|
.expect("to parse stylesheet");
|
|
|
|
|
|
|
|
let provider = gtk::CssProvider::new();
|
|
|
|
provider.load_from_data(&stylesheet);
|
|
|
|
|
2023-12-22 20:08:34 +00:00
|
|
|
#[allow(deprecated)]
|
2023-12-22 19:54:38 +00:00
|
|
|
let context = window.style_context();
|
2023-12-22 20:08:34 +00:00
|
|
|
#[allow(deprecated)]
|
2023-12-22 19:54:38 +00:00
|
|
|
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);
|
|
|
|
|
2023-12-25 06:09:31 +00:00
|
|
|
let navigation = adw::NavigationView::new();
|
2023-12-22 19:54:38 +00:00
|
|
|
|
|
|
|
let layout = gtk::Box::builder()
|
|
|
|
.orientation(gtk::Orientation::Vertical)
|
|
|
|
.build();
|
|
|
|
|
2024-01-20 19:35:10 +00:00
|
|
|
let initial_view = View::Placeholder(PlaceholderView::default().upcast());
|
2023-12-22 19:54:38 +00:00
|
|
|
|
2023-12-28 17:59:29 +00:00
|
|
|
layout.append(&initial_view.widget());
|
2023-12-22 19:54:38 +00:00
|
|
|
|
2023-12-25 06:09:31 +00:00
|
|
|
let nav_layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
|
|
|
nav_layout.append(&adw::HeaderBar::new());
|
|
|
|
nav_layout.append(&layout);
|
|
|
|
navigation.push(
|
|
|
|
&adw::NavigationPage::builder()
|
|
|
|
.can_pop(false)
|
|
|
|
.title("FitnessTrax")
|
|
|
|
.child(&nav_layout)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
window.set_content(Some(&navigation));
|
2023-12-22 19:54:38 +00:00
|
|
|
window.present();
|
|
|
|
|
2023-12-25 05:36:13 +00:00
|
|
|
let gesture = gtk::GestureClick::new();
|
|
|
|
gesture.connect_released(|_, _, _, _| println!("detected gesture"));
|
|
|
|
layout.add_controller(gesture);
|
|
|
|
|
2023-12-22 19:54:38 +00:00
|
|
|
let s = Self {
|
2023-12-29 00:09:12 +00:00
|
|
|
app: ft_app,
|
2023-12-22 19:54:38 +00:00
|
|
|
layout,
|
|
|
|
current_view: Rc::new(RefCell::new(initial_view)),
|
|
|
|
settings: gio::Settings::new(app_id),
|
2023-12-25 06:09:31 +00:00
|
|
|
navigation,
|
2023-12-22 19:54:38 +00:00
|
|
|
};
|
|
|
|
|
2023-12-25 05:36:13 +00:00
|
|
|
s.load_records();
|
2023-12-29 00:09:12 +00:00
|
|
|
|
2023-12-29 14:24:37 +00:00
|
|
|
s.navigation.connect_popped({
|
|
|
|
let s = s.clone();
|
2024-01-20 19:35:10 +00:00
|
|
|
move |_, _| {
|
|
|
|
if let View::Historical(_) = *s.current_view.borrow() {
|
|
|
|
s.load_records();
|
|
|
|
}
|
2023-12-29 14:24:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-22 19:54:38 +00:00
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2023-12-29 02:45:55 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-12-25 05:36:13 +00:00
|
|
|
fn show_historical_view(&self, records: Vec<Record<TraxRecord>>) {
|
2024-01-20 22:04:20 +00:00
|
|
|
let view = View::Historical(HistoricalView::new(self.app.clone(), records, {
|
2023-12-25 05:36:13 +00:00
|
|
|
let s = self.clone();
|
|
|
|
Rc::new(move |date, records| {
|
|
|
|
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
|
|
|
layout.append(&adw::HeaderBar::new());
|
2024-01-18 14:00:08 +00:00
|
|
|
// layout.append(&DayDetailView::new(date, records, s.app.clone()));
|
|
|
|
layout.append(&DayDetailView::new(DayDetailViewModel::new(
|
|
|
|
date,
|
|
|
|
records,
|
|
|
|
s.app.clone(),
|
|
|
|
)));
|
2023-12-25 05:36:13 +00:00
|
|
|
let page = &adw::NavigationPage::builder()
|
|
|
|
.title(date.format("%Y-%m-%d").to_string())
|
|
|
|
.child(&layout)
|
|
|
|
.build();
|
|
|
|
s.navigation.push(page);
|
|
|
|
})
|
|
|
|
}));
|
2023-12-29 02:45:55 +00:00
|
|
|
self.swap_main(view);
|
2023-12-22 19:54:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 05:36:13 +00:00
|
|
|
fn load_records(&self) {
|
|
|
|
glib::spawn_future_local({
|
|
|
|
let s = self.clone();
|
|
|
|
async move {
|
|
|
|
let end = Local::now().date_naive();
|
|
|
|
let start = end - Duration::days(7);
|
|
|
|
match s.app.records(start, end).await {
|
|
|
|
Ok(records) => s.show_historical_view(records),
|
|
|
|
Err(_) => s.show_welcome_view(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-22 19:54:38 +00:00
|
|
|
// Switch views.
|
|
|
|
//
|
|
|
|
// This function only replaces the old view with the one which matches the current view state.
|
|
|
|
// It is responsible for ensuring that the new view goes into the layout in the correct
|
|
|
|
// position.
|
|
|
|
fn swap_main(&self, view: View) {
|
|
|
|
let mut current_widget = self.current_view.borrow_mut();
|
2023-12-28 17:59:29 +00:00
|
|
|
self.layout.remove(¤t_widget.widget());
|
2023-12-22 19:54:38 +00:00
|
|
|
*current_widget = view;
|
2023-12-28 17:59:29 +00:00
|
|
|
self.layout.append(¤t_widget.widget());
|
2023-12-22 19:54:38 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 02:45:55 +00:00
|
|
|
fn on_apply_config(&self, path: PathBuf) {
|
|
|
|
glib::spawn_future_local({
|
|
|
|
let s = self.clone();
|
|
|
|
async move {
|
|
|
|
if s.app.open_db(path.clone()).await.is_ok() {
|
2023-12-29 03:18:51 +00:00
|
|
|
let _ = s.settings.set("series-path", path.to_str().unwrap());
|
2023-12-25 05:36:13 +00:00
|
|
|
s.load_records();
|
2023-12-29 02:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-12-22 19:54:38 +00:00
|
|
|
}
|
|
|
|
}
|