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

80 lines
2.2 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 {
2024-01-16 04:27:55 +00:00
pub fn new(weight: Option<ft_core::Weight>) -> Self {
let label = gtk::Label::builder()
.css_classes(["card", "weight-view"])
.can_focus(true)
.build();
match weight {
Some(w) => label.set_text(&format!("{:?}", w.weight)),
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(weight: Option<ft_core::Weight>) -> Self {
Self {
entry: TextEntry::new(
"0 kg",
weight.map(|w| w.weight),
|val: &si::Kilogram<f64>| val.to_string(),
|v: &str| v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError),
),
}
}
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()
}
}