about summary refs log tree commit diff
path: root/src/ui/tasks_group.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/ui/tasks_group.rs')
-rw-r--r--src/ui/tasks_group.rs112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/ui/tasks_group.rs b/src/ui/tasks_group.rs
new file mode 100644
index 0000000..86c20dc
--- /dev/null
+++ b/src/ui/tasks_group.rs
@@ -0,0 +1,112 @@
+// Furtherance - Track your time without being tracked
+// Copyright (C) 2022  Ricky Kresslein <rk@lakoliu.com>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without 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 this program.  If not, see <https://www.gnu.org/licenses/>.
+
+use adw::subclass::prelude::*;
+use gtk::subclass::prelude::*;
+use gtk::{glib, prelude::*};
+
+use crate::ui::FurTaskRow;
+use crate::database;
+
+mod imp {
+    use super::*;
+    use glib::subclass;
+    use gtk::CompositeTemplate;
+
+    use std::cell::RefCell;
+
+    #[derive(Default, Debug, CompositeTemplate)]
+    #[template(resource = "/com/lakoliu/Furtherance/gtk/tasks_group.ui")]
+    pub struct FurTasksGroup {
+        #[template_child]
+        pub listbox_box: TemplateChild<gtk::Box>,
+        pub models: RefCell<Vec<gtk::SortListModel>>,
+    }
+
+    #[glib::object_subclass]
+    impl ObjectSubclass for FurTasksGroup {
+        const NAME: &'static str = "FurTasksGroup";
+        type ParentType = adw::PreferencesGroup;
+        type Type = super::FurTasksGroup;
+
+        fn class_init(klass: &mut Self::Class) {
+            Self::bind_template(klass);
+        }
+
+        fn instance_init(obj: &subclass::InitializingObject<Self>) {
+            obj.init_template();
+        }
+    }
+
+    impl ObjectImpl for FurTasksGroup {}
+    impl WidgetImpl for FurTasksGroup {}
+    impl PreferencesGroupImpl for FurTasksGroup {}
+}
+
+glib::wrapper! {
+    pub struct FurTasksGroup(
+        ObjectSubclass<imp::FurTasksGroup>)
+        @extends gtk::Widget, adw::PreferencesGroup;
+
+}
+
+impl FurTasksGroup {
+    pub fn new() -> Self {
+        glib::Object::new(&[]).expect("Failed to create `FurTaskGroup`.")
+    }
+
+    pub fn add_task_model(&self, tasks: Vec<database::Task>) {
+        let imp = imp::FurTasksGroup::from_instance(&self);
+
+        let listbox = gtk::ListBox::new();
+        listbox.add_css_class("content");
+        listbox.set_selection_mode(gtk::SelectionMode::None);
+        imp.listbox_box.append(&listbox);
+
+        // Check if tasks have the same name. If they do, make one listbox row for all of them.
+        // If they don't, move on.
+        let mut tasks_by_name: Vec<Vec<database::Task>> = Vec::new();
+        let mut unique: bool;
+
+        for task in &tasks {
+            unique = true;
+            for i in 0..tasks_by_name.len() {
+                if tasks_by_name[i][0].task_name == task.task_name {
+                    tasks_by_name[i].push(task.clone());
+                    unique = false;
+                }
+            }
+            if unique {
+                // Add unique task to list for group name
+                let mut new_name_list: Vec<database::Task> = Vec::new();
+                new_name_list.push(task.clone());
+                tasks_by_name.push(new_name_list);
+            }
+        }
+
+        for same_name in tasks_by_name {
+            let listbox_row = FurTaskRow::new();
+            listbox_row.set_row_labels(same_name);
+            listbox.append(&listbox_row);
+        }
+
+        listbox.connect_row_activated(move |_, row| {
+            row.activate_action("task-row.open-details", None).unwrap();
+        });
+
+    }
+}
+