Compare commits

...

3 Commits

Author SHA1 Message Date
Savanni D'Gerinel ea867812bc Clean up warnings and remove printlns 2024-02-08 11:07:59 -05:00
Savanni D'Gerinel cb55d6aae0 Reload data when the user saves on the DayEdit panel
This required some big overhauls. The view model no longer takes records. It only takes the date that it is responsible for, and it will ask the database for records pertaining to that date. This means that once the view model has saved all of its records, it can simply reload those records from the database. This has the effect that as soon as the user moves from DayEdit back to DayDetail, all of the interesting information has been repopulated.
2024-02-08 11:01:46 -05:00
Savanni D'Gerinel cfc7df5e2f The view model can no longer be initialized without an app 2024-02-08 10:32:57 -05:00
10 changed files with 78 additions and 129 deletions

View File

@ -201,7 +201,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

@ -95,6 +95,20 @@ impl App {
.await .await
.unwrap() .unwrap()
} }
pub async fn get_record(&self, id: RecordId) -> Result<Option<Record<TraxRecord>>, AppError> {
let db = self.database.clone();
self.runtime
.spawn_blocking(move || {
if let Some(ref db) = *db.read().unwrap() {
Ok(db.get(&id))
} else {
Err(AppError::NoDatabase)
}
})
.await
.unwrap()
}
} }
#[async_trait] #[async_trait]

View File

@ -99,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,
@ -135,14 +131,15 @@ impl AppWindow {
} }
fn show_historical_view(&self, interval: DayInterval) { fn show_historical_view(&self, interval: DayInterval) {
let view = View::Historical(HistoricalView::new(self.app.clone(), interval, { let on_select_day = {
let s = self.clone(); let s = self.clone();
Rc::new(move |date| { move |date| {
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.unwrap();
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0); let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
layout.append(&adw::HeaderBar::new()); layout.append(&adw::HeaderBar::new());
let view_model = DayDetailViewModel::new(date, s.app.clone()).await.unwrap(); // layout.append(&DayDetailView::new(date, records, s.app.clone()));
layout.append(&DayDetailView::new(view_model)); layout.append(&DayDetailView::new(view_model));
let page = &adw::NavigationPage::builder() let page = &adw::NavigationPage::builder()
.title(date.format("%Y-%m-%d").to_string()) .title(date.format("%Y-%m-%d").to_string())
@ -150,8 +147,14 @@ impl AppWindow {
.build(); .build();
s.navigation.push(page); s.navigation.push(page);
}); });
}) }
})); };
let view = View::Historical(HistoricalView::new(
self.app.clone(),
interval,
Rc::new(on_select_day),
));
self.swap_main(view); self.swap_main(view);
} }

View File

