Show the weight for each day as a summary in some hard-coded records. #128

Merged
savanni merged 4 commits from fitnesstrax/daily-record into main 2023-12-25 00:21:02 +00:00
6 changed files with 39 additions and 5 deletions
Showing only changes of commit 3dc8be0d26 - Show all commits

1
Cargo.lock generated
View File

@ -1022,6 +1022,7 @@ version = "0.1.0"
dependencies = [
"async-channel",
"chrono",
"dimensioned 0.8.0",
"emseries",
"ft-core",
"gio",

View File

@ -9,6 +9,7 @@ edition = "2021"
adw = { version = "0.5", package = "libadwaita", features = [ "v1_2" ] }
async-channel = { version = "2.1" }
chrono = { version = "0.4" }
dimensioned = { version = "0.8", features = [ "serde" ] }
emseries = { path = "../../emseries" }
ft-core = { path = "../core" }
gio = { version = "0.18" }

View File

@ -16,12 +16,15 @@ You should have received a copy of the GNU General Public License along with Fit
// use chrono::NaiveDate;
// use ft_core::TraxRecord;
use ft_core::TraxRecord;
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell;
#[derive(Default)]
pub struct DaySummaryPrivate {
date: gtk::Label,
weight: RefCell<Option<gtk::Label>>,
}
#[glib::object_subclass]
@ -33,6 +36,7 @@ impl ObjectSubclass for DaySummaryPrivate {
fn new() -> Self {
Self {
date: gtk::Label::new(None),
weight: RefCell::new(None),
}
}
}
@ -60,4 +64,18 @@ impl DaySummary {
.date
.set_text(&date.format("%y-%m-%d").to_string());
}
pub fn set_records(&self, records: Vec<TraxRecord>) {
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()
{
let label = gtk::Label::new(Some(&format!("{}", weight_record.weight)));
self.append(&label);
*self.imp().weight.borrow_mut() = Some(label);
}
}
}

View File

@ -15,7 +15,8 @@ You should have received a copy of the GNU General Public License along with Fit
*/
use crate::components::DaySummary;
use ft_core::TraxRecord;
use dimensioned::si::KG;
use ft_core::{TraxRecord, Weight};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell;
@ -56,7 +57,10 @@ impl HistoricalView {
let day_records: Vec<DayRecords> = vec![DayRecords::new(
chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
vec![],
vec![TraxRecord::Weight(Weight {
date: chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
weight: 100. * KG,
})],
)];
let model = gio::ListStore::new::<DayRecords>();
@ -86,6 +90,7 @@ impl HistoricalView {
.expect("should be a DaySummary");
summary.set_date(records.date());
summary.set_records(records.records());
});
let lst = gtk::ListView::builder()

View File

@ -4,4 +4,4 @@ use emseries::DateTimeTz;
mod legacy;
mod types;
pub use types::TraxRecord;
pub use types::{TraxRecord, Weight};

View File

@ -48,8 +48,8 @@ pub struct TimeDistance {
/// need to track more than a single weight in a day.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Weight {
date: NaiveDate,
weight: si::Kilogram<f64>,
pub date: NaiveDate,
pub weight: si::Kilogram<f64>,
}
/// The unified data structure for all records that are part of the app.
@ -64,6 +64,15 @@ pub enum TraxRecord {
Weight(Weight),
}
impl TraxRecord {
pub fn is_weight(&self) -> bool {
match self {
TraxRecord::Weight(_) => true,
_ => false,
}
}
}
impl Recordable for TraxRecord {
fn timestamp(&self) -> Timestamp {
match self {