Clean up warnings and remove printlns

This commit is contained in:
Savanni D'Gerinel 2024-01-29 23:42:14 -05:00
parent 07f487b139
commit 31d74588fb
11 changed files with 50 additions and 143 deletions

View File

@ -110,7 +110,7 @@ where
.map_err(EmseriesReadError::JSONParseError) .map_err(EmseriesReadError::JSONParseError)
.and_then(Record::try_from) .and_then(Record::try_from)
{ {
Ok(record) => records.insert(record.id.clone(), record.clone()), Ok(record) => records.insert(record.id, record.clone()),
Err(EmseriesReadError::RecordDeleted(id)) => records.remove(&id), Err(EmseriesReadError::RecordDeleted(id)) => records.remove(&id),
Err(err) => return Err(err), Err(err) => return Err(err),
}; };
@ -126,7 +126,7 @@ where
pub fn put(&mut self, entry: T) -> Result<RecordId, EmseriesWriteError> { pub fn put(&mut self, entry: T) -> Result<RecordId, EmseriesWriteError> {
let uuid = RecordId::default(); let uuid = RecordId::default();
let record = Record { let record = Record {
id: uuid.clone(), id: uuid,
data: entry, data: entry,
}; };
self.update(record)?; self.update(record)?;
@ -136,7 +136,7 @@ where
/// Update an existing record. The [RecordId] of the record passed into this function must match /// Update an existing record. The [RecordId] of the record passed into this function must match
/// the [RecordId] of a record already in the database. /// the [RecordId] of a record already in the database.
pub fn update(&mut self, record: Record<T>) -> Result<(), EmseriesWriteError> { pub fn update(&mut self, record: Record<T>) -> Result<(), EmseriesWriteError> {
self.records.insert(record.id.clone(), record.clone()); self.records.insert(record.id, record.clone());
let write_res = match serde_json::to_string(&RecordOnDisk { let write_res = match serde_json::to_string(&RecordOnDisk {
id: record.id, id: record.id,
data: Some(record.data), data: Some(record.data),
@ -166,7 +166,7 @@ where
self.records.remove(uuid); self.records.remove(uuid);
let rec: RecordOnDisk<T> = RecordOnDisk { let rec: RecordOnDisk<T> = RecordOnDisk {
id: uuid.clone(), id: *uuid,
data: None, data: None,
}; };
match serde_json::to_string(&rec) { match serde_json::to_string(&rec) {

View File

@ -190,7 +190,7 @@ mod test {
impl Recordable for WeightRecord { impl Recordable for WeightRecord {
fn timestamp(&self) -> Timestamp { fn timestamp(&self) -> Timestamp {
Timestamp::Date(self.date.clone()) Timestamp::Date(self.date)
} }
fn tags(&self) -> Vec<String> { fn tags(&self) -> Vec<String> {

View File

@ -20,7 +20,7 @@ extern crate emseries;
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use chrono::{format::Fixed, prelude::*}; use chrono::{prelude::*};
use chrono_tz::Etc::UTC; use chrono_tz::Etc::UTC;
use dimensioned::si::{Kilogram, Meter, Second, M, S}; use dimensioned::si::{Kilogram, Meter, Second, M, S};
@ -42,7 +42,7 @@ mod test {
impl Recordable for BikeTrip { impl Recordable for BikeTrip {
fn timestamp(&self) -> Timestamp { fn timestamp(&self) -> Timestamp {
Timestamp::DateTime(self.datetime.clone()) Timestamp::DateTime(self.datetime)
} }
fn tags(&self) -> Vec<String> { fn tags(&self) -> Vec<String> {
Vec::new() Vec::new()
@ -99,7 +99,7 @@ mod test {
] ]
} }
fn run_test<T>(test: T) -> () fn run_test<T>(test: T)
where where
T: FnOnce(tempfile::TempPath), T: FnOnce(tempfile::TempPath),
{ {
@ -108,7 +108,7 @@ mod test {
test(tmp_path); test(tmp_path);
} }
fn run<T>(test: T) -> () fn run<T>(test: T)
where where
T: FnOnce(Series<BikeTrip>), T: FnOnce(Series<BikeTrip>),
{ {
@ -280,8 +280,7 @@ mod test {
UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0) UTC.with_ymd_and_hms(2011, 11, 04, 0, 0, 0)
.unwrap() .unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap()), .with_timezone(&FixedOffset::east_opt(0).unwrap()),
) ),
.into(),
true, true,
), ),
|l, r| l.timestamp().cmp(&r.timestamp()), |l, r| l.timestamp().cmp(&r.timestamp()),

View File

@ -22,8 +22,7 @@ use crate::{
}; };
use adw::prelude::*; use adw::prelude::*;
use chrono::{Duration, Local}; use chrono::{Duration, Local};
use emseries::Record;
use ft_core::TraxRecord;
use gio::resources_lookup_data; use gio::resources_lookup_data;
use gtk::STYLE_PROVIDER_PRIORITY_USER; use gtk::STYLE_PROVIDER_PRIORITY_USER;
use std::{cell::RefCell, path::PathBuf, rc::Rc}; use std::{cell::RefCell, path::PathBuf, rc::Rc};
@ -100,10 +99,6 @@ impl AppWindow {
window.set_content(Some(&navigation)); window.set_content(Some(&navigation));
window.present(); window.present();
let gesture = gtk::GestureClick::new();
gesture.connect_released(|_, _, _, _| println!("detected gesture"));
layout.add_controller(gesture);
let s = Self { let s = Self {
app: ft_app, app: ft_app,
layout, layout,
@ -137,7 +132,7 @@ impl AppWindow {
fn show_historical_view(&self, start_date: chrono::NaiveDate, end_date: chrono::NaiveDate) { fn show_historical_view(&self, start_date: chrono::NaiveDate, end_date: chrono::NaiveDate) {
let on_select_day = { let on_select_day = {
let s = self.clone(); let s = self.clone();
move |date, records| { move |date, _records| {
let s = s.clone(); let s = s.clone();
glib::spawn_future_local(async move { glib::spawn_future_local(async move {
let view_model = DayDetailViewModel::new(date, s.app.clone()).await; let view_model = DayDetailViewModel::new(date, s.app.clone()).await;
@ -172,7 +167,7 @@ impl AppWindow {
let end = Local::now().date_naive(); let end = Local::now().date_naive();
let start = end - Duration::days(7); let start = end - Duration::days(7);
match s.app.records(start, end).await { match s.app.records(start, end).await {
Ok(records) => s.show_historical_view(start, end), Ok(_records) => s.show_historical_view(start, end),
Err(_) => s.show_welcome_view(), Err(_) => s.show_welcome_view(),
} }
} }

View File

@ -105,7 +105,9 @@ impl DaySummary {
self.append(&row); self.append(&row);
let biking_summary = view_model.biking_summary(); let biking_summary = view_model.biking_summary();
time_distance_summary(biking_summary.0, biking_summary.1).map(|label| self.append(&label)); if let Some(label) = time_distance_summary(biking_summary.0, biking_summary.1) {
self.append(&label);
}
} }
} }
@ -261,10 +263,12 @@ impl DayEdit {
glib::spawn_future_local({ glib::spawn_future_local({
let s = self.clone(); let s = self.clone();
async move { async move {
let view_model = {
let view_model = s.imp().view_model.borrow(); let view_model = s.imp().view_model.borrow();
let view_model = view_model view_model
.as_ref() .clone()
.expect("DayEdit has not been initialized with the view model"); .expect("DayEdit has not been initialized with the view model")
};
let _ = view_model.save().await; let _ = view_model.save().await;
(s.imp().on_finished.borrow())() (s.imp().on_finished.borrow())()
} }
@ -272,7 +276,6 @@ impl DayEdit {
} }
fn add_row(&self, workout: Record<TraxRecord>) { fn add_row(&self, workout: Record<TraxRecord>) {
println!("adding a row for {:?}", workout);
let workout_rows = self.imp().workout_rows.borrow(); let workout_rows = self.imp().workout_rows.borrow();
let workout_id = workout.id; let workout_id = workout.id;
@ -286,10 +289,7 @@ impl DayEdit {
| TraxRecord::Walk(ref w) => { | TraxRecord::Walk(ref w) => {
workout_rows.append(&TimeDistanceEdit::new(workout_type, w.clone(), { workout_rows.append(&TimeDistanceEdit::new(workout_type, w.clone(), {
let s = self.clone(); let s = self.clone();
move |type_, data| { move |type_, data| s.update_workout(workout_id, type_, data)
println!("update workout callback on workout: {:?}", workout_id);
s.update_workout(workout_id, type_, data)
}
})); }));
} }
_ => {} _ => {}
@ -297,7 +297,6 @@ impl DayEdit {
} }
fn update_workout(&self, id: RecordId, type_: RecordType, data: ft_core::TimeDistance) { fn update_workout(&self, id: RecordId, type_: RecordType, data: ft_core::TimeDistance) {
println!("update workout");
let data = match type_ { let data = match type_ {
RecordType::BikeRide => TraxRecord::BikeRide(data), RecordType::BikeRide => TraxRecord::BikeRide(data),
RecordType::Row => TraxRecord::Row(data), RecordType::Row => TraxRecord::Row(data),
@ -319,7 +318,7 @@ fn control_buttons(s: &DayEdit, view_model: &DayDetailViewModel) -> ActionGroup
ActionGroup::builder() ActionGroup::builder()
.primary_action("Save", { .primary_action("Save", {
let s = s.clone(); let s = s.clone();
let view_model = view_model.clone(); let _view_model = view_model.clone();
move || s.finish() move || s.finish()
}) })
.secondary_action("Cancel", { .secondary_action("Cancel", {

View File

@ -11,7 +11,8 @@ FitnessTrax is distributed in the hope that it will be useful, but WITHOUT ANY W
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details. 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; mod action_group;

View File

@ -20,7 +20,6 @@ use crate::types::{
use gtk::prelude::*; use gtk::prelude::*;
use std::{cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
pub type Renderer<T> = dyn Fn(&T) -> String;
pub type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>; pub type Parser<T> = dyn Fn(&str) -> Result<T, ParseError>;
pub type OnUpdate<T> = dyn Fn(Option<T>); pub type OnUpdate<T> = dyn Fn(Option<T>);

View File

@ -25,7 +25,7 @@ use dimensioned::si;
use ft_core::{RecordType, TimeDistance}; use ft_core::{RecordType, TimeDistance};
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;
pub fn time_distance_summary( pub fn time_distance_summary(
distance: DistanceFormatter, distance: DistanceFormatter,
@ -112,7 +112,7 @@ pub fn time_distance_detail(type_: ft_core::RecordType, record: ft_core::TimeDis
pub struct TimeDistanceEditPrivate { pub struct TimeDistanceEditPrivate {
type_: RefCell<RecordType>, type_: RefCell<RecordType>,
workout: RefCell<TimeDistance>, workout: RefCell<TimeDistance>,
on_update: Rc<RefCell<Box<dyn Fn(RecordType, TimeDistance)>>>, on_update: RefCell<Box<dyn Fn(RecordType, TimeDistance)>>,
} }
impl Default for TimeDistanceEditPrivate { impl Default for TimeDistanceEditPrivate {
@ -120,7 +120,7 @@ impl Default for TimeDistanceEditPrivate {
Self { Self {
type_: RefCell::new(RecordType::BikeRide), type_: RefCell::new(RecordType::BikeRide),
workout: RefCell::new(TimeDistance::new(chrono::Utc::now().into())), workout: RefCell::new(TimeDistance::new(chrono::Utc::now().into())),
on_update: Rc::new(RefCell::new(Box::new(|_, _| {}))), on_update: RefCell::new(Box::new(|_, _| {})),
} }
} }
} }
@ -156,7 +156,6 @@ impl TimeDistanceEdit {
where where
OnUpdate: Fn(ft_core::RecordType, ft_core::TimeDistance) + 'static, OnUpdate: Fn(ft_core::RecordType, ft_core::TimeDistance) + 'static,
{ {
println!("new TimeDistanceEdit");
let s = Self::default(); let s = Self::default();
*s.imp().type_.borrow_mut() = type_; *s.imp().type_.borrow_mut() = type_;
@ -202,7 +201,6 @@ impl TimeDistanceEdit {
} }
fn update_distance(&self, distance: Option<DistanceFormatter>) { fn update_distance(&self, distance: Option<DistanceFormatter>) {
println!("update distance");
let mut workout = self.imp().workout.borrow_mut(); let mut workout = self.imp().workout.borrow_mut();
workout.distance = distance.map(|d| *d); workout.distance = distance.map(|d| *d);
(self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone()); (self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone());

View File

@ -246,14 +246,6 @@ impl From<si::Second<f64>> for DurationFormatter {
} }
} }
/*
fn take_digits(s: String) -> String {
s.chars()
.take_while(|t| t.is_ascii_digit())
.collect::<String>()
}
*/
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -48,10 +48,10 @@ impl<T: Clone + emseries::Recordable> RecordState<T> {
fn data(&self) -> Option<&Record<T>> { fn data(&self) -> Option<&Record<T>> {
match self { match self {
RecordState::Original(ref r) => Some(&r), RecordState::Original(ref r) => Some(r),
RecordState::New(ref r) => None, RecordState::New(ref _r) => None,
RecordState::Updated(ref r) => Some(&r), RecordState::Updated(ref r) => Some(r),
RecordState::Deleted(ref r) => Some(&r), RecordState::Deleted(ref r) => Some(r),
} }
} }
@ -193,29 +193,22 @@ impl DayDetailViewModel {
self.records self.records
.write() .write()
.unwrap() .unwrap()
.insert(new_record.id.clone(), RecordState::New(new_record.clone())); .insert(new_record.id, RecordState::New(new_record.clone()));
println!(
"record added: {:?}",
self.records.read().unwrap().get(&new_record.id)
);
new_record new_record
} }
pub fn update_record(&self, update: Record<TraxRecord>) { pub fn update_record(&self, update: Record<TraxRecord>) {
println!("updating a record: {:?}", update);
let mut records = self.records.write().unwrap(); let mut records = self.records.write().unwrap();
records records
.entry(update.id) .entry(update.id)
.and_modify(|mut record| record.set_value(update.data)); .and_modify(|record| record.set_value(update.data));
println!("record updated: {:?}", records.get(&update.id));
} }
pub fn records(&self) -> Vec<Record<TraxRecord>> { pub fn records(&self) -> Vec<Record<TraxRecord>> {
let read_lock = self.records.read().unwrap(); let read_lock = self.records.read().unwrap();
read_lock read_lock
.iter() .iter()
.map(|(_, record_state)| record_state.data()) .filter_map(|(_, record_state)| record_state.data())
.filter_map(|r| r)
.cloned() .cloned()
.collect::<Vec<Record<TraxRecord>>>() .collect::<Vec<Record<TraxRecord>>>()
} }
@ -271,7 +264,6 @@ impl DayDetailViewModel {
.collect::<Vec<RecordState<TraxRecord>>>(); .collect::<Vec<RecordState<TraxRecord>>>();
for record in records { for record in records {
println!("saving record: {:?}", record);
match record { match record {
RecordState::New(Record { data, .. }) => { RecordState::New(Record { data, .. }) => {
let _ = s.app.put_record(data).await; let _ = s.app.put_record(data).await;
@ -290,7 +282,12 @@ impl DayDetailViewModel {
} }
pub fn revert(&self) { pub fn revert(&self) {
self.populate_records(); glib::spawn_future({
let s = self.clone();
async move {
s.populate_records().await;
}
});
} }
async fn populate_records(&self) { async fn populate_records(&self) {
@ -304,7 +301,7 @@ impl DayDetailViewModel {
*self.weight.write().unwrap() = weight_records *self.weight.write().unwrap() = weight_records
.first() .first()
.and_then(|r| match r.data { .and_then(|r| match r.data {
TraxRecord::Weight(ref w) => Some((r.id.clone(), w.clone())), TraxRecord::Weight(ref w) => Some((r.id, w.clone())),
_ => None, _ => None,
}) })
.map(|(id, w)| RecordState::Original(Record { id, data: w })); .map(|(id, w)| RecordState::Original(Record { id, data: w }));
@ -312,14 +309,14 @@ impl DayDetailViewModel {
*self.steps.write().unwrap() = step_records *self.steps.write().unwrap() = step_records
.first() .first()
.and_then(|r| match r.data { .and_then(|r| match r.data {
TraxRecord::Steps(ref w) => Some((r.id.clone(), w.clone())), TraxRecord::Steps(ref w) => Some((r.id, w.clone())),
_ => None, _ => None,
}) })
.map(|(id, w)| RecordState::Original(Record { id, data: w })); .map(|(id, w)| RecordState::Original(Record { id, data: w }));
*self.records.write().unwrap() = records *self.records.write().unwrap() = records
.into_iter() .into_iter()
.map(|r| (r.id.clone(), RecordState::Original(r))) .map(|r| (r.id, RecordState::Original(r)))
.collect::<HashMap<RecordId, RecordState<TraxRecord>>>(); .collect::<HashMap<RecordId, RecordState<TraxRecord>>>();
} }
} }

View File

@ -17,12 +17,12 @@ You should have received a copy of the GNU General Public License along with Fit
use crate::{ use crate::{
app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel, app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel,
}; };
use chrono::NaiveDate;
use emseries::Record; use emseries::Record;
use ft_core::TraxRecord; use ft_core::TraxRecord;
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, collections::HashMap, rc::Rc}; use std::{cell::RefCell, rc::Rc};
/// The historical view will show a window into the main database. It will show some version of /// The historical view will show a window into the main database. It will show some version of
/// 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
@ -75,11 +75,7 @@ impl ObjectSubclass for HistoricalViewPrivate {
.expect("should be a DaySummary"); .expect("should be a DaySummary");
if let Some(app) = app.borrow().clone() { if let Some(app) = app.borrow().clone() {
let _ = glib::spawn_future_local(async move { glib::spawn_future_local(async move {
println!(
"setting up a DayDetailViewModel for {}",
date.date().format("%Y-%m-%d")
);
let view_model = DayDetailViewModel::new(date.date(), app.clone()).await; let view_model = DayDetailViewModel::new(date.date(), app.clone()).await;
summary.set_data(view_model); summary.set_data(view_model);
}); });
@ -111,7 +107,7 @@ impl HistoricalView {
*s.imp().app.borrow_mut() = Some(app); *s.imp().app.borrow_mut() = Some(app);
let mut model = gio::ListStore::new::<Date>(); let mut model = gio::ListStore::new::<Date>();
model.extend(interval.days().map(|d| Date::new(d))); model.extend(interval.days().map(Date::new));
s.imp() s.imp()
.list_view .list_view
.set_model(Some(&gtk::NoSelection::new(Some(model)))); .set_model(Some(&gtk::NoSelection::new(Some(model))));
@ -161,75 +157,6 @@ impl Date {
} }
pub fn date(&self) -> chrono::NaiveDate { pub fn date(&self) -> chrono::NaiveDate {
self.imp().date.borrow().clone() *self.imp().date.borrow()
}
}
#[cfg(test)]
mod test {
use super::GroupedRecords;
use crate::types::DayInterval;
use chrono::{FixedOffset, NaiveDate, TimeZone};
use dimensioned::si::{KG, M, S};
use emseries::{Record, RecordId};
use ft_core::{Steps, TimeDistance, TraxRecord, Weight};
#[test]
fn groups_records() {
let records = vec![
Record {
id: RecordId::default(),
data: TraxRecord::Steps(Steps {
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
count: 1500,
}),
},
Record {
id: RecordId::default(),
data: TraxRecord::Weight(Weight {
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
weight: 85. * KG,
}),
},
Record {
id: RecordId::default(),
data: TraxRecord::Weight(Weight {
date: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
weight: 86. * KG,
}),
},
Record {
id: RecordId::default(),
data: TraxRecord::BikeRide(TimeDistance {
datetime: FixedOffset::west_opt(10 * 60 * 60)
.unwrap()
.with_ymd_and_hms(2019, 6, 15, 12, 0, 0)
.unwrap(),
distance: Some(1000. * M),
duration: Some(150. * S),
comments: Some("Test Comments".to_owned()),
}),
},
Record {
id: RecordId::default(),
data: TraxRecord::BikeRide(TimeDistance {
datetime: FixedOffset::west_opt(10 * 60 * 60)
.unwrap()
.with_ymd_and_hms(2019, 6, 15, 23, 0, 0)
.unwrap(),
distance: Some(1000. * M),
duration: Some(150. * S),
comments: Some("Test Comments".to_owned()),
}),
},
];
let groups = GroupedRecords::new(DayInterval {
start: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
end: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
})
.with_data(records)
.data;
assert_eq!(groups.len(), 3);
} }
} }