@ -148,31 +148,6 @@ impl DayDetail {
.build(), .build(),
); );
/*
let click_controller = gtk::GestureClick::new();
click_controller.connect_released({
let s = s.clone();
move |_, _, _, _| {
println!("clicked outside of focusable entity");
if let Some(widget) = s.focus_child().and_downcast_ref::<WeightView>() {
println!("focused child is the weight view");
widget.blur();
}
}
});
s.add_controller(click_controller);
*/
/*
let weight_record = records.iter().find_map(|record| match record {
Record {
id,
data: ft_core::TraxRecord::Weight(record),
} => Some((id.clone(), record.clone())),
_ => None,
});
*/
let top_row = gtk::Box::builder() let top_row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal) .orientation(gtk::Orientation::Horizontal)
.build(); .build();
@ -272,11 +247,23 @@ impl DayEdit {
} }
fn finish(&self) { fn finish(&self) {
(self.imp().on_finished.borrow())() glib::spawn_future_local({
let s = self.clone();
async move {
let view_model = {
let view_model = s.imp().view_model.borrow();
view_model
.as_ref()
.expect("DayEdit has not been initialized with the view model")
.clone()
};
let _ = view_model.async_save().await;
(s.imp().on_finished.borrow())()
}
});
} }
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();
#[allow(clippy::single_match)] #[allow(clippy::single_match)]
@ -308,11 +295,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(); move || s.finish()
move || {
view_model.save();
s.finish();
}
}) })
.secondary_action("Cancel", { .secondary_action("Cancel", {
let s = s.clone(); let s = s.clone();

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

@ -25,7 +25,7 @@ use dimensioned::si;
use ft_core::{TimeDistance, TimeDistanceActivity}; use ft_core::{TimeDistance, TimeDistanceActivity};
use glib::Object; use glib::Object;
use gtk::{prelude::*, subclass::prelude::*}; use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, rc::Rc}; use std::{rc::Rc, cell::RefCell};
pub fn time_distance_summary( pub fn time_distance_summary(
distance: DistanceFormatter, distance: DistanceFormatter,

View File

@ -256,14 +256,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

@ -120,51 +120,15 @@ impl DayDetailViewModel {
date: chrono::NaiveDate, date: chrono::NaiveDate,
provider: impl RecordProvider + 'static, provider: impl RecordProvider + 'static,
) -> Result<Self, ReadError> { ) -> Result<Self, ReadError> {
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) = let s = Self {
provider
.records(date, date)
.await?
.into_iter()
.partition(|r| r.data.is_weight());
let (step_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
records.into_iter().partition(|r| r.data.is_steps());
if weight_records.len() > 1 {
eprintln!("warning: multiple weight records found for {}. This is unsupported and the one presented is unpredictable.", date.format("%Y-%m-%d"));
}
if step_records.len() > 1 {
eprintln!("warning: multiple step records found for {}. This is unsupported and the one presented is unpredictable.", date.format("%Y-%m-%d"));
}
Ok(Self {
provider: Arc::new(provider), provider: Arc::new(provider),
date, date,
weight: Arc::new(RwLock::new( weight: Arc::new(RwLock::new(None)),
weight_records steps: Arc::new(RwLock::new(None)),
.first() records: Arc::new(RwLock::new(HashMap::new())),
.and_then(|r| match r.data { };
TraxRecord::Weight(ref w) => Some((r.id, w.clone())), s.populate_records().await;
_ => None, Ok(s)
})
.map(|(id, w)| RecordState::Original(Record { id, data: w })),
)),
steps: Arc::new(RwLock::new(
step_records
.first()
.and_then(|r| match r.data {
TraxRecord::Steps(ref w) => Some((r.id, w.clone())),
_ => None,
})
.map(|(id, w)| RecordState::Original(Record { id, data: w })),
)),
records: Arc::new(RwLock::new(
records
.into_iter()
.map(|r| (r.id, RecordState::Original(r)))
.collect::<HashMap<RecordId, RecordState<TraxRecord>>>(),
)),
})
} }
pub fn weight(&self) -> Option<si::Kilogram<f64>> { pub fn weight(&self) -> Option<si::Kilogram<f64>> {
@ -266,12 +230,10 @@ impl DayDetailViewModel {
} }
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(|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>> {
@ -308,7 +270,6 @@ impl DayDetailViewModel {
pub fn save(&self) { pub fn save(&self) {
let s = self.clone(); let s = self.clone();
glib::spawn_future(async move { s.async_save().await }); glib::spawn_future(async move { s.async_save().await });
} }
@ -377,6 +338,7 @@ impl DayDetailViewModel {
} }
} }
} }
self.populate_records().await;
} }
pub async fn revert(&self) { pub async fn revert(&self) {

View File

@ -18,8 +18,6 @@ use crate::{
app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel, app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel,
}; };
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, rc::Rc};
@ -60,15 +58,12 @@ impl ObjectSubclass for HistoricalViewPrivate {
factory.connect_bind({ factory.connect_bind({
let app = s.app.clone(); let app = s.app.clone();
move |_, list_item| { move |_, list_item| {
let app = app.clone();
let list_item = list_item.clone();
glib::spawn_future_local(async move {
let date = list_item let date = list_item
.downcast_ref::<gtk::ListItem>() .downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem") .expect("should be a ListItem")
.item() .item()
.and_downcast::<Date>() .and_downcast::<Date>()
.expect("should be a DaySummary"); .expect("should be a Date");
let summary = list_item let summary = list_item
.downcast_ref::<gtk::ListItem>() .downcast_ref::<gtk::ListItem>()
@ -79,12 +74,12 @@ impl ObjectSubclass for HistoricalViewPrivate {
if let Some(app) = app.borrow().clone() { if let Some(app) = app.borrow().clone() {
glib::spawn_future_local(async move { glib::spawn_future_local(async move {
let view_model = let view_model = DayDetailViewModel::new(date.date(), app.clone())
DayDetailViewModel::new(date.date(), app).await.unwrap(); .await
.unwrap();
summary.set_data(view_model); summary.set_data(view_model);
}); });
} }
});
} }
}); });