Compare commits

...

22 Commits

Author SHA1 Message Date
Savanni D'Gerinel 31d74588fb Clean up warnings and remove printlns 2024-01-31 09:42:07 -05:00
Savanni D'Gerinel 07f487b139 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-01-31 09:38:28 -05:00
Savanni D'Gerinel 79d705c1d0 The view model can no longer be initialized without an app 2024-01-31 09:38:28 -05:00
Savanni D'Gerinel 2d476f266c Create Duration and Distance structures to handle rendering
These structures handle parsing and rendering of a Duration and a Distance, allowing that knowledge to be centralized and reused. Then I'm using those structures in a variety of places in order to ensure that the information gets rendered consistently.
2024-01-31 09:38:28 -05:00
Savanni D'Gerinel 798fbff320 Show existing time/distance workout rows in day detail and editor 2024-01-31 09:28:22 -05:00
Savanni D'Gerinel 6e26923629 Save new time/distance records
This sets up a bunch of callbacks. We're starting to get into Callback Hell, where there are things that need knowledge that I really don't want them to have.

However, edit fields for TimeDistanceEdit now propogate data back into the view model, which is then able to save the results.
2024-01-31 09:28:22 -05:00
Savanni D'Gerinel 4fdf390ecf Build the facilities to add a new time/distance workout
This adds the code to show the new records in the UI, plus it adds them to the view model. Some of the representation changed in order to facilitate linking UI elements to particular records. There are now some buttons to create workouts of various types, clicking on a button adds a new row to the UI, and it also adds a new record to the view model. Saving the view model writes the records to the database.
2024-01-31 09:17:59 -05:00
Savanni D'Gerinel 29b1e6054b Make emseries::Record copyable 2024-01-31 09:16:35 -05:00
Savanni D'Gerinel 0f5af82cb5 Build some convenienc functions for measurement entry fields
Move the weight field into text_entry
2024-01-31 09:16:35 -05:00
Savanni D'Gerinel 525cc88c25 Add buttons with icons to represent workouts 2024-01-31 09:12:55 -05:00
Savanni D'Gerinel 06d118060e Add a test program for gnome icons 2024-01-31 09:12:55 -05:00
Savanni D'Gerinel 251077b0c1 Implement the Edit Cancel button 2024-01-31 09:12:55 -05:00
Savanni D'Gerinel ce8bed13f9 Render time distance details in the day detail view 2024-01-31 09:09:04 -05:00
Savanni D'Gerinel 279810f7d7 Show a summary of the day's biking stats when there is one 2024-01-31 09:09:04 -05:00
Savanni D'Gerinel 772188b470 Set up text entry fields for all of the common metrics 2024-01-31 08:56:54 -05:00
Savanni D'Gerinel bc31522c95 Add the on_update callback to TextEntry, and test the component 2024-01-31 08:44:46 -05:00
Savanni D'Gerinel 6c68564a77 Create a function which safely initializes GTK once
This is only available in test code, and it allows GUI component tests to run without having to worry about double-initializing GTK
2024-01-31 08:40:55 -05:00
Savanni D'Gerinel 55c1a6372f Rename the formatters 2024-01-31 08:38:17 -05:00
Savanni D'Gerinel 2cbd539bf4 Add a type for managing Time of Day values 2024-01-31 08:27:42 -05:00
Savanni D'Gerinel 7d14308def Create automated testing for weight, duration, and distance formatters 2024-01-31 08:10:46 -05:00
Savanni D'Gerinel dcd6301bb9 Introduce structures for formatting and parsing Duration and Distance values
These aren't in use yet.
2024-01-30 10:11:30 -05:00
Savanni D'Gerinel 69567db486 Introduce a structure for formatting and parsing Weight values 2024-01-30 10:08:10 -05:00
28 changed files with 1326 additions and 585 deletions

11
Cargo.lock generated
View File

