Compare commits
24 Commits
31d74588fb
...
ea867812bc
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | ea867812bc | |
Savanni D'Gerinel | cb55d6aae0 | |
Savanni D'Gerinel | cfc7df5e2f | |
Savanni D'Gerinel | 9fc9d2b758 | |
Savanni D'Gerinel | 76f4b31466 | |
Savanni D'Gerinel | 73052a0694 | |
Savanni D'Gerinel | 2c42c35dfe | |
Savanni D'Gerinel | afe693fe10 | |
Savanni D'Gerinel | af1422d523 | |
Savanni D'Gerinel | 792e20d44b | |
Savanni D'Gerinel | 8016188b29 | |
Savanni D'Gerinel | 74df2880bb | |
Savanni D'Gerinel | 96c4201680 | |
Savanni D'Gerinel | ecdd38ebbc | |
Savanni D'Gerinel | 2fb8728856 | |
Savanni D'Gerinel | a7d43ef184 | |
Savanni D'Gerinel | 9727d35116 | |
Savanni D'Gerinel | 1d6155d9e5 | |
Savanni D'Gerinel | a8bf540517 | |
Savanni D'Gerinel | 3db870d790 | |
Savanni D'Gerinel | 24276d172b | |
Savanni D'Gerinel | 96317f5692 | |
Savanni D'Gerinel | c1e797f3ae | |
Savanni D'Gerinel | 304008c674 |
|
@ -133,6 +133,17 @@ dependencies = [
|
|||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atoi"
|
||||
version = "2.0.0"
|
||||
|
@ -420,7 +431,7 @@ dependencies = [
|
|||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -736,7 +747,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -928,11 +939,13 @@ name = "fitnesstrax"
|
|||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"async-channel",
|
||||
"async-trait",
|
||||
"chrono",
|
||||
"chrono-tz",
|
||||
"dimensioned 0.8.0",
|
||||
"emseries",
|
||||
"ft-core",
|
||||
"gdk4",
|
||||
"gio",
|
||||
"glib",
|
||||
"glib-build-tools 0.18.0",
|
||||
|
@ -1132,7 +1145,7 @@ checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1367,7 +1380,7 @@ dependencies = [
|
|||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1794,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"
|
||||
|
@ -2443,7 +2466,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2648,7 +2671,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2792,9 +2815,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.70"
|
||||
version = "1.0.78"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
|
||||
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
@ -2836,9 +2859,9 @@ checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
|
|||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
version = "1.0.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
@ -3321,7 +3344,7 @@ checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3720,9 +3743,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.41"
|
||||
version = "2.0.48"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44c8b28c477cc3bf0e7966561e3460130e1255f7a1cf71931075f1c5e7a7e269"
|
||||
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -3818,7 +3841,7 @@ checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -3942,7 +3965,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -4063,7 +4086,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -4433,7 +4456,7 @@ dependencies = [
|
|||
"once_cell",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
|
@ -4467,7 +4490,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
@ -4708,7 +4731,7 @@ checksum = "b3c129550b3e6de3fd0ba67ba5c81818f9805e58b8d7fee80a3a59d2c9fc601a"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.41",
|
||||
"syn 2.0.48",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
70
Cargo.nix
70
Cargo.nix
|
@ -655,6 +655,32 @@ rec {
|
|||
};
|
||||
resolvedDefaultFeatures = [ "default" "std" ];
|
||||
};
|
||||
"async-trait" = rec {
|
||||
crateName = "async-trait";
|
||||
version = "0.1.77";
|
||||
edition = "2021";
|
||||
sha256 = "1adf1jh2yg39rkpmqjqyr9xyd6849p0d95425i6imgbhx0syx069";
|
||||
procMacro = true;
|
||||
authors = [
|
||||
"David Tolnay <dtolnay@gmail.com>"
|
||||
];
|
||||
dependencies = [
|
||||
{
|
||||
name = "proc-macro2";
|
||||
packageId = "proc-macro2";
|
||||
}
|
||||
{
|
||||
name = "quote";
|
||||
packageId = "quote";
|
||||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" "visit-mut" ];
|
||||
}
|
||||
];
|
||||
|
||||
};
|
||||
"atoi" = rec {
|
||||
crateName = "atoi";
|
||||
version = "2.0.0";
|
||||
|
@ -1487,7 +1513,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" ];
|
||||
}
|
||||
];
|
||||
|
@ -2394,7 +2420,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
}
|
||||
];
|
||||
features = {
|
||||
|
@ -2972,6 +2998,10 @@ rec {
|
|||
name = "async-channel";
|
||||
packageId = "async-channel";
|
||||
}
|
||||
{
|
||||
name = "async-trait";
|
||||
packageId = "async-trait";
|
||||
}
|
||||
{
|
||||
name = "chrono";
|
||||
packageId = "chrono";
|
||||
|
@ -3576,7 +3606,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" ];
|
||||
}
|
||||
];
|
||||
|
@ -4341,7 +4371,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" ];
|
||||
}
|
||||
];
|
||||
|
@ -7690,7 +7720,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" ];
|
||||
}
|
||||
];
|
||||
|
@ -8187,7 +8217,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" "visit-mut" ];
|
||||
}
|
||||
];
|
||||
|
@ -8529,9 +8559,9 @@ rec {
|
|||
};
|
||||
"proc-macro2" = rec {
|
||||
crateName = "proc-macro2";
|
||||
version = "1.0.70";
|
||||
version = "1.0.78";
|
||||
edition = "2021";
|
||||
sha256 = "0fzxg3dkrjy101vv5b6llc8mh74xz1vhhsaiwrn68kzvynxqy9rr";
|
||||
sha256 = "1bjak27pqdn4f4ih1c9nr3manzyavsgqmf76ygw9k76q8pb2lhp2";
|
||||
authors = [
|
||||
"David Tolnay <dtolnay@gmail.com>"
|
||||
"Alex Crichton <alex@alexcrichton.com>"
|
||||
|
@ -8665,9 +8695,9 @@ rec {
|
|||
};
|
||||
"quote" = rec {
|
||||
crateName = "quote";
|
||||
version = "1.0.33";
|
||||
version = "1.0.35";
|
||||
edition = "2018";
|
||||
sha256 = "1biw54hbbr12wdwjac55z1m2x2rylciw83qnjn564a3096jgqrsj";
|
||||
sha256 = "1vv8r2ncaz4pqdr78x7f138ka595sp2ncr1sa2plm4zxbsmwj7i9";
|
||||
authors = [
|
||||
"David Tolnay <dtolnay@gmail.com>"
|
||||
];
|
||||
|
@ -10269,7 +10299,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
}
|
||||
];
|
||||
features = {
|
||||
|
@ -11748,11 +11778,11 @@ rec {
|
|||
};
|
||||
resolvedDefaultFeatures = [ "clone-impls" "default" "derive" "extra-traits" "full" "parsing" "printing" "proc-macro" "quote" ];
|
||||
};
|
||||
"syn 2.0.41" = rec {
|
||||
"syn 2.0.48" = rec {
|
||||
crateName = "syn";
|
||||
version = "2.0.41";
|
||||
version = "2.0.48";
|
||||
edition = "2021";
|
||||
sha256 = "0sg2lzkwbwbm229p3kx1yxai43hkc0s1wmk6g47bzhvw8y6b5j24";
|
||||
sha256 = "0gqgfygmrxmp8q32lia9p294kdd501ybn6kn2h4gqza0irik2d8g";
|
||||
authors = [
|
||||
"David Tolnay <dtolnay@gmail.com>"
|
||||
];
|
||||
|
@ -11990,7 +12020,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -12406,7 +12436,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" ];
|
||||
}
|
||||
];
|
||||
|
@ -12820,7 +12850,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
usesDefaultFeatures = false;
|
||||
features = [ "full" "parsing" "printing" "visit-mut" "clone-impls" "extra-traits" "proc-macro" ];
|
||||
}
|
||||
|
@ -13814,7 +13844,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "full" ];
|
||||
}
|
||||
{
|
||||
|
@ -13904,7 +13934,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
features = [ "visit" "full" ];
|
||||
}
|
||||
{
|
||||
|
@ -15388,7 +15418,7 @@ rec {
|
|||
}
|
||||
{
|
||||
name = "syn";
|
||||
packageId = "syn 2.0.41";
|
||||
packageId = "syn 2.0.48";
|
||||
}
|
||||
];
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ members = [
|
|||
"geo-types",
|
||||
"gm-control-panel",
|
||||
"hex-grid",
|
||||
"icon-test",
|
||||
"ifc",
|
||||
"kifu/core",
|
||||
"kifu/gtk",
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -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()),
|
||||
|
|
|
@ -8,6 +8,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
adw = { version = "0.5", package = "libadwaita", features = [ "v1_4" ] }
|
||||
async-channel = { version = "2.1" }
|
||||
async-trait = { version = "0.1" }
|
||||
chrono = { version = "0.4" }
|
||||
chrono-tz = { version = "0.8" }
|
||||
dimensioned = { version = "0.8", features = [ "serde" ] }
|
||||
|
@ -15,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" ] }
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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 |
|
@ -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 |
|
@ -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 |
|
@ -14,6 +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 async_trait::async_trait;
|
||||
use chrono::NaiveDate;
|
||||
use emseries::{time_range, Record, RecordId, Series, Timestamp};
|
||||
use ft_core::TraxRecord;
|
||||
|
@ -34,6 +35,32 @@ pub enum AppError {
|
|||
Unhandled,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ReadError {
|
||||
#[error("no database loaded")]
|
||||
NoDatabase,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum WriteError {
|
||||
#[error("no database loaded")]
|
||||
NoDatabase,
|
||||
#[error("unhandled error")]
|
||||
Unhandled,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait RecordProvider: Send + Sync {
|
||||
async fn records(
|
||||
&self,
|
||||
start: NaiveDate,
|
||||
end: NaiveDate,
|
||||
) -> Result<Vec<Record<TraxRecord>>, ReadError>;
|
||||
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError>;
|
||||
async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), WriteError>;
|
||||
async fn delete_record(&self, id: RecordId) -> Result<(), WriteError>;
|
||||
}
|
||||
|
||||
/// The real, headless application. This is where all of the logic will reside.
|
||||
#[derive(Clone)]
|
||||
pub struct App {
|
||||
|
@ -57,11 +84,40 @@ impl App {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn records(
|
||||
pub async fn open_db(&self, path: PathBuf) -> Result<(), AppError> {
|
||||
let db_ref = self.database.clone();
|
||||
self.runtime
|
||||
.spawn_blocking(move || {
|
||||
let db = Series::open(path).map_err(|_| AppError::FailedToOpenDatabase)?;
|
||||
*db_ref.write().unwrap() = Some(db);
|
||||
Ok(())
|
||||
})
|
||||
.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]
|
||||
impl RecordProvider for App {
|
||||
async fn records(
|
||||
&self,
|
||||
start: NaiveDate,
|
||||
end: NaiveDate,
|
||||
) -> Result<Vec<Record<TraxRecord>>, AppError> {
|
||||
) -> Result<Vec<Record<TraxRecord>>, ReadError> {
|
||||
let db = self.database.clone();
|
||||
self.runtime
|
||||
.spawn_blocking(move || {
|
||||
|
@ -77,14 +133,14 @@ impl App {
|
|||
.collect::<Vec<Record<TraxRecord>>>();
|
||||
Ok(records)
|
||||
} else {
|
||||
Err(AppError::NoDatabase)
|
||||
Err(ReadError::NoDatabase)
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub async fn put_record(&self, record: TraxRecord) -> Result<RecordId, AppError> {
|
||||
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError> {
|
||||
let db = self.database.clone();
|
||||
self.runtime
|
||||
.spawn_blocking(move || {
|
||||
|
@ -97,10 +153,10 @@ impl App {
|
|||
})
|
||||
.await
|
||||
.unwrap()
|
||||
.map_err(|_| AppError::Unhandled)
|
||||
.map_err(|_| WriteError::Unhandled)
|
||||
}
|
||||
|
||||
pub async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), AppError> {
|
||||
async fn update_record(&self, record: Record<TraxRecord>) -> Result<(), WriteError> {
|
||||
let db = self.database.clone();
|
||||
self.runtime
|
||||
.spawn_blocking(move || {
|
||||
|
@ -112,18 +168,10 @@ impl App {
|
|||
})
|
||||
.await
|
||||
.unwrap()
|
||||
.map_err(|_| AppError::Unhandled)
|
||||
.map_err(|_| WriteError::Unhandled)
|
||||
}
|
||||
|
||||
pub async fn open_db(&self, path: PathBuf) -> Result<(), AppError> {
|
||||
let db_ref = self.database.clone();
|
||||
self.runtime
|
||||
.spawn_blocking(move || {
|
||||
let db = Series::open(path).map_err(|_| AppError::FailedToOpenDatabase)?;
|
||||
*db_ref.write().unwrap() = Some(db);
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
async fn delete_record(&self, _id: RecordId) -> Result<(), WriteError> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
@ -125,6 +121,7 @@ impl AppWindow {
|
|||
s
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn show_welcome_view(&self) {
|
||||
let view = View::Welcome(WelcomeView::new({
|
||||
let s = self.clone();
|
||||
|
@ -133,25 +130,31 @@ 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, interval: DayInterval) {
|
||||
let on_select_day = {
|
||||
let s = self.clone();
|
||||
Rc::new(move |date, records| {
|
||||
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(),
|
||||
)));
|
||||
let page = &adw::NavigationPage::builder()
|
||||
.title(date.format("%Y-%m-%d").to_string())
|
||||
.child(&layout)
|
||||
.build();
|
||||
s.navigation.push(page);
|
||||
})
|
||||
}));
|
||||
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());
|
||||
// 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())
|
||||
.child(&layout)
|
||||
.build();
|
||||
s.navigation.push(page);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let view = View::Historical(HistoricalView::new(
|
||||
self.app.clone(),
|
||||
interval,
|
||||
Rc::new(on_select_day),
|
||||
));
|
||||
self.swap_main(view);
|
||||
}
|
||||
|
||||
|
@ -161,10 +164,7 @@ impl AppWindow {
|
|||
async move {
|
||||
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),
|
||||
Err(_) => s.show_welcome_view(),
|
||||
}
|
||||
s.show_historical_view(DayInterval { start, end });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -181,6 +181,7 @@ impl AppWindow {
|
|||
self.layout.append(¤t_widget.widget());
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn on_apply_config(&self, path: PathBuf) {
|
||||
glib::spawn_future_local({
|
||||
let s = self.clone();
|
||||
|
|
|
@ -17,12 +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},
|
||||
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,
|
||||
|
@ -96,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,35 +148,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 = WeightLabel::new(view_model.weight());
|
||||
let weight_view = WeightLabel::new(view_model.weight().map(WeightFormatter::from));
|
||||
top_row.append(&weight_view.widget());
|
||||
|
||||
let steps_view = Steps::new(view_model.steps());
|
||||
|
@ -170,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
|
||||
}
|
||||
|
@ -222,12 +170,22 @@ impl DayDetail {
|
|||
|
||||
pub struct DayEditPrivate {
|
||||
on_finished: RefCell<Box<dyn Fn()>>,
|
||||
#[allow(unused)]
|
||||
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,60 +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(), {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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> {
|
||||
|
@ -106,6 +106,7 @@ impl<T: Clone + std::fmt::Debug + 'static> TextEntry<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn time_field<OnUpdate>(
|
||||
value: Option<TimeFormatter>,
|
||||
on_update: OnUpdate,
|
||||
|
@ -122,6 +123,7 @@ where
|
|||
)
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn distance_field<OnUpdate>(
|
||||
value: Option<DistanceFormatter>,
|
||||
on_update: OnUpdate,
|
||||
|
@ -138,6 +140,7 @@ where
|
|||
)
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn duration_field<OnUpdate>(
|
||||
value: Option<DurationFormatter>,
|
||||
on_update: OnUpdate,
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
@ -17,92 +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::{RecordType, 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(
|
||||
>k::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label(record.datetime.format("%H:%M").to_string())
|
||||
.build(),
|
||||
);
|
||||
|
||||
first_row.append(
|
||||
>k::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label(format!("{:?}", record.activity))
|
||||
.build(),
|
||||
);
|
||||
|
||||
first_row.append(
|
||||
>k::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(
|
||||
>k::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(
|
||||
>k::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(type_: RecordType, 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(
|
||||
>k::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label(record.datetime.format("%H:%M").to_string())
|
||||
.build(),
|
||||
);
|
||||
|
||||
first_row.append(
|
||||
>k::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label(format!("{:?}", type_))
|
||||
.build(),
|
||||
);
|
||||
|
||||
first_row.append(
|
||||
>k::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label(
|
||||
record
|
||||
.distance
|
||||
.map(|dist| format!("{}", dist))
|
||||
.unwrap_or("".to_owned()),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
|
||||
first_row.append(
|
||||
>k::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label(
|
||||
record
|
||||
.duration
|
||||
.map(|duration| format!("{}", duration))
|
||||
.unwrap_or("".to_owned()),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
|
||||
s.append(&first_row);
|
||||
|
||||
s.append(
|
||||
>k::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(>k::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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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/>.
|
||||
*/
|
||||
|
||||
use crate::{
|
||||
components::TextEntry,
|
||||
types::{FormatOption, WeightFormatter},
|
||||
};
|
||||
use crate::types::{FormatOption, WeightFormatter};
|
||||
use gtk::prelude::*;
|
||||
|
||||
pub struct WeightLabel {
|
||||
|
|
|
@ -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());
|
||||
});
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ impl Iterator for DayIterator {
|
|||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum FormatOption {
|
||||
Abbreviated,
|
||||
#[allow(unused)]
|
||||
Full,
|
||||
}
|
||||
|
||||
|
@ -60,6 +61,7 @@ pub enum FormatOption {
|
|||
pub struct TimeFormatter(chrono::NaiveTime);
|
||||
|
||||
impl TimeFormatter {
|
||||
#[allow(unused)]
|
||||
pub fn format(&self, option: FormatOption) -> String {
|
||||
match option {
|
||||
FormatOption::Abbreviated => self.0.format("%H:%M"),
|
||||
|
@ -68,6 +70,7 @@ impl TimeFormatter {
|
|||
.to_string()
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn parse(s: &str) -> Result<TimeFormatter, ParseError> {
|
||||
let parts = s
|
||||
.split(':')
|
||||
|
@ -104,6 +107,7 @@ impl From<chrono::NaiveTime> for TimeFormatter {
|
|||
pub struct WeightFormatter(si::Kilogram<f64>);
|
||||
|
||||
impl WeightFormatter {
|
||||
#[allow(unused)]
|
||||
pub fn format(&self, option: FormatOption) -> String {
|
||||
match option {
|
||||
FormatOption::Abbreviated => format!("{} kg", self.0.value_unsafe),
|
||||
|
@ -111,6 +115,7 @@ impl WeightFormatter {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn parse(s: &str) -> Result<WeightFormatter, ParseError> {
|
||||
s.parse::<f64>()
|
||||
.map(|w| WeightFormatter(w * si::KG))
|
||||
|
@ -149,6 +154,7 @@ impl From<si::Kilogram<f64>> for WeightFormatter {
|
|||
pub struct DistanceFormatter(si::Meter<f64>);
|
||||
|
||||
impl DistanceFormatter {
|
||||
#[allow(unused)]
|
||||
pub fn format(&self, option: FormatOption) -> String {
|
||||
match option {
|
||||
FormatOption::Abbreviated => format!("{} km", self.0.value_unsafe / 1000.),
|
||||
|
@ -156,6 +162,7 @@ impl DistanceFormatter {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn parse(s: &str) -> Result<DistanceFormatter, ParseError> {
|
||||
let value = s.parse::<f64>().map_err(|_| ParseError)?;
|
||||
Ok(DistanceFormatter(value * 1000. * si::M))
|
||||
|
@ -193,6 +200,7 @@ impl From<si::Meter<f64>> for DistanceFormatter {
|
|||
pub struct DurationFormatter(si::Second<f64>);
|
||||
|
||||
impl DurationFormatter {
|
||||
#[allow(unused)]
|
||||
pub fn format(&self, option: FormatOption) -> String {
|
||||
let (hours, minutes) = self.hours_and_minutes();
|
||||
let (h, m) = match option {
|
||||
|
@ -206,11 +214,13 @@ impl DurationFormatter {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn parse(s: &str) -> Result<DurationFormatter, ParseError> {
|
||||
let value = s.parse::<f64>().map_err(|_| ParseError)?;
|
||||
Ok(DurationFormatter(value * 60. * si::S))
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn hours_and_minutes(&self) -> (i64, i64) {
|
||||
let minutes: i64 = (self.0.value_unsafe / 60.).round() as i64;
|
||||
let hours: i64 = minutes / 60;
|
||||
|
@ -246,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::*;
|
||||
|
|
|
@ -14,44 +14,64 @@ 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, types::WeightFormatter};
|
||||
use crate::app::{ReadError, RecordProvider};
|
||||
use dimensioned::si;
|
||||
use emseries::{Record, RecordId, Recordable};
|
||||
use ft_core::TraxRecord;
|
||||
use ft_core::{TimeDistance, TimeDistanceActivity, TraxRecord};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
ops::Deref,
|
||||
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>),
|
||||
#[allow(unused)]
|
||||
Deleted(Record<T>),
|
||||
}
|
||||
|
||||
impl<T: Clone + emseries::Recordable> RecordState<T> {
|
||||
#[allow(unused)]
|
||||
fn id(&self) -> Option<&RecordId> {
|
||||
fn exists(&self) -> bool {
|
||||
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),
|
||||
RecordState::Original(_) => true,
|
||||
RecordState::New(_) => true,
|
||||
RecordState::Updated(_) => true,
|
||||
RecordState::Deleted(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_value(self, value: T) -> RecordState<T> {
|
||||
#[allow(unused)]
|
||||
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 {
|
||||
|
@ -68,19 +88,27 @@ 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 {}
|
||||
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) => &mut r.data,
|
||||
RecordState::Updated(ref mut r) => &mut r.data,
|
||||
RecordState::Deleted(ref mut r) => &mut r.data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
#[derive(Clone)]
|
||||
pub struct DayDetailViewModel {
|
||||
app: Option<App>,
|
||||
provider: Arc<dyn RecordProvider>,
|
||||
pub date: chrono::NaiveDate,
|
||||
weight: Arc<RwLock<Option<RecordState<ft_core::Weight>>>>,
|
||||
steps: Arc<RwLock<Option<RecordState<ft_core::Steps>>>>,
|
||||
|
@ -88,58 +116,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,
|
||||
provider: impl RecordProvider + 'static,
|
||||
) -> Result<Self, ReadError> {
|
||||
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<WeightFormatter> {
|
||||
(*self.weight.read().unwrap())
|
||||
.as_ref()
|
||||
.map(|w| WeightFormatter::from(w.weight))
|
||||
pub fn weight(&self) -> Option<si::Kilogram<f64>> {
|
||||
(*self.weight.read().unwrap()).as_ref().map(|w| w.weight)
|
||||
}
|
||||
|
||||
pub fn set_weight(&self, new_weight: WeightFormatter) {
|
||||
pub fn set_weight(&self, new_weight: si::Kilogram<f64>) {
|
||||
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 {
|
||||
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);
|
||||
|
@ -156,81 +164,482 @@ 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);
|
||||
}
|
||||
|
||||
pub fn new_time_distance(&self, activity: TimeDistanceActivity) -> Record<TimeDistance> {
|
||||
let id = RecordId::default();
|
||||
let workout = TimeDistance {
|
||||
datetime: chrono::Local::now().into(),
|
||||
activity,
|
||||
distance: None,
|
||||
duration: None,
|
||||
comments: None,
|
||||
};
|
||||
let tr = TraxRecord::from(workout.clone());
|
||||
self.records
|
||||
.write()
|
||||
.unwrap()
|
||||
.insert(id, RecordState::New(Record { id, data: tr }));
|
||||
Record { id, data: workout }
|
||||
}
|
||||
|
||||
pub fn time_distance_records(&self) -> Vec<Record<TimeDistance>> {
|
||||
self.records
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|(_, record)| record.exists())
|
||||
.filter_map(|(id, record_state)| match **record_state {
|
||||
TraxRecord::TimeDistance(ref workout) => Some(Record {
|
||||
id: *id,
|
||||
data: workout.clone(),
|
||||
}),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn time_distance_summary(
|
||||
&self,
|
||||
activity: TimeDistanceActivity,
|
||||
) -> (si::Meter<f64>, si::Second<f64>) {
|
||||
self.time_distance_records()
|
||||
.into_iter()
|
||||
.filter(|rec| rec.data.activity == activity)
|
||||
.fold(
|
||||
(0. * si::M, 0. * si::S),
|
||||
|(distance, duration), workout| match (workout.data.distance, workout.data.duration)
|
||||
{
|
||||
(Some(distance_), Some(duration_)) => {
|
||||
(distance + distance_, duration + duration_)
|
||||
}
|
||||
(Some(distance_), None) => (distance + distance_, duration),
|
||||
(None, Some(duration_)) => (distance, duration + duration_),
|
||||
(None, None) => (distance, duration),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
data: (**record).clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn remove_record(&self, id: RecordId) {
|
||||
let mut record_set = self.records.write().unwrap();
|
||||
let updated_record = match record_set.remove(&id) {
|
||||
Some(RecordState::Original(r)) => Some(RecordState::Deleted(r)),
|
||||
Some(RecordState::New(_)) => None,
|
||||
Some(RecordState::Updated(r)) => Some(RecordState::Deleted(r)),
|
||||
Some(RecordState::Deleted(r)) => Some(RecordState::Deleted(r)),
|
||||
None => None,
|
||||
};
|
||||
if let Some(updated_record) = updated_record {
|
||||
record_set.insert(id, updated_record);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
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::Original(_)) => {}
|
||||
Some(RecordState::Updated(weight)) => {
|
||||
let _ = app
|
||||
.update_record(Record {
|
||||
id: weight.id,
|
||||
data: TraxRecord::Weight(weight.data),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
Some(RecordState::Deleted(_)) => {}
|
||||
None => {}
|
||||
}
|
||||
let s = self.clone();
|
||||
glib::spawn_future(async move { s.async_save().await });
|
||||
}
|
||||
|
||||
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::Original(_)) => {}
|
||||
Some(RecordState::Updated(steps)) => {
|
||||
let _ = app
|
||||
.update_record(Record {
|
||||
id: steps.id,
|
||||
data: TraxRecord::Steps(steps.data),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
Some(RecordState::Deleted(_)) => {}
|
||||
None => {}
|
||||
}
|
||||
pub async fn async_save(&self) {
|
||||
let weight_record = self.weight.read().unwrap().clone();
|
||||
match weight_record {
|
||||
Some(RecordState::New(data)) => {
|
||||
let _ = self
|
||||
.provider
|
||||
.put_record(TraxRecord::Weight(data.data))
|
||||
.await;
|
||||
}
|
||||
Some(RecordState::Original(_)) => {}
|
||||
Some(RecordState::Updated(weight)) => {
|
||||
let _ = self
|
||||
.provider
|
||||
.update_record(Record {
|
||||
id: weight.id,
|
||||
data: TraxRecord::Weight(weight.data),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
Some(RecordState::Deleted(_)) => {}
|
||||
None => {}
|
||||
}
|
||||
|
||||
let records = s
|
||||
.records
|
||||
.write()
|
||||
.unwrap()
|
||||
.drain()
|
||||
.map(|(_, record)| record)
|
||||
.collect::<Vec<RecordState<TraxRecord>>>();
|
||||
let steps_record = self.steps.read().unwrap().clone();
|
||||
match steps_record {
|
||||
Some(RecordState::New(data)) => {
|
||||
let _ = self.provider.put_record(TraxRecord::Steps(data.data)).await;
|
||||
}
|
||||
Some(RecordState::Original(_)) => {}
|
||||
Some(RecordState::Updated(steps)) => {
|
||||
let _ = self
|
||||
.provider
|
||||
.update_record(Record {
|
||||
id: steps.id,
|
||||
data: TraxRecord::Steps(steps.data),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
Some(RecordState::Deleted(_)) => {}
|
||||
None => {}
|
||||
}
|
||||
|
||||
for record in records {
|
||||
match record {
|
||||
RecordState::New(data) => {
|
||||
let _ = app.put_record(data).await;
|
||||
}
|
||||
RecordState::Original(_) => {}
|
||||
RecordState::Updated(r) => {
|
||||
let _ = app.update_record(r.clone()).await;
|
||||
}
|
||||
RecordState::Deleted(_) => unimplemented!(),
|
||||
}
|
||||
}
|
||||
let records = self
|
||||
.records
|
||||
.write()
|
||||
.unwrap()
|
||||
.drain()
|
||||
.map(|(_, record)| record)
|
||||
.collect::<Vec<RecordState<TraxRecord>>>();
|
||||
|
||||
for record in records {
|
||||
println!("saving record: {:?}", record);
|
||||
match record {
|
||||
RecordState::New(data) => {
|
||||
let _ = self.provider.put_record(data.data).await;
|
||||
}
|
||||
RecordState::Original(_) => {}
|
||||
RecordState::Updated(r) => {
|
||||
let _ = self.provider.update_record(r.clone()).await;
|
||||
}
|
||||
RecordState::Deleted(r) => {
|
||||
let _ = self.provider.delete_record(r.id).await;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
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>>>();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, FixedOffset};
|
||||
use dimensioned::si;
|
||||
use emseries::Record;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct MockProvider {
|
||||
records: Arc<RwLock<HashMap<RecordId, Record<TraxRecord>>>>,
|
||||
|
||||
put_records: Arc<RwLock<Vec<Record<TraxRecord>>>>,
|
||||
updated_records: Arc<RwLock<Vec<Record<TraxRecord>>>>,
|
||||
deleted_records: Arc<RwLock<Vec<RecordId>>>,
|
||||
}
|
||||
|
||||
impl MockProvider {
|
||||
fn new(records: Vec<Record<TraxRecord>>) -> Self {
|
||||
let record_map = records
|
||||
.into_iter()
|
||||
.map(|r| (r.id, r))
|
||||
.collect::<HashMap<RecordId, Record<TraxRecord>>>();
|
||||
Self {
|
||||
records: Arc::new(RwLock::new(record_map)),
|
||||
put_records: Arc::new(RwLock::new(vec![])),
|
||||
updated_records: Arc::new(RwLock::new(vec![])),
|
||||
deleted_records: Arc::new(RwLock::new(vec![])),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl RecordProvider for MockProvider {
|
||||
async fn records(
|
||||
&self,
|
||||
start: NaiveDate,
|
||||
end: NaiveDate,
|
||||
) -> Result<Vec<Record<TraxRecord>>, ReadError> {
|
||||
let start = emseries::Timestamp::Date(start);
|
||||
let end = emseries::Timestamp::Date(end);
|
||||
Ok(self
|
||||
.records
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|(_, r)| r)
|
||||
.filter(|r| r.timestamp() >= start && r.timestamp() <= end)
|
||||
.cloned()
|
||||
.collect::<Vec<Record<TraxRecord>>>())
|
||||
}
|
||||
|
||||
async fn put_record(&self, record: TraxRecord) -> Result<RecordId, WriteError> {
|
||||
let id = RecordId::default();
|
||||
let record = Record {
|
||||
id: id,
|
||||
data: record,
|
||||
};
|
||||
self.put_records.write().unwrap().push(record.clone());
|
||||
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, record);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_record(&self, id: RecordId) -> Result<(), WriteError> {
|
||||
self.deleted_records.write().unwrap().push(id);
|
||||
let _ = self.records.write().unwrap().remove(&id);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_empty_view_model() -> (DayDetailViewModel, MockProvider) {
|
||||
let provider = MockProvider::new(vec![]);
|
||||
|
||||
let oct_13 = chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap();
|
||||
let model = DayDetailViewModel::new(oct_13, provider.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
(model, provider)
|
||||
}
|
||||
|
||||
async fn create_view_model() -> (DayDetailViewModel, MockProvider) {
|
||||
let oct_12 = chrono::NaiveDate::from_ymd_opt(2023, 10, 12).unwrap();
|
||||
let oct_13 = chrono::NaiveDate::from_ymd_opt(2023, 10, 13).unwrap();
|
||||
let oct_13_am: DateTime<FixedOffset> = oct_13
|
||||
.clone()
|
||||
.and_hms_opt(3, 28, 0)
|
||||
.unwrap()
|
||||
.and_utc()
|
||||
.with_timezone(&FixedOffset::east_opt(5 * 3600).unwrap());
|
||||
let provider = MockProvider::new(vec![
|
||||
Record {
|
||||
id: RecordId::default(),
|
||||
data: TraxRecord::Weight(ft_core::Weight {
|
||||
date: oct_12,
|
||||
weight: 93. * si::KG,
|
||||
}),
|
||||
},
|
||||
Record {
|
||||
id: RecordId::default(),
|
||||
data: TraxRecord::Weight(ft_core::Weight {
|
||||
date: oct_13,
|
||||
weight: 95. * si::KG,
|
||||
}),
|
||||
},
|
||||
Record {
|
||||
id: RecordId::default(),
|
||||
data: TraxRecord::Steps(ft_core::Steps {
|
||||
date: oct_13,
|
||||
count: 2500,
|
||||
}),
|
||||
},
|
||||
Record {
|
||||
id: RecordId::default(),
|
||||
data: TraxRecord::TimeDistance(ft_core::TimeDistance {
|
||||
datetime: oct_13_am.clone(),
|
||||
activity: TimeDistanceActivity::BikeRide,
|
||||
distance: Some(15000. * si::M),
|
||||
duration: Some(3600. * si::S),
|
||||
comments: Some("somecomments present".to_owned()),
|
||||
}),
|
||||
},
|
||||
]);
|
||||
let model = DayDetailViewModel::new(oct_13, provider.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
(model, provider)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_honors_only_the_first_weight_and_step_record() {
|
||||
let (view_model, _provider) = create_view_model().await;
|
||||
assert_eq!(view_model.weight(), Some(95. * si::KG));
|
||||
assert_eq!(view_model.steps(), Some(2500));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_create_a_weight_and_stepcount() {
|
||||
let (view_model, provider) = create_empty_view_model().await;
|
||||
assert_eq!(view_model.weight(), None);
|
||||
assert_eq!(view_model.steps(), None);
|
||||
|
||||
view_model.set_weight(95. * si::KG);
|
||||
view_model.set_steps(250);
|
||||
|
||||
assert_eq!(view_model.weight(), Some(95. * si::KG));
|
||||
assert_eq!(view_model.steps(), Some(250));
|
||||
|
||||
view_model.set_weight(93. * si::KG);
|
||||
view_model.set_steps(255);
|
||||
|
||||
assert_eq!(view_model.weight(), Some(93. * si::KG));
|
||||
assert_eq!(view_model.steps(), Some(255));
|
||||
|
||||
view_model.async_save().await;
|
||||
|
||||
println!("provider: {:?}", provider);
|
||||
assert_eq!(provider.put_records.read().unwrap().len(), 2);
|
||||
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_construct_new_records() {
|
||||
let (view_model, provider) = create_empty_view_model().await;
|
||||
assert_eq!(
|
||||
view_model.time_distance_summary(TimeDistanceActivity::BikeRide),
|
||||
(0. * si::M, 0. * si::S)
|
||||
);
|
||||
|
||||
let mut record = view_model.new_time_distance(TimeDistanceActivity::BikeRide);
|
||||
record.data.duration = Some(60. * si::S);
|
||||
view_model.async_save().await;
|
||||
|
||||
assert_eq!(provider.put_records.read().unwrap().len(), 1);
|
||||
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_update_a_new_record_before_saving() {
|
||||
let (view_model, provider) = create_empty_view_model().await;
|
||||
assert_eq!(
|
||||
view_model.time_distance_summary(TimeDistanceActivity::BikeRide),
|
||||
(0. * si::M, 0. * si::S)
|
||||
);
|
||||
|
||||
let mut record = view_model.new_time_distance(TimeDistanceActivity::BikeRide);
|
||||
record.data.duration = Some(60. * si::S);
|
||||
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),
|
||||
(0. * si::M, 60. * si::S)
|
||||
);
|
||||
assert_eq!(
|
||||
view_model.time_distance_summary(TimeDistanceActivity::Running),
|
||||
(0. * si::M, 0. * si::S)
|
||||
);
|
||||
view_model.async_save().await;
|
||||
|
||||
assert_eq!(provider.put_records.read().unwrap().len(), 1);
|
||||
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_update_an_existing_record() {
|
||||
let (view_model, provider) = create_view_model().await;
|
||||
let mut workout = view_model.time_distance_records().first().cloned().unwrap();
|
||||
|
||||
workout.data.duration = Some(1800. * si::S);
|
||||
view_model.update_record(workout.map(TraxRecord::TimeDistance));
|
||||
|
||||
assert_eq!(
|
||||
view_model.time_distance_summary(TimeDistanceActivity::BikeRide),
|
||||
(15000. * si::M, 1800. * si::S)
|
||||
);
|
||||
|
||||
view_model.async_save().await;
|
||||
|
||||
assert_eq!(provider.put_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.updated_records.read().unwrap().len(), 1);
|
||||
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_remove_a_new_record() {
|
||||
let (view_model, provider) = create_empty_view_model().await;
|
||||
assert_eq!(
|
||||
view_model.time_distance_summary(TimeDistanceActivity::BikeRide),
|
||||
(0. * si::M, 0. * si::S)
|
||||
);
|
||||
|
||||
let record = view_model.new_time_distance(TimeDistanceActivity::BikeRide);
|
||||
view_model.remove_record(record.id);
|
||||
view_model.save();
|
||||
|
||||
assert_eq!(provider.put_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.deleted_records.read().unwrap().len(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_delete_an_existing_record() {
|
||||
let (view_model, provider) = create_view_model().await;
|
||||
let workout = view_model.time_distance_records().first().cloned().unwrap();
|
||||
|
||||
view_model.remove_record(workout.id);
|
||||
assert_eq!(
|
||||
view_model.time_distance_summary(TimeDistanceActivity::BikeRide),
|
||||
(0. * si::M, 0. * si::S)
|
||||
);
|
||||
view_model.async_save().await;
|
||||
|
||||
assert_eq!(provider.put_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.updated_records.read().unwrap().len(), 0);
|
||||
assert_eq!(provider.deleted_records.read().unwrap().len(), 1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,20 +57,28 @@ impl DayDetailView {
|
|||
}
|
||||
|
||||
fn view(&self) {
|
||||
self.imp()
|
||||
.container
|
||||
.swap(&DayDetail::new(self.imp().view_model.borrow().clone(), {
|
||||
let s = self.clone();
|
||||
move || s.edit()
|
||||
}));
|
||||
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 s = self.clone();
|
||||
move || s.view()
|
||||
}));
|
||||
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()
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,12 +17,10 @@ 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 +54,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 +73,12 @@ 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
|
||||
.unwrap();
|
||||
summary.set_data(view_model);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -96,13 +96,9 @@ 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,
|
||||
SelectFn: Fn(chrono::NaiveDate) + 'static,
|
||||
{
|
||||
let s: Self = Object::builder().build();
|
||||
s.set_orientation(gtk::Orientation::Vertical);
|
||||
|
@ -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(>k::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());
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -136,12 +127,9 @@ 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());
|
||||
pub fn set_interval(&self, interval: DayInterval) {
|
||||
let mut model = gio::ListStore::new::<Date>();
|
||||
model.extend(interval.days().map(Date::new));
|
||||
self.imp()
|
||||
.list_view
|
||||
.set_model(Some(>k::NoSelection::new(Some(model))));
|
||||
|
@ -153,152 +141,30 @@ impl HistoricalView {
|
|||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@ pub use welcome_view::WelcomeView;
|
|||
|
||||
pub enum View {
|
||||
Placeholder(PlaceholderView),
|
||||
#[allow(unused)]
|
||||
Welcome(WelcomeView),
|
||||
Historical(HistoricalView),
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
mod legacy;
|
||||
|
||||
mod types;
|
||||
pub use types::{RecordType, Steps, TimeDistance, TraxRecord, Weight};
|
||||
pub use types::{Steps, TimeDistance, TimeDistanceActivity, TraxRecord, Weight};
|
||||
|
|
|
@ -33,6 +33,15 @@ impl Recordable for Steps {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum TimeDistanceActivity {
|
||||
BikeRide,
|
||||
Running,
|
||||
Rowing,
|
||||
Swimming,
|
||||
Walking,
|
||||
}
|
||||
|
||||
/// TimeDistance represents workouts characterized by a duration and a distance travelled. These
|
||||
/// sorts of workouts can occur many times a day, depending on how one records things. I might
|
||||
/// record a single 30-km workout if I go on a long-distanec ride. Or I might record multiple 5km
|
||||
|
@ -48,6 +57,8 @@ pub struct TimeDistance {
|
|||
/// in the database, but we can still get a Naive Date from the DateTime, which will still read
|
||||
/// as the original day.
|
||||
pub datetime: DateTime<FixedOffset>,
|
||||
/// The activity
|
||||
pub activity: TimeDistanceActivity,
|
||||
/// The distance travelled. This is optional because such a workout makes sense even without
|
||||
/// the distance.
|
||||
pub distance: Option<si::Meter<f64>>,
|
||||
|
@ -57,6 +68,16 @@ pub struct TimeDistance {
|
|||
pub comments: Option<String>,
|
||||
}
|
||||
|
||||
impl Recordable for TimeDistance {
|
||||
fn timestamp(&self) -> Timestamp {
|
||||
Timestamp::DateTime(self.datetime)
|
||||
}
|
||||
|
||||
fn tags(&self) -> Vec<String> {
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)]
|
||||
|
@ -75,42 +96,15 @@ impl Recordable for Weight {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum RecordType {
|
||||
BikeRide,
|
||||
Row,
|
||||
Run,
|
||||
Steps,
|
||||
Swim,
|
||||
Walk,
|
||||
Weight,
|
||||
}
|
||||
|
||||
/// The unified data structure for all records that are part of the app.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub enum TraxRecord {
|
||||
BikeRide(TimeDistance),
|
||||
Row(TimeDistance),
|
||||
Run(TimeDistance),
|
||||
TimeDistance(TimeDistance),
|
||||
Steps(Steps),
|
||||
Swim(TimeDistance),
|
||||
Walk(TimeDistance),
|
||||
Weight(Weight),
|
||||
}
|
||||
|
||||
impl TraxRecord {
|
||||
pub fn workout_type(&self) -> RecordType {
|
||||
match self {
|
||||
TraxRecord::BikeRide(_) => RecordType::BikeRide,
|
||||
TraxRecord::Row(_) => RecordType::Row,
|
||||
TraxRecord::Run(_) => RecordType::Run,
|
||||
TraxRecord::Steps(_) => RecordType::Steps,
|
||||
TraxRecord::Swim(_) => RecordType::Swim,
|
||||
TraxRecord::Walk(_) => RecordType::Walk,
|
||||
TraxRecord::Weight(_) => RecordType::Weight,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_weight(&self) -> bool {
|
||||
matches!(self, TraxRecord::Weight(_))
|
||||
}
|
||||
|
@ -118,17 +112,35 @@ impl TraxRecord {
|
|||
pub fn is_steps(&self) -> bool {
|
||||
matches!(self, TraxRecord::Steps(_))
|
||||
}
|
||||
|
||||
pub fn is_time_distance(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
TraxRecord::TimeDistance(TimeDistance {
|
||||
activity: TimeDistanceActivity::BikeRide,
|
||||
..
|
||||
}) | TraxRecord::TimeDistance(TimeDistance {
|
||||
activity: TimeDistanceActivity::Running,
|
||||
..
|
||||
}) | TraxRecord::TimeDistance(TimeDistance {
|
||||
activity: TimeDistanceActivity::Rowing,
|
||||
..
|
||||
}) | TraxRecord::TimeDistance(TimeDistance {
|
||||
activity: TimeDistanceActivity::Swimming,
|
||||
..
|
||||
}) | TraxRecord::TimeDistance(TimeDistance {
|
||||
activity: TimeDistanceActivity::Walking,
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Recordable for TraxRecord {
|
||||
fn timestamp(&self) -> Timestamp {
|
||||
match self {
|
||||
TraxRecord::BikeRide(rec) => Timestamp::DateTime(rec.datetime),
|
||||
TraxRecord::Row(rec) => Timestamp::DateTime(rec.datetime),
|
||||
TraxRecord::Run(rec) => Timestamp::DateTime(rec.datetime),
|
||||
TraxRecord::TimeDistance(rec) => Timestamp::DateTime(rec.datetime),
|
||||
TraxRecord::Steps(rec) => rec.timestamp(),
|
||||
TraxRecord::Swim(rec) => Timestamp::DateTime(rec.datetime),
|
||||
TraxRecord::Walk(rec) => Timestamp::DateTime(rec.datetime),
|
||||
TraxRecord::Weight(rec) => rec.timestamp(),
|
||||
}
|
||||
}
|
||||
|
@ -138,6 +150,12 @@ impl Recordable for TraxRecord {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<TimeDistance> for TraxRecord {
|
||||
fn from(td: TimeDistance) -> Self {
|
||||
Self::TimeDistance(td)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
pkgs.gst_all_1.gstreamer
|
||||
pkgs.gtk4
|
||||
pkgs.libadwaita
|
||||
pkgs.librsvg
|
||||
pkgs.nodejs
|
||||
pkgs.openssl
|
||||
pkgs.pipewire
|
||||
|
|
|
@ -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" ] }
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue