From 1c2c4982a111fab9028df231df021f65301da085 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 18 Jan 2024 07:43:18 -0500 Subject: [PATCH] Update the record in the detail view on save --- Cargo.lock | 1 + fitnesstrax/app/Cargo.toml | 1 + fitnesstrax/app/src/app.rs | 5 ++++ fitnesstrax/app/src/components/day.rs | 1 - fitnesstrax/app/src/components/text_entry.rs | 2 -- fitnesstrax/app/src/views/day_detail_view.rs | 28 +++++++++++++++----- 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3340ab5..c823cc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1031,6 +1031,7 @@ dependencies = [ "glib-build-tools 0.18.0", "gtk4", "libadwaita", + "thiserror", "tokio", ] diff --git a/fitnesstrax/app/Cargo.toml b/fitnesstrax/app/Cargo.toml index 980cc44..2f58d54 100644 --- a/fitnesstrax/app/Cargo.toml +++ b/fitnesstrax/app/Cargo.toml @@ -16,6 +16,7 @@ ft-core = { path = "../core" } gio = { version = "0.18" } glib = { version = "0.18" } gtk = { version = "0.7", package = "gtk4", features = [ "v4_10" ] } +thiserror = { version = "1.0" } tokio = { version = "1.34", features = [ "full" ] } [build-dependencies] diff --git a/fitnesstrax/app/src/app.rs b/fitnesstrax/app/src/app.rs index 655b2c4..94a6e00 100644 --- a/fitnesstrax/app/src/app.rs +++ b/fitnesstrax/app/src/app.rs @@ -22,11 +22,16 @@ use std::{ path::PathBuf, sync::{Arc, RwLock}, }; +use thiserror::Error; use tokio::runtime::Runtime; +#[derive(Debug, Error)] pub enum AppError { + #[error("no database loaded")] NoDatabase, + #[error("failed to open the database")] FailedToOpenDatabase, + #[error("unhandled error")] Unhandled, } diff --git a/fitnesstrax/app/src/components/day.rs b/fitnesstrax/app/src/components/day.rs index 2a3d8b7..1652d6c 100644 --- a/fitnesstrax/app/src/components/day.rs +++ b/fitnesstrax/app/src/components/day.rs @@ -281,7 +281,6 @@ impl DayEdit { let s = s.clone(); let records = records.clone(); move || { - println!("saving to database"); let weight_record = records.iter().find_map(|record| match record { Record { id, diff --git a/fitnesstrax/app/src/components/text_entry.rs b/fitnesstrax/app/src/components/text_entry.rs index cc6c2a0..996f1ed 100644 --- a/fitnesstrax/app/src/components/text_entry.rs +++ b/fitnesstrax/app/src/components/text_entry.rs @@ -73,7 +73,6 @@ impl TextEntry { } match (self.parser)(buffer.text().as_str()) { Ok(v) => { - println!("setting the value: {}", (self.renderer)(&v)); *self.value.borrow_mut() = Some(v); self.widget.remove_css_class("error"); } @@ -86,7 +85,6 @@ impl TextEntry { pub fn value(&self) -> Option { let v = self.value.borrow().clone(); - println!("retrieving the value: {:?}", v.map(|v| (self.renderer)(&v))); self.value.borrow().clone() } diff --git a/fitnesstrax/app/src/views/day_detail_view.rs b/fitnesstrax/app/src/views/day_detail_view.rs index f9b5128..c73d1f0 100644 --- a/fitnesstrax/app/src/views/day_detail_view.rs +++ b/fitnesstrax/app/src/views/day_detail_view.rs @@ -109,40 +109,56 @@ impl DayDetailView { let s = self.clone(); let app = self.imp().app.clone(); Box::new(move |record| { + let s = s.clone(); let app = app.clone(); glib::spawn_future_local({ async move { match &*app.borrow() { Some(app) => { - let _ = app.put_record(record).await; + let id = app + .put_record(record.clone()) + .await + .expect("successful write"); + s.imp() + .records + .borrow_mut() + .push(Record { id, data: record }); } None => {} } + s.view(); } }); - s.view(); }) } fn on_update_record(&self) -> Box)> { let s = self.clone(); let app = self.imp().app.clone(); - Box::new(move |record| { + Box::new(move |updated_record| { let app = app.clone(); + + let mut records = s.imp().records.borrow_mut(); + let idx = records.iter().position(|r| r.id == updated_record.id); + match idx { + Some(i) => records[i] = updated_record.clone(), + None => records.push(updated_record.clone()), + } + glib::spawn_future_local({ + let s = s.clone(); async move { - println!("record: {:?}", record); match &*app.borrow() { Some(app) => { - let _ = app.update_record(record).await; + let _ = app.update_record(updated_record).await; } None => { println!("no app!"); } } + s.view(); } }); - s.view(); }) } }