Create an EditView "component"
This is a super tiny data structure that covers an edit mode, a view mode, and an unconfigured mode. It's mostly a container so that views don't have to preserve everything directly.
This commit is contained in:
parent
2e3d5fc5a4
commit
b7b9b1b29f
|
@ -16,7 +16,7 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
|
|
||||||
// use chrono::NaiveDate;
|
// use chrono::NaiveDate;
|
||||||
// use ft_core::TraxRecord;
|
// use ft_core::TraxRecord;
|
||||||
use crate::components::{ParseError, TextEntry};
|
use crate::components::{EditView, ParseError, TextEntry};
|
||||||
use chrono::{Local, NaiveDate};
|
use chrono::{Local, NaiveDate};
|
||||||
use dimensioned::si;
|
use dimensioned::si;
|
||||||
use emseries::Record;
|
use emseries::Record;
|
||||||
|
@ -221,49 +221,21 @@ impl DayDetail {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq)]
|
|
||||||
enum WeightViewMode {
|
|
||||||
View,
|
|
||||||
Edit,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WeightViewPrivate {
|
pub struct WeightViewPrivate {
|
||||||
date: RefCell<NaiveDate>,
|
date: RefCell<NaiveDate>,
|
||||||
record: RefCell<Option<Weight>>,
|
record: RefCell<Option<Weight>>,
|
||||||
view: RefCell<gtk::Label>,
|
|
||||||
edit: RefCell<TextEntry<si::Kilogram<f64>>>,
|
|
||||||
current: RefCell<gtk::Widget>,
|
|
||||||
mode: RefCell<WeightViewMode>,
|
|
||||||
|
|
||||||
// If I create a swappable component and don't make it a true GTK component, the way
|
widget: RefCell<EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>>,
|
||||||
// TextEntry<T> ended up not a true GTK component, on_edit_finished will not need to be a
|
|
||||||
// RefCell.
|
|
||||||
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for WeightViewPrivate {
|
impl Default for WeightViewPrivate {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let view = gtk::Label::builder()
|
|
||||||
.css_classes(["card", "weight-view"])
|
|
||||||
.halign(gtk::Align::Start)
|
|
||||||
.can_focus(true)
|
|
||||||
.build();
|
|
||||||
let edit = TextEntry::<si::Kilogram<f64>>::new(
|
|
||||||
"weight",
|
|
||||||
None,
|
|
||||||
|w: &si::Kilogram<f64>| w.to_string(),
|
|
||||||
|w: &str| w.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError),
|
|
||||||
);
|
|
||||||
|
|
||||||
let current = view.clone();
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
date: RefCell::new(Local::now().date_naive()),
|
date: RefCell::new(Local::now().date_naive()),
|
||||||
record: RefCell::new(None),
|
record: RefCell::new(None),
|
||||||
view: RefCell::new(view),
|
widget: RefCell::new(EditView::Unconfigured),
|
||||||
edit: RefCell::new(edit),
|
|
||||||
current: RefCell::new(current.upcast()),
|
|
||||||
mode: RefCell::new(WeightViewMode::View),
|
|
||||||
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -301,20 +273,26 @@ impl WeightView {
|
||||||
*s.imp().record.borrow_mut() = weight;
|
*s.imp().record.borrow_mut() = weight;
|
||||||
s.view();
|
s.view();
|
||||||
|
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self) {
|
||||||
|
let view = gtk::Label::builder()
|
||||||
|
.css_classes(["card", "weight-view"])
|
||||||
|
.halign(gtk::Align::Start)
|
||||||
|
.can_focus(true)
|
||||||
|
.build();
|
||||||
|
|
||||||
let view_click_controller = gtk::GestureClick::new();
|
let view_click_controller = gtk::GestureClick::new();
|
||||||
view_click_controller.connect_released({
|
view_click_controller.connect_released({
|
||||||
let s = s.clone();
|
let s = self.clone();
|
||||||
move |_, _, _, _| {
|
move |_, _, _, _| {
|
||||||
s.edit();
|
s.edit();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
s.imp().view.borrow().add_controller(view_click_controller);
|
view.add_controller(view_click_controller);
|
||||||
s
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view(&self) {
|
|
||||||
let view = self.imp().view.borrow();
|
|
||||||
match *self.imp().record.borrow() {
|
match *self.imp().record.borrow() {
|
||||||
Some(ref record) => {
|
Some(ref record) => {
|
||||||
view.remove_css_class("dim_label");
|
view.remove_css_class("dim_label");
|
||||||
|
@ -325,35 +303,49 @@ impl WeightView {
|
||||||
view.set_label("No weight recorded");
|
view.set_label("No weight recorded");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*self.imp().mode.borrow_mut() = WeightViewMode::View;
|
|
||||||
self.swap(view.clone().upcast());
|
self.swap(EditView::View(view));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn edit(&self) {
|
fn edit(&self) {
|
||||||
let edit = self.imp().edit.borrow();
|
let edit = TextEntry::<si::Kilogram<f64>>::new(
|
||||||
|
"weight",
|
||||||
|
None,
|
||||||
|
|val: &si::Kilogram<f64>| val.to_string(),
|
||||||
|
|v: &str| v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError),
|
||||||
|
);
|
||||||
|
|
||||||
match *self.imp().record.borrow() {
|
match *self.imp().record.borrow() {
|
||||||
Some(ref record) => edit.set_value(Some(record.weight)),
|
Some(ref record) => edit.set_value(Some(record.weight)),
|
||||||
None => edit.set_value(None),
|
None => edit.set_value(None),
|
||||||
}
|
}
|
||||||
*self.imp().mode.borrow_mut() = WeightViewMode::Edit;
|
|
||||||
self.swap(edit.widget());
|
self.swap(EditView::Edit(edit.clone()));
|
||||||
edit.grab_focus();
|
edit.grab_focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn swap(&self, new_view: gtk::Widget) {
|
fn swap(&self, new_view: EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>) {
|
||||||
let mut current = self.imp().current.borrow_mut();
|
let mut widget = self.imp().widget.borrow_mut();
|
||||||
self.remove(&*current);
|
match *widget {
|
||||||
self.append(&new_view);
|
EditView::Unconfigured => {}
|
||||||
*current = new_view;
|
EditView::View(ref view) => self.remove(view),
|
||||||
|
EditView::Edit(ref editor) => self.remove(&editor.widget()),
|
||||||
|
}
|
||||||
|
|
||||||
|
match new_view {
|
||||||
|
EditView::Unconfigured => {}
|
||||||
|
EditView::View(ref view) => self.append(view),
|
||||||
|
EditView::Edit(ref editor) => self.append(&editor.widget()),
|
||||||
|
}
|
||||||
|
*widget = new_view;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn blur(&self) {
|
fn blur(&self) {
|
||||||
if *self.imp().mode.borrow() == WeightViewMode::Edit {
|
match *self.imp().widget.borrow() {
|
||||||
println!("on_blur");
|
EditView::Unconfigured => {}
|
||||||
let weight = self.imp().edit.borrow().value();
|
EditView::View(_) => {}
|
||||||
|
EditView::Edit(ref editor) => {
|
||||||
println!("new weight: {:?}", weight);
|
let weight = editor.value();
|
||||||
|
|
||||||
// This has really turned into rubbish
|
// This has really turned into rubbish
|
||||||
// on_edit_finished needs to accept a full record now.
|
// on_edit_finished needs to accept a full record now.
|
||||||
// needs to be possible to delete a record if the value is None
|
// needs to be possible to delete a record if the value is None
|
||||||
|
@ -378,8 +370,6 @@ impl WeightView {
|
||||||
(_, None) => None,
|
(_, None) => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("updated record: {:?}", record);
|
|
||||||
|
|
||||||
match record {
|
match record {
|
||||||
Some(record) => {
|
Some(record) => {
|
||||||
self.imp().on_edit_finished.borrow()(record.weight);
|
self.imp().on_edit_finished.borrow()(record.weight);
|
||||||
|
@ -388,6 +378,8 @@ impl WeightView {
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.view();
|
self.view();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
/*
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum EditView<View, Edit> {
|
||||||
|
Unconfigured,
|
||||||
|
View(View),
|
||||||
|
Edit(Edit),
|
||||||
|
}
|
|
@ -17,6 +17,9 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
mod day;
|
mod day;
|
||||||
pub use day::{DayDetail, DaySummary};
|
pub use day::{DayDetail, DaySummary};
|
||||||
|
|
||||||
|
mod edit_view;
|
||||||
|
pub use edit_view::EditView;
|
||||||
|
|
||||||
mod text_entry;
|
mod text_entry;
|
||||||
pub use text_entry::{ParseError, TextEntry};
|
pub use text_entry::{ParseError, TextEntry};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue