Compare commits

..

No commits in common. "c075b7ed6e8366193dfe9435613cc14a3239cd20" and "104760c754d200cef656d7ca2c5e00a957e08248" have entirely different histories.

6 changed files with 264 additions and 238 deletions

View File

@ -1,135 +0,0 @@
/*
Copyright 2024, 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 glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
#[derive(Default)]
pub struct ActionGroupPrivate;
#[glib::object_subclass]
impl ObjectSubclass for ActionGroupPrivate {
const NAME: &'static str = "ActionGroup";
type Type = ActionGroup;
type ParentType = gtk::Box;
}
impl ObjectImpl for ActionGroupPrivate {}
impl WidgetImpl for ActionGroupPrivate {}
impl BoxImpl for ActionGroupPrivate {}
glib::wrapper! {
pub struct ActionGroup(ObjectSubclass<ActionGroupPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl ActionGroup {
fn new(builder: ActionGroupBuilder) -> Self {
let s: Self = Object::builder().build();
s.set_orientation(builder.orientation);
let primary_button = builder.primary_action.button();
let secondary_button = builder.secondary_action.map(|action| action.button());
let tertiary_button = builder.tertiary_action.map(|action| action.button());
if let Some(button) = tertiary_button {
s.append(&button);
}
s.set_halign(gtk::Align::End);
if let Some(button) = secondary_button {
s.append(&button);
}
s.append(&primary_button);
s
}
pub fn builder() -> ActionGroupBuilder {
ActionGroupBuilder {
orientation: gtk::Orientation::Horizontal,
primary_action: Action {
label: "Ok".to_owned(),
action: Box::new(|| {}),
},
secondary_action: None,
tertiary_action: None,
}
}
}
struct Action {
label: String,
action: Box<dyn Fn()>,
}
impl Action {
fn button(self) -> gtk::Button {
let button = gtk::Button::builder().label(self.label).build();
button.connect_clicked(move |_| (self.action)());
button
}
}
pub struct ActionGroupBuilder {
orientation: gtk::Orientation,
primary_action: Action,
secondary_action: Option<Action>,
tertiary_action: Option<Action>,
}
impl ActionGroupBuilder {
pub fn orientation(mut self, orientation: gtk::Orientation) -> Self {
self.orientation = orientation;
self
}
pub fn primary_action<A>(mut self, label: &str, action: A) -> Self
where
A: Fn() + 'static,
{
self.primary_action = Action {
label: label.to_owned(),
action: Box::new(action),
};
self
}
pub fn secondary_action<A>(mut self, label: &str, action: A) -> Self
where
A: Fn() + 'static,
{
self.secondary_action = Some(Action {
label: label.to_owned(),
action: Box::new(action),
});
self
}
pub fn tertiary_action<A>(mut self, label: &str, action: A) -> Self
where
A: Fn() + 'static,
{
self.tertiary_action = Some(Action {
label: label.to_owned(),
action: Box::new(action),
});
self
}
pub fn build(self) -> ActionGroup {
ActionGroup::new(self)
}
}

View File

@ -16,11 +16,11 @@ 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::{ActionGroup, TimeDistanceView, Weight}; use crate::components::{TimeDistanceView, Weight};
use emseries::Record; use emseries::Record;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, rc::Rc}; use std::cell::RefCell;
use super::weight::WeightEdit; use super::weight::WeightEdit;
@ -102,6 +102,40 @@ impl DaySummary {
} }
} }
#[derive(Default)]
struct CommandRowPrivate;
#[glib::object_subclass]
impl ObjectSubclass for CommandRowPrivate {
const NAME: &'static str = "DayDetailCommandRow";
type Type = CommandRow;
type ParentType = gtk::Box;
}
impl ObjectImpl for CommandRowPrivate {}
impl WidgetImpl for CommandRowPrivate {}
impl BoxImpl for CommandRowPrivate {}
glib::wrapper! {
struct CommandRow(ObjectSubclass<CommandRowPrivate>) @extends gtk::Box, gtk::Widget;
}
impl CommandRow {
fn new<OnEdit>(on_edit: OnEdit) -> Self
where
OnEdit: Fn() + 'static,
{
let s: Self = Object::builder().build();
s.set_halign(gtk::Align::End);
let edit_button = gtk::Button::builder().label("Edit").build();
edit_button.connect_clicked(move |_| on_edit());
s.append(&edit_button);
s
}
}
pub struct DayDetailPrivate { pub struct DayDetailPrivate {
date: gtk::Label, date: gtk::Label,
weight: RefCell<Option<gtk::Label>>, weight: RefCell<Option<gtk::Label>>,
@ -146,11 +180,7 @@ impl DayDetail {
s.set_orientation(gtk::Orientation::Vertical); s.set_orientation(gtk::Orientation::Vertical);
s.set_hexpand(true); s.set_hexpand(true);
s.append( s.append(&CommandRow::new(on_edit));
&ActionGroup::builder()
.primary_action("Edit", Box::new(on_edit))
.build(),
);
/* /*
let click_controller = gtk::GestureClick::new(); let click_controller = gtk::GestureClick::new();
@ -231,14 +261,14 @@ impl DayDetail {
pub struct DayEditPrivate { pub struct DayEditPrivate {
date: gtk::Label, date: gtk::Label,
weight: Rc<WeightEdit>, weight: WeightEdit,
} }
impl Default for DayEditPrivate { impl Default for DayEditPrivate {
fn default() -> Self { fn default() -> Self {
Self { Self {
date: gtk::Label::new(None), date: gtk::Label::new(None),
weight: Rc::new(WeightEdit::new(None)), weight: WeightEdit::new(None),
} }
} }
} }
@ -259,61 +289,31 @@ glib::wrapper! {
} }
impl DayEdit { impl DayEdit {
pub fn new<PutRecordFn, UpdateRecordFn, CancelFn>( pub fn new<PutRecordFn, UpdateRecordFn>(
date: chrono::NaiveDate, date: chrono::NaiveDate,
records: Vec<Record<ft_core::TraxRecord>>, records: Vec<Record<ft_core::TraxRecord>>,
on_put_record: PutRecordFn, on_put_record: PutRecordFn,
on_update_record: UpdateRecordFn, on_update_record: UpdateRecordFn,
on_cancel: CancelFn,
) -> Self ) -> Self
where where
PutRecordFn: Fn(ft_core::TraxRecord) + 'static, PutRecordFn: Fn(ft_core::TraxRecord) + 'static,
UpdateRecordFn: Fn(Record<ft_core::TraxRecord>) + 'static, UpdateRecordFn: Fn(Record<ft_core::TraxRecord>) + 'static,
CancelFn: Fn() + 'static,
{ {
let s: Self = Object::builder().build(); let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.set_hexpand(true);
s.append( /*, move |weight| {
&ActionGroup::builder() on_update_record(Record {
.primary_action("Save", { id: id.clone(),
let s = s.clone(); data: ft_core::TraxRecord::Weight(ft_core::Weight { date, weight }),
let records = records.clone(); })
move || { }*/
println!("saving to database");
let weight_record = records.iter().find_map(|record| match record {
Record {
id,
data: ft_core::TraxRecord::Weight(w),
} => Some((id, w)),
_ => None,
});
let weight = s.imp().weight.value(); /*, move |weight| {
on_put_record(ft_core::TraxRecord::Weight(ft_core::Weight {
if let Some(weight) = weight { date,
match weight_record { weight,
Some((id, _)) => on_update_record(Record { }));
id: id.clone(), }*/
data: ft_core::TraxRecord::Weight(ft_core::Weight {
date,
weight,
}),
}),
None => {
on_put_record(ft_core::TraxRecord::Weight(ft_core::Weight {
date,
weight,
}))
}
}
};
}
})
.secondary_action("Cancel", on_cancel)
.build(),
);
let weight_record = records.iter().find_map(|record| match record { let weight_record = records.iter().find_map(|record| match record {
Record { Record {
@ -323,11 +323,11 @@ impl DayEdit {
_ => None, _ => None,
}); });
match weight_record { let weight_view = match weight_record {
Some((_id, data)) => s.imp().weight.set_value(Some(data.weight)), Some((_id, data)) => WeightEdit::new(Some(data.clone())),
None => s.imp().weight.set_value(None), None => WeightEdit::new(None),
}; };
s.append(&s.imp().weight.widget()); s.append(&weight_view.widget());
s s
} }

View File

@ -14,9 +14,6 @@ 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/>. You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
*/ */
mod action_group;
pub use action_group::ActionGroup;
mod day; mod day;
pub use day::{DayDetail, DayEdit, DaySummary}; pub use day::{DayDetail, DayEdit, DaySummary};

View File

@ -20,25 +20,15 @@ use std::{cell::RefCell, rc::Rc};
pub struct ParseError; pub struct ParseError;
#[derive(Clone)] #[derive(Clone)]
pub struct TextEntry<T: Clone + std::fmt::Debug> { pub struct TextEntry<T: Clone> {
value: Rc<RefCell<Option<T>>>, value: Rc<RefCell<Option<T>>>,
widget: gtk::Entry, widget: gtk::Entry,
renderer: Rc<Box<dyn Fn(&T) -> String>>, renderer: Rc<Box<dyn Fn(&T) -> String>>,
parser: Rc<Box<dyn Fn(&str) -> Result<T, ParseError>>>, parser: Rc<Box<dyn Fn(&str) -> Result<T, ParseError>>>,
} }
impl<T: Clone + std::fmt::Debug> std::fmt::Debug for TextEntry<T> {
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. // I do not understand why the data should be 'static.
impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> { impl<T: Clone + 'static> TextEntry<T> {
pub fn new<R, V>(placeholder: &str, value: Option<T>, renderer: R, parser: V) -> Self pub fn new<R, V>(placeholder: &str, value: Option<T>, renderer: R, parser: V) -> Self
where where
R: Fn(&T) -> String + 'static, R: Fn(&T) -> String + 'static,
@ -73,7 +63,6 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
} }
match (self.parser)(buffer.text().as_str()) { match (self.parser)(buffer.text().as_str()) {
Ok(v) => { Ok(v) => {
println!("setting the value: {}", (self.renderer)(&v));
*self.value.borrow_mut() = Some(v); *self.value.borrow_mut() = Some(v);
self.widget.remove_css_class("error"); self.widget.remove_css_class("error");
} }
@ -85,8 +74,6 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
} }
pub fn value(&self) -> Option<T> { pub fn value(&self) -> Option<T> {
let v = self.value.borrow().clone();
println!("retrieving the value: {:?}", v.map(|v| (self.renderer)(&v)));
self.value.borrow().clone() self.value.borrow().clone()
} }

View File

@ -22,7 +22,204 @@ use gtk::{prelude::*, subclass::prelude::*};
use std::{borrow::Borrow, cell::RefCell}; use std::{borrow::Borrow, cell::RefCell};
#[derive(Default)] #[derive(Default)]
pub struct WeightViewPrivate {} pub struct WeightViewPrivate {
/*
date: RefCell<NaiveDate>,
record: RefCell<Option<Weight>>,
widget: RefCell<EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>>,
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
*/
}
/*
impl Default for WeightViewPrivate {
fn default() -> Self {
Self {
date: RefCell::new(Local::now().date_naive()),
record: RefCell::new(None),
widget: RefCell::new(EditView::Unconfigured),
container: Singleton::default(),
on_edit_finished: RefCell::new(Box::new(|_| {})),
}
}
}
*/
/*
#[glib::object_subclass]
impl ObjectSubclass for WeightViewPrivate {
const NAME: &'static str = "WeightView";
type Type = WeightView;
type ParentType = gtk::Label;
}
impl ObjectImpl for WeightViewPrivate {}
impl WidgetImpl for WeightViewPrivate {}
impl LabelImpl for WeightViewPrivate {}
glib::wrapper! {
pub struct WeightView(ObjectSubclass<WeightViewPrivate>) @extends gtk::Label, gtk::Widget;
}
impl WeightView {
pub fn new<OnEditFinished>(
date: NaiveDate,
weight: Option<Weight>,
on_edit_finished: OnEditFinished,
) -> Self
where
OnEditFinished: Fn(si::Kilogram<f64>) + 'static,
{
let s: Self = Object::builder().build();
s.set_css_classes(&["card", "weight-view"]);
s.set_can_focus(true);
s.append(&s.imp().container);
match weight {
Some(weight) => {
s.remove_css_class("dim_label");
s.set_label(&format!("{:?}", weight));
}
None => {
s.add_css_class("dim_label");
s.set_label("No weight recorded");
}
}
/*
*s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished);
*s.imp().date.borrow_mut() = date;
*s.imp().record.borrow_mut() = weight;
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();
view_click_controller.connect_released({
let s = self.clone();
move |_, _, _, _| {
s.edit();
}
});
view.add_controller(view_click_controller);
match *self.imp().record.borrow() {
Some(ref record) => {
view.remove_css_class("dim_label");
view.set_label(&format!("{:?}", record.weight));
}
None => {
view.add_css_class("dim_label");
view.set_label("No weight recorded");
}
}
// self.swap(EditView::View(view));
}
fn edit(&self) {
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() {
Some(ref record) => edit.set_value(Some(record.weight)),
None => edit.set_value(None),
}
// self.swap(EditView::Edit(edit.clone()));
edit.grab_focus();
}
*/
/*
fn swap(&self, new_view: EditView<gtk::Label, TextEntry<si::Kilogram<f64>>>) {
match new_view {
EditView::Unconfigured => {}
EditView::View(view) => self.imp().container.swap(&view.upcast()),
EditView::Edit(editor) => self.imp().container.swap(&editor.widget()),
}
/*
let mut widget = self.imp().widget.borrow_mut();
match *widget {
EditView::Unconfigured => {}
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;
*/
}
*/
/*
pub fn blur(&self) {
match *self.imp().widget.borrow() {
EditView::Unconfigured => {}
EditView::View(_) => {}
EditView::Edit(ref editor) => {
let weight = editor.value();
// This has really turned into rubbish
// on_edit_finished needs to accept a full record now.
// needs to be possible to delete a record if the value is None
// it's hard to be sure whether I need the full record object or if I need to update
// it. I probably don't. I think I need to borrow it and call on_edit_finished with an
// updated version of it.
// on_edit_finished still doesn't have a way to support a delete operation
let record = match (self.imp().record.borrow().clone(), weight) {
// update an existing record
(Some(record), Some(weight)) => Some(Weight {
date: record.date,
weight,
}),
// create a new record
(None, Some(weight)) => Some(Weight {
date: self.imp().date.borrow().clone(),
weight,
}),
// do nothing or delete an existing record
(_, None) => None,
};
match record {
Some(record) => {
self.imp().on_edit_finished.borrow()(record.weight);
*self.imp().record.borrow_mut() = Some(record);
}
None => {}
}
}
}
self.view();
}
*/
}
*/
pub struct Weight { pub struct Weight {
label: gtk::Label, label: gtk::Label,
@ -48,7 +245,6 @@ impl Weight {
} }
} }
#[derive(Debug)]
pub struct WeightEdit { pub struct WeightEdit {
entry: TextEntry<si::Kilogram<f64>>, entry: TextEntry<si::Kilogram<f64>>,
} }
@ -65,10 +261,6 @@ impl WeightEdit {
} }
} }
pub fn set_value(&self, value: Option<si::Kilogram<f64>>) {
self.entry.set_value(value);
}
pub fn value(&self) -> Option<si::Kilogram<f64>> { pub fn value(&self) -> Option<si::Kilogram<f64>> {
self.entry.value() self.entry.value()
} }

