/* Copyright 2023, Savanni D'Gerinel 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 . */ use crate::{ app::{AppInvocation, AppResponse}, components::day_detail, views::{HistoricalView, PlaceholderView, View, ViewName, WelcomeView}, }; use adw::prelude::*; use async_channel::Sender; use chrono::{FixedOffset, NaiveDate, TimeZone}; use dimensioned::si::{KG, M, S}; use ft_core::{Steps, TimeDistance, TraxRecord, Weight}; use gio::resources_lookup_data; use gtk::STYLE_PROVIDER_PRIORITY_USER; use std::path::PathBuf; use std::{cell::RefCell, 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_tx: Sender, layout: gtk::Box, current_view: Rc>, settings: gio::Settings, overlay: gtk::Overlay, modal: RefCell>, } 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. /// /// app is a core [crate::app::App] object which encapsulates all of the basic logic. pub fn new( app_id: &str, resource_path: &str, adw_app: &adw::Application, app_tx: Sender, ) -> 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); #[allow(deprecated)] let context = window.style_context(); #[allow(deprecated)] context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER); let overlay = gtk::Overlay::new(); let header = adw::HeaderBar::builder() .title_widget(>k::Label::new(Some("FitnessTrax"))) .build(); let layout = gtk::Box::builder() .orientation(gtk::Orientation::Vertical) .build(); let initial_view = View::Placeholder(PlaceholderView::new().upcast()); layout.append(&header); layout.append(initial_view.widget()); overlay.set_child(Some(&layout)); window.set_content(Some(&overlay)); window.present(); let gesture = gtk::GestureClick::new(); gesture.connect_released(|_, _, _, _| println!("detected gesture")); layout.add_controller(gesture); let s = Self { app_tx, layout, current_view: Rc::new(RefCell::new(initial_view)), settings: gio::Settings::new(app_id), overlay, modal: RefCell::new(None), }; s } pub fn change_view(&self, view: ViewName) { self.swap_main(self.construct_view(view)); } pub fn open_modal(&self, modal: gtk::Widget) { self.close_modal(); self.overlay.add_overlay(&modal); *self.modal.borrow_mut() = Some(modal); } pub fn close_modal(&self) { match *self.modal.borrow() { Some(ref widget) => self.overlay.remove_overlay(widget), None => {} } *self.modal.borrow_mut() = None; } pub fn process_response(&self, response: AppResponse) { match response { AppResponse::DatabaseChanged(db_path) => { self.settings .set_string("series-path", db_path.to_str().unwrap()) .unwrap(); self.change_view(ViewName::Historical); } AppResponse::NoDatabase => { self.change_view(ViewName::Welcome); } AppResponse::Records => { self.change_view(ViewName::Historical); } } } // 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(); self.layout.remove(&*current_widget.widget()); *current_widget = view; self.layout.append(&*current_widget.widget()); } fn construct_view(&self, view: ViewName) -> View { match view { ViewName::Welcome => View::Welcome( WelcomeView::new({ let s = self.clone(); Box::new(move |path: PathBuf| { s.app_tx .send_blocking(AppInvocation::OpenDatabase(path)) .unwrap(); }) }) .upcast(), ), ViewName::Historical => View::Historical( HistoricalView::new( vec![ TraxRecord::Steps(Steps { date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(), count: 1500, }), TraxRecord::Weight(Weight { date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(), weight: 85. * KG, }), TraxRecord::Weight(Weight { date: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(), weight: 86. * KG, }), TraxRecord::BikeRide(TimeDistance { datetime: FixedOffset::west_opt(10 * 60 * 60) .unwrap() .with_ymd_and_hms(2019, 6, 15, 12, 0, 0) .unwrap(), distance: Some(1000. * M), duration: Some(150. * S), comments: Some("Test Comments".to_owned()), }), TraxRecord::BikeRide(TimeDistance { datetime: FixedOffset::west_opt(10 * 60 * 60) .unwrap() .with_ymd_and_hms(2019, 6, 15, 23, 0, 0) .unwrap(), distance: Some(1000. * M), duration: Some(150. * S), comments: Some("Test Comments".to_owned()), }), ], { let s = self.clone(); Rc::new(move |date, records| s.open_modal(day_detail(date, records))) }, ) .upcast(), ), } } }