[gnome-clocks] Move sorting in the model



commit 006a097c1b2a42600fe76eed82af5cdfbc51ef94
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Feb 8 15:57:39 2016 +0100

    Move sorting in the model

 src/alarm.vala   |    2 +-
 src/widgets.vala |   36 ++++++++++++++++--------------------
 src/world.vala   |   23 ++++++++++++-----------
 3 files changed, 29 insertions(+), 32 deletions(-)
---
diff --git a/src/alarm.vala b/src/alarm.vala
index d834f7e..efe18b7 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -667,7 +667,7 @@ public class Face : Gtk.Stack, Clocks.Clock {
             if (response == 1) {
                 var alarm = new Item ();
                 ((SetupDialog) dialog).apply_to_alarm (alarm);
-                alarms.append (alarm);
+                alarms.add (alarm);
                 alarm.reset();
                 save ();
             }
diff --git a/src/widgets.vala b/src/widgets.vala
index c6b7cf4..8115f46 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -270,6 +270,7 @@ public interface ContentItem : GLib.Object {
 
 public class ContentStore : GLib.Object, GLib.ListModel {
     private ListStore store;
+    private CompareDataFunc sort_func;
 
     public ContentStore () {
         store = new ListStore (typeof (ContentItem));
@@ -290,8 +291,20 @@ public class ContentStore : GLib.Object, GLib.ListModel {
         return store.get_item (position);
     }
 
-    public void append (ContentItem *item) {
-        store.append (item);
+    public void set_sorting(owned CompareDataFunc sort) {
+        sort_func = (owned) sort;
+
+        // TODO: we should re-sort, but for now we only
+        // set this before adding any item
+        assert (store.get_n_items () == 0);
+    }
+
+    public void add (ContentItem *item) {
+        if (sort_func == null) {
+            store.append (item);
+        } else {
+            store.insert_sorted (item, sort_func);
+        }
     }
 
     public delegate void ForeachFunc(ContentItem item);
@@ -346,11 +359,10 @@ public class ContentStore : GLib.Object, GLib.ListModel {
         foreach (var v in variant) {
             ContentItem? i = deserialize_item (v);
             if (i != null) {
-                store.append (i);
+                add (i);
             }
         }
     }
-
 }
 
 private class IconView : Gtk.IconView {
@@ -693,22 +705,6 @@ public class ContentView : Gtk.Bin {
             break;
         }
     }
-
-    public delegate int SortFunc(ContentItem item1, ContentItem item2);
-
-    public void set_sorting(Gtk.SortType sort_type, SortFunc sort_func) {
-        var sortable = icon_view.get_model () as Gtk.TreeSortable;
-        sortable.set_sort_column_id (0, sort_type);
-        sortable.set_sort_func (0, (model, iter1, iter2) => {
-            ContentItem item1;
-            ContentItem item2;
-
-            model.get (iter1, 0, out item1);
-            model.get (iter2, 0, out item2);
-
-            return sort_func (item1, item2);
-        });
-    }
 }
 
 public class AmPmToggleButton : Gtk.Button {
diff --git a/src/world.vala b/src/world.vala
index 5a9d188..c68ed28 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -263,6 +263,16 @@ public class Face : Gtk.Stack, Clocks.Clock {
         locations = new ContentStore ();
         settings = new GLib.Settings ("org.gnome.clocks");
 
+        locations.set_sorting((item1, item2) => {
+            var offset1 = ((Item) item1).location.get_timezone().get_offset();
+            var offset2 = ((Item) item2).location.get_timezone().get_offset();
+            if (offset1 < offset2)
+                return -1;
+            if (offset1 > offset2)
+                return 1;
+            return 0;
+        });
+
         day_pixbuf = Utils.load_image ("day.png");
         night_pixbuf = Utils.load_image ("night.png");
 
@@ -285,15 +295,6 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         content_view.bind_model (locations);
         content_view.set_header_bar (header_bar);
-        content_view.set_sorting(Gtk.SortType.ASCENDING, (item1, item2) => {
-            var offset1 = ((Item) item1).location.get_timezone().get_offset();
-            var offset2 = ((Item) item2).location.get_timezone().get_offset();
-            if (offset1 < offset2)
-                return -1;
-            if (offset1 > offset2)
-                return 1;
-            return 0;
-        });
 
         load ();
 
@@ -375,14 +376,14 @@ public class Face : Gtk.Stack, Clocks.Clock {
             item.automatic = true;
             item.selectable = false;
             item.title_icon = "find-location-symbolic";
-            locations.append (item);
+            locations.add (item);
         });
 
         yield geo_info.seek ();
     }
 
     private void add_location_item (Item item) {
-        locations.append (item);
+        locations.add (item);
         save ();
     }
 


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