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;
|
2024-01-15 20:53:01 +00:00
|
|
|
use crate::components::{TimeDistanceView, Weight};
|
2023-12-26 15:45:50 +00:00
|
|
|
use emseries::Record;
|
2023-12-22 22:32:45 +00:00
|
|
|
use glib::Object;
|
|
|
|
use gtk::{prelude::*, subclass::prelude::*};
|
2024-01-02 04:58:55 +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
|
|
|
|
}
|
|
|
|
|
2024-01-15 20:53:01 +00:00
|
|
|
pub fn set_data(&self, date: chrono::NaiveDate, records: Vec<Record<ft_core::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);
|
|
|
|
}
|
|
|
|
|
2023-12-26 15:45:50 +00:00
|
|
|
if let Some(Record {
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::Weight(weight_record),
|
2023-12-26 15:45:50 +00:00
|
|
|
..
|
|
|
|
}) = records.iter().filter(|f| f.data.is_weight()).next()
|
2023-12-22 23:53:29 +00:00
|
|
|
{
|
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
|
|
|
|
2023-12-28 02:49:44 +00:00
|
|
|
/*
|
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(),
|
|
|
|
);
|
2023-12-28 02:49:44 +00:00
|
|
|
*/
|
2023-12-25 05:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 20:53:01 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct CommandRowPrivate;
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
impl ObjectSubclass for CommandRowPrivate {
|
|
|
|
const NAME: &'static str = "DayDetailCommandRow";
|
|
|
|
type Type = CommandRow;
|
|
|
|
type ParentType = gtk::Box;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ObjectImpl for CommandRowPrivate {}
|
|
|
|
impl WidgetImpl for CommandRowPrivate {}
|
|
|
|
impl BoxImpl for CommandRowPrivate {}
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
struct CommandRow(ObjectSubclass<CommandRowPrivate>) @extends gtk::Box, gtk::Widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandRow {
|
|
|
|
fn new() -> Self {
|
|
|
|
let s: Self = Object::builder().build();
|
|
|
|
s.set_halign(gtk::Align::End);
|
|
|
|
s.append(>k::Button::builder().label("Edit").build());
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-25 05:36:13 +00:00
|
|
|
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 {
|
2023-12-29 14:24:37 +00:00
|
|
|
pub fn new<PutRecordFn, UpdateRecordFn>(
|
2023-12-28 14:51:41 +00:00
|
|
|
date: chrono::NaiveDate,
|
2024-01-15 20:53:01 +00:00
|
|
|
records: Vec<Record<ft_core::TraxRecord>>,
|
2023-12-29 14:24:37 +00:00
|
|
|
on_put_record: PutRecordFn,
|
2023-12-28 14:51:41 +00:00
|
|
|
on_update_record: UpdateRecordFn,
|
|
|
|
) -> Self
|
|
|
|
where
|
2024-01-15 20:53:01 +00:00
|
|
|
PutRecordFn: Fn(ft_core::TraxRecord) + 'static,
|
|
|
|
UpdateRecordFn: Fn(Record<ft_core::TraxRecord>) + '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-15 20:53:01 +00:00
|
|
|
s.append(&CommandRow::new());
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2023-12-26 15:45:50 +00:00
|
|
|
let weight_view = match weight_record {
|
2024-01-15 20:53:01 +00:00
|
|
|
Some((id, data)) => Weight::new(Some(data.clone()), move |weight| {
|
2023-12-28 14:51:41 +00:00
|
|
|
on_update_record(Record {
|
|
|
|
id: id.clone(),
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::Weight(ft_core::Weight { date, weight }),
|
2023-12-28 14:51:41 +00:00
|
|
|
})
|
2023-12-26 15:45:50 +00:00
|
|
|
}),
|
2024-01-15 20:53:01 +00:00
|
|
|
None => Weight::new(None, move |weight| {
|
|
|
|
on_put_record(ft_core::TraxRecord::Weight(ft_core::Weight {
|
|
|
|
date,
|
|
|
|
weight,
|
|
|
|
}));
|
2023-12-26 15:45:50 +00:00
|
|
|
}),
|
|
|
|
};
|
2024-01-15 20:53:01 +00:00
|
|
|
s.append(&weight_view.widget());
|
2023-12-26 15:24:19 +00:00
|
|
|
|
2023-12-25 05:36:13 +00:00
|
|
|
records.into_iter().for_each(|record| {
|
|
|
|
let record_view = match record {
|
2023-12-26 15:45:50 +00:00
|
|
|
Record {
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::BikeRide(record),
|
2023-12-26 15:45:50 +00:00
|
|
|
..
|
|
|
|
} => Some(
|
2024-01-15 20:53:01 +00:00
|
|
|
TimeDistanceView::new(ft_core::RecordType::BikeRide, record)
|
|
|
|
.upcast::<gtk::Widget>(),
|
2023-12-26 15:24:19 +00:00
|
|
|
),
|
2023-12-26 15:45:50 +00:00
|
|
|
Record {
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::Row(record),
|
2023-12-26 15:45:50 +00:00
|
|
|
..
|
2024-01-15 20:53:01 +00:00
|
|
|
} => Some(
|
|
|
|
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
|
|
|
|
),
|
2023-12-26 15:45:50 +00:00
|
|
|
Record {
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::Run(record),
|
2023-12-26 15:45:50 +00:00
|
|
|
..
|
2024-01-15 20:53:01 +00:00
|
|
|
} => Some(
|
|
|
|
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
|
|
|
|
),
|
2023-12-26 15:45:50 +00:00
|
|
|
Record {
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::Swim(record),
|
2023-12-26 15:45:50 +00:00
|
|
|
..
|
2024-01-15 20:53:01 +00:00
|
|
|
} => Some(
|
|
|
|
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
|
|
|
|
),
|
2023-12-26 15:45:50 +00:00
|
|
|
Record {
|
2024-01-15 20:53:01 +00:00
|
|
|
data: ft_core::TraxRecord::Walk(record),
|
2023-12-26 15:45:50 +00:00
|
|
|
..
|
2024-01-15 20:53:01 +00:00
|
|
|
} => Some(
|
|
|
|
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
|
|
|
|
),
|
2023-12-26 15:24:19 +00:00
|
|
|
_ => 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
|
|
|
}
|