[gnome-todo] all-tasks-panel: Fix sorting function



commit 470b35351027cdd159f5078a3762c6572a50dcd0
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sat Sep 21 11:13:25 2019 -0300

    all-tasks-panel: Fix sorting function
    
    The gtd_task_compare() function checks first
    the task position. Every since we added custom
    positions and the migration paths, all tasks
    have a position. This breaks the All tasks panel.
    
    Fix the sorting function in the All tasks panel
    to check for the due date first.
    
    https://gitlab.gnome.org/GNOME/gnome-todo/issues/278

 plugins/all-tasks-panel/gtd-all-tasks-panel.c | 40 +++++++++++++++++++++++++--
 1 file changed, 37 insertions(+), 3 deletions(-)
---
diff --git a/plugins/all-tasks-panel/gtd-all-tasks-panel.c b/plugins/all-tasks-panel/gtd-all-tasks-panel.c
index 8f69575..43c267c 100644
--- a/plugins/all-tasks-panel/gtd-all-tasks-panel.c
+++ b/plugins/all-tasks-panel/gtd-all-tasks-panel.c
@@ -239,10 +239,44 @@ sort_func (gconstpointer a,
            gconstpointer b,
            gpointer      user_data)
 {
-  GtdTask *task_a = (GtdTask*) a;
-  GtdTask *task_b = (GtdTask*) b;
+  g_autoptr (GDateTime) dt1 = NULL;
+  g_autoptr (GDateTime) dt2 = NULL;
+  GtdTask *task1;
+  GtdTask *task2;
+  GDate dates[2];
+  gint result;
+
+  task1 = (GtdTask*) a;
+  task2 = (GtdTask*) b;
+
+  dt1 = gtd_task_get_due_date (task1);
+  dt2 = gtd_task_get_due_date (task2);
+
+  if (!dt1 && !dt2)
+    return gtd_task_compare (task1, task2);
+  else if (!dt1)
+    return 1;
+  else if (!dt2)
+    return -1;
+
+  g_date_clear (dates, 2);
+
+  g_date_set_dmy (&dates[0],
+                  g_date_time_get_day_of_month (dt1),
+                  g_date_time_get_month (dt1),
+                  g_date_time_get_year (dt1));
+
+  g_date_set_dmy (&dates[1],
+                  g_date_time_get_day_of_month (dt2),
+                  g_date_time_get_month (dt2),
+                  g_date_time_get_year (dt2));
+
+  result = g_date_days_between (&dates[1], &dates[0]);
+
+  if (result != 0)
+    return result;
 
-  return gtd_task_compare (task_a, task_b);
+  return gtd_task_compare (task1, task2);
 }
 
 static gboolean


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