Save real data to the database. Load data on app start.
This commit is contained in:
parent
e3cdc70b2c
commit
709f8064bf
|
@ -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/>.
|
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::{Record, RecordId, Series};
|
use crate::types::DayInterval;
|
||||||
|
use emseries::{time_range, Record, RecordId, Series, Timestamp};
|
||||||
use ft_core::TraxRecord;
|
use ft_core::TraxRecord;
|
||||||
use std::{
|
use std::{
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
@ -29,7 +30,7 @@ pub enum AppInvocation {
|
||||||
|
|
||||||
/// Request a set of records from the core.
|
/// Request a set of records from the core.
|
||||||
// Note: this will require a time range, but doesn't yet.
|
// Note: this will require a time range, but doesn't yet.
|
||||||
RequestRecords,
|
RequestRecords(DayInterval),
|
||||||
|
|
||||||
UpdateRecord(Record<TraxRecord>),
|
UpdateRecord(Record<TraxRecord>),
|
||||||
|
|
||||||
|
@ -52,7 +53,7 @@ pub enum AppResponse {
|
||||||
|
|
||||||
/// The database is open and here is a set of records. Typically, the set of records will be
|
/// 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.
|
/// 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
|
/// 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
|
/// change has happened. Further, the UI needs to save PathBuf to settings, because the
|
||||||
|
@ -82,31 +83,39 @@ impl App {
|
||||||
self.open_db(&db_path);
|
self.open_db(&db_path);
|
||||||
AppResponse::DatabaseChanged(db_path)
|
AppResponse::DatabaseChanged(db_path)
|
||||||
}
|
}
|
||||||
AppInvocation::RequestRecords => {
|
AppInvocation::RequestRecords(interval) => match &*self.database.read().unwrap() {
|
||||||
if self.database.read().unwrap().is_none() {
|
Some(database) => {
|
||||||
AppResponse::NoDatabase
|
let records = database
|
||||||
} else {
|
.search(time_range(
|
||||||
AppResponse::Records
|
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() {
|
AppInvocation::UpdateRecord(record) => match *self.database.write().unwrap() {
|
||||||
Some(ref mut database) => {
|
Some(ref mut database) => {
|
||||||
database.update(record).unwrap();
|
database.update(record).unwrap();
|
||||||
AppResponse::Records
|
AppResponse::Records(vec![])
|
||||||
}
|
}
|
||||||
None => AppResponse::NoDatabase,
|
None => AppResponse::NoDatabase,
|
||||||
},
|
},
|
||||||
AppInvocation::PutRecord(record) => match *self.database.write().unwrap() {
|
AppInvocation::PutRecord(record) => match *self.database.write().unwrap() {
|
||||||
Some(ref mut database) => {
|
Some(ref mut database) => {
|
||||||
database.put(record).unwrap();
|
database.put(record).unwrap();
|
||||||
AppResponse::Records
|
AppResponse::Records(vec![])
|
||||||
}
|
}
|
||||||
None => AppResponse::NoDatabase,
|
None => AppResponse::NoDatabase,
|
||||||
},
|
},
|
||||||
AppInvocation::DeleteRecord(record_id) => match *self.database.write().unwrap() {
|
AppInvocation::DeleteRecord(record_id) => match *self.database.write().unwrap() {
|
||||||
Some(ref mut database) => {
|
Some(ref mut database) => {
|
||||||
database.delete(&record_id).unwrap();
|
database.delete(&record_id).unwrap();
|
||||||
AppResponse::Records
|
AppResponse::Records(vec![])
|
||||||
}
|
}
|
||||||
None => AppResponse::NoDatabase,
|
None => AppResponse::NoDatabase,
|
||||||
},
|
},
|
||||||
|
|
|
@ -133,13 +133,13 @@ impl AppWindow {
|
||||||
self.settings
|
self.settings
|
||||||
.set_string("series-path", db_path.to_str().unwrap())
|
.set_string("series-path", db_path.to_str().unwrap())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
self.change_view(ViewName::Historical);
|
self.change_view(ViewName::Historical(vec![]));
|
||||||
}
|
}
|
||||||
AppResponse::NoDatabase => {
|
AppResponse::NoDatabase => {
|
||||||
self.change_view(ViewName::Welcome);
|
self.change_view(ViewName::Welcome);
|
||||||
}
|
}
|
||||||
AppResponse::Records => {
|
AppResponse::Records(records) => {
|
||||||
self.change_view(ViewName::Historical);
|
self.change_view(ViewName::Historical(records));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,8 +169,8 @@ impl AppWindow {
|
||||||
})
|
})
|
||||||
.upcast(),
|
.upcast(),
|
||||||
),
|
),
|
||||||
ViewName::Historical => View::Historical(
|
ViewName::Historical(records) => View::Historical(
|
||||||
HistoricalView::new(vec![], {
|
HistoricalView::new(records, {
|
||||||
let s = self.clone();
|
let s = self.clone();
|
||||||
Rc::new(move |date: chrono::NaiveDate, records| {
|
Rc::new(move |date: chrono::NaiveDate, records| {
|
||||||
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
let layout = gtk::Box::new(gtk::Orientation::Vertical, 0);
|
||||||
|
|
|
@ -320,8 +320,10 @@ impl WeightView {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn blur(&self) {
|
fn blur(&self) {
|
||||||
if *self.imp().current.borrow() == *self.imp().edit.borrow() {
|
let edit = self.imp().edit.borrow();
|
||||||
self.imp().on_edit_finished.borrow()(0. * si::KG);
|
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();
|
self.view();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,11 +17,13 @@ You should have received a copy of the GNU General Public License along with Fit
|
||||||
mod app;
|
mod app;
|
||||||
mod app_window;
|
mod app_window;
|
||||||
mod components;
|
mod components;
|
||||||
|
mod types;
|
||||||
mod views;
|
mod views;
|
||||||
|
|
||||||
use adw::prelude::*;
|
use adw::prelude::*;
|
||||||
use app_window::AppWindow;
|
use app_window::AppWindow;
|
||||||
use std::{env, path::PathBuf};
|
use std::{env, path::PathBuf};
|
||||||
|
use types::DayInterval;
|
||||||
|
|
||||||
const APP_ID_DEV: &str = "com.luminescent-dreams.fitnesstrax.dev";
|
const APP_ID_DEV: &str = "com.luminescent-dreams.fitnesstrax.dev";
|
||||||
const APP_ID_PROD: &str = "com.luminescent-dreams.fitnesstrax";
|
const APP_ID_PROD: &str = "com.luminescent-dreams.fitnesstrax";
|
||||||
|
@ -86,7 +88,9 @@ fn main() {
|
||||||
glib::spawn_future_local(async move {
|
glib::spawn_future_local(async move {
|
||||||
// The app requests data to start with. This kicks everything off. The response from
|
// The app requests data to start with. This kicks everything off. The response from
|
||||||
// the app will cause the window to be updated shortly.
|
// 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 {
|
while let Ok(response) = ui_rx.recv().await {
|
||||||
window.process_response(response);
|
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,7 +14,7 @@ General Public License for more details.
|
||||||
You should have received a copy of the GNU General Public License along with FitnessTrax. If not, see <https://www.gnu.org/licenses/>.
|
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 crate::{components::DaySummary, types::DayInterval};
|
||||||
use chrono::{Duration, Local, NaiveDate};
|
use chrono::{Duration, Local, NaiveDate};
|
||||||
use emseries::Record;
|
use emseries::Record;
|
||||||
use ft_core::TraxRecord;
|
use ft_core::TraxRecord;
|
||||||
|
@ -196,52 +196,6 @@ impl GroupedRecords {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)]
|
|
||||||
struct DayInterval {
|
|
||||||
start: NaiveDate,
|
|
||||||
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 {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::GroupedRecords;
|
use super::GroupedRecords;
|
||||||
|
|
|
@ -23,10 +23,13 @@ pub use placeholder_view::PlaceholderView;
|
||||||
mod welcome_view;
|
mod welcome_view;
|
||||||
pub use welcome_view::WelcomeView;
|
pub use welcome_view::WelcomeView;
|
||||||
|
|
||||||
|
use emseries::Record;
|
||||||
|
use ft_core::TraxRecord;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum ViewName {
|
pub enum ViewName {
|
||||||
Welcome,
|
Welcome,
|
||||||
Historical,
|
Historical(Vec<Record<TraxRecord>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum View {
|
pub enum View {
|
||||||
|
|
Loading…
Reference in New Issue