From eb80833b5d736180baa49c7f3787fe30eba161db Mon Sep 17 00:00:00 2001 From: Ricky Kresslein Date: Thu, 14 Apr 2022 17:44:21 +0300 Subject: Add daily time sums (Issue #17) --- src/ui/task_row.rs | 18 ++++++++++++------ src/ui/tasks_group.rs | 7 +++++++ src/ui/tasks_page.rs | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ui/task_row.rs b/src/ui/task_row.rs index f55aba4..fe8db68 100755 --- a/src/ui/task_row.rs +++ b/src/ui/task_row.rs @@ -29,6 +29,7 @@ use crate::settings_manager; mod imp { use super::*; use glib::subclass; + use std::cell::RefCell; #[derive(Debug, CompositeTemplate, Default)] #[template(resource = "/com/lakoliu/Furtherance/gtk/task_row.ui")] @@ -37,8 +38,9 @@ mod imp { pub task_name_label: TemplateChild, #[template_child] pub total_time_label: TemplateChild, - // pub tasks: Vec, + pub tasks: Lazy>>, + pub total_time: RefCell, } #[glib::object_subclass] @@ -115,20 +117,19 @@ impl FurTaskRow { self.add_controller(&gesture); // Add up all durations for task of said name to create total_time - let mut total_time: i64 = 0; for task in &task_list { if task.task_name == task.task_name { let start_time = DateTime::parse_from_rfc3339(&task.start_time).unwrap(); let stop_time = DateTime::parse_from_rfc3339(&task.stop_time).unwrap(); let duration = stop_time - start_time; - total_time += duration.num_seconds(); + *imp.total_time.borrow_mut() += duration.num_seconds(); } } // Format total time to readable string - let h = total_time / 3600; - let m = total_time % 3600 / 60; - let s = total_time % 60; + let h = *imp.total_time.borrow() / 3600; + let m = *imp.total_time.borrow() % 3600 / 60; + let s = *imp.total_time.borrow() % 60; let mut total_time_str = format!("{:02}:{:02}:{:02}", h, m, s); if !settings_manager::get_bool("show-seconds") { @@ -142,5 +143,10 @@ impl FurTaskRow { let imp = imp::FurTaskRow::from_instance(&self); imp.tasks.lock().unwrap().to_vec() } + + pub fn get_total_time(&self) -> i64 { + let imp = imp::FurTaskRow::from_instance(&self); + *imp.total_time.borrow() + } } diff --git a/src/ui/tasks_group.rs b/src/ui/tasks_group.rs index 564ecb0..d6da39c 100755 --- a/src/ui/tasks_group.rs +++ b/src/ui/tasks_group.rs @@ -33,7 +33,9 @@ mod imp { pub struct FurTasksGroup { #[template_child] pub listbox_box: TemplateChild, + pub models: RefCell>, + pub day_total_time: RefCell, } #[glib::object_subclass] @@ -99,13 +101,18 @@ impl FurTasksGroup { for same_name in tasks_by_name { let listbox_row = FurTaskRow::new(); listbox_row.set_row_labels(same_name); + *imp.day_total_time.borrow_mut() += listbox_row.get_total_time(); listbox.append(&listbox_row); } listbox.connect_row_activated(move |_, row| { row.activate_action("task-row.open-details", None).unwrap(); }); + } + pub fn get_total_day_time(&self) -> i64 { + let imp = imp::FurTasksGroup::from_instance(&self); + *imp.day_total_time.borrow() } } diff --git a/src/ui/tasks_page.rs b/src/ui/tasks_page.rs index 82819c7..ac4e20e 100755 --- a/src/ui/tasks_page.rs +++ b/src/ui/tasks_page.rs @@ -149,8 +149,22 @@ impl FurTasksPage { } else { group.set_title(&uniq_date_list[i]); } + self.add(&group); group.add_task_model(tasks_sorted_by_day[i].clone()); + + // Set total time for each day + let day_total_time = group.get_total_day_time(); + // Format total time to readable string + let h = day_total_time / 3600; + let m = day_total_time % 3600 / 60; + let s = day_total_time % 60; + let mut total_time_str = format!("{:02}:{:02}:{:02}", h, m, s); + if !settings_manager::get_bool("show-seconds") { + total_time_str = format!("{:02}:{:02}", h, m); + } + group.set_description(Some(&total_time_str)); + imp.all_groups.borrow_mut().push(group); } } -- cgit 1.4.1