Render and be able to edit bike rides (and sorta other time distance workouts) #169

Merged
savanni merged 13 commits from fitnesstrax/time-distance-workout into main 2024-02-09 00:05:26 +00:00
26 changed files with 652 additions and 396 deletions

11
Cargo.lock generated
View File

@ -945,6 +945,7 @@ dependencies = [
"dimensioned 0.8.0",
"emseries",
"ft-core",
"gdk4",
"gio",
"glib",
"glib-build-tools 0.18.0",
@ -1806,6 +1807,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),
};
@ -124,19 +124,16 @@ where
/// Put a new record into the database. A unique id will be assigned to the record and
/// returned.
pub fn put(&mut self, entry: T) -> Result<RecordId, EmseriesWriteError> {
let uuid = RecordId::default();
let record = Record {
id: uuid.clone(),
data: entry,
};
let id = RecordId::default();
let record = Record { id, data: entry };
self.update(record)?;
Ok(uuid)
Ok(id)
}
/// 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 +163,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 {
@ -166,6 +166,17 @@ impl<T: Clone + Recordable> Record<T> {
pub fn timestamp(&self) -> Timestamp {
self.data.timestamp()
}
pub fn map<Map, U>(self, map: Map) -> Record<U>
where
Map: Fn(T) -> U,
U: Clone + Recordable,
{
Record {
id: self.id,
data: map(self.data),
}
}
}
#[cfg(test)]
@ -190,7 +201,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

@ -16,6 +16,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

@ -95,6 +95,20 @@ impl App {
.await
.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]

View File

@ -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,
@ -135,14 +131,15 @@ impl AppWindow {
}
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();
Rc::new(move |date| {
move |date| {
let s = s.clone();
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);
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));
let page = &adw::NavigationPage::builder()
.title(date.format("%Y-%m-%d").to_string())
@ -150,8 +147,14 @@ impl AppWindow {
.build();
s.navigation.push(page);
});
})
}));
}
};
let view = View::Historical(HistoricalView::new(
self.app.clone(),
interval,
Rc::new(on_select_day),
));
self.swap_main(view);
}

View File

