2023-12-22 22:32:45 +00:00
|
|
|
/*
|
2024-01-16 04:27:55 +00:00
|
|
|
Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
2023-12-22 22:32:45 +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 chrono::NaiveDate;
|
|
|
|
// use ft_core::TraxRecord;
|
2024-01-18 14:00:08 +00:00
|
|
|
use crate::{
|
2024-01-21 15:50:18 +00:00
|
|
|
components::{
|
|
|
|
steps_editor, time_distance_summary, weight_field, ActionGroup, Steps, WeightLabel,
|
|
|
|
},
|
2024-02-03 20:28:33 +00:00
|
|
|
types::WeightFormatter,
|
2024-01-20 16:16:31 +00:00
|
|
|
view_models::DayDetailViewModel,
|
2024-01-18 14:00:08 +00:00
|
|
|
};
|
2024-01-21 15:50:18 +00:00
|
|
|
use ft_core::TimeDistanceActivity;
|
2023-12-22 22:32:45 +00:00
|
|
|
use glib::Object;
|
|
|
|
use gtk::{prelude::*, subclass::prelude::*};
|
2024-01-20 19:35:10 +00:00
|
|
|
use std::cell::RefCell;
|
2023-12-22 22:32:45 +00:00
|
|
|
|
2024-01-23 13:28:39 +00:00
|
|
|
use super::time_distance_detail;
|
|
|
|
|
2023-12-22 22:32:45 +00:00
|
|
|
pub struct DaySummaryPrivate {
|
|
|
|
date: gtk::Label,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for DaySummaryPrivate {
|
|
|
|
const NAME: &'static str = "DaySummary";
|
|
|
|
type Type = DaySummary;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
|
|
|
|
fn new() -> Self {
|
2023-12-24 17:00:12 +00:00
|
|
|
let date = gtk::Label::builder()
|
2023-12-25 05:36:13 +00:00
|
|
|
.css_classes(["day-summary__date"])
|
2023-12-24 17:00:12 +00:00
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.build();
|
2024-01-20 22:04:20 +00:00
|
|
|
Self { date }
|
2023-12-22 22:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for DaySummaryPrivate {}
|
|
|
|
impl WidgetImpl for DaySummaryPrivate {}
|
|
|
|
impl BoxImpl for DaySummaryPrivate {}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
2023-12-24 17:00:12 +00:00
|
|
|
/// The DaySummary displays one day's activities in a narrative style. This is meant to give
|
|
|
|
/// an overall feel of everything that happened during the day without going into details.
|
2023-12-22 22:32:45 +00:00
|
|
|
pub struct DaySummary(ObjectSubclass<DaySummaryPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
|
|
|
}
|
|
|
|
|
2024-01-20 19:35:10 +00:00
|
|
|
impl Default for DaySummary {
|
|
|
|
fn default() -> Self {
|
2023-12-22 22:32:45 +00:00
|
|
|
let s: Self = Object::builder().build();
|
|
|
|
s.set_orientation(gtk::Orientation::Vertical);
|
2023-12-25 05:36:13 +00:00
|
|
|
s.set_css_classes(&["day-summary"]);
|
2023-12-22 22:32:45 +00:00
|
|
|
|
|
|
|
s.append(&s.imp().date);
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
2024-01-20 19:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DaySummary {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
2023-12-22 22:32:45 +00:00
|
|
|
|
2024-01-20 22:04:20 +00:00
|
|
|
pub fn set_data(&self, view_model: DayDetailViewModel) {
|
2023-12-22 22:32:45 +00:00
|
|
|
self.imp()
|
|
|
|
.date
|
2024-01-20 22:04:20 +00:00
|
|
|
.set_text(&view_model.date.format("%Y-%m-%d").to_string());
|
|
|
|
|
|
|
|
let row = gtk::Box::builder().build();
|
2023-12-22 23:53:29 +00:00
|
|
|
|
2024-01-20 22:04:20 +00:00
|
|
|
let label = gtk::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.css_classes(["day-summary__weight"])
|
|
|
|
.build();
|
|
|
|
if let Some(w) = view_model.weight() {
|
|
|
|
label.set_label(&w.to_string())
|
2023-12-22 23:53:29 +00:00
|
|
|
}
|
2024-01-20 22:04:20 +00:00
|
|
|
row.append(&label);
|
2023-12-22 23:53:29 +00:00
|
|
|
|
2024-01-20 22:04:20 +00:00
|
|
|
self.append(&label);
|
|
|
|
|
|
|
|
let label = gtk::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.css_classes(["day-summary__weight"])
|
|
|
|
.build();
|
|
|
|
if let Some(s) = view_model.steps() {
|
2024-01-30 15:08:10 +00:00
|
|
|
label.set_label(&format!("{} steps", s));
|
2023-12-22 23:53:29 +00:00
|
|
|
}
|
2024-01-20 22:04:20 +00:00
|
|
|
row.append(&label);
|
|
|
|
self.append(&row);
|
2024-01-21 15:50:18 +00:00
|
|
|
|
|
|
|
let biking_summary = view_model.time_distance_summary(TimeDistanceActivity::BikeRide);
|
|
|
|
if let Some(label) = time_distance_summary(biking_summary.0, biking_summary.1) {
|
|
|
|
self.append(&label);
|
|
|
|
}
|
2023-12-25 05:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 14:00:08 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct DayDetailPrivate {}
|
2023-12-25 05:36:13 +00:00
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for DayDetailPrivate {
|
|
|
|
const NAME: &'static str = "DayDetail";
|
|
|
|
type Type = DayDetail;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for DayDetailPrivate {}
|
|
|
|
impl WidgetImpl for DayDetailPrivate {}
|
|
|
|
impl BoxImpl for DayDetailPrivate {}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
pub struct DayDetail(ObjectSubclass<DayDetailPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DayDetail {
|
2024-01-18 14:00:08 +00:00
|
|
|
pub fn new<OnEdit>(view_model: DayDetailViewModel, on_edit: OnEdit) -> Self
|
2023-12-28 14:51:41 +00:00
|
|
|
where
|
2024-01-16 04:27:55 +00:00
|
|
|
OnEdit: Fn() + 'static,
|
2023-12-28 14:51:41 +00:00
|
|
|
{
|
2023-12-25 05:36:13 +00:00
|
|
|
let s: Self = Object::builder().build();
|
|
|
|
s.set_orientation(gtk::Orientation::Vertical);
|
2024-01-15 20:53:01 +00:00
|
|
|
s.set_hexpand(true);
|
2023-12-25 05:36:13 +00:00
|
|
|
|
2024-01-18 03:13:55 +00:00
|
|
|
s.append(
|
|
|
|
&ActionGroup::builder()
|
2024-01-20 19:35:10 +00:00
|
|
|
.primary_action("Edit", Box::new(on_edit))
|
2024-01-18 03:13:55 +00:00
|
|
|
.build(),
|
|
|
|
);
|
2024-01-15 20:53:01 +00:00
|
|
|
|
|
|
|
/*
|
2023-12-26 15:45:50 +00:00
|
|
|
let click_controller = gtk::GestureClick::new();
|
|
|
|
click_controller.connect_released({
|
|
|
|
let s = s.clone();
|
|
|
|
move |_, _, _, _| {
|
|
|
|
println!("clicked outside of focusable entity");
|
|
|
|
if let Some(widget) = s.focus_child().and_downcast_ref::<WeightView>() {
|
|
|
|
println!("focused child is the weight view");
|
|
|
|
widget.blur();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
s.add_controller(click_controller);
|
2024-01-15 20:53:01 +00:00
|
|
|
*/
|
2023-12-26 15:45:50 +00:00
|
|
|
|
2024-01-18 14:00:08 +00:00
|
|
|
/*
|
2023-12-26 15:24:19 +00:00
|
|
|
let weight_record = records.iter().find_map(|record| match record {
|
2023-12-26 15:45:50 +00:00
|
|
|
Record {
|
|
|
|
id,
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::Weight(record),
|
2023-12-26 15:45:50 +00:00
|
|
|
} => Some((id.clone(), record.clone())),
|
2023-12-26 15:24:19 +00:00
|
|
|
_ => None,
|
|
|
|
});
|
2024-01-18 14:00:08 +00:00
|
|
|
*/
|
2023-12-26 15:24:19 +00:00
|
|
|
|
2024-01-20 20:59:03 +00:00
|
|
|
let top_row = gtk::Box::builder()
|
|
|
|
.orientation(gtk::Orientation::Horizontal)
|
|
|
|
.build();
|
2024-02-03 20:28:33 +00:00
|
|
|
let weight_view = WeightLabel::new(view_model.weight().map(WeightFormatter::from));
|
2024-01-20 20:59:03 +00:00
|
|
|
top_row.append(&weight_view.widget());
|
|
|
|
|
|
|
|
let steps_view = Steps::new(view_model.steps());
|
|
|
|
top_row.append(&steps_view.widget());
|
|
|
|
|
|
|
|
s.append(&top_row);
|
2023-12-26 15:24:19 +00:00
|
|
|
|
2024-01-23 13:28:39 +00:00
|
|
|
let records = view_model.time_distance_records();
|
|
|
|
for emseries::Record { data, .. } in records {
|
|
|
|
s.append(&time_distance_detail(data));
|
|
|
|
}
|
2023-12-25 05:36:13 +00:00
|
|
|
|
|
|
|
s
|
2023-12-22 23:53:29 +00:00
|
|
|
}
|
2023-12-22 22:32:45 +00:00
|
|
|
}
|
2024-01-16 04:27:55 +00:00
|
|
|
|
|
|
|
pub struct DayEditPrivate {
|
2024-01-18 14:00:08 +00:00
|
|
|
on_finished: RefCell<Box<dyn Fn()>>,
|
2024-02-08 04:36:03 +00:00
|
|
|
#[allow(unused)]
|
2024-01-29 14:18:36 +00:00
|
|
|
workout_rows: RefCell<gtk::Box>,
|
|
|
|
view_model: RefCell<Option<DayDetailViewModel>>,
|
2024-01-16 04:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for DayEditPrivate {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-01-18 14:00:08 +00:00
|
|
|
on_finished: RefCell::new(Box::new(|| {})),
|
2024-01-29 14:18:36 +00:00
|
|
|
workout_rows: RefCell::new(
|
|
|
|
gtk::Box::builder()
|
|
|
|
.orientation(gtk::Orientation::Vertical)
|
|
|
|
.hexpand(true)
|
|
|
|
.build(),
|
|
|
|
),
|
|
|
|
view_model: RefCell::new(None),
|
2024-01-16 04:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for DayEditPrivate {
|
|
|
|
const NAME: &'static str = "DayEdit";
|
|
|
|
type Type = DayEdit;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for DayEditPrivate {}
|
|
|
|
impl WidgetImpl for DayEditPrivate {}
|
|
|
|
impl BoxImpl for DayEditPrivate {}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
pub struct DayEdit(ObjectSubclass<DayEditPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DayEdit {
|
2024-01-18 14:00:08 +00:00
|
|
|
pub fn new<OnFinished>(view_model: DayDetailViewModel, on_finished: OnFinished) -> Self
|
2024-01-16 04:27:55 +00:00
|
|
|
where
|
2024-01-18 14:00:08 +00:00
|
|
|
OnFinished: Fn() + 'static,
|
2024-01-16 04:27:55 +00:00
|
|
|
{
|
|
|
|
let s: Self = Object::builder().build();
|
2024-01-18 03:13:55 +00:00
|
|
|
s.set_orientation(gtk::Orientation::Vertical);
|
|
|
|
s.set_hexpand(true);
|
2024-01-18 14:00:08 +00:00
|
|
|
*s.imp().on_finished.borrow_mut() = Box::new(on_finished);
|
2024-01-29 14:18:36 +00:00
|
|
|
*s.imp().view_model.borrow_mut() = Some(view_model.clone());
|
2024-01-18 14:00:08 +00:00
|
|
|
|
2024-01-23 13:46:35 +00:00
|
|
|
s.append(&control_buttons(&s, &view_model));
|
|
|
|
s.append(&weight_and_steps_row(&view_model));
|
2024-01-25 14:03:55 +00:00
|
|
|
s.append(&workout_buttons());
|
2024-01-20 20:59:03 +00:00
|
|
|
|
2024-01-16 04:27:55 +00:00
|
|
|
s
|
|
|
|
}
|
2024-01-18 14:00:08 +00:00
|
|
|
|
|
|
|
fn finish(&self) {
|
|
|
|
(self.imp().on_finished.borrow())()
|
|
|
|
}
|
2024-01-16 04:27:55 +00:00
|
|
|
}
|
2024-01-23 13:46:35 +00:00
|
|
|
|
|
|
|
fn control_buttons(s: &DayEdit, view_model: &DayDetailViewModel) -> ActionGroup {
|
|
|
|
ActionGroup::builder()
|
|
|
|
.primary_action("Save", {
|
|
|
|
let s = s.clone();
|
|
|
|
let view_model = view_model.clone();
|
|
|
|
move || {
|
|
|
|
view_model.save();
|
|
|
|
s.finish();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.secondary_action("Cancel", {
|
|
|
|
let s = s.clone();
|
|
|
|
let view_model = view_model.clone();
|
|
|
|
move || {
|
|
|
|
let s = s.clone();
|
|
|
|
let view_model = view_model.clone();
|
|
|
|
glib::spawn_future_local(async move {
|
|
|
|
view_model.revert().await;
|
|
|
|
s.finish();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
|
|
|
|
let row = gtk::Box::builder()
|
|
|
|
.orientation(gtk::Orientation::Horizontal)
|
|
|
|
.build();
|
|
|
|
row.append(
|
|
|
|
&weight_field(view_model.weight().map(WeightFormatter::from), {
|
|
|
|
let view_model = view_model.clone();
|
|
|
|
move |w| match w {
|
|
|
|
Some(w) => view_model.set_weight(*w),
|
|
|
|
None => eprintln!("have not implemented record delete"),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.widget(),
|
|
|
|
);
|
|
|
|
|
|
|
|
row.append(
|
|
|
|
&steps_editor(view_model.steps(), {
|
|
|
|
let view_model = view_model.clone();
|
|
|
|
move |s| match s {
|
|
|
|
Some(s) => view_model.set_steps(s),
|
|
|
|
None => eprintln!("have not implemented record delete"),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.widget(),
|
|
|
|
);
|
|
|
|
|
|
|
|
row
|
|
|
|
}
|
2024-01-25 14:03:55 +00:00
|
|
|
|
|
|
|
fn workout_buttons() -> gtk::Box {
|
|
|
|
let sunrise_button = gtk::Button::builder()
|
|
|
|
.icon_name("daytime-sunrise-symbolic")
|
|
|
|
.width_request(64)
|
|
|
|
.height_request(64)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let walking_button = gtk::Button::builder()
|
|
|
|
.icon_name("walking2-symbolic")
|
|
|
|
.width_request(64)
|
|
|
|
.height_request(64)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let running_button = gtk::Button::builder()
|
|
|
|
.icon_name("running-symbolic")
|
|
|
|
.width_request(64)
|
|
|
|
.height_request(64)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let layout = gtk::Box::builder()
|
|
|
|
.orientation(gtk::Orientation::Vertical)
|
|
|
|
.build();
|
|
|
|
let row = gtk::Box::builder()
|
|
|
|
.orientation(gtk::Orientation::Horizontal)
|
|
|
|
.build();
|
|
|
|
row.append(&sunrise_button);
|
|
|
|
row.append(&walking_button);
|
|
|
|
row.append(&running_button);
|
|
|
|
layout.append(&row);
|
|
|
|
|
|
|
|
layout
|
|
|
|
}
|