[gnome-todo] task-list: Recursively add and remove subtasks



commit 31b7047c420d8424e18a11e89b0f2f58b7cfbd59
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Sep 12 20:07:57 2018 -0300

    task-list: Recursively add and remove subtasks
    
    Only adding the first level subtasks is not worth it.

 src/gtd-task-list.c | 56 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 34 insertions(+), 22 deletions(-)
---
diff --git a/src/gtd-task-list.c b/src/gtd-task-list.c
index 2207eab..96df848 100644
--- a/src/gtd-task-list.c
+++ b/src/gtd-task-list.c
@@ -147,6 +147,22 @@ add_task (GtdTaskList *self,
   return g_sequence_iter_get_position (iter);
 }
 
+static void
+recursively_add_subtasks (GtdTaskList *self,
+                          GtdTask     *task)
+{
+  GtdTask *aux;
+
+  for (aux = gtd_task_get_first_subtask (task);
+       aux;
+       aux = gtd_task_get_next_sibling (aux))
+    {
+      add_task (self, aux);
+
+      recursively_add_subtasks (self, aux);
+    }
+}
+
 static guint
 remove_task (GtdTaskList *self,
              GtdTask     *task)
@@ -172,6 +188,22 @@ remove_task (GtdTaskList *self,
   return position;
 }
 
+static void
+recursively_remove_subtasks (GtdTaskList *self,
+                             GtdTask     *task)
+{
+  GtdTask *aux;
+
+  for (aux = gtd_task_get_first_subtask (task);
+       aux;
+       aux = gtd_task_get_next_sibling (aux))
+    {
+      remove_task (self, aux);
+
+      recursively_remove_subtasks (self, aux);
+    }
+}
+
 
 /*
  * Callbacks
@@ -649,7 +681,6 @@ void
 gtd_task_list_add_task (GtdTaskList *self,
                         GtdTask     *task)
 {
-  GtdTask *aux;
   gint64 n_added;
   guint position;
 
@@ -661,20 +692,7 @@ gtd_task_list_add_task (GtdTaskList *self,
   position = add_task (self, task);
 
   /* Also remove subtasks */
-  for (aux = gtd_task_get_first_subtask (task);
-       aux;
-       aux = gtd_task_get_next_sibling (aux))
-    {
-      guint subtask_position;
-
-      subtask_position = add_task (self, aux);
-
-      /*
-       * Subtasks should never have a position bigger than
-       * their parent tasks.
-       */
-      g_assert (subtask_position > position);
-    }
+  recursively_add_subtasks (self, task);
 
   GTD_TRACE_MSG ("Adding %ld tasks at %u", n_added, position);
 
@@ -726,7 +744,6 @@ void
 gtd_task_list_remove_task (GtdTaskList *list,
                            GtdTask     *task)
 {
-  GtdTask *aux;
   gint64 n_removed;
   guint position;
 
@@ -738,12 +755,7 @@ gtd_task_list_remove_task (GtdTaskList *list,
   position = remove_task (list, task);
 
   /* Also remove subtasks */
-  for (aux = gtd_task_get_first_subtask (task);
-       aux;
-       aux = gtd_task_get_next_sibling (aux))
-    {
-      remove_task (list, aux);
-    }
+  recursively_remove_subtasks (list, task);
 
   GTD_TRACE_MSG ("Removing %ld tasks at %u", n_removed, position);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]