@ -17,13 +17,19 @@ 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_field, ActionGroup, Steps, WeightLabel},
types::WeightFormatter,
components::{
steps_editor, time_distance_summary, weight_field, ActionGroup, Steps, WeightLabel,
},
types::{DistanceFormatter, DurationFormatter, WeightFormatter},
view_models::DayDetailViewModel,
};
use emseries::{Record, RecordId};
use ft_core::{TimeDistanceActivity, 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,
@ -97,8 +103,15 @@ impl DaySummary {
label.set_label(&format!("{} steps", s));
}
row.append(&label);
self.append(&row);
let biking_summary = view_model.time_distance_summary(TimeDistanceActivity::BikeRide);
if let Some(label) = time_distance_summary(
DistanceFormatter::from(biking_summary.0),
DurationFormatter::from(biking_summary.1),
) {
self.append(&label);
}
}
}
@ -135,31 +148,6 @@ 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();
@ -171,51 +159,10 @@ 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.time_distance_records();
for emseries::Record { data, .. } in records {
s.append(&time_distance_detail(data));
}
s
}
@ -266,61 +213,189 @@ 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()
.primary_action("Save", {
let s = s.clone();
let view_model = view_model.clone();
move || {
view_model.save();
s.finish();
}
})
.secondary_action("Cancel", {
let s = s.clone();
let view_model = view_model.clone();
move || {
view_model.revert();
s.finish();
}
})
.build(),
);
let workout_buttons = workout_buttons(view_model.clone(), {
let s = s.clone();
move |workout| s.add_row(workout)
});
let top_row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.build();
top_row.append(
&weight_field(view_model.weight().map(WeightFormatter::from), {
let view_model = view_model.clone();
move |w| match w {
Some(w) => view_model.set_weight(*w),
None => eprintln!("have not implemented record delete"),
view_model
.records()
.into_iter()
.filter_map({
let s = s.clone();
move |record| match record.data {
TraxRecord::TimeDistance(workout) => Some(TimeDistanceEdit::new(workout, {
let s = s.clone();
move |data| {
s.update_workout(record.id, data);
}
})),
_ => None,
}
})
.widget(),
);
.for_each(|row| s.imp().workout_rows.borrow().append(&row));
top_row.append(
&steps_editor(view_model.steps(), {
let view_model = view_model.clone();
move |s| match s {
Some(s) => view_model.set_steps(s),
None => eprintln!("have not implemented record delete"),
}
})
.widget(),
);
s.append(&top_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) {
(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>) {
let workout_rows = self.imp().workout_rows.borrow();
#[allow(clippy::single_match)]
match workout.data {
TraxRecord::TimeDistance(r) => workout_rows.append(&TimeDistanceEdit::new(r, {
let s = self.clone();
move |data| {
println!("update workout callback on workout: {:?}", workout.id);
s.update_workout(workout.id, data)
}
})),
_ => {}
}
}
fn update_workout(&self, id: RecordId, data: ft_core::TimeDistance) {
if let Some(ref view_model) = *self.imp().view_model.borrow() {
let record = Record {
id,
data: TraxRecord::TimeDistance(data),
};
view_model.update_record(record);
}
}
}
fn control_buttons(s: &DayEdit, view_model: &DayDetailViewModel) -> ActionGroup {
ActionGroup::builder()
.primary_action("Save", {
let s = s.clone();
move || s.finish()
})
.secondary_action("Cancel", {
let s = s.clone();
let view_model = view_model.clone();
move || {
let s = s.clone();
let view_model = view_model.clone();
glib::spawn_future_local(async move {
view_model.revert().await;
s.finish();
});
}
})
.build()
}
fn weight_and_steps_row(view_model: &DayDetailViewModel) -> gtk::Box {
let row = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.build();
row.append(
&weight_field(view_model.weight().map(WeightFormatter::from), {
let view_model = view_model.clone();
move |w| match w {
Some(w) => view_model.set_weight(*w),
None => eprintln!("have not implemented record delete"),
}
})
.widget(),
);
row.append(
&steps_editor(view_model.steps(), {
let view_model = view_model.clone();
move |s| match s {
Some(s) => view_model.set_steps(s),
None => eprintln!("have not implemented record delete"),
}
})
.widget(),
);
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_time_distance(TimeDistanceActivity::Walking);
add_row(workout.map(TraxRecord::TimeDistance));
}
});
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_time_distance(TimeDistanceActivity::Running);
add_row(workout.map(TraxRecord::TimeDistance));
}
});
*/
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_time_distance(TimeDistanceActivity::BikeRide);
add_row(workout.map(TraxRecord::TimeDistance));
}
});
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,10 +28,10 @@ mod steps;
pub use steps::{steps_editor, Steps};
mod text_entry;
pub use text_entry::{weight_field, 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::WeightLabel;

View File

@ -50,7 +50,7 @@ where
"0",
value,
|v| format!("{}", v),
move |v| v.parse::<u32>().map_err(|_| ParseError),
|v| v.parse::<u32>().map_err(|_| ParseError),
on_update,
)
}

View File

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

View File

