From 56d0a536669e35807f789783c120b91576d43d9e Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 17 Jan 2024 22:35:13 -0500 Subject: [PATCH] Fix how DayEdit deals with the weight field --- fitnesstrax/app/src/components/day.rs | 8 ++++---- fitnesstrax/app/src/components/text_entry.rs | 17 +++++++++++++++-- fitnesstrax/app/src/components/weight.rs | 5 +++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index c72a270..31f01f0 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -309,11 +309,11 @@ impl DayEdit { _ => None, }); - let weight_view = match weight_record { - Some((_id, data)) => WeightEdit::new(Some(data.clone())), - None => WeightEdit::new(None), + match weight_record { + Some((_id, data)) => s.imp().weight.set_value(Some(data.weight)), + None => s.imp().weight.set_value(None), }; - s.append(&weight_view.widget()); + s.append(&s.imp().weight.widget()); s } diff --git a/fitnesstrax/app/src/components/text_entry.rs b/fitnesstrax/app/src/components/text_entry.rs index 750ca2e..cc6c2a0 100644 --- a/fitnesstrax/app/src/components/text_entry.rs +++ b/fitnesstrax/app/src/components/text_entry.rs @@ -20,15 +20,25 @@ use std::{cell::RefCell, rc::Rc}; pub struct ParseError; #[derive(Clone)] -pub struct TextEntry { +pub struct TextEntry { value: Rc>>, widget: gtk::Entry, renderer: Rc String>>, parser: Rc Result>>, } +impl std::fmt::Debug for TextEntry { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + write!( + f, + "{{ value: {:?}, widget: {:?} }}", + self.value, self.widget + ) + } +} + // I do not understand why the data should be 'static. -impl TextEntry { +impl TextEntry { pub fn new(placeholder: &str, value: Option, renderer: R, parser: V) -> Self where R: Fn(&T) -> String + 'static, @@ -63,6 +73,7 @@ impl TextEntry { } match (self.parser)(buffer.text().as_str()) { Ok(v) => { + println!("setting the value: {}", (self.renderer)(&v)); *self.value.borrow_mut() = Some(v); self.widget.remove_css_class("error"); } @@ -74,6 +85,8 @@ impl TextEntry { } pub fn value(&self) -> Option { + let v = self.value.borrow().clone(); + println!("retrieving the value: {:?}", v.map(|v| (self.renderer)(&v))); self.value.borrow().clone() } diff --git a/fitnesstrax/app/src/components/weight.rs b/fitnesstrax/app/src/components/weight.rs index e2fb631..552b107 100644 --- a/fitnesstrax/app/src/components/weight.rs +++ b/fitnesstrax/app/src/components/weight.rs @@ -48,6 +48,7 @@ impl Weight { } } +#[derive(Debug)] pub struct WeightEdit { entry: TextEntry>, } @@ -64,6 +65,10 @@ impl WeightEdit { } } + pub fn set_value(&self, value: Option>) { + self.entry.set_value(value); + } + pub fn value(&self) -> Option> { self.entry.value() }