Compare commits

..

No commits in common. "39acfe7950014f31fcf899109a9a8670340b0a47" and "3a716ee546913efb3e4d7e3198e4d466e24aa1a8" have entirely different histories.

5 changed files with 35 additions and 140 deletions

View File

@ -120,7 +120,7 @@ pub trait Recordable {
/// Uniquely identifies a record.
///
/// This is a wrapper around a basic uuid with some extra convenience methods.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub struct RecordId(Uuid);
impl Default for RecordId {

View File

@ -23,13 +23,13 @@ use crate::{
},
view_models::DayDetailViewModel,
};
use emseries::Record;
use dimensioned::si;
use ft_core::{RecordType, TraxRecord};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, rc::Rc};
use std::cell::RefCell;
use super::{time_distance::TimeDistanceEdit, time_distance_detail};
use super::time_distance_detail;
pub struct DaySummaryPrivate {
date: gtk::Label,
@ -198,19 +198,12 @@ impl DayDetail {
pub struct DayEditPrivate {
on_finished: RefCell<Box<dyn Fn()>>,
workout_rows: RefCell<gtk::Box>,
}
impl Default for DayEditPrivate {
fn default() -> Self {
Self {
on_finished: RefCell::new(Box::new(|| {})),
workout_rows: RefCell::new(
gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.hexpand(true)
.build(),
),
}
}
}
@ -240,15 +233,9 @@ impl DayEdit {
s.set_hexpand(true);
*s.imp().on_finished.borrow_mut() = Box::new(on_finished);
let workout_buttons = workout_buttons(view_model.clone(), {
let s = s.clone();
move |workout| s.add_row(workout)
});
s.append(&control_buttons(&s, &view_model));
s.append(&weight_and_steps_row(&view_model));
s.append(&*s.imp().workout_rows.borrow());
s.append(&workout_buttons);
s.append(&workout_buttons());
s
}
@ -256,24 +243,6 @@ impl DayEdit {
fn finish(&self) {
(self.imp().on_finished.borrow())()
}
fn add_row(&self, workout: Record<TraxRecord>) {
println!("add_row: {:?}", workout);
let workout_rows = self.imp().workout_rows.borrow();
let workout_type = workout.data.workout_type();
match workout.data {
TraxRecord::BikeRide(w)
| TraxRecord::Row(w)
| TraxRecord::Swim(w)
| TraxRecord::Run(w)
| TraxRecord::Walk(w) => {
workout_rows.append(&TimeDistanceEdit::new(workout_type, w, |_, _| {}))
}
_ => {}
}
}
}
fn control_buttons(s: &DayEdit, view_model: &DayDetailViewModel) -> ActionGroup {
@ -322,37 +291,24 @@ fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
row
}
fn workout_buttons<AddRow>(view_model: DayDetailViewModel, add_row: AddRow) -> gtk::Box
where
AddRow: Fn(Record<TraxRecord>) + 'static,
{
let add_row = Rc::new(add_row);
fn workout_buttons() -> gtk::Box {
let sunrise_button = gtk::Button::builder()
.icon_name("daytime-sunrise-symbolic")
.width_request(64)
.height_request(64)
.build();
let walking_button = gtk::Button::builder()
.icon_name("walking2-symbolic")
.width_request(64)
.height_request(64)
.build();
walking_button.connect_clicked({
let view_model = view_model.clone();
let add_row = add_row.clone();
move |_| {
let workout = view_model.new_record(RecordType::Walk);
&add_row(workout);
}
});
let running_button = gtk::Button::builder()
.icon_name("running-symbolic")
.width_request(64)
.height_request(64)
.build();
running_button.connect_clicked({
let view_model = view_model.clone();
move |_| {
let workout = view_model.new_record(RecordType::Walk);
add_row(workout);
}
});
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
@ -360,6 +316,7 @@ where
let row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.build();
row.append(&sunrise_button);
row.append(&walking_button);
row.append(&running_button);
layout.append(&row);

View File

@ -132,7 +132,6 @@ impl Default for TimeDistanceEdit {
fn default() -> Self {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Horizontal);
s.set_hexpand(true);
s.set_css_classes(&["time-distance-edit"]);
s
@ -140,17 +139,12 @@ impl Default for TimeDistanceEdit {
}
impl TimeDistanceEdit {
pub fn new<OnUpdate>(type_: RecordType, record: TimeDistance, on_update: OnUpdate) -> Self
fn empty<OnUpdate>(on_update: OnUpdate) -> Self
where
OnUpdate: Fn(&ft_core::RecordType, &ft_core::TimeDistance),
{
println!("new TimeDistanceEdit");
let s = Self::default();
s.append(&gtk::Label::new(Some(
record.datetime.format("%H:%M").to_string().as_ref(),
)));
s
}

View File

@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with Fit
use crate::app::App;
use dimensioned::si;
use emseries::{Record, RecordId, Recordable};
use ft_core::{RecordType, TimeDistance, TraxRecord};
use ft_core::{TimeDistance, TraxRecord};
use std::{
collections::HashMap,
ops::Deref,
@ -27,7 +27,7 @@ use std::{
#[derive(Clone, Debug)]
enum RecordState<T: Clone + Recordable> {
Original(Record<T>),
New(Record<T>),
New(T),
Updated(Record<T>),
#[allow(unused)]
Deleted(Record<T>),
@ -53,21 +53,13 @@ impl<T: Clone + emseries::Recordable> RecordState<T> {
}
}
fn set_value(&mut self, value: T) {
*self = match self {
RecordState::Original(r) => RecordState::Updated(Record { data: value, ..*r }),
RecordState::New(_) => RecordState::New(Record {
id: RecordId::default(),
data: value,
}),
RecordState::Updated(r) => RecordState::Updated(Record { data: value, ..*r }),
RecordState::Deleted(r) => RecordState::Updated(Record { data: value, ..*r }),
};
fn with_value(self, value: T) -> RecordState<T> {
match self {
RecordState::Original(r) => RecordState::Updated(Record { data: value, ..r }),
RecordState::New(_) => RecordState::New(value),
RecordState::Updated(r) => RecordState::Updated(Record { data: value, ..r }),
RecordState::Deleted(r) => RecordState::Updated(Record { data: value, ..r }),
}
fn with_value(mut self, value: T) -> RecordState<T> {
self.set_value(value);
self
}
#[allow(unused)]
@ -86,7 +78,7 @@ impl<T: Clone + emseries::Recordable> Deref for RecordState<T> {
fn deref(&self) -> &Self::Target {
match self {
RecordState::Original(ref r) => &r.data,
RecordState::New(ref r) => &r.data,
RecordState::New(ref r) => r,
RecordState::Updated(ref r) => &r.data,
RecordState::Deleted(ref r) => &r.data,
}
@ -134,12 +126,9 @@ impl DayDetailViewModel {
date: self.date,
weight: new_weight,
}),
None => RecordState::New(Record {
id: RecordId::default(),
data: ft_core::Weight {
None => RecordState::New(ft_core::Weight {
date: self.date,
weight: new_weight,
},
}),
};
*record = Some(new_record);
@ -156,12 +145,9 @@ impl DayDetailViewModel {
date: self.date,
count: new_count,
}),
None => RecordState::New(Record {
id: RecordId::default(),
data: ft_core::Steps {
None => RecordState::New(ft_core::Steps {
date: self.date,
count: new_count,
},
}),
};
*record = Some(new_record);
@ -191,25 +177,6 @@ impl DayDetailViewModel {
)
}
pub fn new_record(&self, type_: RecordType) -> Record<TraxRecord> {
let new_record = Record {
id: RecordId::default(),
data: ft_core::TraxRecord::new(type_, chrono::Local::now().into()),
};
self.records
.write()
.unwrap()
.insert(new_record.id.clone(), RecordState::New(new_record.clone()));
new_record
}
pub fn update_record(&self, update: Record<TraxRecord>) {
let mut records = self.records.write().unwrap();
records
.entry(update.id)
.and_modify(|mut record| record.set_value(update.data));
}
pub fn records(&self) -> Vec<Record<TraxRecord>> {
let read_lock = self.records.read().unwrap();
read_lock
@ -227,8 +194,8 @@ impl DayDetailViewModel {
if let Some(app) = s.app {
let weight_record = s.weight.read().unwrap().clone();
match weight_record {
Some(RecordState::New(Record { data, .. })) => {
let _ = app.put_record(TraxRecord::Weight(data)).await;
Some(RecordState::New(weight)) => {
let _ = app.put_record(TraxRecord::Weight(weight)).await;
}
Some(RecordState::Original(_)) => {}
Some(RecordState::Updated(weight)) => {
@ -245,8 +212,8 @@ impl DayDetailViewModel {
let steps_record = s.steps.read().unwrap().clone();
match steps_record {
Some(RecordState::New(Record { data, .. })) => {
let _ = app.put_record(TraxRecord::Steps(data)).await;
Some(RecordState::New(steps)) => {
let _ = app.put_record(TraxRecord::Steps(steps)).await;
}
Some(RecordState::Original(_)) => {}
Some(RecordState::Updated(steps)) => {
@ -271,7 +238,7 @@ impl DayDetailViewModel {
for record in records {
match record {
RecordState::New(Record { data, .. }) => {
RecordState::New(data) => {
let _ = app.put_record(data).await;
}
RecordState::Original(_) => {}

View File

@ -57,17 +57,6 @@ pub struct TimeDistance {
pub comments: Option<String>,
}
impl TimeDistance {
pub fn new(time: DateTime<FixedOffset>) -> Self {
Self {
datetime: time,
distance: None,
duration: None,
comments: None,
}
}
}
/// A singular daily weight measurement. Weight changes slowly enough that it seems unlikely to
/// need to track more than a single weight in a day.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@ -110,18 +99,6 @@ pub enum TraxRecord {
}
impl TraxRecord {
pub fn new(type_: RecordType, time: DateTime<FixedOffset>) -> TraxRecord {
match type_ {
RecordType::BikeRide => TraxRecord::BikeRide(TimeDistance::new(time)),
RecordType::Row => TraxRecord::Row(TimeDistance::new(time)),
RecordType::Run => TraxRecord::Run(TimeDistance::new(time)),
RecordType::Steps => unimplemented!(),
RecordType::Swim => unimplemented!(),
RecordType::Walk => TraxRecord::Walk(TimeDistance::new(time)),
RecordType::Weight => unimplemented!(),
}
}
pub fn workout_type(&self) -> RecordType {
match self {
TraxRecord::BikeRide(_) => RecordType::BikeRide,