@ -17,94 +17,204 @@ 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 ft_core::TimeDistance;
use crate::{
components::{distance_field, duration_field, time_field},
types::{DistanceFormatter, DurationFormatter, FormatOption, TimeFormatter},
};
use dimensioned::si;
use ft_core::{TimeDistance, TimeDistanceActivity};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::cell::RefCell;
use std::{rc::Rc, cell::RefCell};
#[derive(Default)]
pub struct TimeDistanceViewPrivate {
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)))
}
pub fn time_distance_detail(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(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(record.datetime.format("%H:%M").to_string())
.build(),
);
first_row.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(format!("{:?}", record.activity))
.build(),
);
first_row.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(
record
.distance
.map(|dist| DistanceFormatter::from(dist).format(FormatOption::Abbreviated))
.unwrap_or("".to_owned()),
)
.build(),
);
first_row.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(
record
.duration
.map(|duration| {
DurationFormatter::from(duration).format(FormatOption::Abbreviated)
})
.unwrap_or("".to_owned()),
)
.build(),
);
layout.append(&first_row);
layout.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(
record
.comments
.map(|comments| comments.to_string())
.unwrap_or("".to_owned()),
)
.build(),
);
layout
}
type OnUpdate = Rc<RefCell<Box<dyn Fn(TimeDistance)>>>;
pub struct TimeDistanceEditPrivate {
#[allow(unused)]
record: RefCell<Option<TimeDistance>>,
workout: RefCell<ft_core::TimeDistance>,
on_update: OnUpdate,
}
impl Default for TimeDistanceEditPrivate {
fn default() -> Self {
Self {
workout: RefCell::new(TimeDistance {
datetime: chrono::Utc::now().into(),
activity: TimeDistanceActivity::BikeRide,
duration: None,
distance: None,
comments: None,
}),
on_update: Rc::new(RefCell::new(Box::new(|_| {}))),
}
}
}
#[glib::object_subclass]
impl ObjectSubclass for TimeDistanceViewPrivate {
const NAME: &'static str = "TimeDistanceView";
type Type = TimeDistanceView;
impl ObjectSubclass for TimeDistanceEditPrivate {
const NAME: &'static str = "TimeDistanceEdit";
type Type = TimeDistanceEdit;
type ParentType = gtk::Box;
}
impl ObjectImpl for TimeDistanceViewPrivate {}
impl WidgetImpl for TimeDistanceViewPrivate {}
impl BoxImpl for TimeDistanceViewPrivate {}
impl ObjectImpl for TimeDistanceEditPrivate {}
impl WidgetImpl for TimeDistanceEditPrivate {}
impl BoxImpl for TimeDistanceEditPrivate {}
glib::wrapper! {
pub struct TimeDistanceView(ObjectSubclass<TimeDistanceViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
pub struct TimeDistanceEdit(ObjectSubclass<TimeDistanceEditPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
}
impl TimeDistanceView {
pub fn new(record: TimeDistance) -> Self {
impl Default for TimeDistanceEdit {
fn default() -> Self {
let s: Self = Object::builder().build();
s.set_orientation(gtk::Orientation::Vertical);
s.set_hexpand(true);
let first_row = gtk::Box::builder().homogeneous(true).build();
first_row.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(record.datetime.format("%H:%M").to_string())
.build(),
);
/*
first_row.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(format!("{:?}", type_))
.build(),
);
*/
first_row.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(
record
.distance
.map(|dist| format!("{}", dist))
.unwrap_or("".to_owned()),
)
.build(),
);
first_row.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(
record
.duration
.map(|duration| format!("{}", duration))
.unwrap_or("".to_owned()),
)
.build(),
);
s.append(&first_row);
s.append(
&gtk::Label::builder()
.halign(gtk::Align::Start)
.label(
record
.comments
.map(|comments| comments.to_string())
.unwrap_or("".to_owned()),
)
.build(),
);
s.set_css_classes(&["time-distance-edit"]);
s
}
}
impl TimeDistanceEdit {
pub fn new<OnUpdate>(workout: TimeDistance, on_update: OnUpdate) -> Self
where
OnUpdate: Fn(TimeDistance) + 'static,
{
let s = Self::default();
*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())(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())(workout.clone());
}
}

View File

@ -14,9 +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::{
types::{FormatOption, WeightFormatter},
};
use crate::types::{FormatOption, WeightFormatter};
use gtk::prelude::*;
pub struct WeightLabel {

View File

@ -62,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

@ -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)]
mod test {
use super::*;

View File

@ -14,10 +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/>.
*/
#[allow(unused_imports)]
use crate::app::{ReadError, RecordProvider, WriteError};
#[allow(unused_imports)]
use chrono::NaiveDate;
use crate::app::{ReadError, RecordProvider};
use dimensioned::si;
use emseries::{Record, RecordId, Recordable};
use ft_core::{TimeDistance, TimeDistanceActivity, TraxRecord};
@ -27,25 +24,21 @@ use std::{
sync::{Arc, RwLock},
};
// These are actually a used imports. Clippy isn't detecting their use, probably because of complexity around the async trait macros.
#[allow(unused_imports)]
use crate::app::WriteError;
#[allow(unused_imports)]
use chrono::NaiveDate;
#[derive(Clone, Debug)]
enum RecordState<T: Clone + Recordable> {
Original(Record<T>),
New(T),
New(Record<T>),
Updated(Record<T>),
Deleted(Record<T>),
}
impl<T: Clone + emseries::Recordable> RecordState<T> {
#[allow(unused)]
fn id(&self) -> Option<&RecordId> {
match self {
RecordState::Original(ref r) => Some(&r.id),
RecordState::New(ref r) => None,
RecordState::Updated(ref r) => Some(&r.id),
RecordState::Deleted(ref r) => Some(&r.id),
}
}
fn exists(&self) -> bool {
match self {
RecordState::Original(_) => true,
@ -55,21 +48,22 @@ impl<T: Clone + emseries::Recordable> RecordState<T> {
}
}
#[allow(unused)]
fn data(&self) -> Option<&Record<T>> {
match self {
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 {
id: r.id.clone(),
data: value,
}),
RecordState::New(_) => RecordState::New(value),
RecordState::Updated(r) => RecordState::Updated(Record {
id: r.id.clone(),
data: value,
}),
RecordState::Deleted(r) => RecordState::Updated(Record {
id: r.id.clone(),
data: value,
}),
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 }),
};
}
@ -94,7 +88,7 @@ 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,
}
@ -105,7 +99,7 @@ impl<T: Clone + emseries::Recordable> std::ops::DerefMut for RecordState<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
RecordState::Original(ref mut r) => &mut r.data,
RecordState::New(ref mut r) => r,
RecordState::New(ref mut r) => &mut r.data,
RecordState::Updated(ref mut r) => &mut r.data,
RecordState::Deleted(ref mut r) => &mut r.data,
}
@ -126,51 +120,15 @@ impl DayDetailViewModel {
date: chrono::NaiveDate,
provider: impl RecordProvider + 'static,
) -> Result<Self, ReadError> {
let (weight_records, records): (Vec<Record<TraxRecord>>, Vec<Record<TraxRecord>>) =
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 {
let s = Self {
provider: Arc::new(provider),
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;
Ok(s)
}
pub fn weight(&self) -> Option<si::Kilogram<f64>> {
@ -184,9 +142,12 @@ impl DayDetailViewModel {
date: self.date,
weight: new_weight,
}),
None => RecordState::New(ft_core::Weight {
date: self.date,
weight: new_weight,
None => RecordState::New(Record {
id: RecordId::default(),
data: ft_core::Weight {
date: self.date,
weight: new_weight,
},
}),
};
*record = Some(new_record);
@ -203,9 +164,12 @@ impl DayDetailViewModel {
date: self.date,
count: new_count,
}),
None => RecordState::New(ft_core::Steps {
date: self.date,
count: new_count,
None => RecordState::New(Record {
id: RecordId::default(),
data: ft_core::Steps {
date: self.date,
count: new_count,
},
}),
};
*record = Some(new_record);
@ -224,24 +188,10 @@ impl DayDetailViewModel {
self.records
.write()
.unwrap()
.insert(id.clone(), RecordState::New(tr));
println!(
"records after new_time_distance: {:?}",
self.records.read().unwrap()
);
.insert(id, RecordState::New(Record { id, data: tr }));
Record { id, data: workout }
}
pub fn update_time_distance(&self, workout: Record<TimeDistance>) {
let id = workout.id.clone();
let data = workout.data.clone();
let mut record_set = self.records.write().unwrap();
record_set.entry(id).and_modify(|record_state| {
record_state.set_value(TraxRecord::TimeDistance(data));
});
}
pub fn time_distance_records(&self) -> Vec<Record<TimeDistance>> {
self.records
.read()
@ -250,7 +200,7 @@ impl DayDetailViewModel {
.filter(|(_, record)| record.exists())
.filter_map(|(id, record_state)| match **record_state {
TraxRecord::TimeDistance(ref workout) => Some(Record {
id: id.clone(),
id: *id,
data: workout.clone(),
}),
_ => None,
@ -279,11 +229,27 @@ impl DayDetailViewModel {
)
}
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>>>()
}
#[allow(unused)]
fn get_record(&self, id: &RecordId) -> Option<Record<TraxRecord>> {
let record_set = self.records.read().unwrap();
record_set.get(id).map(|record| Record {
id: id.clone(),
id: *id,
data: (**record).clone(),
})
}
@ -303,18 +269,19 @@ impl DayDetailViewModel {
}
pub fn save(&self) {
glib::spawn_future({
let s = self.clone();
async move { s.async_save().await }
});
let s = self.clone();
glib::spawn_future(async move { s.async_save().await });
}
pub async fn async_save(&self) {
println!("async_save");
let weight_record = self.weight.read().unwrap().clone();
match weight_record {
Some(RecordState::New(data)) => {
let _ = self.provider.put_record(TraxRecord::Weight(data)).await;
let _ = self
.provider
.put_record(TraxRecord::Weight(data.data))
.await;
}
Some(RecordState::Original(_)) => {}
Some(RecordState::Updated(weight)) => {
@ -333,7 +300,7 @@ impl DayDetailViewModel {
let steps_record = self.steps.read().unwrap().clone();
match steps_record {
Some(RecordState::New(data)) => {
let _ = self.provider.put_record(TraxRecord::Steps(data)).await;
let _ = self.provider.put_record(TraxRecord::Steps(data.data)).await;
}
Some(RecordState::Original(_)) => {}
Some(RecordState::Updated(steps)) => {
@ -361,7 +328,7 @@ impl DayDetailViewModel {
println!("saving record: {:?}", record);
match record {
RecordState::New(data) => {
let _ = self.provider.put_record(data).await;
let _ = self.provider.put_record(data.data).await;
}
RecordState::Original(_) => {}
RecordState::Updated(r) => {
@ -372,10 +339,41 @@ impl DayDetailViewModel {
}
}
}
self.populate_records().await;
}
pub fn revert(&self) {
unimplemented!();
pub async fn revert(&self) {
self.populate_records().await;
}
async fn populate_records(&self) {
let records = self.provider.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>>>();
}
}
@ -383,7 +381,7 @@ impl DayDetailViewModel {
mod test {
use super::*;
use async_trait::async_trait;
use chrono::{DateTime, FixedOffset, TimeZone};
use chrono::{DateTime, FixedOffset};
use dimensioned::si;
use emseries::Record;
@ -400,7 +398,7 @@ mod test {
fn new(records: Vec<Record<TraxRecord>>) -> Self {
let record_map = records
.into_iter()
.map(|r| (r.id.clone(), r))
.map(|r| (r.id, r))
.collect::<HashMap<RecordId, Record<TraxRecord>>>();
Self {
records: Arc::new(RwLock::new(record_map)),
@ -434,26 +432,23 @@ mod test {
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError> {
let id = RecordId::default();
let record = Record {
id: id.clone(),
id: id,
data: record,
};
self.put_records.write().unwrap().push(record.clone());
self.records.write().unwrap().insert(id.clone(), record);
self.records.write().unwrap().insert(id, record);
Ok(id)
}
async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), WriteError> {
println!("updated record: {:?}", record);
self.updated_records.write().unwrap().push(record.clone());
self.records
.write()
.unwrap()
.insert(record.id.clone(), record);
self.records.write().unwrap().insert(record.id, record);
Ok(())
}
async fn delete_record(&self, id: RecordId) -> Result<(), WriteError> {
self.deleted_records.write().unwrap().push(id.clone());
self.deleted_records.write().unwrap().push(id);
let _ = self.records.write().unwrap().remove(&id);
Ok(())
}
@ -577,11 +572,8 @@ mod test {
let mut record = view_model.new_time_distance(TimeDistanceActivity::BikeRide);
record.data.duration = Some(60. * si::S);
view_model.update_time_distance(record.clone());
let record = Record {
id: record.id,
data: TraxRecord::TimeDistance(record.data),
};
let record = record.map(TraxRecord::TimeDistance);
view_model.update_record(record.clone());
assert_eq!(view_model.get_record(&record.id), Some(record));
assert_eq!(
view_model.time_distance_summary(TimeDistanceActivity::BikeRide),
@ -603,10 +595,8 @@ mod test {
let (view_model, provider) = create_view_model().await;
let mut workout = view_model.time_distance_records().first().cloned().unwrap();
println!("found record: {:?}", workout);
workout.data.duration = Some(1800. * si::S);
view_model.update_time_distance(workout.clone());
view_model.update_record(workout.map(TraxRecord::TimeDistance));
assert_eq!(
view_model.time_distance_summary(TimeDistanceActivity::BikeRide),
@ -640,7 +630,7 @@ mod test {
#[tokio::test]
async fn it_can_delete_an_existing_record() {
let (view_model, provider) = create_view_model().await;
let mut workout = view_model.time_distance_records().first().cloned().unwrap();
let workout = view_model.time_distance_records().first().cloned().unwrap();
view_model.remove_record(workout.id);
assert_eq!(

View File

@ -18,8 +18,6 @@ use crate::{
app::App, components::DaySummary, types::DayInterval, view_models::DayDetailViewModel,
};
use glib::Object;
use gtk::{prelude::*, subclass::prelude::*};
use std::{cell::RefCell, rc::Rc};
@ -56,34 +54,32 @@ impl ObjectSubclass for HistoricalViewPrivate {
.single_click_activate(true)
.build(),
};
factory.connect_bind({
let app = s.app.clone();
move |_, list_item| {
let app = app.clone();
let list_item = list_item.clone();
glib::spawn_future_local(async move {
let date = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.item()
.and_downcast::<Date>()
.expect("should be a DaySummary");
let date = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.item()
.and_downcast::<Date>()
.expect("should be a Date");
let summary = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.child()
.and_downcast::<DaySummary>()
.expect("should be a DaySummary");
let summary = list_item
.downcast_ref::<gtk::ListItem>()
.expect("should be a ListItem")
.child()
.and_downcast::<DaySummary>()
.expect("should be a DaySummary");
if let Some(app) = app.borrow().clone() {
glib::spawn_future_local(async move {
let view_model =
DayDetailViewModel::new(date.date(), app).await.unwrap();
summary.set_data(view_model);
});
}
});
if let Some(app) = app.borrow().clone() {
glib::spawn_future_local(async move {
let view_model = DayDetailViewModel::new(date.date(), app.clone())
.await
.unwrap();
summary.set_data(view_model);
});
}
}
});
@ -119,9 +115,7 @@ 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 date = item.downcast_ref::<Date>().unwrap();
on_select_day(date.date());

View File

@ -134,18 +134,6 @@ impl TraxRecord {
})
)
}
/*
pub fn is_time_distance_type(&self, type_: TimeDistanceActivity) -> bool {
match type_ {
TimeDistanceWorkoutType::BikeRide => matches!(self, TraxRecord::BikeRide(_)),
TimeDistanceWorkoutType::Row => matches!(self, TraxRecord::Row(_)),
TimeDistanceWorkoutType::Run => matches!(self, TraxRecord::Run(_)),
TimeDistanceWorkoutType::Swim => matches!(self, TraxRecord::Swim(_)),
TimeDistanceWorkoutType::Walk => matches!(self, TraxRecord::Walk(_)),
}
}
*/
}
impl Recordable for TraxRecord {

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);
}