about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRicky Kresslein <rk@lakoliu.com>2022-04-14 17:44:21 +0300
committerRicky Kresslein <rk@lakoliu.com>2022-04-14 17:44:21 +0300
commiteb80833b5d736180baa49c7f3787fe30eba161db (patch)
treef4fbc30b8f80002a57176d9d77a414160b16f5c0
parentdd7f8f8a6462b81cadf9f5f2a0b2a31757c59c18 (diff)
downloadFurtherance-eb80833b5d736180baa49c7f3787fe30eba161db.tar.zst
Add daily time sums (Issue #17)
-rwxr-xr-xsrc/ui/task_row.rs18
-rwxr-xr-xsrc/ui/tasks_group.rs7
-rwxr-xr-xsrc/ui/tasks_page.rs14
3 files changed, 33 insertions, 6 deletions
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<gtk::Label>,
         #[template_child]
         pub total_time_label: TemplateChild<gtk::Label>,
-        // pub tasks: Vec<database::Task>,
+
         pub tasks: Lazy<Mutex<Vec<Task>>>,
+        pub total_time: RefCell<i64>,
     }
 
     #[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<gtk::Box>,
+
         pub models: RefCell<Vec<gtk::SortListModel>>,
+        pub day_total_time: RefCell<i64>,
     }
 
     #[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);
         }
     }