2023-12-22 22:32:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
|
|
|
|
|
|
|
|
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;
|
2023-12-26 15:24:19 +00:00
|
|
|
use dimensioned::si;
|
|
|
|
use ft_core::{RecordType, TimeDistance, TraxRecord, Weight};
|
2023-12-22 22:32:45 +00:00
|
|
|
use glib::Object;
|
|
|
|
use gtk::{prelude::*, subclass::prelude::*};
|
2023-12-22 23:53:29 +00:00
|
|
|
use std::cell::RefCell;
|
2023-12-22 22:32:45 +00:00
|
|
|
|
|
|
|
pub struct DaySummaryPrivate {
|
|
|
|
date: gtk::Label,
|
2023-12-22 23:53:29 +00:00
|
|
|
weight: RefCell<Option<gtk::Label>>,
|
2023-12-22 22:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
2023-12-22 22:32:45 +00:00
|
|
|
Self {
|
2023-12-24 17:00:12 +00:00
|
|
|
date,
|
2023-12-22 23:53:29 +00:00
|
|
|
weight: RefCell::new(None),
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DaySummary {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-24 17:00:12 +00:00
|
|
|
pub fn set_data(&self, date: chrono::NaiveDate, records: Vec<TraxRecord>) {
|
2023-12-22 22:32:45 +00:00
|
|
|
self.imp()
|
|
|
|
.date
|
2023-12-25 00:13:49 +00:00
|
|
|
.set_text(&date.format("%Y-%m-%d").to_string());
|
2023-12-22 23:53:29 +00:00
|
|
|
|
|
|
|
if let Some(ref weight_label) = *self.imp().weight.borrow() {
|
|
|
|
self.remove(weight_label);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(TraxRecord::Weight(weight_record)) =
|
|
|
|
records.iter().filter(|f| f.is_weight()).next()
|
|
|
|
{
|
2023-12-24 17:00:12 +00:00
|
|
|
let label = gtk::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.label(&format!("{}", weight_record.weight))
|
2023-12-25 05:36:13 +00:00
|
|
|
.css_classes(["day-summary__weight"])
|
2023-12-24 17:00:12 +00:00
|
|
|
.build();
|
2023-12-22 23:53:29 +00:00
|
|
|
self.append(&label);
|
|
|
|
*self.imp().weight.borrow_mut() = Some(label);
|
|
|
|
}
|
2023-12-25 05:36:13 +00:00
|
|
|
|
|
|
|
self.append(
|
|
|
|
>k::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.label("15km of biking in 60 minutes")
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DayDetailPrivate {
|
|
|
|
date: gtk::Label,
|
|
|
|
weight: RefCell<Option<gtk::Label>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for DayDetailPrivate {
|
|
|
|
const NAME: &'static str = "DayDetail";
|
|
|
|
type Type = DayDetail;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
|
|
|
|
fn new() -> Self {
|
|
|
|
let date = gtk::Label::builder()
|
|
|
|
.css_classes(["daysummary-date"])
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.build();
|
|
|
|
Self {
|
|
|
|
date,
|
|
|
|
weight: RefCell::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
pub fn new(date: chrono::NaiveDate, records: Vec<TraxRecord>) -> Self {
|
|
|
|
let s: Self = Object::builder().build();
|
|
|
|
s.set_orientation(gtk::Orientation::Vertical);
|
|
|
|
|
2023-12-26 15:24:19 +00:00
|
|
|
let weight_record = records.iter().find_map(|record| match record {
|
|
|
|
TraxRecord::Weight(record) => Some(record.clone()),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
|
|
|
let weight_view = WeightView::new(weight_record, |weight| {
|
|
|
|
println!("on_blur on the weight view: {:?}", weight)
|
|
|
|
});
|
|
|
|
s.append(&weight_view);
|
|
|
|
|
2023-12-25 05:36:13 +00:00
|
|
|
records.into_iter().for_each(|record| {
|
|
|
|
let record_view = match record {
|
2023-12-26 15:24:19 +00:00
|
|
|
TraxRecord::BikeRide(record) => Some(
|
|
|
|
TimeDistanceView::new(RecordType::BikeRide, record).upcast::<gtk::Widget>(),
|
|
|
|
),
|
|
|
|
TraxRecord::Row(record) => {
|
|
|
|
Some(TimeDistanceView::new(RecordType::Row, record).upcast::<gtk::Widget>())
|
|
|
|
}
|
|
|
|
TraxRecord::Run(record) => {
|
|
|
|
Some(TimeDistanceView::new(RecordType::Row, record).upcast::<gtk::Widget>())
|
|
|
|
}
|
|
|
|
TraxRecord::Swim(record) => {
|
|
|
|
Some(TimeDistanceView::new(RecordType::Row, record).upcast::<gtk::Widget>())
|
|
|
|
}
|
|
|
|
TraxRecord::Walk(record) => {
|
|
|
|
Some(TimeDistanceView::new(RecordType::Row, record).upcast::<gtk::Widget>())
|
|
|
|
}
|
|
|
|
_ => None,
|
2023-12-25 05:36:13 +00:00
|
|
|
};
|
|
|
|
|
2023-12-26 15:24:19 +00:00
|
|
|
if let Some(record_view) = record_view {
|
|
|
|
record_view.add_css_class("day-detail");
|
|
|
|
record_view.set_halign(gtk::Align::Start);
|
2023-12-25 05:36:13 +00:00
|
|
|
|
2023-12-26 15:24:19 +00:00
|
|
|
s.append(&record_view);
|
|
|
|
}
|
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
|
|
|
}
|
2023-12-26 15:24:19 +00:00
|
|
|
|
|
|
|
pub struct WeightViewPrivate {
|
|
|
|
record: RefCell<Option<Weight>>,
|
|
|
|
view: RefCell<gtk::Label>,
|
|
|
|
edit: RefCell<gtk::Entry>,
|
|
|
|
current: RefCell<gtk::Widget>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for WeightViewPrivate {
|
|
|
|
fn default() -> Self {
|
|
|
|
let view = gtk::Label::builder()
|
|
|
|
.css_classes(["card", "weight-view"])
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.can_focus(true)
|
|
|
|
.build();
|
|
|
|
let edit = gtk::Entry::builder().halign(gtk::Align::Start).build();
|
|
|
|
|
|
|
|
let current = view.clone();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
record: RefCell::new(None),
|
|
|
|
view: RefCell::new(view),
|
|
|
|
edit: RefCell::new(edit),
|
|
|
|
current: RefCell::new(current.upcast()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for WeightViewPrivate {
|
|
|
|
const NAME: &'static str = "WeightView";
|
|
|
|
type Type = WeightView;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for WeightViewPrivate {}
|
|
|
|
impl WidgetImpl for WeightViewPrivate {}
|
|
|
|
impl BoxImpl for WeightViewPrivate {}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
pub struct WeightView(ObjectSubclass<WeightViewPrivate>) @extends gtk::Box, gtk::Widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WeightView {
|
|
|
|
pub fn new<OnBlur>(weight: Option<Weight>, on_blur: OnBlur) -> Self
|
|
|
|
where
|
|
|
|
OnBlur: Fn(si::Kilogram<f64>),
|
|
|
|
{
|
|
|
|
let s: Self = Object::builder().build();
|
|
|
|
|
|
|
|
*s.imp().record.borrow_mut() = weight;
|
|
|
|
s.view();
|
|
|
|
|
|
|
|
let view_click_controller = gtk::GestureClick::new();
|
|
|
|
view_click_controller.connect_released({
|
|
|
|
let s = s.clone();
|
|
|
|
move |_, _, _, _| {
|
|
|
|
s.edit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let edit_click_controller = gtk::GestureClick::new();
|
|
|
|
edit_click_controller.connect_released({
|
|
|
|
let s = s.clone();
|
|
|
|
move |_, _, _, _| {
|
|
|
|
s.view();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
s.imp().view.borrow().add_controller(view_click_controller);
|
|
|
|
s.imp().edit.borrow().add_controller(edit_click_controller);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) {
|
|
|
|
let view = self.imp().view.borrow();
|
|
|
|
match *self.imp().record.borrow() {
|
|
|
|
Some(ref record) => {
|
|
|
|
view.remove_css_class("dim_label");
|
|
|
|
view.set_label(&format!("{:?}", record.weight));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
view.add_css_class("dim_label");
|
|
|
|
view.set_label("No weight recorded");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.swap(view.clone().upcast());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn edit(&self) {
|
|
|
|
let edit = self.imp().edit.borrow();
|
|
|
|
match *self.imp().record.borrow() {
|
|
|
|
Some(ref record) => edit.buffer().set_text(&format!("{:?}", record.weight)),
|
|
|
|
None => edit.buffer().set_text(""),
|
|
|
|
}
|
|
|
|
self.swap(edit.clone().upcast());
|
|
|
|
edit.grab_focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn swap(&self, new_view: gtk::Widget) {
|
|
|
|
let mut current = self.imp().current.borrow_mut();
|
|
|
|
self.remove(&*current);
|
|
|
|
self.append(&new_view);
|
|
|
|
*current = new_view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TimeDistanceViewPrivate {
|
|
|
|
record: RefCell<Option<TimeDistance>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for TimeDistanceViewPrivate {
|
|
|
|
const NAME: &'static str = "TimeDistanceView";
|
|
|
|
type Type = TimeDistanceView;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for TimeDistanceViewPrivate {}
|
|
|
|
impl WidgetImpl for TimeDistanceViewPrivate {}
|
|
|
|
impl BoxImpl for TimeDistanceViewPrivate {}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
pub struct TimeDistanceView(ObjectSubclass<TimeDistanceViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TimeDistanceView {
|
|
|
|
pub fn new(type_: RecordType, record: TimeDistance) -> Self {
|
|
|
|
let s: Self = Object::builder().build();
|
|
|
|
s.set_orientation(gtk::Orientation::Vertical);
|
|
|
|
s.set_hexpand(true);
|
|
|
|
|
|
|
|
let first_row = gtk::Box::builder().homogeneous(true).build();
|
|
|
|
|
|
|
|
first_row.append(
|
|
|
|
>k::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.label(&record.datetime.format("%H:%M").to_string())
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
first_row.append(
|
|
|
|
>k::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.label(format!("{:?}", type_))
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
first_row.append(
|
|
|
|
>k::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.label(
|
|
|
|
record
|
|
|
|
.distance
|
|
|
|
.map(|dist| format!("{}", dist))
|
|
|
|
.unwrap_or("".to_owned()),
|
|
|
|
)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
first_row.append(
|
|
|
|
>k::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.label(
|
|
|
|
record
|
|
|
|
.duration
|
|
|
|
.map(|duration| format!("{}", duration))
|
|
|
|
.unwrap_or("".to_owned()),
|
|
|
|
)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
s.append(&first_row);
|
|
|
|
|
|
|
|
s.append(
|
|
|
|
>k::Label::builder()
|
|
|
|
.halign(gtk::Align::Start)
|
|
|
|
.label(
|
|
|
|
record
|
|
|
|
.comments
|
|
|
|
.map(|comments| format!("{}", comments))
|
|
|
|
.unwrap_or("".to_owned()),
|
|
|
|
)
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|