monorepo/fitnesstrax/app/src/components/weight.rs

92 lines
2.6 KiB
Rust
Raw Normal View History

/*
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 crate::components::{EditView, ParseError, Singleton, TextEntry};
use chrono::{Local, NaiveDate};
use dimensioned::si;
use glib::{object::ObjectRef, Object};
use gtk::{prelude::*, subclass::prelude::*};
use std::{borrow::Borrow, cell::RefCell};
#[derive(Default)]
2024-01-18 03:13:55 +00:00
pub struct WeightViewPrivate {}
pub struct Weight {
label: gtk::Label,
}
impl Weight {
pub fn new(weight: Option<si::Kilogram<f64>>) -> Self {
let label = gtk::Label::builder()
.css_classes(["card", "weight-view"])
.can_focus(true)
.build();
match weight {
Some(w) => label.set_text(&format!("{:?}", w)),
None => label.set_text("No weight recorded"),
}
Self { label }
}
pub fn widget(&self) -> gtk::Widget {
self.label.clone().upcast()
}
}
2024-01-16 04:27:55 +00:00
#[derive(Debug)]
2024-01-16 04:27:55 +00:00
pub struct WeightEdit {
entry: TextEntry<si::Kilogram<f64>>,
}
impl WeightEdit {
pub fn new<OnUpdate>(weight: Option<si::Kilogram<f64>>, on_update: OnUpdate) -> Self
where
OnUpdate: Fn(si::Kilogram<f64>) + 'static,
{
2024-01-16 04:27:55 +00:00
Self {
entry: TextEntry::new(
"0 kg",
weight,
2024-01-16 04:27:55 +00:00
|val: &si::Kilogram<f64>| val.to_string(),
move |v: &str| {
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError);
match new_weight {
Ok(w) => {
on_update(w);
Ok(w)
}
Err(err) => Err(err),
}
},
2024-01-16 04:27:55 +00:00
),
}
}
pub fn set_value(&self, value: Option<si::Kilogram<f64>>) {
self.entry.set_value(value);
}
2024-01-16 04:27:55 +00:00
pub fn value(&self) -> Option<si::Kilogram<f64>> {
self.entry.value()
}
pub fn widget(&self) -> gtk::Widget {
self.entry.widget()
}
}