Add a step count editor field #162

Merged
savanni merged 3 commits from fitnesstrax/step-counting into main 2024-01-20 22:08:31 +00:00
4 changed files with 85 additions and 55 deletions
Showing only changes of commit 7ec48ded5d - Show all commits

View File

@ -134,7 +134,7 @@ impl AppWindow {
} }
fn show_historical_view(&self, records: Vec<Record<TraxRecord>>) { fn show_historical_view(&self, records: Vec<Record<TraxRecord>>) {
let view = View::Historical(HistoricalView::new(records, { let view = View::Historical(HistoricalView::new(self.app.clone(), records, {
let s = self.clone(); let s = self.clone();
Rc::new(move |date, records| { Rc::new(move |date, records| {
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0); let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);

View File

@ -20,14 +20,12 @@ use crate::{
components::{steps_editor, weight_editor, ActionGroup, Steps, Weight}, components::{steps_editor, weight_editor, ActionGroup, Steps, Weight},
view_models::DayDetailViewModel, view_models::DayDetailViewModel,
}; };
use emseries::Record;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell; use std::cell::RefCell;
pub struct DaySummaryPrivate { pub struct DaySummaryPrivate {
date: gtk::Label, date: gtk::Label,
weight: RefCell<Option<gtk::Label>>,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -41,10 +39,7 @@ impl ObjectSubclass for DaySummaryPrivate {
.css_classes(["day-summary__date"]) .css_classes(["day-summary__date"])
.halign(gtk::Align::Start) .halign(gtk::Align::Start)
.build(); .build();
Self { Self { date }
date,
weight: RefCell::new(None),
}
} }
} }
@ -75,37 +70,34 @@ impl DaySummary {
Self::default() Self::default()
} }
pub fn set_data(&self, date: chrono::NaiveDate, records: Vec<Record<ft_core::TraxRecord>>) { pub fn set_data(&self, view_model: DayDetailViewModel) {
self.imp() self.imp()
.date .date
.set_text(&date.format("%Y-%m-%d").to_string()); .set_text(&view_model.date.format("%Y-%m-%d").to_string());
if let Some(ref weight_label) = *self.imp().weight.borrow() { let row = gtk::Box::builder().build();
self.remove(weight_label);
let label = gtk::Label::builder()
.halign(gtk::Align::Start)
.css_classes(["day-summary__weight"])
.build();
if let Some(w) = view_model.weight() {
label.set_label(&w.to_string())
} }
row.append(&label);
if let Some(Record { self.append(&label);
data: ft_core::TraxRecord::Weight(weight_record),
.. let label = gtk::Label::builder()
}) = records.iter().find(|f| f.data.is_weight()) .halign(gtk::Align::Start)
{ .css_classes(["day-summary__weight"])
let label = gtk::Label::builder() .build();
.halign(gtk::Align::Start) if let Some(s) = view_model.steps() {
.label(weight_record.weight.to_string()) label.set_label(&format!("{} steps", s.to_string()));
.css_classes(["day-summary__weight"])
.build();
self.append(&label);
*self.imp().weight.borrow_mut() = Some(label);
} }
row.append(&label);
/* self.append(&row);
self.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label("15km of biking in 60 minutes")
.build(),
);
*/
} }
} }

View File

@ -186,6 +186,24 @@ impl DayDetailViewModel {
None => {} None => {}
} }
let steps_record = s.steps.read().unwrap().clone();
match steps_record {
Some(RecordState::New(steps)) => {
let _ = app.put_record(TraxRecord::Steps(steps)).await;
}
Some(RecordState::Original(_)) => {}
Some(RecordState::Updated(steps)) => {
let _ = app
.update_record(Record {
id: steps.id,
data: TraxRecord::Steps(steps.data),
})
.await;
}
Some(RecordState::Deleted(_)) => {}
None => {}
}
let records = s let records = s
.records .records
.write() .write()

View File

@ -14,7 +14,9 @@ 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/>.
*/ */
use crate::{components::DaySummary, types::DayInterval}; use crate::{
app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel,
};
use chrono::NaiveDate; use chrono::NaiveDate;
use emseries::Record; use emseries::Record;
use ft_core::TraxRecord; use ft_core::TraxRecord;
@ -26,7 +28,8 @@ use std::{cell::RefCell, collections::HashMap, rc::Rc};
/// daily summaries, daily details, and will provide all functions the user may need for editing /// daily summaries, daily details, and will provide all functions the user may need for editing
/// records. /// records.
pub struct HistoricalViewPrivate { pub struct HistoricalViewPrivate {
time_window: RefCell<DayInterval>, app: Rc<RefCell<Option<App>>>,
time_window: Rc<RefCell<DayInterval>>,
list_view: gtk::ListView, list_view: gtk::ListView,
} }
@ -45,31 +48,42 @@ impl ObjectSubclass for HistoricalViewPrivate {
.set_child(Some(&DaySummary::new())); .set_child(Some(&DaySummary::new()));
}); });
factory.connect_bind(move |_, list_item| { let s = Self {
let records = list_item app: Rc::new(RefCell::new(None)),
.downcast_ref::<gtk::ListItem>() time_window: Rc::new(RefCell::new(DayInterval::default())),
.expect("should be a ListItem")
.item()
.and_downcast::<DayRecords>()
.expect("should be a DaySummary");
let summary = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.child()
.and_downcast::<DaySummary>()
.expect("should be a DaySummary");
summary.set_data(records.date(), records.records());
});
Self {
time_window: RefCell::new(DayInterval::default()),
list_view: gtk::ListView::builder() list_view: gtk::ListView::builder()
.factory(&factory) .factory(&factory)
.single_click_activate(true) .single_click_activate(true)
.build(), .build(),
} };
factory.connect_bind({
let app = s.app.clone();
move |_, list_item| {
let records = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.item()
.and_downcast::<DayRecords>()
.expect("should be a DaySummary");
let summary = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.child()
.and_downcast::<DaySummary>()
.expect("should be a DaySummary");
if let Some(app) = app.borrow().clone() {
summary.set_data(DayDetailViewModel::new(
records.date(),
records.records(),
app.clone(),
));
}
}
});
s
} }
} }
@ -82,7 +96,11 @@ glib::wrapper! {
} }
impl HistoricalView { impl HistoricalView {
pub fn new<SelectFn>(records: Vec<Record<TraxRecord>>, on_select_day: Rc<SelectFn>) -> Self pub fn new<SelectFn>(
app: App,
records: Vec<Record<TraxRecord>>,
on_select_day: Rc<SelectFn>,
) -> Self
where where
SelectFn: Fn(chrono::NaiveDate, Vec<Record<TraxRecord>>) + 'static, SelectFn: Fn(chrono::NaiveDate, Vec<Record<TraxRecord>>) + 'static,
{ {
@ -90,6 +108,8 @@ impl HistoricalView {
s.set_orientation(gtk::Orientation::Vertical); s.set_orientation(gtk::Orientation::Vertical);
s.set_css_classes(&["historical"]); s.set_css_classes(&["historical"]);
*s.imp().app.borrow_mut() = Some(app);
let grouped_records = let grouped_records =
GroupedRecords::new((*s.imp().time_window.borrow()).clone()).with_data(records); GroupedRecords::new((*s.imp().time_window.borrow()).clone()).with_data(records);