View File

@ -54,7 +54,6 @@ impl DayDetailView {
*s.imp().date.borrow_mut() = date; *s.imp().date.borrow_mut() = date;
*s.imp().records.borrow_mut() = records; *s.imp().records.borrow_mut() = records;
*s.imp().app.borrow_mut() = Some(app);
s.append(&s.imp().container); s.append(&s.imp().container);
@ -96,17 +95,12 @@ impl DayDetailView {
self.imp().container.swap(&DayEdit::new( self.imp().container.swap(&DayEdit::new(
self.imp().date.borrow().clone(), self.imp().date.borrow().clone(),
self.imp().records.borrow().clone(), self.imp().records.borrow().clone(),
self.on_put_record(), |_| {},
self.on_update_record(), |_| {},
{
let s = self.clone();
move || s.view()
},
)); ));
} }
fn on_put_record(&self) -> Box<dyn Fn(ft_core::TraxRecord)> { fn on_put_record(&self) -> Box<dyn Fn(ft_core::TraxRecord)> {
let s = self.clone();
let app = self.imp().app.clone(); let app = self.imp().app.clone();
Box::new(move |record| { Box::new(move |record| {
let app = app.clone(); let app = app.clone();
@ -120,29 +114,20 @@ impl DayDetailView {
} }
} }
}); });
s.view();
}) })
} }
fn on_update_record(&self) -> Box<dyn Fn(Record<ft_core::TraxRecord>)> { /*
let s = self.clone(); fn on_update_record(&self, record: TraxRecord) -> dyn Fn(ft_core::TraxRecord) {
let app = self.imp().app.clone(); let app = self.imp().app.clone();
Box::new(move |record| { move |record| {
let app = app.clone(); let app = app.clone();
glib::spawn_future_local({ glib::spawn_future_local({
async move { async move {
println!("record: {:?}", record); app.update_record(record).await;
match &*app.borrow() {
Some(app) => {
let _ = app.update_record(record).await;
}
None => {
println!("no app!");
}
}
} }
}); });
s.view(); }
})
} }
*/
} }