/* Copyright 2023, Savanni D'Gerinel 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 . */ 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)] pub struct WeightViewPrivate {} pub struct Weight { label: gtk::Label, } impl Weight { pub fn new(weight: Option>) -> 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() } } #[derive(Debug)] pub struct WeightEdit { entry: TextEntry>, } impl WeightEdit { pub fn new(weight: Option>, on_update: OnUpdate) -> Self where OnUpdate: Fn(si::Kilogram) + 'static, { Self { entry: TextEntry::new( "0 kg", weight, |val: &si::Kilogram| val.to_string(), move |v: &str| { let new_weight = v.parse::().map(|w| w * si::KG).map_err(|_| ParseError); match new_weight { Ok(w) => { on_update(w); Ok(w) } Err(err) => Err(err), } }, ), } } pub fn set_value(&self, value: Option>) { self.entry.set_value(value); } pub fn value(&self) -> Option> { self.entry.value() } pub fn widget(&self) -> gtk::Widget { self.entry.widget() } }