Thoroughly fill out the day detail view model along with proper automated testing #179
|
@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
// use ft_core::TraxRecord;
|
// use ft_core::TraxRecord;
|
||||||
use crate::{
|
use crate::{
|
||||||
components::{steps_editor, weight_field, ActionGroup, Steps, WeightLabel},
|
components::{steps_editor, weight_field, ActionGroup, Steps, WeightLabel},
|
||||||
|
types::WeightFormatter,
|
||||||
view_models::DayDetailViewModel,
|
view_models::DayDetailViewModel,
|
||||||
};
|
};
|
||||||
use glib::Object;
|
use glib::Object;
|
||||||
|
@ -162,7 +163,7 @@ impl DayDetail {
|
||||||
let top_row = gtk::Box::builder()
|
let top_row = gtk::Box::builder()
|
||||||
.orientation(gtk::Orientation::Horizontal)
|
.orientation(gtk::Orientation::Horizontal)
|
||||||
.build();
|
.build();
|
||||||
let weight_view = WeightLabel::new(view_model.weight());
|
let weight_view = WeightLabel::new(view_model.weight().map(WeightFormatter::from));
|
||||||
top_row.append(&weight_view.widget());
|
top_row.append(&weight_view.widget());
|
||||||
|
|
||||||
let steps_view = Steps::new(view_model.steps());
|
let steps_view = Steps::new(view_model.steps());
|
||||||
|
@ -293,10 +294,10 @@ impl DayEdit {
|
||||||
.orientation(gtk::Orientation::Horizontal)
|
.orientation(gtk::Orientation::Horizontal)
|
||||||
.build();
|
.build();
|
||||||
top_row.append(
|
top_row.append(
|
||||||
&weight_field(view_model.weight(), {
|
&weight_field(view_model.weight().map(WeightFormatter::from), {
|
||||||
let view_model = view_model.clone();
|
let view_model = view_model.clone();
|
||||||
move |w| match w {
|
move |w| match w {
|
||||||
Some(w) => view_model.set_weight(w),
|
Some(w) => view_model.set_weight(*w),
|
||||||
None => eprintln!("have not implemented record delete"),
|
None => eprintln!("have not implemented record delete"),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,13 +14,11 @@ 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::{
|
use crate::app::{ReadError, RecordProvider, WriteError};
|
||||||
app::{ReadError, RecordProvider, WriteError},
|
|
||||||
types::WeightFormatter,
|
|
||||||
};
|
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
|
use dimensioned::si;
|
||||||
use emseries::{Record, RecordId, Recordable};
|
use emseries::{Record, RecordId, Recordable};
|
||||||
use ft_core::TraxRecord;
|
use ft_core::{RecordType, TimeDistance, TimeDistanceWorkoutType, TraxRecord};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
ops::Deref,
|
ops::Deref,
|
||||||
|
@ -79,6 +77,17 @@ impl<T: Clone + emseries::Recordable> Deref for RecordState<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Clone + emseries::Recordable> std::ops::DerefMut for RecordState<T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
match self {
|
||||||
|
RecordState::Original(ref mut r) => &mut r.data,
|
||||||
|
RecordState::New(ref mut r) => r,
|
||||||
|
RecordState::Updated(ref mut r) => &mut r.data,
|
||||||
|
RecordState::Deleted(ref mut r) => &mut r.data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DayDetailViewModel {
|
pub struct DayDetailViewModel {
|
||||||
provider: Arc<dyn RecordProvider>,
|
provider: Arc<dyn RecordProvider>,
|
||||||
|
@ -132,22 +141,20 @@ impl DayDetailViewModel {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn weight(&self) -> Option<WeightFormatter> {
|
pub fn weight(&self) -> Option<si::Kilogram<f64>> {
|
||||||
(*self.weight.read().unwrap())
|
(*self.weight.read().unwrap()).as_ref().map(|w| (*w).weight)
|
||||||
.as_ref()
|
|
||||||
.map(|w| WeightFormatter::from(w.weight))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_weight(&self, new_weight: WeightFormatter) {
|
pub fn set_weight(&self, new_weight: si::Kilogram<f64>) {
|
||||||
let mut record = self.weight.write().unwrap();
|
let mut record = self.weight.write().unwrap();
|
||||||
let new_record = match *record {
|
let new_record = match *record {
|
||||||
Some(ref rstate) => rstate.clone().with_value(ft_core::Weight {
|
Some(ref rstate) => rstate.clone().with_value(ft_core::Weight {
|
||||||
date: self.date,
|
date: self.date,
|
||||||
weight: *new_weight,
|
weight: new_weight,
|
||||||
}),
|
}),
|
||||||
None => RecordState::New(ft_core::Weight {
|
None => RecordState::New(ft_core::Weight {
|
||||||
date: self.date,
|
date: self.date,
|
||||||
weight: *new_weight,
|
weight: new_weight,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
*record = Some(new_record);
|
*record = Some(new_record);
|
||||||
|
@ -172,72 +179,163 @@ impl DayDetailViewModel {
|
||||||
*record = Some(new_record);
|
*record = Some(new_record);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_time_distance(&self, type_: TimeDistanceWorkoutType) -> Record<TimeDistance> {
|
||||||
|
let id = RecordId::default();
|
||||||
|
let workout = TimeDistance {
|
||||||
|
datetime: chrono::Local::now().into(),
|
||||||
|
distance: None,
|
||||||
|
duration: None,
|
||||||
|
comments: None,
|
||||||
|
};
|
||||||
|
let tr = TraxRecord::from_time_distance(type_, workout.clone());
|
||||||
|
self.records
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.insert(id.clone(), RecordState::New(tr));
|
||||||
|
println!(
|
||||||
|
"records after new_time_distance: {:?}",
|
||||||
|
self.records.read().unwrap()
|
||||||
|
);
|
||||||
|
Record { id, data: workout }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_time_distance(&self, workout: Record<TimeDistance>) {
|
||||||
|
let id = workout.id.clone();
|
||||||
|
let data = workout.data.clone();
|
||||||
|
|
||||||
|
self.records
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.entry(id)
|
||||||
|
.and_modify(|r| match **r {
|
||||||
|
TraxRecord::BikeRide(ref mut v) => *v = data,
|
||||||
|
TraxRecord::Row(ref mut v) => *v = data,
|
||||||
|
TraxRecord::Run(ref mut v) => *v = data,
|
||||||
|
TraxRecord::Swim(ref mut v) => *v = data,
|
||||||
|
TraxRecord::Walk(ref mut v) => *v = data,
|
||||||
|
_ => {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn time_distance_records(&self, type_: RecordType) -> Vec<Record<TimeDistance>> {
|
||||||
|
unimplemented!("time_distance_records")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn time_distance_summary(
|
||||||
|
&self,
|
||||||
|
type_: TimeDistanceWorkoutType,
|
||||||
|
) -> (si::Meter<f64>, si::Second<f64>) {
|
||||||
|
self.records
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, workout_state)| workout_state.is_time_distance_type(type_))
|
||||||
|
.filter_map(|(id, record_state)| match **record_state {
|
||||||
|
TraxRecord::BikeRide(ref workout)
|
||||||
|
| TraxRecord::Row(ref workout)
|
||||||
|
| TraxRecord::Run(ref workout)
|
||||||
|
| TraxRecord::Swim(ref workout)
|
||||||
|
| TraxRecord::Walk(ref workout) => Some(workout),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.fold((0. * si::M, 0. * si::S), |(distance, duration), workout| {
|
||||||
|
println!("folding workout: {:?}", workout);
|
||||||
|
match (workout.distance, workout.duration) {
|
||||||
|
(Some(distance_), Some(duration_)) => {
|
||||||
|
(distance + distance_, duration + duration_)
|
||||||
|
}
|
||||||
|
(Some(distance_), None) => (distance + distance_, duration),
|
||||||
|
(None, Some(duration_)) => (distance, duration + duration_),
|
||||||
|
(None, None) => (distance, duration),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_record(&self, id: &RecordId) -> Option<Record<TraxRecord>> {
|
||||||
|
let record_set = self.records.read().unwrap();
|
||||||
|
match record_set.get(&id) {
|
||||||
|
Some(record) => Some(Record {
|
||||||
|
id: id.clone(),
|
||||||
|
data: (**record).clone(),
|
||||||
|
}),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_record(&self, id: RecordId) {
|
||||||
|
unimplemented!("remove_record")
|
||||||
|
}
|
||||||
|
|
||||||
pub fn save(&self) {
|
pub fn save(&self) {
|
||||||
glib::spawn_future({
|
glib::spawn_future({
|
||||||
let s = self.clone();
|
let s = self.clone();
|
||||||
async move {
|
async move { s.async_save().await }
|
||||||
let weight_record = s.weight.read().unwrap().clone();
|
|
||||||
match weight_record {
|
|
||||||
Some(RecordState::New(data)) => {
|
|
||||||
let _ = s.provider.put_record(TraxRecord::Weight(data)).await;
|
|
||||||
}
|
|
||||||
Some(RecordState::Original(_)) => {}
|
|
||||||
Some(RecordState::Updated(weight)) => {
|
|
||||||
let _ = s
|
|
||||||
.provider
|
|
||||||
.update_record(Record {
|
|
||||||
id: weight.id,
|
|
||||||
data: TraxRecord::Weight(weight.data),
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
Some(RecordState::Deleted(_)) => {}
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let steps_record = s.steps.read().unwrap().clone();
|
|
||||||
match steps_record {
|
|
||||||
Some(RecordState::New(data)) => {
|
|
||||||
let _ = s.provider.put_record(TraxRecord::Steps(data)).await;
|
|
||||||
}
|
|
||||||
Some(RecordState::Original(_)) => {}
|
|
||||||
Some(RecordState::Updated(steps)) => {
|
|
||||||
let _ = s
|
|
||||||
.provider
|
|
||||||
.update_record(Record {
|
|
||||||
id: steps.id,
|
|
||||||
data: TraxRecord::Steps(steps.data),
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
Some(RecordState::Deleted(_)) => {}
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let records = s
|
|
||||||
.records
|
|
||||||
.write()
|
|
||||||
.unwrap()
|
|
||||||
.drain()
|
|
||||||
.map(|(_, record)| record)
|
|
||||||
.collect::<Vec<RecordState<TraxRecord>>>();
|
|
||||||
|
|
||||||
for record in records {
|
|
||||||
match record {
|
|
||||||
RecordState::New(data) => {
|
|
||||||
let _ = s.provider.put_record(data).await;
|
|
||||||
}
|
|
||||||
RecordState::Original(_) => {}
|
|
||||||
RecordState::Updated(r) => {
|
|
||||||
let _ = s.provider.update_record(r.clone()).await;
|
|
||||||
}
|
|
||||||
RecordState::Deleted(_) => unimplemented!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn async_save(&self) {
|
||||||
|
println!("async_save");
|
||||||
|
let weight_record = self.weight.read().unwrap().clone();
|
||||||
|
match weight_record {
|
||||||
|
Some(RecordState::New(data)) => {
|
||||||
|
let _ = self.provider.put_record(TraxRecord::Weight(data)).await;
|
||||||
|
}
|
||||||
|
Some(RecordState::Original(_)) => {}
|
||||||
|
Some(RecordState::Updated(weight)) => {
|
||||||
|
let _ = self
|
||||||
|
.provider
|
||||||
|
.update_record(Record {
|
||||||
|
id: weight.id,
|
||||||
|
data: TraxRecord::Weight(weight.data),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
Some(RecordState::Deleted(_)) => {}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
let steps_record = self.steps.read().unwrap().clone();
|
||||||
|
match steps_record {
|
||||||
|
Some(RecordState::New(data)) => {
|
||||||
|
let _ = self.provider.put_record(TraxRecord::Steps(data)).await;
|
||||||
|
}
|
||||||
|
Some(RecordState::Original(_)) => {}
|
||||||
|
Some(RecordState::Updated(steps)) => {
|
||||||
|
let _ = self
|
||||||
|
.provider
|
||||||
|
.update_record(Record {
|
||||||
|
id: steps.id,
|
||||||
|
data: TraxRecord::Steps(steps.data),
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
Some(RecordState::Deleted(_)) => {}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
let records = self
|
||||||
|
.records
|
||||||
|
.write()
|
||||||
|
.unwrap()
|
||||||
|
.drain()
|
||||||
|
.map(|(_, record)| record)
|
||||||
|
.collect::<Vec<RecordState<TraxRecord>>>();
|
||||||
|
|
||||||
|
for record in records {
|
||||||
|
println!("saving record: {:?}", record);
|
||||||
|
match record {
|
||||||
|
RecordState::New(data) => {
|
||||||
|
let _ = self.provider.put_record(data).await;
|
||||||
|
}
|
||||||
|
RecordState::Original(_) => {}
|
||||||
|
RecordState::Updated(r) => {
|
||||||
|
let _ = self.provider.update_record(r.clone()).await;
|
||||||
|
}
|
||||||
|
RecordState::Deleted(_) => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn revert(&self) {
|
pub fn revert(&self) {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
|
@ -247,11 +345,32 @@ impl DayDetailViewModel {
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use chrono::{DateTime, FixedOffset, TimeZone};
|
||||||
use dimensioned::si;
|
use dimensioned::si;
|
||||||
use emseries::Record;
|
use emseries::Record;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
struct MockProvider {
|
struct MockProvider {
|
||||||
records: Vec<Record<TraxRecord>>,
|
records: Arc<RwLock<HashMap<RecordId, Record<TraxRecord>>>>,
|
||||||
|
|
||||||
|
put_records: Arc<RwLock<Vec<Record<TraxRecord>>>>,
|
||||||
|
updated_records: Arc<RwLock<Vec<Record<TraxRecord>>>>,
|
||||||
|
deleted_records: Arc<RwLock<Vec<RecordId>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MockProvider {
|
||||||
|
fn new(records: Vec<Record<TraxRecord>>) -> Self {
|
||||||
|
let record_map = records
|
||||||
|
.into_iter()
|
||||||
|
.map(|r| (r.id.clone(), r))
|
||||||
|
.collect::<HashMap<RecordId, Record<TraxRecord>>>();
|
||||||
|
Self {
|
||||||
|
records: Arc::new(RwLock::new(record_map)),
|
||||||
|
put_records: Arc::new(RwLock::new(vec![])),
|
||||||
|
updated_records: Arc::new(RwLock::new(vec![])),
|
||||||
|
deleted_records: Arc::new(RwLock::new(vec![])),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
|
@ -265,14 +384,24 @@ mod test {
|
||||||
let end = emseries::Timestamp::Date(end);
|
let end = emseries::Timestamp::Date(end);
|
||||||
Ok(self
|
Ok(self
|
||||||
.records
|
.records
|
||||||
|
.read()
|
||||||
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
|
.map(|(_, r)| r)
|
||||||
.filter(|r| r.timestamp() >= start && r.timestamp() <= end)
|
.filter(|r| r.timestamp() >= start && r.timestamp() <= end)
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect::<Vec<Record<TraxRecord>>>())
|
.collect::<Vec<Record<TraxRecord>>>())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError> {
|
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError> {
|
||||||
Err(WriteError::NoDatabase)
|
let id = RecordId::default();
|
||||||
|
let record = Record {
|
||||||
|
id: id.clone(),
|
||||||
|
data: record,
|
||||||
|
};
|
||||||
|
self.put_records.write().unwrap().push(record.clone());
|
||||||
|
self.records.write().unwrap().insert(id.clone(), record);
|
||||||
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), WriteError> {
|
async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), WriteError> {
|
||||||
|
@ -284,89 +413,220 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn with_view_model<TestFn>(test: TestFn)
|
async fn create_empty_view_model() -> (DayDetailViewModel, MockProvider) {
|
||||||
where
|
let provider = MockProvider::new(vec![]);
|
||||||
TestFn: Fn(DayDetailViewModel),
|
|
||||||
{
|
let oct_13 = chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap();
|
||||||
|
let model = DayDetailViewModel::new(oct_13, provider.clone())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
(model, provider)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_view_model() -> (DayDetailViewModel, MockProvider) {
|
||||||
let oct_12 = chrono::NaiveDate::from_ymd_opt(2023, 10, 12).unwrap();
|
let oct_12 = chrono::NaiveDate::from_ymd_opt(2023, 10, 12).unwrap();
|
||||||
let oct_13 = chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap();
|
let oct_13 = chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap();
|
||||||
let provider = MockProvider {
|
let oct_13_am: DateTime<FixedOffset> = oct_13
|
||||||
records: vec![
|
.clone()
|
||||||
Record {
|
.and_hms_opt(3, 28, 0)
|
||||||
id: RecordId::default(),
|
.unwrap()
|
||||||
data: TraxRecord::Weight(ft_core::Weight {
|
.and_utc()
|
||||||
date: oct_12.clone(),
|
.with_timezone(&FixedOffset::east_opt(5 * 3600).unwrap());
|
||||||
weight: 93. * si::KG,
|
let provider = MockProvider::new(vec![
|
||||||
}),
|
Record {
|
||||||
},
|
id: RecordId::default(),
|
||||||
Record {
|
data: TraxRecord::Weight(ft_core::Weight {
|
||||||
id: RecordId::default(),
|
date: oct_12.clone(),
|
||||||
data: TraxRecord::Weight(ft_core::Weight {
|
weight: 93. * si::KG,
|
||||||
date: oct_13.clone(),
|
}),
|
||||||
weight: 95. * si::KG,
|
},
|
||||||
}),
|
Record {
|
||||||
},
|
id: RecordId::default(),
|
||||||
Record {
|
data: TraxRecord::Weight(ft_core::Weight {
|
||||||
id: RecordId::default(),
|
date: oct_13.clone(),
|
||||||
data: TraxRecord::Steps(ft_core::Steps {
|
weight: 95. * si::KG,
|
||||||
date: oct_13.clone(),
|
}),
|
||||||
count: 2500,
|
},
|
||||||
}),
|
Record {
|
||||||
},
|
id: RecordId::default(),
|
||||||
Record {
|
data: TraxRecord::Steps(ft_core::Steps {
|
||||||
id: RecordId::default(),
|
date: oct_13.clone(),
|
||||||
data: TraxRecord::Weight(ft_core::Weight {
|
count: 2500,
|
||||||
date: oct_13.clone(),
|
}),
|
||||||
weight: 91. * si::KG,
|
},
|
||||||
}),
|
Record {
|
||||||
},
|
id: RecordId::default(),
|
||||||
Record {
|
data: TraxRecord::Weight(ft_core::Weight {
|
||||||
id: RecordId::default(),
|
date: oct_13.clone(),
|
||||||
data: TraxRecord::Steps(ft_core::Steps {
|
weight: 91. * si::KG,
|
||||||
date: oct_13.clone(),
|
}),
|
||||||
count: 2750,
|
},
|
||||||
}),
|
Record {
|
||||||
},
|
id: RecordId::default(),
|
||||||
],
|
data: TraxRecord::Steps(ft_core::Steps {
|
||||||
};
|
date: oct_13.clone(),
|
||||||
let model = DayDetailViewModel::new(oct_13, provider).await.unwrap();
|
count: 2750,
|
||||||
test(model)
|
}),
|
||||||
|
},
|
||||||
|
Record {
|
||||||
|
id: RecordId::default(),
|
||||||
|
data: TraxRecord::BikeRide(ft_core::TimeDistance {
|
||||||
|
datetime: oct_13_am.clone(),
|
||||||
|
distance: Some(15000. * si::M),
|
||||||
|
duration: Some(3600. * si::S),
|
||||||
|
comments: Some("somecomments present".to_owned()),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
let model = DayDetailViewModel::new(oct_13, provider.clone())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
(model, provider)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn it_honors_only_the_first_weight_and_step_record() {
|
async fn it_honors_only_the_first_weight_and_step_record() {
|
||||||
with_view_model(|view_model| {
|
let (view_model, provider) = create_view_model().await;
|
||||||
assert_eq!(view_model.weight().map(|val| *val), Some(95. * si::KG));
|
assert_eq!(view_model.weight(), Some(95. * si::KG));
|
||||||
assert_eq!(view_model.steps(), Some(2500));
|
assert_eq!(view_model.steps(), Some(2500));
|
||||||
})
|
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
#[ignore]
|
async fn it_can_create_a_weight_and_stepcount() {
|
||||||
fn it_enforces_one_weight_and_stepcount_per_day() {}
|
let (view_model, provider) = create_empty_view_model().await;
|
||||||
|
assert_eq!(view_model.weight(), None);
|
||||||
|
assert_eq!(view_model.steps(), None);
|
||||||
|
|
||||||
#[test]
|
view_model.set_weight(95. * si::KG);
|
||||||
#[ignore]
|
view_model.set_steps(250);
|
||||||
fn it_can_construct_new_records() {}
|
|
||||||
|
|
||||||
#[test]
|
assert_eq!(view_model.weight(), Some(95. * si::KG));
|
||||||
#[ignore]
|
assert_eq!(view_model.steps(), Some(250));
|
||||||
fn it_can_update_an_existing_record() {}
|
|
||||||
|
|
||||||
#[test]
|
view_model.set_weight(93. * si::KG);
|
||||||
#[ignore]
|
view_model.set_steps(255);
|
||||||
fn it_can_remove_a_new_record() {}
|
|
||||||
|
|
||||||
#[test]
|
assert_eq!(view_model.weight(), Some(93. * si::KG));
|
||||||
#[ignore]
|
assert_eq!(view_model.steps(), Some(255));
|
||||||
fn it_can_delete_an_existing_record() {}
|
|
||||||
|
|
||||||
#[test]
|
view_model.async_save().await;
|
||||||
#[ignore]
|
|
||||||
fn it_retrieve_records_by_workout() {}
|
|
||||||
|
|
||||||
#[test]
|
println!("provider: {:?}", provider);
|
||||||
|
assert_eq!(provider.put_records.read().unwrap().len(), 2);
|
||||||
|
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn it_can_construct_new_records() {
|
||||||
|
let (view_model, provider) = create_empty_view_model().await;
|
||||||
|
assert_eq!(
|
||||||
|
view_model.time_distance_summary(TimeDistanceWorkoutType::BikeRide),
|
||||||
|
(0. * si::M, 0. * si::S)
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut record = view_model.new_time_distance(TimeDistanceWorkoutType::BikeRide);
|
||||||
|
record.data.duration = Some(60. * si::S);
|
||||||
|
view_model.async_save().await;
|
||||||
|
|
||||||
|
assert_eq!(provider.put_records.read().unwrap().len(), 1);
|
||||||
|
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn it_can_update_a_new_record_before_saving() {
|
||||||
|
let (view_model, provider) = create_empty_view_model().await;
|
||||||
|
assert_eq!(
|
||||||
|
view_model.time_distance_summary(TimeDistanceWorkoutType::BikeRide),
|
||||||
|
(0. * si::M, 0. * si::S)
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut record = view_model.new_time_distance(TimeDistanceWorkoutType::BikeRide);
|
||||||
|
record.data.duration = Some(60. * si::S);
|
||||||
|
view_model.update_time_distance(record.clone());
|
||||||
|
let record = Record {
|
||||||
|
id: record.id,
|
||||||
|
data: TraxRecord::BikeRide(record.data),
|
||||||
|
};
|
||||||
|
assert_eq!(view_model.get_record(&record.id), Some(record));
|
||||||
|
assert_eq!(
|
||||||
|
view_model.time_distance_summary(TimeDistanceWorkoutType::BikeRide),
|
||||||
|
(0. * si::M, 60. * si::S)
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
view_model.time_distance_summary(TimeDistanceWorkoutType::Run),
|
||||||
|
(0. * si::M, 0. * si::S)
|
||||||
|
);
|
||||||
|
view_model.async_save().await;
|
||||||
|
|
||||||
|
assert_eq!(provider.put_records.read().unwrap().len(), 1);
|
||||||
|
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn it_summarizes_records_by_workout_type() {}
|
async fn it_can_update_an_existing_record() {
|
||||||
|
let (view_model, provider) = create_view_model().await;
|
||||||
|
let mut workout = view_model
|
||||||
|
.time_distance_records(RecordType::BikeRide)
|
||||||
|
.first()
|
||||||
|
.cloned()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
workout.data.duration = Some(1800. * si::S);
|
||||||
|
view_model.update_time_distance(workout.clone());
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
view_model.time_distance_summary(TimeDistanceWorkoutType::BikeRide),
|
||||||
|
(15000. * si::M, 1800. * si::S)
|
||||||
|
);
|
||||||
|
|
||||||
|
view_model.save();
|
||||||
|
|
||||||
|
assert_eq!(provider.put_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.updated_records.read().unwrap().len(), 1);
|
||||||
|
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore]
|
||||||
|
async fn it_can_remove_a_new_record() {
|
||||||
|
let (view_model, provider) = create_empty_view_model().await;
|
||||||
|
assert_eq!(
|
||||||
|
view_model.time_distance_summary(TimeDistanceWorkoutType::BikeRide),
|
||||||
|
(0. * si::M, 0. * si::S)
|
||||||
|
);
|
||||||
|
|
||||||
|
let record = view_model.new_time_distance(TimeDistanceWorkoutType::BikeRide);
|
||||||
|
view_model.remove_record(record.id);
|
||||||
|
view_model.save();
|
||||||
|
|
||||||
|
assert_eq!(provider.put_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
#[ignore]
|
||||||
|
async fn it_can_delete_an_existing_record() {
|
||||||
|
let (view_model, provider) = create_view_model().await;
|
||||||
|
let mut workout = view_model
|
||||||
|
.time_distance_records(RecordType::BikeRide)
|
||||||
|
.first()
|
||||||
|
.cloned()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
view_model.remove_record(workout.id);
|
||||||
|
assert_eq!(
|
||||||
|
view_model.time_distance_summary(TimeDistanceWorkoutType::BikeRide),
|
||||||
|
(0. * si::M, 0. * si::S)
|
||||||
|
);
|
||||||
|
view_model.save();
|
||||||
|
|
||||||
|
assert_eq!(provider.put_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||||
|
assert_eq!(provider.deleted_records.read().unwrap().len(), 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
mod legacy;
|
mod legacy;
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
pub use types::{RecordType, Steps, TimeDistance, TraxRecord, Weight};
|
pub use types::{RecordType, Steps, TimeDistance, TimeDistanceWorkoutType, TraxRecord, Weight};
|
||||||
|
|
|
@ -57,6 +57,16 @@ pub struct TimeDistance {
|
||||||
pub comments: Option<String>,
|
pub comments: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Recordable for TimeDistance {
|
||||||
|
fn timestamp(&self) -> Timestamp {
|
||||||
|
Timestamp::DateTime(self.datetime)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tags(&self) -> Vec<String> {
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A singular daily weight measurement. Weight changes slowly enough that it seems unlikely to
|
/// 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.
|
/// need to track more than a single weight in a day.
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
|
@ -86,6 +96,15 @@ pub enum RecordType {
|
||||||
Weight,
|
Weight,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
|
pub enum TimeDistanceWorkoutType {
|
||||||
|
BikeRide,
|
||||||
|
Row,
|
||||||
|
Run,
|
||||||
|
Swim,
|
||||||
|
Walk,
|
||||||
|
}
|
||||||
|
|
||||||
/// The unified data structure for all records that are part of the app.
|
/// The unified data structure for all records that are part of the app.
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub enum TraxRecord {
|
pub enum TraxRecord {
|
||||||
|
@ -99,6 +118,16 @@ pub enum TraxRecord {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TraxRecord {
|
impl TraxRecord {
|
||||||
|
pub fn from_time_distance(type_: TimeDistanceWorkoutType, workout: TimeDistance) -> Self {
|
||||||
|
match type_ {
|
||||||
|
TimeDistanceWorkoutType::BikeRide => Self::BikeRide(workout),
|
||||||
|
TimeDistanceWorkoutType::Run => Self::Run(workout),
|
||||||
|
TimeDistanceWorkoutType::Row => Self::Row(workout),
|
||||||
|
TimeDistanceWorkoutType::Swim => Self::Swim(workout),
|
||||||
|
TimeDistanceWorkoutType::Walk => Self::Walk(workout),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn workout_type(&self) -> RecordType {
|
pub fn workout_type(&self) -> RecordType {
|
||||||
match self {
|
match self {
|
||||||
TraxRecord::BikeRide(_) => RecordType::BikeRide,
|
TraxRecord::BikeRide(_) => RecordType::BikeRide,
|
||||||
|
@ -118,6 +147,27 @@ impl TraxRecord {
|
||||||
pub fn is_steps(&self) -> bool {
|
pub fn is_steps(&self) -> bool {
|
||||||
matches!(self, TraxRecord::Steps(_))
|
matches!(self, TraxRecord::Steps(_))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_time_distance(&self) -> bool {
|
||||||
|
matches!(
|
||||||
|
self,
|
||||||
|
TraxRecord::BikeRide(_)
|
||||||
|
| TraxRecord::Row(_)
|
||||||
|
| TraxRecord::Run(_)
|
||||||
|
| TraxRecord::Swim(_)
|
||||||
|
| TraxRecord::Walk(_)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_time_distance_type(&self, type_: TimeDistanceWorkoutType) -> bool {
|
||||||
|
match type_ {
|
||||||
|
TimeDistanceWorkoutType::BikeRide => matches!(self, TraxRecord::BikeRide(_)),
|
||||||
|
TimeDistanceWorkoutType::Row => matches!(self, TraxRecord::Row(_)),
|
||||||
|
TimeDistanceWorkoutType::Run => matches!(self, TraxRecord::Run(_)),
|
||||||
|
TimeDistanceWorkoutType::Swim => matches!(self, TraxRecord::Swim(_)),
|
||||||
|
TimeDistanceWorkoutType::Walk => matches!(self, TraxRecord::Walk(_)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Recordable for TraxRecord {
|
impl Recordable for TraxRecord {
|
||||||
|
|
Loading…
Reference in New Issue