@ -933,6 +933,7 @@ dependencies = [
"dimensioned 0.8.0",
"emseries",
"ft-core",
"gdk4",
"gio",
"glib",
"glib-build-tools 0.18.0",
@ -1794,6 +1795,16 @@ dependencies = [
"cc",
]
[[package]]
name = "icon-test"
version = "0.1.0"
dependencies = [
"gio",
"glib",
"gtk4",
"libadwaita",
]
[[package]]
name = "idna"
version = "0.1.5"

View File

@ -16,6 +16,7 @@ members = [
"geo-types",
"gm-control-panel",
"hex-grid",
"icon-test",
"ifc",
"kifu/core",
"kifu/gtk",

View File

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

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, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
pub struct RecordId(Uuid);
impl Default for RecordId {
@ -190,7 +190,7 @@ mod test {
impl Recordable for WeightRecord {
fn timestamp(&self) -> Timestamp {
Timestamp::Date(self.date.clone())
Timestamp::Date(self.date)
}
fn tags(&self) -> Vec<String> {

View File

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

View File

@ -15,6 +15,7 @@ emseries = { path = "../../emseries" }
ft-core = { path = "../core" }
gio = { version = "0.18" }
glib = { version = "0.18" }
gdk = { version = "0.7", package = "gdk4" }
gtk = { version = "0.7", package = "gtk4", features = [ "v4_10" ] }
thiserror = { version = "1.0" }
tokio = { version = "1.34", features = [ "full" ] }

View File

@ -3,4 +3,15 @@
<gresource prefix="/com/luminescent-dreams/fitnesstrax/">
<file>style.css</file>
</gresource>
<gresource prefix="/com/luminescent-dreams/fitnesstrax/icons/scalable/actions/">
<file preprocess="xml-stripblanks">walking2-symbolic.svg</file>
</gresource>
<gresource prefix="/com/luminescent-dreams/fitnesstrax/icons/scalable/actions">
<file preprocess="xml-stripblanks">running-symbolic.svg</file>
</gresource>
<gresource prefix="/com/luminescent-dreams/fitnesstrax/icons/scalable/actions">
<file preprocess="xml-stripblanks">cycling-symbolic.svg</file>
</gresource>
</gresources>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="16px" viewBox="0 0 16 16" width="16px"><filter id="a" height="100%" width="100%" x="0%" y="0%"><feColorMatrix color-interpolation-filters="sRGB" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/></filter><mask id="b"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.5"/></g></mask><clipPath id="c"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><mask id="d"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.7"/></g></mask><clipPath id="e"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><mask id="f"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.35"/></g></mask><clipPath id="g"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><path d="m 9.5 2 c -0.828125 0 -1.5 0.671875 -1.5 1.5 s 0.671875 1.5 1.5 1.5 s 1.5 -0.671875 1.5 -1.5 s -0.671875 -1.5 -1.5 -1.5 z m 0 0"/><path d="m 4.285156 13 c 0 0.703125 -0.582031 1.285156 -1.285156 1.285156 s -1.285156 -0.582031 -1.285156 -1.285156 s 0.582031 -1.285156 1.285156 -1.285156 s 1.285156 0.582031 1.285156 1.285156 z m -4.285156 0 c 0 1.675781 1.324219 3 3 3 s 3 -1.324219 3 -3 s -1.324219 -3 -3 -3 s -3 1.324219 -3 3 z m 0 0"/><path d="m 8.992188 13.007812 v -3.003906 c 0 -0.359375 -0.1875 -0.6875 -0.5 -0.867187 l -2.558594 -1.476563 l 0.363281 1.363282 l 1.671875 -2.890626 l -1.367188 0.363282 l 0.910157 0.527344 l -0.40625 -0.4375 c 0.773437 1.621093 1.96875 1.933593 1.96875 1.933593 s 0.578125 0.242188 1.9375 0.429688 c 0.546875 0.074219 1.050781 -0.304688 1.128906 -0.851563 c 0.074219 -0.550781 -0.308594 -1.054687 -0.855469 -1.128906 c -1.179687 -0.164062 -1.601562 -0.355469 -1.601562 -0.355469 s -0.425782 -0.164062 -0.769532 -0.886719 c -0.089843 -0.183593 -0.226562 -0.335937 -0.402343 -0.4375 l -0.910157 -0.523437 c -0.476562 -0.277344 -1.089843 -0.113281 -1.363281 0.367187 l -1.671875 2.890626 c -0.277344 0.480468 -0.113281 1.089843 0.367188 1.367187 l 2.558594 1.480469 l -0.5 -0.867188 v 3.003906 c 0 0.550782 0.449218 1 1 1 c 0.554687 0 1 -0.449218 1 -1 z m 0 0"/><path d="m 14.285156 13 c 0 0.703125 -0.582031 1.285156 -1.285156 1.285156 s -1.285156 -0.582031 -1.285156 -1.285156 s 0.582031 -1.285156 1.285156 -1.285156 s 1.285156 0.582031 1.285156 1.285156 z m -4.285156 0 c 0 1.675781 1.324219 3 3 3 s 3 -1.324219 3 -3 s -1.324219 -3 -3 -3 s -3 1.324219 -3 3 z m 0 0"/><g mask="url(#b)"><g clip-path="url(#c)" transform="matrix(1 0 0 1 -600 -120)"><path d="m 550 182 c -0.351562 0.003906 -0.695312 0.101562 -1 0.28125 v 3.4375 c 0.304688 0.179688 0.648438 0.277344 1 0.28125 c 1.105469 0 2 -0.894531 2 -2 s -0.894531 -2 -2 -2 z m 0 5 c -0.339844 0 -0.679688 0.058594 -1 0.175781 v 6.824219 h 4 v -4 c 0 -1.65625 -1.34375 -3 -3 -3 z m 0 0"/></g></g><g mask="url(#d)"><g clip-path="url(#e)" transform="matrix(1 0 0 1 -600 -120)"><path d="m 569 182 v 4 c 1.105469 0 2 -0.894531 2 -2 s -0.894531 -2 -2 -2 z m 0 5 v 7 h 3 v -4 c 0 -1.65625 -1.34375 -3 -3 -3 z m 0 0"/></g></g><g mask="url(#f)"><g clip-path="url(#g)" transform="matrix(1 0 0 1 -600 -120)"><path d="m 573 182.269531 v 3.449219 c 0.613281 -0.355469 0.996094 -1.007812 1 -1.71875 c 0 -0.714844 -0.382812 -1.375 -1 -1.730469 z m 0 4.90625 v 6.824219 h 2 v -4 c 0 -1.269531 -0.800781 -2.402344 -2 -2.824219 z m 0 0"/></g></g></svg>

After

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="16px" viewBox="0 0 16 16" width="16px"><filter id="a" height="100%" width="100%" x="0%" y="0%"><feColorMatrix color-interpolation-filters="sRGB" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/></filter><mask id="b"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.5"/></g></mask><clipPath id="c"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><mask id="d"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.7"/></g></mask><clipPath id="e"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><mask id="f"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.35"/></g></mask><clipPath id="g"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><path d="m 8.5 0 c -0.828125 0 -1.5 0.671875 -1.5 1.5 s 0.671875 1.5 1.5 1.5 s 1.5 -0.671875 1.5 -1.5 s -0.671875 -1.5 -1.5 -1.5 z m -2.5 4 c -0.117188 0 -0.230469 0.027344 -0.335938 0.082031 l -2 1 c -0.144531 0.070313 -0.261718 0.1875 -0.332031 0.332031 l -1 2 c -0.1875 0.371094 -0.039062 0.820313 0.332031 1.007813 c 0.371094 0.183594 0.820313 0.035156 1.003907 -0.335937 l 0.890625 -1.777344 l 1.5625 -0.773438 c -0.042969 0.074219 -0.726563 2.835938 -0.726563 2.835938 c -0.230469 0.949218 0.398438 1.523437 0.398438 1.523437 l 3.351562 2.703125 l 0.90625 2.71875 c 0.175781 0.523438 0.742188 0.808594 1.265625 0.632813 c 0.523438 -0.175781 0.808594 -0.742188 0.632813 -1.265625 l -1 -3 c -0.0625 -0.183594 -0.171875 -0.34375 -0.324219 -0.464844 l -2 -1.597656 l 0.679688 -2.714844 l 0.25 0.625 c 0.085937 0.222656 0.28125 0.390625 0.515624 0.449219 l 2 0.5 c 0.402344 0.097656 0.808594 -0.144531 0.910157 -0.546875 c 0.097656 -0.40625 -0.144531 -0.8125 -0.546875 -0.910156 l -1.628906 -0.40625 l -0.855469 -2.144532 c -0.117188 -0.285156 -0.390625 -0.472656 -0.699219 -0.472656 z m -1.164062 6.328125 l -0.710938 2.128906 l -1.832031 1.835938 c -0.390625 0.390625 -0.390625 1.023437 0 1.414062 s 1.023437 0.390625 1.414062 0 l 2 -2 c 0.109375 -0.109375 0.191407 -0.242187 0.242188 -0.390625 l 0.542969 -1.628906 z m 0 0"/><g mask="url(#b)"><g clip-path="url(#c)" transform="matrix(1 0 0 1 -620 -120)"><path d="m 550 182 c -0.351562 0.003906 -0.695312 0.101562 -1 0.28125 v 3.4375 c 0.304688 0.179688 0.648438 0.277344 1 0.28125 c 1.105469 0 2 -0.894531 2 -2 s -0.894531 -2 -2 -2 z m 0 5 c -0.339844 0 -0.679688 0.058594 -1 0.175781 v 6.824219 h 4 v -4 c 0 -1.65625 -1.34375 -3 -3 -3 z m 0 0"/></g></g><g mask="url(#d)"><g clip-path="url(#e)" transform="matrix(1 0 0 1 -620 -120)"><path d="m 569 182 v 4 c 1.105469 0 2 -0.894531 2 -2 s -0.894531 -2 -2 -2 z m 0 5 v 7 h 3 v -4 c 0 -1.65625 -1.34375 -3 -3 -3 z m 0 0"/></g></g><g mask="url(#f)"><g clip-path="url(#g)" transform="matrix(1 0 0 1 -620 -120)"><path d="m 573 182.269531 v 3.449219 c 0.613281 -0.355469 0.996094 -1.007812 1 -1.71875 c 0 -0.714844 -0.382812 -1.375 -1 -1.730469 z m 0 4.90625 v 6.824219 h 2 v -4 c 0 -1.269531 -0.800781 -2.402344 -2 -2.824219 z m 0 0"/></g></g></svg>

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" height="16px" viewBox="0 0 16 16" width="16px"><filter id="a" height="100%" width="100%" x="0%" y="0%"><feColorMatrix color-interpolation-filters="sRGB" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/></filter><mask id="b"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.5"/></g></mask><clipPath id="c"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><mask id="d"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.7"/></g></mask><clipPath id="e"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><mask id="f"><g filter="url(#a)"><path d="m -1.6 -1.6 h 19.2 v 19.2 h -19.2 z" fill-opacity="0.35"/></g></mask><clipPath id="g"><path d="m 0 0 h 1600 v 1200 h -1600 z"/></clipPath><path d="m 9.5 1.5 c 0 0.828125 -0.671875 1.5 -1.5 1.5 s -1.5 -0.671875 -1.5 -1.5 s 0.671875 -1.5 1.5 -1.5 s 1.5 0.671875 1.5 1.5 z m 0 0"/><path d="m 7 4 c -0.550781 0 -1 0.449219 -1 1 v 4 c 0 0.265625 0.105469 0.519531 0.292969 0.707031 l 0.445312 0.449219 l -2.59375 4.328125 c -0.285156 0.476563 -0.132812 1.089844 0.34375 1.375 c 0.472657 0.28125 1.085938 0.128906 1.367188 -0.34375 l 2.34375 -3.902344 l 0.925781 0.929688 l 0.925781 2.773437 c 0.082031 0.25 0.265625 0.460938 0.5 0.578125 c 0.238281 0.121094 0.515625 0.140625 0.765625 0.054688 c 0.25 -0.082031 0.460938 -0.265625 0.578125 -0.5 c 0.121094 -0.238281 0.140625 -0.515625 0.054688 -0.765625 l -1 -3 c -0.050781 -0.148438 -0.132813 -0.28125 -0.242188 -0.390625 l -1.707031 -1.707031 v -4.585938 c 0 -0.550781 -0.449219 -1 -1 -1 z m 0 0"/><path d="m 6 4 c -0.101562 0 -0.207031 0.019531 -0.300781 0.0625 c 0 0 -2.113281 0.847656 -2.199219 2.90625 v 0.03125 v 2.25 c 0 0.414062 0.335938 0.75 0.75 0.75 s 0.75 -0.335938 0.75 -0.75 v -2.21875 c 0.039062 -0.894531 1.050781 -1.449219 1.207031 -1.53125 h 2.332031 l 1.042969 2.085938 c 0.097657 0.195312 0.273438 0.339843 0.488281 0.394531 l 2 0.5 c 0.191407 0.046875 0.394532 0.015625 0.566407 -0.085938 c 0.171875 -0.101562 0.292969 -0.269531 0.34375 -0.460937 c 0.046875 -0.195313 0.015625 -0.398438 -0.085938 -0.570313 c -0.101562 -0.171875 -0.269531 -0.292969 -0.464843 -0.34375 l -1.664063 -0.414062 l -1.097656 -2.191407 c -0.125 -0.253906 -0.382813 -0.414062 -0.667969 -0.414062 z m 0 0"/><g mask="url(#b)"><g clip-path="url(#c)" transform="matrix(1 0 0 1 -620 -100)"><path d="m 550 182 c -0.351562 0.003906 -0.695312 0.101562 -1 0.28125 v 3.4375 c 0.304688 0.179688 0.648438 0.277344 1 0.28125 c 1.105469 0 2 -0.894531 2 -2 s -0.894531 -2 -2 -2 z m 0 5 c -0.339844 0 -0.679688 0.058594 -1 0.175781 v 6.824219 h 4 v -4 c 0 -1.65625 -1.34375 -3 -3 -3 z m 0 0"/></g></g><g mask="url(#d)"><g clip-path="url(#e)" transform="matrix(1 0 0 1 -620 -100)"><path d="m 569 182 v 4 c 1.105469 0 2 -0.894531 2 -2 s -0.894531 -2 -2 -2 z m 0 5 v 7 h 3 v -4 c 0 -1.65625 -1.34375 -3 -3 -3 z m 0 0"/></g></g><g mask="url(#f)"><g clip-path="url(#g)" transform="matrix(1 0 0 1 -620 -100)"><path d="m 573 182.269531 v 3.449219 c 0.613281 -0.355469 0.996094 -1.007812 1 -1.71875 c 0 -0.714844 -0.382812 -1.375 -1 -1.730469 z m 0 4.90625 v 6.824219 h 2 v -4 c 0 -1.269531 -0.800781 -2.402344 -2 -2.824219 z m 0 0"/></g></g></svg>

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -57,6 +57,20 @@ impl App {
}
}
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()
}
pub async fn records(
&self,
start: NaiveDate,

View File

@ -16,13 +16,13 @@ You should have received a copy of the GNU General Public License along with Fit
use crate::{
app::App,
types::DayInterval,
view_models::DayDetailViewModel,
views::{DayDetailView, HistoricalView, PlaceholderView, View, WelcomeView},
};
use adw::prelude::*;
use chrono::{Duration, Local};
use emseries::Record;
use ft_core::TraxRecord;
use gio::resources_lookup_data;
use gtk::STYLE_PROVIDER_PRIORITY_USER;
use std::{cell::RefCell, path::PathBuf, rc::Rc};
@ -54,7 +54,7 @@ impl AppWindow {
let window = adw::ApplicationWindow::builder()
.application(adw_app)
.width_request(800)
.height_request(600)
.height_request(746)
.build();
let stylesheet = String::from_utf8(
@ -99,10 +99,6 @@ impl AppWindow {
window.set_content(Some(&navigation));
window.present();
let gesture = gtk::GestureClick::new();
gesture.connect_released(|_, _, _, _| println!("detected gesture"));
layout.add_controller(gesture);
let s = Self {
app: ft_app,
layout,
@ -133,25 +129,34 @@ impl AppWindow {
self.swap_main(view);
}
fn show_historical_view(&self, records: Vec<Record<TraxRecord>>) {
let view = View::Historical(HistoricalView::new(self.app.clone(), records, {
fn show_historical_view(&self, start_date: chrono::NaiveDate, end_date: chrono::NaiveDate) {
let on_select_day = {
let s = self.clone();
Rc::new(move |date, records| {
move |date, _records| {
let s = s.clone();
glib::spawn_future_local(async move {
let view_model = DayDetailViewModel::new(date, s.app.clone()).await;
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
layout.append(&adw::HeaderBar::new());
// layout.append(&DayDetailView::new(date, records, s.app.clone()));
layout.append(&DayDetailView::new(DayDetailViewModel::new(
date,
records,
s.app.clone(),
)));
layout.append(&DayDetailView::new(view_model));
let page = &adw::NavigationPage::builder()
.title(date.format("%Y-%m-%d").to_string())
.child(&layout)
.build();
s.navigation.push(page);
})
}));
});
}
};
let view = View::Historical(HistoricalView::new(
self.app.clone(),
DayInterval {
start: start_date,
end: end_date,
},
Rc::new(on_select_day),
));
self.swap_main(view);
}
@ -162,7 +167,7 @@ impl AppWindow {
let end = Local::now().date_naive();
let start = end - Duration::days(7);
match s.app.records(start, end).await {
Ok(records) => s.show_historical_view(records),
Ok(_records) => s.show_historical_view(start, end),
Err(_) => s.show_welcome_view(),
}
}

View File

@ -17,12 +17,18 @@ You should have received a copy of the GNU General Public License along with Fit
// use chrono::NaiveDate;
// use ft_core::TraxRecord;
use crate::{
components::{steps_editor, weight_editor, ActionGroup, Steps, Weight},
components::{
steps_editor, time_distance_summary, weight_field, ActionGroup, Steps, WeightLabel,
},
view_models::DayDetailViewModel,
};
use emseries::{Record, RecordId};
use ft_core::{RecordType, TraxRecord};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell;
use std::{cell::RefCell, rc::Rc};
use super::{time_distance::TimeDistanceEdit, time_distance_detail};
pub struct DaySummaryPrivate {
date: gtk::Label,
@ -93,11 +99,15 @@ impl DaySummary {
.css_classes(["day-summary__weight"])
.build();
if let Some(s) = view_model.steps() {
label.set_label(&format!("{} steps", s.to_string()));
label.set_label(&format!("{} steps", s));
}
row.append(&label);
self.append(&row);
let biking_summary = view_model.biking_summary();
if let Some(label) = time_distance_summary(biking_summary.0, biking_summary.1) {
self.append(&label);
}
}
}
@ -134,35 +144,10 @@ impl DayDetail {
.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()
.orientation(gtk::Orientation::Horizontal)
.build();
let weight_view = Weight::new(view_model.weight());
let weight_view = WeightLabel::new(view_model.weight());
top_row.append(&weight_view.widget());
let steps_view = Steps::new(view_model.steps());
@ -170,51 +155,19 @@ impl DayDetail {
s.append(&top_row);
/*
records.into_iter().for_each(|record| {
let record_view = match record {
Record {
data: ft_core::TraxRecord::BikeRide(record),
..
} => Some(
TimeDistanceView::new(ft_core::RecordType::BikeRide, record)
.upcast::<gtk::Widget>(),
),
Record {
data: ft_core::TraxRecord::Row(record),
..
} => Some(
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
),
Record {
data: ft_core::TraxRecord::Run(record),
..
} => Some(
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
),
Record {
data: ft_core::TraxRecord::Swim(record),
..
} => Some(
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
),
Record {
data: ft_core::TraxRecord::Walk(record),
..
} => Some(
TimeDistanceView::new(ft_core::RecordType::Row, record).upcast::<gtk::Widget>(),
),
_ => None,
};
if let Some(record_view) = record_view {
record_view.add_css_class("day-detail");
record_view.set_halign(gtk::Align::Start);
s.append(&record_view);
let records = view_model.records();
for emseries::Record { data, .. } in records {
match data {
TraxRecord::BikeRide(ride) => {
s.append(&time_distance_detail(RecordType::BikeRide, ride))
}
TraxRecord::Row(row) => s.append(&time_distance_detail(RecordType::Row, row)),
TraxRecord::Run(run) => s.append(&time_distance_detail(RecordType::Run, run)),
TraxRecord::Swim(walk) => s.append(&time_distance_detail(RecordType::Swim, walk)),
TraxRecord::Walk(walk) => s.append(&time_distance_detail(RecordType::Walk, walk)),
_ => {}
}
}
});
*/
s
}
@ -222,12 +175,21 @@ impl DayDetail {
pub struct DayEditPrivate {
on_finished: RefCell<Box<dyn Fn()>>,
workout_rows: RefCell<gtk::Box>,
view_model: RefCell<Option<DayDetailViewModel>>,
}
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(),
),
view_model: RefCell::new(None),
}
}
}
@ -255,18 +217,109 @@ impl DayEdit {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.set_hexpand(true);
*s.imp().on_finished.borrow_mut() = Box::new(on_finished);
*s.imp().view_model.borrow_mut() = Some(view_model.clone());
s.append(
&ActionGroup::builder()
let workout_buttons = workout_buttons(view_model.clone(), {
let s = s.clone();
move |workout| s.add_row(workout)
});
view_model
.records()
.into_iter()
.filter_map({
let s = s.clone();
move |record| {
let workout_type = record.data.workout_type();
match record.data {
TraxRecord::BikeRide(workout)
| TraxRecord::Row(workout)
| TraxRecord::Run(workout)
| TraxRecord::Swim(workout)
| TraxRecord::Walk(workout) => {
Some(TimeDistanceEdit::new(workout_type, workout, {
let s = s.clone();
move |type_, data| {
s.update_workout(record.id, type_, data);
}
}))
}
_ => None,
}
}
})
.for_each(|row| s.imp().workout_rows.borrow().append(&row));
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
}
fn finish(&self) {
glib::spawn_future_local({
let s = self.clone();
async move {
let view_model = {
let view_model = s.imp().view_model.borrow();
view_model
.clone()
.expect("DayEdit has not been initialized with the view model")
};
let _ = view_model.save().await;
(s.imp().on_finished.borrow())()
}
});
}
fn add_row(&self, workout: Record<TraxRecord>) {
let workout_rows = self.imp().workout_rows.borrow();
let workout_id = workout.id;
let workout_type = workout.data.workout_type();
match workout.data {
TraxRecord::BikeRide(ref w)
| TraxRecord::Row(ref w)
| TraxRecord::Swim(ref w)
| TraxRecord::Run(ref w)
| TraxRecord::Walk(ref w) => {
workout_rows.append(&TimeDistanceEdit::new(workout_type, w.clone(), {
let s = self.clone();
move |type_, data| s.update_workout(workout_id, type_, data)
}));
}
_ => {}
}
}
fn update_workout(&self, id: RecordId, type_: RecordType, data: ft_core::TimeDistance) {
let data = match type_ {
RecordType::BikeRide => TraxRecord::BikeRide(data),
RecordType::Row => TraxRecord::Row(data),
RecordType::Swim => TraxRecord::Swim(data),
RecordType::Run => TraxRecord::Run(data),
RecordType::Walk => TraxRecord::Walk(data),
_ => panic!("Record type {:?} is not a Time/Distance record", type_),
};
let record = Record { id, data };
let view_model = self.imp().view_model.borrow();
let view_model = view_model
.as_ref()
.expect("DayEdit has not been initialized with a view model");
view_model.update_record(record);
}
}
fn control_buttons(s: &DayEdit, view_model: &DayDetailViewModel) -> ActionGroup {
ActionGroup::builder()
.primary_action("Save", {
let s = s.clone();
let view_model = view_model.clone();
move || {
view_model.save();
s.finish();
}
let _view_model = view_model.clone();
move || s.finish()
})
.secondary_action("Cancel", {
let s = s.clone();
@ -276,35 +329,93 @@ impl DayEdit {
s.finish();
}
})
.build(),
);
.build()
}
let top_row = gtk::Box::builder()
fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
let row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.build();
top_row.append(
&weight_editor(view_model.weight(), {
row.append(
&weight_field(view_model.weight(), {
let view_model = view_model.clone();
move |w| {
view_model.set_weight(w);
move |w| match w {
Some(w) => view_model.set_weight(w),
None => eprintln!("have not implemented record delete"),
}
})
.widget(),
);
top_row.append(
row.append(
&steps_editor(view_model.steps(), {
let view_model = view_model.clone();
move |s| view_model.set_steps(s)
move |s| match s {
Some(s) => view_model.set_steps(s),
None => eprintln!("have not implemented record delete"),
}
})
.widget(),
);
s.append(&top_row);
s
}
fn finish(&self) {
(self.imp().on_finished.borrow())()
}
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);
/*
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 biking_button = gtk::Button::builder()
.icon_name("cycling-symbolic")
.width_request(64)
.height_request(64)
.build();
biking_button.connect_clicked({
let view_model = view_model.clone();
move |_| {
let workout = view_model.new_record(RecordType::BikeRide);
add_row(workout);
}
});
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
let row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.build();
row.append(&biking_button);
layout.append(&row);
layout
}

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
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;
@ -27,13 +28,13 @@ mod steps;
pub use steps::{steps_editor, Steps};
mod text_entry;
pub use text_entry::{ParseError, TextEntry};
pub use text_entry::{distance_field, duration_field, time_field, weight_field, TextEntry};
mod time_distance;
pub use time_distance::TimeDistanceView;
pub use time_distance::{time_distance_detail, time_distance_summary};
mod weight;
pub use weight::{weight_editor, Weight};
pub use weight::WeightLabel;
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};

View File

@ -14,7 +14,7 @@ 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/>.
*/
use crate::components::{ParseError, TextEntry};
use crate::{components::TextEntry, types::ParseError};
use gtk::prelude::*;
#[derive(Default)]
@ -44,18 +44,13 @@ impl Steps {
pub fn steps_editor<OnUpdate>(value: Option<u32>, on_update: OnUpdate) -> TextEntry<u32>
where
OnUpdate: Fn(u32) + 'static,
OnUpdate: Fn(Option<u32>) + 'static,
{
TextEntry::new(
"0",
value,
|v| format!("{}", v),
move |v| match v.parse::<u32>() {
Ok(val) => {
on_update(val);
Ok(val)
}
Err(_) => Err(ParseError),
},
|v| v.parse::<u32>().map_err(|_| ParseError),
on_update,
)
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of FitnessTrax.
@ -14,22 +14,22 @@ 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/>.
*/
use crate::types::{
DistanceFormatter, DurationFormatter, FormatOption, ParseError, TimeFormatter, WeightFormatter,
};
use gtk::prelude::*;
use std::{cell::RefCell, rc::Rc};
#[derive(Clone, Debug)]
pub struct ParseError;
type Renderer<T> = dyn Fn(&T) -> String;
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>);
#[derive(Clone)]
pub struct TextEntry<T: Clone + std::fmt::Debug> {
value: Rc<RefCell<Option<T>>>,
widget: gtk::Entry,
#[allow(unused)]
renderer: Rc<Renderer<T>>,
parser: Rc<Parser<T>>,
on_update: Rc<OnUpdate<T>>,
}
impl<T: Clone + std::fmt::Debug> std::fmt::Debug for TextEntry<T> {
@ -44,10 +44,17 @@ impl<T: Clone + std::fmt::Debug> std::fmt::Debug for TextEntry<T> {
// I do not understand why the data should be 'static.
impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
pub fn new<R, V>(placeholder: &str, value: Option<T>, renderer: R, parser: V) -> Self
pub fn new<R, V, U>(
placeholder: &str,
value: Option<T>,
renderer: R,
parser: V,
on_update: U,
) -> Self
where
R: Fn(&T) -> String + 'static,
V: Fn(&str) -> Result<T, ParseError> + 'static,
U: Fn(Option<T>) + 'static,
{
let widget = gtk::Entry::builder().placeholder_text(placeholder).build();
if let Some(ref v) = value {
@ -57,8 +64,8 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
let s = Self {
value: Rc::new(RefCell::new(value)),
widget,
renderer: Rc::new(renderer),
parser: Rc::new(parser),
on_update: Rc::new(on_update),
};
s.widget.buffer().connect_text_notify({
@ -73,12 +80,14 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
if buffer.text().is_empty() {
*self.value.borrow_mut() = None;
self.widget.remove_css_class("error");
(self.on_update)(None);
return;
}
match (self.parser)(buffer.text().as_str()) {
Ok(v) => {
*self.value.borrow_mut() = Some(v);
*self.value.borrow_mut() = Some(v.clone());
self.widget.remove_css_class("error");
(self.on_update)(Some(v));
}
// need to change the border to provide a visual indicator of an error
Err(_) => {
@ -87,25 +96,147 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
}
}
#[allow(unused)]
pub fn value(&self) -> Option<T> {
let v = self.value.borrow().clone();
self.value.borrow().clone()
}
pub fn set_value(&self, value: Option<T>) {
if let Some(ref v) = value {
self.widget.set_text(&(self.renderer)(v))
}
*self.value.borrow_mut() = value;
}
#[allow(unused)]
pub fn grab_focus(&self) {
self.widget.grab_focus();
}
pub fn widget(&self) -> gtk::Widget {
self.widget.clone().upcast::<gtk::Widget>()
}
#[cfg(test)]
fn has_parse_error(&self) -> bool {
self.widget.has_css_class("error")
}
}
pub fn time_field<OnUpdate>(
value: Option<TimeFormatter>,
on_update: OnUpdate,
) -> TextEntry<TimeFormatter>
where
OnUpdate: Fn(Option<TimeFormatter>) + 'static,
{
TextEntry::new(
"HH:MM",
value,
|val| val.format(FormatOption::Abbreviated),
TimeFormatter::parse,
on_update,
)
}
pub fn distance_field<OnUpdate>(
value: Option<DistanceFormatter>,
on_update: OnUpdate,
) -> TextEntry<DistanceFormatter>
where
OnUpdate: Fn(Option<DistanceFormatter>) + 'static,
{
TextEntry::new(
"0 km",
value,
|val| val.format(FormatOption::Abbreviated),
DistanceFormatter::parse,
on_update,
)
}
pub fn duration_field<OnUpdate>(
value: Option<DurationFormatter>,
on_update: OnUpdate,
) -> TextEntry<DurationFormatter>
where
OnUpdate: Fn(Option<DurationFormatter>) + 'static,
{
TextEntry::new(
"0 m",
value,
|val| val.format(FormatOption::Abbreviated),
DurationFormatter::parse,
on_update,
)
}
pub fn weight_field<OnUpdate>(
weight: Option<WeightFormatter>,
on_update: OnUpdate,
) -> TextEntry<WeightFormatter>
where
OnUpdate: Fn(Option<WeightFormatter>) + 'static,
{
TextEntry::new(
"0 kg",
weight,
|val| val.format(FormatOption::Abbreviated),
WeightFormatter::parse,
on_update,
)
}
#[cfg(test)]
mod test {
use super::*;
use crate::gtk_init::gtk_init;
fn setup_u32_entry() -> (Rc<RefCell<Option<u32>>>, TextEntry<u32>) {
let current_value = Rc::new(RefCell::new(None));
let entry = TextEntry::new(
"step count",
None,
|steps| format!("{}", steps),
|v| v.parse::<u32>().map_err(|_| ParseError),
{
let current_value = current_value.clone();
move |v| *current_value.borrow_mut() = v
},
);
(current_value, entry)
}
#[test]
fn it_responds_to_field_changes() {
gtk_init();
let (current_value, entry) = setup_u32_entry();
let buffer = entry.widget.buffer();
buffer.set_text("1");
assert_eq!(*current_value.borrow(), Some(1));
buffer.set_text("15");
assert_eq!(*current_value.borrow(), Some(15));
}
#[test]
fn it_preserves_last_value_in_parse_error() {
crate::gtk_init::gtk_init();
let (current_value, entry) = setup_u32_entry();
let buffer = entry.widget.buffer();
buffer.set_text("1");
assert_eq!(*current_value.borrow(), Some(1));
buffer.set_text("a5");
assert_eq!(*current_value.borrow(), Some(1));
assert!(entry.has_parse_error());
}
#[test]
fn it_update_on_empty_strings() {
gtk_init();
let (current_value, entry) = setup_u32_entry();
let buffer = entry.widget.buffer();
buffer.set_text("1");
assert_eq!(*current_value.borrow(), Some(1));
buffer.set_text("");
assert_eq!(*current_value.borrow(), None);
buffer.set_text("1");
assert_eq!(*current_value.borrow(), Some(1));
buffer.set_text("1a");
assert_eq!(*current_value.borrow(), Some(1));
assert!(entry.has_parse_error());
buffer.set_text("");
assert_eq!(*current_value.borrow(), None);
assert!(!entry.has_parse_error());
}
}

View File

@ -17,38 +17,39 @@ You should have received a copy of the GNU General Public License along with Fit
// use crate::components::{EditView, ParseError, TextEntry};
// use chrono::{Local, NaiveDate};
// use dimensioned::si;
use crate::{
components::{distance_field, duration_field, time_field},
types::{DistanceFormatter, DurationFormatter, FormatOption, TimeFormatter},
};
use dimensioned::si;
use ft_core::{RecordType, TimeDistance};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell;
#[derive(Default)]
pub struct TimeDistanceViewPrivate {
#[allow(unused)]
record: RefCell<Option<TimeDistance>>,
pub fn time_distance_summary(
distance: DistanceFormatter,
duration: DurationFormatter,
) -> Option<gtk::Label> {
let text = match (*distance > si::M, *duration > si::S) {
(true, true) => Some(format!(
"{} of biking in {}",
distance.format(FormatOption::Full),
duration.format(FormatOption::Full)
)),
(true, false) => Some(format!("{} of biking", distance.format(FormatOption::Full))),
(false, true) => Some(format!("{} of biking", duration.format(FormatOption::Full))),
(false, false) => None,
};
text.map(|text| gtk::Label::new(Some(&text)))
}
#[glib::object_subclass]
impl ObjectSubclass for TimeDistanceViewPrivate {
const NAME: &'static str = "TimeDistanceView";
type Type = TimeDistanceView;
type ParentType = gtk::Box;
}
impl ObjectImpl for TimeDistanceViewPrivate {}
impl WidgetImpl for TimeDistanceViewPrivate {}
impl BoxImpl for TimeDistanceViewPrivate {}
glib::wrapper! {
pub struct TimeDistanceView(ObjectSubclass<TimeDistanceViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl TimeDistanceView {
pub fn new(type_: RecordType, record: TimeDistance) -> Self {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.set_hexpand(true);
pub fn time_distance_detail(type_: ft_core::RecordType, record: ft_core::TimeDistance) -> gtk::Box {
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.hexpand(true)
.build();
let first_row = gtk::Box::builder().homogeneous(true).build();
first_row.append(
@ -71,7 +72,7 @@ impl TimeDistanceView {
.label(
record
.distance
.map(|dist| format!("{}", dist))
.map(|dist| DistanceFormatter::from(dist).format(FormatOption::Abbreviated))
.unwrap_or("".to_owned()),
)
.build(),
@ -83,15 +84,17 @@ impl TimeDistanceView {
.label(
record
.duration
.map(|duration| format!("{}", duration))
.map(|duration| {
DurationFormatter::from(duration).format(FormatOption::Abbreviated)
})
.unwrap_or("".to_owned()),
)
.build(),
);
s.append(&first_row);
layout.append(&first_row);
s.append(
layout.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(
@ -103,6 +106,109 @@ impl TimeDistanceView {
.build(),
);
layout
}
pub struct TimeDistanceEditPrivate {
type_: RefCell<RecordType>,
workout: RefCell<TimeDistance>,
on_update: RefCell<Box<dyn Fn(RecordType, TimeDistance)>>,
}
impl Default for TimeDistanceEditPrivate {
fn default() -> Self {
Self {
type_: RefCell::new(RecordType::BikeRide),
workout: RefCell::new(TimeDistance::new(chrono::Utc::now().into())),
on_update: RefCell::new(Box::new(|_, _| {})),
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for TimeDistanceEditPrivate {
const NAME: &'static str = "TimeDistanceEdit";
type Type = TimeDistanceEdit;
type ParentType = gtk::Box;
}
impl ObjectImpl for TimeDistanceEditPrivate {}
impl WidgetImpl for TimeDistanceEditPrivate {}
impl BoxImpl for TimeDistanceEditPrivate {}
glib::wrapper! {
pub struct TimeDistanceEdit(ObjectSubclass<TimeDistanceEditPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl Default for TimeDistanceEdit {
fn default() -> Self {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.set_hexpand(true);
s.set_css_classes(&["time-distance-edit"]);
s
}
}
impl TimeDistanceEdit {
pub fn new<OnUpdate>(type_: RecordType, workout: TimeDistance, on_update: OnUpdate) -> Self
where
OnUpdate: Fn(ft_core::RecordType, ft_core::TimeDistance) + 'static,
{
let s = Self::default();
*s.imp().type_.borrow_mut() = type_;
*s.imp().workout.borrow_mut() = workout.clone();
*s.imp().on_update.borrow_mut() = Box::new(on_update);
let details_row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.build();
details_row.append(
&time_field(
Some(TimeFormatter::from(workout.datetime.naive_local().time())),
{
let s = s.clone();
move |t| s.update_time(t)
},
)
.widget(),
);
details_row.append(
&distance_field(workout.distance.map(DistanceFormatter::from), {
let s = s.clone();
move |d| s.update_distance(d)
})
.widget(),
);
details_row.append(
&duration_field(workout.duration.map(DurationFormatter::from), {
let s = s.clone();
move |d| s.update_duration(d)
})
.widget(),
);
s.append(&details_row);
s.append(&gtk::Entry::new());
s
}
fn update_time(&self, time: Option<TimeFormatter>) {
unimplemented!()
}
fn update_distance(&self, distance: Option<DistanceFormatter>) {
let mut workout = self.imp().workout.borrow_mut();
workout.distance = distance.map(|d| *d);
(self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone());
}
fn update_duration(&self, duration: Option<DurationFormatter>) {
let mut workout = self.imp().workout.borrow_mut();
workout.duration = duration.map(|d| *d);
(self.imp().on_update.borrow())(self.imp().type_.borrow().clone(), workout.clone());
}
}

View File

@ -1,5 +1,5 @@
/*
Copyright 2023, Savanni D'Gerinel <savanni@luminescent-dreams.com>
Copyright 2023-2024, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of FitnessTrax.
@ -14,23 +14,26 @@ 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/>.
*/
use crate::components::{ParseError, TextEntry};
use crate::{
components::TextEntry,
types::{FormatOption, WeightFormatter},
};
use dimensioned::si;
use gtk::prelude::*;
pub struct Weight {
pub struct WeightLabel {
label: gtk::Label,
}
impl Weight {
pub fn new(weight: Option<si::Kilogram<f64>>) -> Self {
impl WeightLabel {
pub fn new(weight: Option<WeightFormatter>) -> Self {
let label = gtk::Label::builder()
.css_classes(["card", "weight-view"])
.can_focus(true)
.build();
match weight {
Some(w) => label.set_text(&format!("{:?}", w)),
Some(w) => label.set_text(&w.format(FormatOption::Abbreviated)),
None => label.set_text("No weight recorded"),
}
@ -41,27 +44,3 @@ impl Weight {
self.label.clone().upcast()
}
}
pub fn weight_editor<OnUpdate>(
weight: Option<si::Kilogram<f64>>,
on_update: OnUpdate,
) -> TextEntry<si::Kilogram<f64>>
where
OnUpdate: Fn(si::Kilogram<f64>) + 'static,
{
TextEntry::new(
"0 kg",
weight,
|val: &si::Kilogram<f64>| val.to_string(),
move |v: &str| {
let new_weight = v.parse::<f64>().map(|w| w * si::KG).map_err(|_| ParseError);
match new_weight {
Ok(w) => {
on_update(w);
Ok(w)
}
Err(err) => Err(err),
}
},
)
}

View File

@ -0,0 +1,10 @@
use std::sync::Once;
static INITIALIZED: Once = Once::new();
pub fn gtk_init() {
INITIALIZED.call_once(|| {
eprintln!("initializing GTK");
let _ = gtk::init();
});
}

View File

@ -17,6 +17,8 @@ You should have received a copy of the GNU General Public License along with Fit
mod app;
mod app_window;
mod components;
#[cfg(test)]
mod gtk_init;
mod types;
mod view_models;
mod views;
@ -60,6 +62,9 @@ fn main() {
.build();
adw_app.connect_activate(move |adw_app| {
let icon_theme = gtk::IconTheme::for_display(&gdk::Display::default().unwrap());
icon_theme.add_resource_path(&(RESOURCE_BASE_PATH.to_owned() + "/icons/scalable/actions"));
AppWindow::new(app_id, RESOURCE_BASE_PATH, adw_app, ft_app.clone());
});

View File

@ -1,4 +1,8 @@
use chrono::{Duration, Local, NaiveDate};
use chrono::{Local, NaiveDate};
use dimensioned::si;
#[derive(Clone, Debug, PartialEq)]
pub struct ParseError;
// This interval doesn't feel right, either. The idea that I have a specific interval type for just
// NaiveDate is odd. This should be genericized, as should the iterator. Also, it shouldn't live
@ -12,7 +16,7 @@ pub struct DayInterval {
impl Default for DayInterval {
fn default() -> Self {
Self {
start: (Local::now() - Duration::days(7)).date_naive(),
start: (Local::now() - chrono::Duration::days(7)).date_naive(),
end: Local::now().date_naive(),
}
}
@ -38,10 +42,298 @@ impl Iterator for DayIterator {
fn next(&mut self) -> Option<Self::Item> {
if self.current <= self.end {
let val = self.current;
self.current += Duration::days(1);
self.current += chrono::Duration::days(1);
Some(val)
} else {
None
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FormatOption {
Abbreviated,
Full,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TimeFormatter(chrono::NaiveTime);
impl TimeFormatter {
pub fn format(&self, option: FormatOption) -> String {
match option {
FormatOption::Abbreviated => self.0.format("%H:%M"),
FormatOption::Full => self.0.format("%H:%M:%S"),
}
.to_string()
}
pub fn parse(s: &str) -> Result<TimeFormatter, ParseError> {
let parts = s
.split(':')
.map(|part| part.parse::<u32>().map_err(|_| ParseError))
.collect::<Result<Vec<u32>, ParseError>>()?;
match parts.len() {
0 => Err(ParseError),
1 => Err(ParseError),
2 => Ok(TimeFormatter(
chrono::NaiveTime::from_hms_opt(parts[0], parts[1], 0).unwrap(),
)),
3 => Ok(TimeFormatter(
chrono::NaiveTime::from_hms_opt(parts[0], parts[1], parts[2]).unwrap(),
)),
_ => Err(ParseError),
}
}
}
impl std::ops::Deref for TimeFormatter {
type Target = chrono::NaiveTime;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<chrono::NaiveTime> for TimeFormatter {
fn from(value: chrono::NaiveTime) -> Self {
Self(value)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct WeightFormatter(si::Kilogram<f64>);
impl WeightFormatter {
pub fn format(&self, option: FormatOption) -> String {
match option {
FormatOption::Abbreviated => format!("{} kg", self.0.value_unsafe),
FormatOption::Full => format!("{} kilograms", self.0.value_unsafe),
}
}
pub fn parse(s: &str) -> Result<WeightFormatter, ParseError> {
s.parse::<f64>()
.map(|w| WeightFormatter(w * si::KG))
.map_err(|_| ParseError)
}
}
impl std::ops::Add for WeightFormatter {
type Output = WeightFormatter;
fn add(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 + rside.0)
}
}
impl std::ops::Sub for WeightFormatter {
type Output = WeightFormatter;
fn sub(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 - rside.0)
}
}
impl std::ops::Deref for WeightFormatter {
type Target = si::Kilogram<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<si::Kilogram<f64>> for WeightFormatter {
fn from(value: si::Kilogram<f64>) -> Self {
Self(value)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct DistanceFormatter(si::Meter<f64>);
impl DistanceFormatter {
pub fn format(&self, option: FormatOption) -> String {
match option {
FormatOption::Abbreviated => format!("{} km", self.0.value_unsafe / 1000.),
FormatOption::Full => format!("{} kilometers", self.0.value_unsafe / 1000.),
}
}
pub fn parse(s: &str) -> Result<DistanceFormatter, ParseError> {
let value = s.parse::<f64>().map_err(|_| ParseError)?;
Ok(DistanceFormatter(value * 1000. * si::M))
}
}
impl std::ops::Add for DistanceFormatter {
type Output = DistanceFormatter;
fn add(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 + rside.0)
}
}
impl std::ops::Sub for DistanceFormatter {
type Output = DistanceFormatter;
fn sub(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 - rside.0)
}
}
impl std::ops::Deref for DistanceFormatter {
type Target = si::Meter<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<si::Meter<f64>> for DistanceFormatter {
fn from(value: si::Meter<f64>) -> Self {
Self(value)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct DurationFormatter(si::Second<f64>);
impl DurationFormatter {
pub fn format(&self, option: FormatOption) -> String {
let (hours, minutes) = self.hours_and_minutes();
let (h, m) = match option {
FormatOption::Abbreviated => ("h", "m"),
FormatOption::Full => (" hours", " minutes"),
};
if hours > 0 {
format!("{}{} {}{}", hours, h, minutes, m)
} else {
format!("{}{}", minutes, m)
}
}
pub fn parse(s: &str) -> Result<DurationFormatter, ParseError> {
let value = s.parse::<f64>().map_err(|_| ParseError)?;
Ok(DurationFormatter(value * 60. * si::S))
}
fn hours_and_minutes(&self) -> (i64, i64) {
let minutes: i64 = (self.0.value_unsafe / 60.).round() as i64;
let hours: i64 = minutes / 60;
let minutes = minutes - (hours * 60);
(hours, minutes)
}
}
impl std::ops::Add for DurationFormatter {
type Output = DurationFormatter;
fn add(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 + rside.0)
}
}
impl std::ops::Sub for DurationFormatter {
type Output = DurationFormatter;
fn sub(self, rside: Self) -> Self::Output {
Self::Output::from(self.0 - rside.0)
}
}
impl std::ops::Deref for DurationFormatter {
type Target = si::Second<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<si::Second<f64>> for DurationFormatter {
fn from(value: si::Second<f64>) -> Self {
Self(value)
}
}
#[cfg(test)]
mod test {
use super::*;
use dimensioned::si;
#[test]
fn it_parses_weight_values() {
assert_eq!(
WeightFormatter::parse("15.3"),
Ok(WeightFormatter(15.3 * si::KG))
);
assert_eq!(WeightFormatter::parse("15.ab"), Err(ParseError));
}
#[test]
fn it_formats_weight_values() {
assert_eq!(
WeightFormatter::from(15.3 * si::KG).format(FormatOption::Abbreviated),
"15.3 kg"
);
assert_eq!(
WeightFormatter::from(15.3 * si::KG).format(FormatOption::Full),
"15.3 kilograms"
);
}
#[test]
fn it_parses_distance_values() {
assert_eq!(
DistanceFormatter::parse("70"),
Ok(DistanceFormatter(70000. * si::M))
);
assert_eq!(DistanceFormatter::parse("15.ab"), Err(ParseError));
}
#[test]
fn it_formats_distance_values() {
assert_eq!(
DistanceFormatter::from(70000. * si::M).format(FormatOption::Abbreviated),
"70 km"
);
assert_eq!(
DistanceFormatter::from(70000. * si::M).format(FormatOption::Full),
"70 kilometers"
);
}
#[test]
fn it_parses_duration_values() {
assert_eq!(
DurationFormatter::parse("70"),
Ok(DurationFormatter(4200. * si::S))
);
assert_eq!(DurationFormatter::parse("15.ab"), Err(ParseError));
}
#[test]
fn it_formats_duration_values() {
assert_eq!(
DurationFormatter::from(4200. * si::S).format(FormatOption::Abbreviated),
"1h 10m"
);
assert_eq!(
DurationFormatter::from(4200. * si::S).format(FormatOption::Full),
"1 hours 10 minutes"
);
}
#[test]
fn it_parses_time_values() {
assert_eq!(
TimeFormatter::parse("13:25"),
Ok(TimeFormatter::from(
chrono::NaiveTime::from_hms_opt(13, 25, 0).unwrap()
)),
);
assert_eq!(
TimeFormatter::parse("13:25:50"),
Ok(TimeFormatter::from(
chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap()
)),
);
}
#[test]
fn it_formats_time_values() {
let time = TimeFormatter::from(chrono::NaiveTime::from_hms_opt(13, 25, 50).unwrap());
assert_eq!(time.format(FormatOption::Abbreviated), "13:25".to_owned());
assert_eq!(time.format(FormatOption::Full), "13:25:50".to_owned());
}
}

View File

@ -14,10 +14,12 @@ 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/>.
*/
use crate::app::App;
use dimensioned::si;
use crate::{
app::App,
types::{DistanceFormatter, DurationFormatter, WeightFormatter},
};
use emseries::{Record, RecordId, Recordable};
use ft_core::TraxRecord;
use ft_core::{RecordType, TimeDistance, TraxRecord};
use std::{
collections::HashMap,
ops::Deref,
@ -27,7 +29,7 @@ use std::{
#[derive(Clone, Debug)]
enum RecordState<T: Clone + Recordable> {
Original(Record<T>),
New(T),
New(Record<T>),
Updated(Record<T>),
#[allow(unused)]
Deleted(Record<T>),
@ -44,15 +46,29 @@ impl<T: Clone + emseries::Recordable> RecordState<T> {
}
}
fn with_value(self, value: T) -> RecordState<T> {
fn data(&self) -> Option<&Record<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 }),
RecordState::Original(ref r) => Some(r),
RecordState::New(ref _r) => None,
RecordState::Updated(ref r) => Some(r),
RecordState::Deleted(ref r) => Some(r),
}
}
fn set_value(&mut self, value: T) {
*self = match self {
RecordState::Original(r) => RecordState::Updated(Record { data: value, ..*r }),
RecordState::New(r) => RecordState::New(Record { data: value, ..*r }),
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)]
fn with_delete(self) -> Option<RecordState<T>> {
match self {
@ -69,19 +85,16 @@ 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,
RecordState::New(ref r) => &r.data,
RecordState::Updated(ref r) => &r.data,
RecordState::Deleted(ref r) => &r.data,
}
}
}
#[derive(Default)]
struct DayDetailViewModelInner {}
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct DayDetailViewModel {
app: Option<App>,
app: App,
pub date: chrono::NaiveDate,
weight: Arc<RwLock<Option<RecordState<ft_core::Weight>>>>,
steps: Arc<RwLock<Option<RecordState<ft_core::Steps>>>>,
@ -89,56 +102,38 @@ pub struct DayDetailViewModel {
}
impl DayDetailViewModel {
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>, app: App) -> Self {
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
records.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());
Self {
app: Some(app),
pub async fn new(date: chrono::NaiveDate, app: App) -> Self {
let s = Self {
app,
date,
weight: Arc::new(RwLock::new(
weight_records
.first()
.and_then(|r| match r.data {
TraxRecord::Weight(ref w) => Some((r.id.clone(), w.clone())),
_ => None,
})
.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.clone(), w.clone())),
_ => None,
})
.map(|(id, w)| RecordState::Original(Record { id, data: w })),
)),
records: Arc::new(RwLock::new(
records
.into_iter()
.map(|r| (r.id.clone(), RecordState::Original(r)))
.collect::<HashMap<RecordId, RecordState<TraxRecord>>>(),
)),
}
weight: Arc::new(RwLock::new(None)),
steps: Arc::new(RwLock::new(None)),
records: Arc::new(RwLock::new(HashMap::new())),
};
s.populate_records().await;
s
}
pub fn weight(&self) -> Option<si::Kilogram<f64>> {
(*self.weight.read().unwrap()).as_ref().map(|w| w.weight)
pub fn weight(&self) -> Option<WeightFormatter> {
(*self.weight.read().unwrap())
.as_ref()
.map(|w| WeightFormatter::from(w.weight))
}
pub fn set_weight(&self, new_weight: si::Kilogram<f64>) {
pub fn set_weight(&self, new_weight: WeightFormatter) {
let mut record = self.weight.write().unwrap();
let new_record = match *record {
Some(ref rstate) => rstate.clone().with_value(ft_core::Weight {
date: self.date,
weight: new_weight,
weight: *new_weight,
}),
None => RecordState::New(ft_core::Weight {
None => RecordState::New(Record {
id: RecordId::default(),
data: ft_core::Weight {
date: self.date,
weight: new_weight,
weight: *new_weight,
},
}),
};
*record = Some(new_record);
@ -155,27 +150,82 @@ impl DayDetailViewModel {
date: self.date,
count: new_count,
}),
None => RecordState::New(ft_core::Steps {
None => RecordState::New(Record {
id: RecordId::default(),
data: ft_core::Steps {
date: self.date,
count: new_count,
},
}),
};
*record = Some(new_record);
}
pub fn save(&self) {
pub fn biking_summary(&self) -> (DistanceFormatter, DurationFormatter) {
self.records.read().unwrap().iter().fold(
(DistanceFormatter::default(), DurationFormatter::default()),
|(acc_distance, acc_duration), (_, record)| match record.data() {
Some(Record {
data:
TraxRecord::BikeRide(TimeDistance {
distance, duration, ..
}),
..
}) => (
distance
.map(|distance| acc_distance + DistanceFormatter::from(distance))
.unwrap_or(acc_distance),
duration
.map(|duration| acc_duration + DurationFormatter::from(duration))
.unwrap_or(acc_duration),
),
_ => (acc_distance, acc_duration),
},
)
}
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, 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(|record| record.set_value(update.data));
}
pub fn records(&self) -> Vec<Record<TraxRecord>> {
let read_lock = self.records.read().unwrap();
read_lock
.iter()
.filter_map(|(_, record_state)| record_state.data())
.cloned()
.collect::<Vec<Record<TraxRecord>>>()
}
pub fn save(&self) -> glib::JoinHandle<()> {
glib::spawn_future({
let s = self.clone();
async move {
if let Some(app) = s.app {
let weight_record = s.weight.read().unwrap().clone();
match weight_record {
Some(RecordState::New(weight)) => {
let _ = app.put_record(TraxRecord::Weight(weight)).await;
Some(RecordState::New(Record { data, .. })) => {
let _ = s.app.put_record(TraxRecord::Weight(data)).await;
}
Some(RecordState::Original(_)) => {}
Some(RecordState::Updated(weight)) => {
let _ = app
let _ = s
.app
.update_record(Record {
id: weight.id,
data: TraxRecord::Weight(weight.data),
@ -188,12 +238,13 @@ impl DayDetailViewModel {
let steps_record = s.steps.read().unwrap().clone();
match steps_record {
Some(RecordState::New(steps)) => {
let _ = app.put_record(TraxRecord::Steps(steps)).await;
Some(RecordState::New(Record { data, .. })) => {
let _ = s.app.put_record(TraxRecord::Steps(data)).await;
}
Some(RecordState::Original(_)) => {}
Some(RecordState::Updated(steps)) => {
let _ = app
let _ = s
.app
.update_record(Record {
id: steps.id,
data: TraxRecord::Steps(steps.data),
@ -214,22 +265,86 @@ impl DayDetailViewModel {
for record in records {
match record {
RecordState::New(data) => {
let _ = app.put_record(data).await;
RecordState::New(Record { data, .. }) => {
let _ = s.app.put_record(data).await;
}
RecordState::Original(_) => {}
RecordState::Updated(r) => {
let _ = app.update_record(r.clone()).await;
let _ = s.app.update_record(r.clone()).await;
}
RecordState::Deleted(_) => unimplemented!(),
}
}
s.populate_records().await;
}
})
}
pub fn revert(&self) {
glib::spawn_future({
let s = self.clone();
async move {
s.populate_records().await;
}
});
}
pub fn revert(&self) {
unimplemented!();
async fn populate_records(&self) {
let records = self.app.records(self.date, self.date).await.unwrap();
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
records.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());
*self.weight.write().unwrap() = weight_records
.first()
.and_then(|r| match r.data {
TraxRecord::Weight(ref w) => Some((r.id, w.clone())),
_ => None,
})
.map(|(id, w)| RecordState::Original(Record { id, data: w }));
*self.steps.write().unwrap() = 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 }));
*self.records.write().unwrap() = records
.into_iter()
.map(|r| (r.id, RecordState::Original(r)))
.collect::<HashMap<RecordId, RecordState<TraxRecord>>>();
}
}
/*
struct SavedRecordIterator<'a> {
read_lock: RwLockReadGuard<'a, HashMap<RecordId, RecordState<TraxRecord>>>,
iter: Box<dyn Iterator<Item = &'a Record<TraxRecord>> + 'a>,
}
impl<'a> SavedRecordIterator<'a> {
fn new(records: Arc<RwLock<HashMap<RecordId, RecordState<TraxRecord>>>>) -> Self {
let read_lock = records.read().unwrap();
let iter = read_lock
.iter()
.map(|(_, record_state)| record_state.data())
.filter_map(|r| r);
Self {
read_lock,
iter: Box::new(iter),
}
}
}
impl<'a> Iterator for SavedRecordIterator<'a> {
type Item = &'a Record<TraxRecord>;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
*/

View File

@ -25,7 +25,7 @@ use std::cell::RefCell;
#[derive(Default)]
pub struct DayDetailViewPrivate {
container: Singleton,
view_model: RefCell<DayDetailViewModel>,
view_model: RefCell<Option<DayDetailViewModel>>,
}
#[glib::object_subclass]
@ -47,7 +47,7 @@ glib::wrapper! {
impl DayDetailView {
pub fn new(view_model: DayDetailViewModel) -> Self {
let s: Self = Object::builder().build();
*s.imp().view_model.borrow_mut() = view_model;
*s.imp().view_model.borrow_mut() = Some(view_model);
s.append(&s.imp().container);
@ -57,18 +57,26 @@ impl DayDetailView {
}
fn view(&self) {
self.imp()
.container
.swap(&DayDetail::new(self.imp().view_model.borrow().clone(), {
let view_model = self.imp().view_model.borrow();
let view_model = view_model
.as_ref()
.expect("DayDetailView has not been initialized with a view_model")
.clone();
self.imp().container.swap(&DayDetail::new(view_model, {
let s = self.clone();
move || s.edit()
}));
}
fn edit(&self) {
self.imp()
.container
.swap(&DayEdit::new(self.imp().view_model.borrow().clone(), {
let view_model = self.imp().view_model.borrow();
let view_model = view_model
.as_ref()
.expect("DayDetailView has not been initialized with a view_model")
.clone();
self.imp().container.swap(&DayEdit::new(view_model, {
let s = self.clone();
move || s.view()
}));

View File

@ -17,12 +17,12 @@ You should have received a copy of the GNU General Public License along with Fit
use crate::{
app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel,
};
use chrono::NaiveDate;
use emseries::Record;
use ft_core::TraxRecord;
use glib::Object;
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
/// daily summaries, daily details, and will provide all functions the user may need for editing
@ -56,15 +56,16 @@ impl ObjectSubclass for HistoricalViewPrivate {
.single_click_activate(true)
.build(),
};
factory.connect_bind({
let app = s.app.clone();
move |_, list_item| {
let records = list_item
let date = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.item()
.and_downcast::<DayRecords>()
.expect("should be a DaySummary");
.and_downcast::<Date>()
.expect("should be a Date");
let summary = list_item
.downcast_ref::<gtk::ListItem>()
@ -74,11 +75,10 @@ impl ObjectSubclass for HistoricalViewPrivate {
.expect("should be a DaySummary");
if let Some(app) = app.borrow().clone() {
summary.set_data(DayDetailViewModel::new(
records.date(),
records.records(),
app.clone(),
));
glib::spawn_future_local(async move {
let view_model = DayDetailViewModel::new(date.date(), app.clone()).await;
summary.set_data(view_model);
});
}
}
});
@ -96,11 +96,7 @@ glib::wrapper! {
}
impl HistoricalView {
pub fn new<SelectFn>(
app: App,
records: Vec<Record<TraxRecord>>,
on_select_day: Rc<SelectFn>,
) -> Self
pub fn new<SelectFn>(app: App, interval: DayInterval, on_select_day: Rc<SelectFn>) -> Self
where
SelectFn: Fn(chrono::NaiveDate, Vec<Record<TraxRecord>>) + 'static,
{
@ -110,11 +106,8 @@ impl HistoricalView {
*s.imp().app.borrow_mut() = Some(app);
let grouped_records =
GroupedRecords::new((*s.imp().time_window.borrow()).clone()).with_data(records);
let mut model = gio::ListStore::new::<DayRecords>();
model.extend(grouped_records.items());
let mut model = gio::ListStore::new::<Date>();
model.extend(interval.days().map(Date::new));
s.imp()
.list_view
.set_model(Some(&gtk::NoSelection::new(Some(model))));
@ -122,12 +115,10 @@ impl HistoricalView {
s.imp().list_view.connect_activate({
let on_select_day = on_select_day.clone();
move |s, idx| {
// This gets triggered whenever the user clicks on an item on the list. What we
// actually want to do here is to open a modal dialog that shows all of the details of
// the day and which allows the user to edit items within that dialog.
// This gets triggered whenever the user clicks on an item on the list.
let item = s.model().unwrap().item(idx).unwrap();
let records = item.downcast_ref::<DayRecords>().unwrap();
on_select_day(records.date(), records.records());
let date = item.downcast_ref::<Date>().unwrap();
on_select_day(date.date(), vec![]);
}
});
@ -136,169 +127,36 @@ impl HistoricalView {
s
}
pub fn set_records(&self, records: Vec<Record<TraxRecord>>) {
println!("set_records: {:?}", records);
let grouped_records =
GroupedRecords::new((self.imp().time_window.borrow()).clone()).with_data(records);
let mut model = gio::ListStore::new::<DayRecords>();
model.extend(grouped_records.items());
self.imp()
.list_view
.set_model(Some(&gtk::NoSelection::new(Some(model))));
}
pub fn time_window(&self) -> DayInterval {
self.imp().time_window.borrow().clone()
}
}
#[derive(Default)]
pub struct DayRecordsPrivate {
pub struct DatePrivate {
date: RefCell<chrono::NaiveDate>,
records: RefCell<Vec<Record<TraxRecord>>>,
}
#[glib::object_subclass]
impl ObjectSubclass for DayRecordsPrivate {
const NAME: &'static str = "DayRecords";
type Type = DayRecords;
impl ObjectSubclass for DatePrivate {
const NAME: &'static str = "Date";
type Type = Date;
}
impl ObjectImpl for DayRecordsPrivate {}
impl ObjectImpl for DatePrivate {}
glib::wrapper! {
pub struct DayRecords(ObjectSubclass<DayRecordsPrivate>);
pub struct Date(ObjectSubclass<DatePrivate>);
}
impl DayRecords {
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>) -> Self {
impl Date {
pub fn new(date: chrono::NaiveDate) -> Self {
let s: Self = Object::builder().build();
*s.imp().date.borrow_mut() = date;
*s.imp().records.borrow_mut() = records;
s
}
pub fn date(&self) -> chrono::NaiveDate {
*self.imp().date.borrow()
}
pub fn records(&self) -> Vec<Record<TraxRecord>> {
self.imp().records.borrow().clone()
}
pub fn add_record(&self, record: Record<TraxRecord>) {
self.imp().records.borrow_mut().push(record);
}
}
// This isn't feeling quite right. DayRecords is a glib object, but I'm not sure that I want to
// really be passing that around. It seems not generic enough. I feel like this whole grouped
// records thing can be made more generic.
struct GroupedRecords {
interval: DayInterval,
data: HashMap<NaiveDate, DayRecords>,
}
impl GroupedRecords {
fn new(interval: DayInterval) -> Self {
let mut s = Self {
interval: interval.clone(),
data: HashMap::new(),
};
interval.days().for_each(|date| {
let _ = s.data.insert(date, DayRecords::new(date, vec![]));
});
s
}
fn with_data(mut self, records: Vec<Record<TraxRecord>>) -> Self {
records.into_iter().for_each(|record| {
self.data
.entry(record.date())
.and_modify(|entry: &mut DayRecords| (*entry).add_record(record.clone()))
.or_insert(DayRecords::new(record.date(), vec![record]));
});
self
}
fn items(&self) -> impl Iterator<Item = DayRecords> + '_ {
self.interval.days().map(|date| {
self.data
.get(&date)
.cloned()
.unwrap_or(DayRecords::new(date, vec![]))
})
}
}
#[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);
}
}

View File

@ -57,6 +57,17 @@ 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)]
@ -99,6 +110,18 @@ 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,
@ -118,6 +141,14 @@ impl TraxRecord {
pub fn is_steps(&self) -> bool {
matches!(self, TraxRecord::Steps(_))
}
pub fn is_bike_ride(&self) -> bool {
matches!(self, TraxRecord::BikeRide(_))
}
pub fn is_run(&self) -> bool {
matches!(self, TraxRecord::Run(_))
}
}
impl Recordable for TraxRecord {

View File

@ -32,6 +32,7 @@
pkgs.gst_all_1.gstreamer
pkgs.gtk4
pkgs.libadwaita
pkgs.librsvg
pkgs.nodejs
pkgs.openssl
pkgs.pipewire

12
icon-test/Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "icon-test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
adw = { version = "0.5", package = "libadwaita", features = [ "v1_4" ] }
gio = { version = "0.18" }
glib = { version = "0.18" }
gtk = { version = "0.7", package = "gtk4", features = [ "v4_10" ] }

38
icon-test/src/main.rs Normal file
View File

@ -0,0 +1,38 @@
use adw::prelude::*;
fn main() {
let adw_app = adw::Application::builder().build();
adw_app.connect_activate(move |adw_app| {
let window = gtk::ApplicationWindow::builder()
.application(adw_app)
.width_request(400)
.height_request(400)
.build();
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();
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.valign(gtk::Align::Start)
.build();
layout.append(&sunrise_button);
layout.append(&walking_button);
window.set_child(Some(&layout));
window.present();
});
let args: Vec<String> = std::env::args().collect();
ApplicationExtManual::run_with_args(&adw_app, &args);
}