Compare commits
15 Commits
26fc8dae9a
...
709f8064bf
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | 709f8064bf | |
Savanni D'Gerinel | e3cdc70b2c | |
Savanni D'Gerinel | e2866e6ab8 | |
Savanni D'Gerinel | 434815ef43 | |
Savanni D'Gerinel | ddb72d71d4 | |
Savanni D'Gerinel | eae24ae313 | |
Savanni D'Gerinel | 3e681dbffd | |
Savanni D'Gerinel | 9e0cdb50ea | |
Savanni D'Gerinel | e00d0bdf51 | |
Savanni D'Gerinel | 7b851ba051 | |
Savanni D'Gerinel | aecd188151 | |
Savanni D'Gerinel | 55e9805975 | |
Savanni D'Gerinel | 60d72bdb9b | |
Savanni D'Gerinel | b2cb7fddd5 | |
Savanni D'Gerinel | 43516ac58e |
|
@ -178,7 +178,7 @@ impl fmt::Display for RecordId {
|
|||
/// directly, as the database will create them.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct Record<T: Clone + Recordable> {
|
||||
pub(crate) id: RecordId,
|
||||
pub id: RecordId,
|
||||
pub data: T,
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ 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_2" ] }
|
||||
adw = { version = "0.5", package = "libadwaita", features = [ "v1_4" ] }
|
||||
async-channel = { version = "2.1" }
|
||||
chrono = { version = "0.4" }
|
||||
chrono-tz = { version = "0.8" }
|
||||
|
|
|
@ -2,32 +2,38 @@
|
|||
margin: 64px;
|
||||
}
|
||||
|
||||
.welcome-title {
|
||||
.welcome__title {
|
||||
font-size: larger;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.welcome-content {
|
||||
.welcome__content {
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.welcome-footer {
|
||||
.welcome__footer {
|
||||
}
|
||||
|
||||
.dialog-row {
|
||||
margin: 8px 0px 8px 0px;
|
||||
.historical {
|
||||
margin: 32px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.day-summary {
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.daysummary {
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.daysummary-date {
|
||||
.day-summary__date {
|
||||
font-size: larger;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.daysummary-weight {
|
||||
.day-summary__weight {
|
||||
margin: 4px;
|
||||
}
|
||||
|
||||
.weight-view {
|
||||
padding: 8px;
|
||||
margin: 8px;
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,8 @@ 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 emseries::Series;
|
||||
use crate::types::DayInterval;
|
||||
use emseries::{time_range, Record, RecordId, Series, Timestamp};
|
||||
use ft_core::TraxRecord;
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
|
@ -29,7 +30,13 @@ pub enum AppInvocation {
|
|||
|
||||
/// Request a set of records from the core.
|
||||
// Note: this will require a time range, but doesn't yet.
|
||||
RequestRecords,
|
||||
RequestRecords(DayInterval),
|
||||
|
||||
UpdateRecord(Record<TraxRecord>),
|
||||
|
||||
PutRecord(TraxRecord),
|
||||
|
||||
DeleteRecord(RecordId),
|
||||
}
|
||||
|
||||
/// Responses are messages that the core sends to the UI. Though they are called responses, the
|
||||
|
@ -46,7 +53,7 @@ pub enum AppResponse {
|
|||
|
||||
/// The database is open and here is a set of records. Typically, the set of records will be
|
||||
/// all of the records within a time frame, but this can actually be any set of records.
|
||||
Records,
|
||||
Records(Vec<Record<TraxRecord>>),
|
||||
|
||||
/// The database has been changed. This message is useful for telling the UI that a significant
|
||||
/// change has happened. Further, the UI needs to save PathBuf to settings, because the
|
||||
|
@ -76,13 +83,42 @@ impl App {
|
|||
self.open_db(&db_path);
|
||||
AppResponse::DatabaseChanged(db_path)
|
||||
}
|
||||
AppInvocation::RequestRecords => {
|
||||
if self.database.read().unwrap().is_none() {
|
||||
AppResponse::NoDatabase
|
||||
} else {
|
||||
AppResponse::Records
|
||||
AppInvocation::RequestRecords(interval) => match &*self.database.read().unwrap() {
|
||||
Some(database) => {
|
||||
let records = database
|
||||
.search(time_range(
|
||||
Timestamp::Date(interval.start),
|
||||
true,
|
||||
Timestamp::Date(interval.end),
|
||||
true,
|
||||
))
|
||||
.map(|record| record.clone())
|
||||
.collect::<Vec<Record<TraxRecord>>>();
|
||||
AppResponse::Records(records)
|
||||
}
|
||||
None => AppResponse::NoDatabase,
|
||||
},
|
||||
AppInvocation::UpdateRecord(record) => match *self.database.write().unwrap() {
|
||||
Some(ref mut database) => {
|
||||
database.update(record).unwrap();
|
||||
AppResponse::Records(vec![])
|
||||
}
|
||||
None => AppResponse::NoDatabase,
|
||||
},
|
||||
AppInvocation::PutRecord(record) => match *self.database.write().unwrap() {
|
||||
Some(ref mut database) => {
|
||||
database.put(record).unwrap();
|
||||
AppResponse::Records(vec![])
|
||||
}
|
||||
None => AppResponse::NoDatabase,
|
||||
},
|
||||
AppInvocation::DeleteRecord(record_id) => match *self.database.write().unwrap() {
|
||||
Some(ref mut database) => {
|
||||
database.delete(&record_id).unwrap();
|
||||
AppResponse::Records(vec![])
|
||||
}
|
||||
None => AppResponse::NoDatabase,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,14 +16,13 @@ You should have received a copy of the GNU General Public License along with Fit
|
|||
|
||||
use crate::{
|
||||
app::{AppInvocation, AppResponse},
|
||||
components::DayDetail,
|
||||
views::{HistoricalView, PlaceholderView, View, ViewName, WelcomeView},
|
||||
};
|
||||
use adw::prelude::*;
|
||||
use async_channel::Sender;
|
||||
use chrono::{NaiveDate, TimeZone};
|
||||
use chrono_tz::America::Anchorage;
|
||||
use chrono::{FixedOffset, NaiveDate, TimeZone};
|
||||
use dimensioned::si::{KG, M, S};
|
||||
use emseries::DateTimeTz;
|
||||
use ft_core::{Steps, TimeDistance, TraxRecord, Weight};
|
||||
use gio::resources_lookup_data;
|
||||
use gtk::STYLE_PROVIDER_PRIORITY_USER;
|
||||
|
@ -38,6 +37,7 @@ pub struct AppWindow {
|
|||
layout: gtk::Box,
|
||||
current_view: Rc<RefCell<View>>,
|
||||
settings: gio::Settings,
|
||||
navigation: adw::NavigationView,
|
||||
}
|
||||
|
||||
impl AppWindow {
|
||||
|
@ -77,9 +77,13 @@ impl AppWindow {
|
|||
#[allow(deprecated)]
|
||||
context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);
|
||||
|
||||
let navigation = adw::NavigationView::new();
|
||||
|
||||
/*
|
||||
let header = adw::HeaderBar::builder()
|
||||
.title_widget(>k::Label::new(Some("FitnessTrax")))
|
||||
.build();
|
||||
*/
|
||||
|
||||
let layout = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
|
@ -87,17 +91,33 @@ impl AppWindow {
|
|||
|
||||
let initial_view = View::Placeholder(PlaceholderView::new().upcast());
|
||||
|
||||
layout.append(&header);
|
||||
// layout.append(&header);
|
||||
layout.append(initial_view.widget());
|
||||
|
||||
window.set_content(Some(&layout));
|
||||
let nav_layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
nav_layout.append(&adw::HeaderBar::new());
|
||||
nav_layout.append(&layout);
|
||||
navigation.push(
|
||||
&adw::NavigationPage::builder()
|
||||
.can_pop(false)
|
||||
.title("FitnessTrax")
|
||||
.child(&nav_layout)
|
||||
.build(),
|
||||
);
|
||||
|
||||
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_tx,
|
||||
layout,
|
||||
current_view: Rc::new(RefCell::new(initial_view)),
|
||||
settings: gio::Settings::new(app_id),
|
||||
navigation,
|
||||
};
|
||||
|
||||
s
|
||||
|
@ -113,13 +133,13 @@ impl AppWindow {
|
|||
self.settings
|
||||
.set_string("series-path", db_path.to_str().unwrap())
|
||||
.unwrap();
|
||||
self.change_view(ViewName::Historical);
|
||||
self.change_view(ViewName::Historical(vec![]));
|
||||
}
|
||||
AppResponse::NoDatabase => {
|
||||
self.change_view(ViewName::Welcome);
|
||||
}
|
||||
AppResponse::Records => {
|
||||
self.change_view(ViewName::Historical);
|
||||
AppResponse::Records(records) => {
|
||||
self.change_view(ViewName::Historical(records));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -149,37 +169,36 @@ impl AppWindow {
|
|||
})
|
||||
.upcast(),
|
||||
),
|
||||
ViewName::Historical => View::Historical(
|
||||
HistoricalView::new(vec![
|
||||
TraxRecord::Steps(Steps {
|
||||
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
|
||||
count: 1500,
|
||||
}),
|
||||
TraxRecord::Weight(Weight {
|
||||
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
|
||||
weight: 85. * KG,
|
||||
}),
|
||||
TraxRecord::Weight(Weight {
|
||||
date: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
|
||||
weight: 86. * KG,
|
||||
}),
|
||||
TraxRecord::BikeRide(TimeDistance {
|
||||
datetime: DateTimeTz(
|
||||
Anchorage.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()),
|
||||
}),
|
||||
TraxRecord::BikeRide(TimeDistance {
|
||||
datetime: DateTimeTz(
|
||||
Anchorage.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()),
|
||||
}),
|
||||
])
|
||||
ViewName::Historical(records) => View::Historical(
|
||||
HistoricalView::new(records, {
|
||||
let s = self.clone();
|
||||
Rc::new(move |date: chrono::NaiveDate, records| {
|
||||
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||
layout.append(&adw::HeaderBar::new());
|
||||
layout.append(&DayDetail::new(
|
||||
date,
|
||||
records,
|
||||
{
|
||||
let app_tx = s.app_tx.clone();
|
||||
move |record| {
|
||||
let _ = app_tx.send_blocking(AppInvocation::PutRecord(record));
|
||||
}
|
||||
},
|
||||
{
|
||||
let app_tx = s.app_tx.clone();
|
||||
move |record| {
|
||||
let _ =
|
||||
app_tx.send_blocking(AppInvocation::UpdateRecord(record));
|
||||
}
|
||||
},
|
||||
));
|
||||
let page = &adw::NavigationPage::builder()
|
||||
.title(date.format("%Y-%m-%d").to_string())
|
||||
.child(&layout)
|
||||
.build();
|
||||
s.navigation.push(page);
|
||||
})
|
||||
})
|
||||
.upcast(),
|
||||
),
|
||||
}
|
||||
|
|
|
@ -16,12 +16,13 @@ You should have received a copy of the GNU General Public License along with Fit
|
|||
|
||||
// use chrono::NaiveDate;
|
||||
// use ft_core::TraxRecord;
|
||||
use ft_core::TraxRecord;
|
||||
use dimensioned::si;
|
||||
use emseries::Record;
|
||||
use ft_core::{RecordType, TimeDistance, TraxRecord, Weight};
|
||||
use glib::Object;
|
||||
use gtk::{prelude::*, subclass::prelude::*};
|
||||
use std::cell::RefCell;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DaySummaryPrivate {
|
||||
date: gtk::Label,
|
||||
weight: RefCell<Option<gtk::Label>>,
|
||||
|
@ -35,7 +36,7 @@ impl ObjectSubclass for DaySummaryPrivate {
|
|||
|
||||
fn new() -> Self {
|
||||
let date = gtk::Label::builder()
|
||||
.css_classes(["daysummary-date"])
|
||||
.css_classes(["day-summary__date"])
|
||||
.halign(gtk::Align::Start)
|
||||
.build();
|
||||
Self {
|
||||
|
@ -59,14 +60,14 @@ impl DaySummary {
|
|||
pub fn new() -> Self {
|
||||
let s: Self = Object::builder().build();
|
||||
s.set_orientation(gtk::Orientation::Vertical);
|
||||
s.set_css_classes(&["daysummary"]);
|
||||
s.set_css_classes(&["day-summary"]);
|
||||
|
||||
s.append(&s.imp().date);
|
||||
|
||||
s
|
||||
}
|
||||
|
||||
pub fn set_data(&self, date: chrono::NaiveDate, records: Vec<TraxRecord>) {
|
||||
pub fn set_data(&self, date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>) {
|
||||
self.imp()
|
||||
.date
|
||||
.set_text(&date.format("%Y-%m-%d").to_string());
|
||||
|
@ -75,16 +76,339 @@ impl DaySummary {
|
|||
self.remove(weight_label);
|
||||
}
|
||||
|
||||
if let Some(TraxRecord::Weight(weight_record)) =
|
||||
records.iter().filter(|f| f.is_weight()).next()
|
||||
if let Some(Record {
|
||||
data: TraxRecord::Weight(weight_record),
|
||||
..
|
||||
}) = records.iter().filter(|f| f.data.is_weight()).next()
|
||||
{
|
||||
let label = gtk::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label(&format!("{}", weight_record.weight))
|
||||
.css_classes(["daysummary-weight"])
|
||||
.css_classes(["day-summary__weight"])
|
||||
.build();
|
||||
self.append(&label);
|
||||
*self.imp().weight.borrow_mut() = Some(label);
|
||||
}
|
||||
|
||||
/*
|
||||
self.append(
|
||||
>k::Label::builder()
|
||||
.halign(gtk::Align::Start)
|
||||
.label("15km of biking in 60 minutes")
|
||||
.build(),
|
||||
);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DayDetailPrivate {
|
||||
date: gtk::Label,
|
||||
weight: RefCell<Option<gtk::Label>>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for DayDetailPrivate {
|
||||
const NAME: &'static str = "DayDetail";
|
||||
type Type = DayDetail;
|
||||
type ParentType = gtk::Box;
|
||||
|
||||
fn new() -> Self {
|
||||
let date = gtk::Label::builder()
|
||||
.css_classes(["daysummary-date"])
|
||||
.halign(gtk::Align::Start)
|
||||
.build();
|
||||
Self {
|
||||
date,
|
||||
weight: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectImpl for DayDetailPrivate {}
|
||||
impl WidgetImpl for DayDetailPrivate {}
|
||||
impl BoxImpl for DayDetailPrivate {}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct DayDetail(ObjectSubclass<DayDetailPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
||||
}
|
||||
|
||||
impl DayDetail {
|
||||
pub fn new<SaveRecordFn, UpdateRecordFn>(
|
||||
date: chrono::NaiveDate,
|
||||
records: Vec<Record<TraxRecord>>,
|
||||
on_save_record: SaveRecordFn,
|
||||
on_update_record: UpdateRecordFn,
|
||||
) -> Self
|
||||
where
|
||||
SaveRecordFn: Fn(TraxRecord) + 'static,
|
||||
UpdateRecordFn: Fn(Record<TraxRecord>) + 'static,
|
||||
{
|
||||
let s: Self = Object::builder().build();
|
||||
s.set_orientation(gtk::Orientation::Vertical);
|
||||
|
||||
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: TraxRecord::Weight(record),
|
||||
} => Some((id.clone(), record.clone())),
|
||||
_ => None,
|
||||
});
|
||||
|
||||
let weight_view = match weight_record {
|
||||
Some((id, data)) => WeightView::new(Some(data.clone()), move |weight| {
|
||||
on_update_record(Record {
|
||||
id: id.clone(),
|
||||
data: TraxRecord::Weight(Weight { date, weight }),
|
||||
})
|
||||
}),
|
||||
None => WeightView::new(None, move |weight| {
|
||||
on_save_record(TraxRecord::Weight(Weight { date, weight }));
|
||||
}),
|
||||
};
|
||||
s.append(&weight_view);
|
||||
|
||||
records.into_iter().for_each(|record| {
|
||||
let record_view = match record {
|
||||
Record {
|
||||
data: TraxRecord::BikeRide(record),
|
||||
..
|
||||
} => Some(
|
||||
TimeDistanceView::new(RecordType::BikeRide, record).upcast::<gtk::Widget>(),
|
||||
),
|
||||
Record {
|
||||
data: TraxRecord::Row(record),
|
||||
..
|
||||
} => Some(TimeDistanceView::new(RecordType::Row, record).upcast::<gtk::Widget>()),
|
||||
Record {
|
||||
data: TraxRecord::Run(record),
|
||||
..
|
||||
} => Some(TimeDistanceView::new(RecordType::Row, record).upcast::<gtk::Widget>()),
|
||||
Record {
|
||||
data: TraxRecord::Swim(record),
|
||||
..
|
||||
} => Some(TimeDistanceView::new(RecordType::Row, record).upcast::<gtk::Widget>()),
|
||||
Record {
|
||||
data: TraxRecord::Walk(record),
|
||||
..
|
||||
} => Some(TimeDistanceView::new(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);
|
||||
}
|
||||
});
|
||||
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WeightViewPrivate {
|
||||
record: RefCell<Option<Weight>>,
|
||||
view: RefCell<gtk::Label>,
|
||||
edit: RefCell<gtk::Entry>,
|
||||
current: RefCell<gtk::Widget>,
|
||||
on_edit_finished: RefCell<Box<dyn Fn(si::Kilogram<f64>)>>,
|
||||
}
|
||||
|
||||
impl Default for WeightViewPrivate {
|
||||
fn default() -> Self {
|
||||
let view = gtk::Label::builder()
|
||||
.css_classes(["card", "weight-view"])
|
||||
.halign(gtk::Align::Start)
|
||||
.can_focus(true)
|
||||
.build();
|
||||
let edit = gtk::Entry::builder().halign(gtk::Align::Start).build();
|
||||
|
||||
let current = view.clone();
|
||||
|
||||
Self {
|
||||
record: RefCell::new(None),
|
||||
view: RefCell::new(view),
|
||||
edit: RefCell::new(edit),
|
||||
current: RefCell::new(current.upcast()),
|
||||
on_edit_finished: RefCell::new(Box::new(|_| {})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for WeightViewPrivate {
|
||||
const NAME: &'static str = "WeightView";
|
||||
type Type = WeightView;
|
||||
type ParentType = gtk::Box;
|
||||
}
|
||||
|
||||
impl ObjectImpl for WeightViewPrivate {}
|
||||
impl WidgetImpl for WeightViewPrivate {}
|
||||
impl BoxImpl for WeightViewPrivate {}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct WeightView(ObjectSubclass<WeightViewPrivate>) @extends gtk::Box, gtk::Widget;
|
||||
}
|
||||
|
||||
impl WeightView {
|
||||
pub fn new<OnEditFinished>(weight: Option<Weight>, on_edit_finished: OnEditFinished) -> Self
|
||||
where
|
||||
OnEditFinished: Fn(si::Kilogram<f64>) + 'static,
|
||||
{
|
||||
let s: Self = Object::builder().build();
|
||||
|
||||
*s.imp().on_edit_finished.borrow_mut() = Box::new(on_edit_finished);
|
||||
|
||||
*s.imp().record.borrow_mut() = weight;
|
||||
s.view();
|
||||
|
||||
let view_click_controller = gtk::GestureClick::new();
|
||||
view_click_controller.connect_released({
|
||||
let s = s.clone();
|
||||
move |_, _, _, _| {
|
||||
s.edit();
|
||||
}
|
||||
});
|
||||
|
||||
s.imp().view.borrow().add_controller(view_click_controller);
|
||||
s
|
||||
}
|
||||
|
||||
fn view(&self) {
|
||||
let view = self.imp().view.borrow();
|
||||
match *self.imp().record.borrow() {
|
||||
Some(ref record) => {
|
||||
view.remove_css_class("dim_label");
|
||||
view.set_label(&format!("{:?}", record.weight));
|
||||
}
|
||||
None => {
|
||||
view.add_css_class("dim_label");
|
||||
view.set_label("No weight recorded");
|
||||
}
|
||||
}
|
||||
self.swap(view.clone().upcast());
|
||||
}
|
||||
|
||||
fn edit(&self) {
|
||||
let edit = self.imp().edit.borrow();
|
||||
match *self.imp().record.borrow() {
|
||||
Some(ref record) => edit.buffer().set_text(&format!("{:?}", record.weight)),
|
||||
None => edit.buffer().set_text(""),
|
||||
}
|
||||
self.swap(edit.clone().upcast());
|
||||
edit.grab_focus();
|
||||
}
|
||||
|
||||
fn swap(&self, new_view: gtk::Widget) {
|
||||
let mut current = self.imp().current.borrow_mut();
|
||||
self.remove(&*current);
|
||||
self.append(&new_view);
|
||||
*current = new_view;
|
||||
}
|
||||
|
||||
fn blur(&self) {
|
||||
let edit = self.imp().edit.borrow();
|
||||
if *self.imp().current.borrow() == *edit {
|
||||
let w = edit.buffer().text().parse::<f64>().unwrap();
|
||||
self.imp().on_edit_finished.borrow()(w * si::KG);
|
||||
self.view();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TimeDistanceViewPrivate {
|
||||
record: RefCell<Option<TimeDistance>>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for TimeDistanceViewPrivate {
|
||||
const NAME: &'static str = "TimeDistanceView";
|
||||
type Type = TimeDistanceView;
|
||||
type ParentType = gtk::Box;
|
||||
}
|
||||
|
||||
impl ObjectImpl for TimeDistanceViewPrivate {}
|
||||
impl WidgetImpl for TimeDistanceViewPrivate {}
|
||||
impl BoxImpl for TimeDistanceViewPrivate {}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct TimeDistanceView(ObjectSubclass<TimeDistanceViewPrivate>) @extends gtk::Box, gtk::Widget, @implements gtk::Orientable;
|
||||
}
|
||||
|
||||
impl TimeDistanceView {
|
||||
pub fn new(type_: RecordType, record: TimeDistance) -> Self {
|
||||
let s: Self = Object::builder().build();
|
||||
s.set_orientation(gtk::Orientation::Vertical);
|
||||
s.set_hexpand(true);
|
||||
|
||||
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| format!("{}", comments))
|
||||
.unwrap_or("".to_owned()),
|
||||
)
|
||||
.build(),
|
||||
);
|
||||
|
||||
s
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,8 +15,8 @@ You should have received a copy of the GNU General Public License along with Fit
|
|||
*/
|
||||
|
||||
mod day;
|
||||
pub use day::{DayDetail, DaySummary};
|
||||
|
||||
pub use day::DaySummary;
|
||||
use glib::Object;
|
||||
use gtk::{prelude::*, subclass::prelude::*};
|
||||
use std::{cell::RefCell, path::PathBuf, rc::Rc};
|
||||
|
|
|
@ -17,11 +17,13 @@ You should have received a copy of the GNU General Public License along with Fit
|
|||
mod app;
|
||||
mod app_window;
|
||||
mod components;
|
||||
mod types;
|
||||
mod views;
|
||||
|
||||
use adw::prelude::*;
|
||||
use app_window::AppWindow;
|
||||
use std::{env, path::PathBuf};
|
||||
use types::DayInterval;
|
||||
|
||||
const APP_ID_DEV: &str = "com.luminescent-dreams.fitnesstrax.dev";
|
||||
const APP_ID_PROD: &str = "com.luminescent-dreams.fitnesstrax";
|
||||
|
@ -86,7 +88,9 @@ fn main() {
|
|||
glib::spawn_future_local(async move {
|
||||
// The app requests data to start with. This kicks everything off. The response from
|
||||
// the app will cause the window to be updated shortly.
|
||||
let _ = app_tx.send(app::AppInvocation::RequestRecords).await;
|
||||
let _ = app_tx
|
||||
.send(app::AppInvocation::RequestRecords(DayInterval::default()))
|
||||
.await;
|
||||
|
||||
while let Ok(response) = ui_rx.recv().await {
|
||||
window.process_response(response);
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
use chrono::{Duration, Local, NaiveDate};
|
||||
|
||||
// This interval doesn't feel right, either. The idea that I have a specific interval type for just
|
||||
// NaiveDate is odd. This should be genericized, as should the iterator. Also, it shouldn't live
|
||||
// here, but in utilities.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct DayInterval {
|
||||
pub start: NaiveDate,
|
||||
pub end: NaiveDate,
|
||||
}
|
||||
|
||||
impl Default for DayInterval {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
start: (Local::now() - Duration::days(7)).date_naive(),
|
||||
end: Local::now().date_naive(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DayInterval {
|
||||
pub fn days(&self) -> impl Iterator<Item = NaiveDate> {
|
||||
DayIterator {
|
||||
current: self.start.clone(),
|
||||
end: self.end.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DayIterator {
|
||||
current: NaiveDate,
|
||||
end: NaiveDate,
|
||||
}
|
||||
|
||||
impl Iterator for DayIterator {
|
||||
type Item = NaiveDate;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current <= self.end {
|
||||
let val = self.current.clone();
|
||||
self.current += Duration::days(1);
|
||||
Some(val)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,17 +14,20 @@ 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::DaySummary;
|
||||
use emseries::{Recordable, Timestamp};
|
||||
use crate::{components::DaySummary, types::DayInterval};
|
||||
use chrono::{Duration, Local, NaiveDate};
|
||||
use emseries::Record;
|
||||
use ft_core::TraxRecord;
|
||||
use glib::Object;
|
||||
use gtk::{prelude::*, subclass::prelude::*};
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
use std::{cell::RefCell, collections::HashMap, 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
|
||||
/// records.
|
||||
pub struct HistoricalViewPrivate {}
|
||||
pub struct HistoricalViewPrivate {
|
||||
time_window: RefCell<DayInterval>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for HistoricalViewPrivate {
|
||||
|
@ -33,7 +36,9 @@ impl ObjectSubclass for HistoricalViewPrivate {
|
|||
type ParentType = gtk::Box;
|
||||
|
||||
fn new() -> Self {
|
||||
Self {}
|
||||
Self {
|
||||
time_window: RefCell::new(DayInterval::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,14 +51,19 @@ glib::wrapper! {
|
|||
}
|
||||
|
||||
impl HistoricalView {
|
||||
pub fn new(records: Vec<TraxRecord>) -> Self {
|
||||
pub fn new<SelectFn>(records: Vec<Record<TraxRecord>>, on_select_day: Rc<SelectFn>) -> Self
|
||||
where
|
||||
SelectFn: Fn(chrono::NaiveDate, Vec<Record<TraxRecord>>) + 'static,
|
||||
{
|
||||
let s: Self = Object::builder().build();
|
||||
s.set_orientation(gtk::Orientation::Vertical);
|
||||
s.set_css_classes(&["historical"]);
|
||||
|
||||
let day_records: GroupedRecords = GroupedRecords::from(records);
|
||||
let grouped_records =
|
||||
GroupedRecords::new((*s.imp().time_window.borrow()).clone()).with_data(records);
|
||||
|
||||
let model = gio::ListStore::new::<DayRecords>();
|
||||
model.extend_from_slice(&day_records.0);
|
||||
let mut model = gio::ListStore::new::<DayRecords>();
|
||||
model.extend(grouped_records.items());
|
||||
|
||||
let factory = gtk::SignalListItemFactory::new();
|
||||
factory.connect_setup(move |_, list_item| {
|
||||
|
@ -86,13 +96,16 @@ impl HistoricalView {
|
|||
.factory(&factory)
|
||||
.single_click_activate(true)
|
||||
.build();
|
||||
lst.connect_activate(|s, idx| {
|
||||
lst.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.
|
||||
let item = s.model().unwrap().item(idx).unwrap();
|
||||
let records = item.downcast_ref::<DayRecords>().unwrap();
|
||||
println!("list item activated: [{:?}] {:?}", idx, records.date());
|
||||
on_select_day(records.date(), records.records());
|
||||
}
|
||||
});
|
||||
|
||||
s.append(&lst);
|
||||
|
@ -104,7 +117,7 @@ impl HistoricalView {
|
|||
#[derive(Default)]
|
||||
pub struct DayRecordsPrivate {
|
||||
date: RefCell<chrono::NaiveDate>,
|
||||
records: RefCell<Vec<TraxRecord>>,
|
||||
records: RefCell<Vec<Record<TraxRecord>>>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
|
@ -120,7 +133,7 @@ glib::wrapper! {
|
|||
}
|
||||
|
||||
impl DayRecords {
|
||||
pub fn new(date: chrono::NaiveDate, records: Vec<TraxRecord>) -> Self {
|
||||
pub fn new(date: chrono::NaiveDate, records: Vec<Record<TraxRecord>>) -> Self {
|
||||
let s: Self = Object::builder().build();
|
||||
|
||||
*s.imp().date.borrow_mut() = date;
|
||||
|
@ -133,75 +146,112 @@ impl DayRecords {
|
|||
self.imp().date.borrow().clone()
|
||||
}
|
||||
|
||||
pub fn records(&self) -> Vec<TraxRecord> {
|
||||
pub fn records(&self) -> Vec<Record<TraxRecord>> {
|
||||
self.imp().records.borrow().clone()
|
||||
}
|
||||
|
||||
pub fn add_record(&self, record: TraxRecord) {
|
||||
pub fn add_record(&self, record: Record<TraxRecord>) {
|
||||
self.imp().records.borrow_mut().push(record);
|
||||
}
|
||||
}
|
||||
|
||||
struct GroupedRecords(Vec<DayRecords>);
|
||||
// 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 From<Vec<TraxRecord>> for GroupedRecords {
|
||||
fn from(records: Vec<TraxRecord>) -> GroupedRecords {
|
||||
GroupedRecords(
|
||||
records
|
||||
.into_iter()
|
||||
.fold(HashMap::new(), |mut acc, rec| {
|
||||
let date = match rec.timestamp() {
|
||||
Timestamp::DateTime(dtz) => dtz.0.date_naive(),
|
||||
Timestamp::Date(date) => date,
|
||||
impl GroupedRecords {
|
||||
fn new(interval: DayInterval) -> Self {
|
||||
let mut s = Self {
|
||||
interval: interval.clone(),
|
||||
data: HashMap::new(),
|
||||
};
|
||||
acc.entry(date)
|
||||
.and_modify(|entry: &mut DayRecords| (*entry).add_record(rec.clone()))
|
||||
.or_insert(DayRecords::new(date, vec![rec]));
|
||||
acc
|
||||
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<'a>(&'a self) -> impl Iterator<Item = DayRecords> + 'a {
|
||||
self.interval.days().map(|date| {
|
||||
self.data
|
||||
.get(&date)
|
||||
.map(|rec| rec.clone())
|
||||
.unwrap_or(DayRecords::new(date, vec![]))
|
||||
})
|
||||
.values()
|
||||
.cloned()
|
||||
.collect::<Vec<DayRecords>>(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::GroupedRecords;
|
||||
use chrono::{NaiveDate, TimeZone};
|
||||
use chrono_tz::America::Anchorage;
|
||||
use chrono::{FixedOffset, NaiveDate, TimeZone};
|
||||
use dimensioned::si::{KG, M, S};
|
||||
use emseries::DateTimeTz;
|
||||
use emseries::{Record, RecordId};
|
||||
use ft_core::{Steps, TimeDistance, TraxRecord, Weight};
|
||||
|
||||
#[test]
|
||||
fn groups_records() {
|
||||
let records = vec![
|
||||
TraxRecord::Steps(Steps {
|
||||
Record {
|
||||
id: RecordId::default(),
|
||||
data: TraxRecord::Steps(Steps {
|
||||
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
|
||||
count: 1500,
|
||||
}),
|
||||
TraxRecord::Weight(Weight {
|
||||
},
|
||||
Record {
|
||||
id: RecordId::default(),
|
||||
data: TraxRecord::Weight(Weight {
|
||||
date: NaiveDate::from_ymd_opt(2023, 10, 13).unwrap(),
|
||||
weight: 85. * KG,
|
||||
}),
|
||||
TraxRecord::Weight(Weight {
|
||||
},
|
||||
Record {
|
||||
id: RecordId::default(),
|
||||
data: TraxRecord::Weight(Weight {
|
||||
date: NaiveDate::from_ymd_opt(2023, 10, 14).unwrap(),
|
||||
weight: 86. * KG,
|
||||
}),
|
||||
TraxRecord::BikeRide(TimeDistance {
|
||||
datetime: DateTimeTz(Anchorage.with_ymd_and_hms(2019, 6, 15, 12, 0, 0).unwrap()),
|
||||
},
|
||||
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()),
|
||||
}),
|
||||
TraxRecord::BikeRide(TimeDistance {
|
||||
datetime: DateTimeTz(Anchorage.with_ymd_and_hms(2019, 6, 15, 23, 0, 0).unwrap()),
|
||||
},
|
||||
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::from(records).0;
|
||||
|
|
|
@ -23,10 +23,13 @@ pub use placeholder_view::PlaceholderView;
|
|||
mod welcome_view;
|
||||
pub use welcome_view::WelcomeView;
|
||||
|
||||
use emseries::Record;
|
||||
use ft_core::TraxRecord;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ViewName {
|
||||
Welcome,
|
||||
Historical,
|
||||
Historical(Vec<Record<TraxRecord>>),
|
||||
}
|
||||
|
||||
pub enum View {
|
||||
|
|
|
@ -55,11 +55,10 @@ impl WelcomeView {
|
|||
// branch.
|
||||
let title = gtk::Label::builder()
|
||||
.label("Welcome to FitnessTrax")
|
||||
.css_classes(["welcome-title"])
|
||||
.css_classes(["welcome__title"])
|
||||
.build();
|
||||
|
||||
let content = gtk::Box::builder()
|
||||
.css_classes(["model-content"])
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.vexpand(true)
|
||||
.build();
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
use chrono::NaiveDate;
|
||||
use dimensioned::si;
|
||||
use emseries::DateTimeTz;
|
||||
|
||||
mod legacy;
|
||||
|
||||
mod types;
|
||||
pub use types::{Steps, TimeDistance, TraxRecord, Weight};
|
||||
pub use types::{RecordType, Steps, TimeDistance, TraxRecord, Weight};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use chrono::NaiveDate;
|
||||
use chrono::{DateTime, FixedOffset, NaiveDate};
|
||||
use dimensioned::si;
|
||||
use emseries::{DateTimeTz, Recordable, Timestamp};
|
||||
use emseries::{Recordable, Timestamp};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// SetRep represents workouts like pushups or situps, which involve doing a "set" of a number of
|
||||
|
@ -31,10 +31,8 @@ pub struct Steps {
|
|||
pub struct TimeDistance {
|
||||
/// The precise time (and the relevant timezone) of the workout. One of the edge cases that I
|
||||
/// account for is that a ride which occurred at 11pm in one timezone would then count as 1am
|
||||
/// if one moved two timezones to the east. This is kind of nonsensical from a human
|
||||
/// perspective, so the DateTimeTz keeps track of the precise time in UTC, but also the
|
||||
/// timezone in which the event was recorded.
|
||||
pub datetime: DateTimeTz,
|
||||
/// if one moved two timezones to the east.
|
||||
pub datetime: DateTime<FixedOffset>,
|
||||
/// The distance travelled. This is optional because such a workout makes sense even without
|
||||
/// the distance.
|
||||
pub distance: Option<si::Meter<f64>>,
|
||||
|
@ -52,6 +50,17 @@ pub struct Weight {
|
|||
pub weight: si::Kilogram<f64>,
|
||||
}
|
||||
|
||||
#[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 {
|
||||
|
@ -65,6 +74,18 @@ pub enum TraxRecord {
|
|||
}
|
||||
|
||||
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 {
|
||||
match self {
|
||||
TraxRecord::Weight(_) => true,
|
||||
|
|
|
@ -51,16 +51,16 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1691421349,
|
||||
"narHash": "sha256-RRJyX0CUrs4uW4gMhd/X4rcDG8PTgaaCQM5rXEJOx6g=",
|
||||
"lastModified": 1703200384,
|
||||
"narHash": "sha256-q5j06XOsy0qHOarsYPfZYJPWbTbc8sryRxianlEPJN0=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "011567f35433879aae5024fc6ec53f2a0568a6c4",
|
||||
"rev": "0b3d618173114c64ab666f557504d6982665d328",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
"ref": "nixos-23.05",
|
||||
"ref": "nixos-23.11",
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
description = "Lumenescent Dreams Tools";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "nixpkgs/nixos-23.05";
|
||||
nixpkgs.url = "nixpkgs/nixos-23.11";
|
||||
unstable.url = "nixpkgs/nixos-unstable";
|
||||
pkgs-cargo2nix.url = "github:cargo2nix/cargo2nix";
|
||||
typeshare.url = "github:1Password/typeshare";
|
||||
|
|
Loading…
Reference in New Issue