[recipes] Sort on the cuisine page as well



commit c5e8d6e089fbc0bfc8a3226a07b13a9ce1243ed7
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Jun 26 23:34:27 2017 -0400

    Sort on the cuisine page as well

 src/gr-cuisine-page.c |   53 +++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 49 insertions(+), 4 deletions(-)
---
diff --git a/src/gr-cuisine-page.c b/src/gr-cuisine-page.c
index 4bd6d98..c68d7d1 100644
--- a/src/gr-cuisine-page.c
+++ b/src/gr-cuisine-page.c
@@ -31,6 +31,8 @@
 #include "gr-recipe-tile.h"
 #include "gr-utils.h"
 #include "gr-meal.h"
+#include "gr-list-page.h"
+#include "gr-settings.h"
 
 
 typedef struct
@@ -117,9 +119,9 @@ cuisine_page_finalize (GObject *object)
 }
 
 static int
-sort_func (GtkFlowBoxChild *child1,
-           GtkFlowBoxChild *child2,
-           gpointer         data)
+name_sort_func (GtkFlowBoxChild *child1,
+                GtkFlowBoxChild *child2,
+                 gpointer         data)
 {
         GtkWidget *tile1 = gtk_bin_get_child (GTK_BIN (child1));
         GtkWidget *tile2 = gtk_bin_get_child (GTK_BIN (child2));
@@ -131,6 +133,47 @@ sort_func (GtkFlowBoxChild *child1,
         return strcmp (name1, name2);
 }
 
+static int
+date_sort_func (GtkFlowBoxChild *child1,
+                GtkFlowBoxChild *child2,
+                gpointer         data)
+{
+        GtkWidget *tile1 = gtk_bin_get_child (GTK_BIN (child1));
+        GtkWidget *tile2 = gtk_bin_get_child (GTK_BIN (child2));
+        GrRecipe *recipe1 = gr_recipe_tile_get_recipe (GR_RECIPE_TILE (tile1));
+        GrRecipe *recipe2 = gr_recipe_tile_get_recipe (GR_RECIPE_TILE (tile2));
+        GDateTime *mtime1 = gr_recipe_get_mtime (recipe1);
+        GDateTime *mtime2 = gr_recipe_get_mtime (recipe2);
+
+        return g_date_time_compare (mtime2, mtime1);
+}
+
+static void
+set_sort (GtkFlowBox *box,
+          GrSortKey   sort)
+{
+        switch (sort) {
+        case SORT_BY_NAME:
+                gtk_flow_box_set_sort_func (box, name_sort_func, NULL, NULL);
+                break;
+        case SORT_BY_RECENCY:
+                gtk_flow_box_set_sort_func (box, date_sort_func, NULL, NULL);
+                break;
+        default:
+                g_assert_not_reached ();
+        }
+}
+
+static void
+sort_key_changed (GtkFlowBox *box)
+{
+        if (!gtk_widget_get_visible (gtk_widget_get_ancestor (GTK_WIDGET (box), GR_TYPE_CUISINE_PAGE)))
+                return;
+
+        set_sort (box, g_settings_get_enum (gr_settings_get (), "sort-key"));
+}
+
+
 static void
 populate_initially (GrCuisinePage *self)
 {
@@ -169,7 +212,9 @@ populate_initially (GrCuisinePage *self)
                 gtk_flow_box_set_max_children_per_line (GTK_FLOW_BOX (box), 3);
                 gtk_widget_show (box);
                 gtk_container_add (GTK_CONTAINER (self->category_box), box);
-                gtk_flow_box_set_sort_func (GTK_FLOW_BOX (box), sort_func, self, NULL);
+
+                g_signal_connect_swapped (gr_settings_get (), "changed::sort-key", G_CALLBACK 
(sort_key_changed), box);
+                g_signal_connect_swapped (self, "notify::visible", G_CALLBACK (sort_key_changed), box);
 
 
                 self->categories[i].name = names[i];


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