[gnome-weather] Improving model-view split



commit 3ee417fcb136a24126976aac6c2bc8cb4d2a0d01
Author: Saurabh <srp201201051 gmail com>
Date:   Fri Aug 1 17:31:10 2014 +0530

    Improving model-view split
    
    https://bugzilla.gnome.org/show_bug.cgi?id=733236

 src/shared/world.js |  207 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 113 insertions(+), 94 deletions(-)
---
diff --git a/src/shared/world.js b/src/shared/world.js
index 27e49b5..461c0f8 100644
--- a/src/shared/world.js
+++ b/src/shared/world.js
@@ -40,7 +40,8 @@ const WorldModel = new Lang.Class({
         'no-cityview': { param_types: [] },
         'show-info': { param_types: [ GWeather.Info ] },
         'current-location-changed': { param_types: [ GWeather.Location ] },
-        'validate-listbox': { param_types: [ GWeather.Location ] }
+        'validate-listbox': { param_types: [ GWeather.Location ] },
+        'update-listbox': { param_types: [ GWeather.Location, GWeather.Info, GObject.Boolean ] }
     },
     Properties: {
         'loading': GObject.ParamSpec.boolean('loading', '', '', GObject.ParamFlags.READABLE, false)
@@ -66,6 +67,8 @@ const WorldModel = new Lang.Class({
         this.currentlyLoadedInfo = null;
 
         this.addedCurrentLocation = false;
+
+        this.numberOfLocations = 0;
     },
 
     currentLocationChanged: function(location) {
@@ -117,13 +120,24 @@ const WorldModel = new Lang.Class({
         }
     },
 
-    fillListbox: function (listbox) {
+    fillListbox: function () {
         let locations = this._settings.get_value('locations').deep_unpack();
+        this.numberOfLocations = locations.length;
         if (locations.length != 0) {
             for (let i = 0; i < locations.length && i < 5; i++) {
                 let variant = locations[i];
                 let location = this._world.deserialize(variant);
-                this._addLocationInternal(location, listbox, false);
+                let info = new GWeather.Info({ location: location,
+                                               enabled_providers: this._providers });
+
+                this._infoList[location.get_city_name()] = info;
+
+                info.connect('updated', Lang.bind(this, function(info) {
+                    this._updateLoadingCount(-1);
+                    this.emit('updated', info);
+                }));
+                this.updateInfo(info);
+                this.emit('update-listbox', location, info, false);
             }
         }
     },
@@ -162,75 +176,7 @@ const WorldModel = new Lang.Class({
         return this._loadingCount > 0;
     },
 
-    _addLocationInternal: function(location, listbox, isCurrentLocation) {
-        let grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
-                                  column_spacing: 12,
-                                  margin: 12 });
-
-        let name = location.get_city_name();
-        let locationGrid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
-                                          column_spacing: 12,
-                                          halign: Gtk.Align.START,
-                                          hexpand: true,
-                                          visible: true });
-        let locationLabel = new Gtk.Label({ label: name,
-                                            use_markup: true,
-                                            halign: Gtk.Align.START,
-                                            visible: true });
-        locationGrid.attach(locationLabel, 0, 0, 1, 1);
-        grid.attach(locationGrid, 0, 0, 1, 1);
-
-        let tempLabel = new Gtk.Label({ use_markup: true,
-                                        halign: Gtk.Align.END,
-                                        margin_start: 12,
-                                        visible: true });
-        grid.attach(tempLabel, 1, 0, 1, 1);
-
-        if (isCurrentLocation) {
-            let image = new Gtk.Image({ icon_size: Gtk.IconSize.LARGE_TOOLBAR,
-                                        icon_name: 'mark-location-symbolic',
-                                        use_fallback: true,
-                                        halign: Gtk.Align.START,
-                                        visible: true });
-            locationGrid.attach(image, 1, 0, 1, 1);
-        }
-
-        let image = new Gtk.Image({ icon_size: Gtk.IconSize.LARGE_TOOLBAR,
-                                    use_fallback: true,
-                                    halign: Gtk.Align.END,
-                                    visible: true });
-        grid.attach(image, 2, 0, 1, 1);
-
-        grid.show();
-        if(isCurrentLocation) {
-            if (this.addedCurrentLocation) {
-                let children = listbox.get_children();
-                children[0].destroy();
-            }
-            listbox.insert(grid, 0);
-        } else {
-            if (this.addedCurrentLocation)
-                listbox.insert(grid, 1);
-            else
-                listbox.insert(grid, 0);
-        }
-
-        let info = new GWeather.Info({ location: location,
-                                       enabled_providers: this._providers });
-
-        this._infoList[locationLabel.get_label()] = info;
-
-        info.connect('updated', Lang.bind(this, function(info) {
-            tempLabel.label = info.get_temp_summary();
-            image.icon_name = info.get_symbolic_icon_name();
-
-            this._updateLoadingCount(-1);
-            this.emit('updated', info);
-        }));
-        this.updateInfo(info);
-    },
-
-    addNewLocation: function(newLocation, listbox, isCurrentLocation) {
+    addNewLocation: function(newLocation, isCurrentLocation) {
         if (newLocation) {
             let locations = this._settings.get_value('locations').deep_unpack();
 
@@ -247,15 +193,23 @@ const WorldModel = new Lang.Class({
 
                 locations.push(newLocation.serialize());
 
-                if (locations.length > 5) {
-                    let children = listbox.get_children();
-                    children[children.length-1].destroy();
-                }
+                this.numberOfLocations = locations.length;
 
                 this._settings.set_value('locations', new GLib.Variant('av', locations));
             }
 
-            this._addLocationInternal(newLocation, listbox, isCurrentLocation);
+            let info = new GWeather.Info({ location: newLocation,
+                                           enabled_providers: this._providers });
+
+            this._infoList[newLocation.get_city_name()] = info;
+
+            info.connect('updated', Lang.bind(this, function(info) {
+                this._updateLoadingCount(-1);
+                this.emit('updated', info);
+            }));
+            this.updateInfo(info);
+
+            this.emit('update-listbox', newLocation, info, isCurrentLocation);
             if (!isCurrentLocation) {
                 this.emit('show-info', this._infoList[newLocation.get_city_name()]);
                 this.currentlyLoadedInfo = this._infoList[newLocation.get_city_name()];
@@ -295,8 +249,8 @@ const WorldContentView = new Lang.Class({
 
         this.model = application.model;
 
-        let listbox = builder.get_object('locations-list-box');
-        listbox.set_header_func(function (row, previous) {
+        this._listbox = builder.get_object('locations-list-box');
+        this._listbox.set_header_func(function (row, previous) {
             let hasHeader = row.get_header() != null;
             let shouldHaveHeader = previous != null;
             if (hasHeader != shouldHaveHeader) {
@@ -309,20 +263,18 @@ const WorldContentView = new Lang.Class({
 
         let initialGridLocEntry = builder.get_object('initial-grid-location-entry');
         initialGridLocEntry.connect('notify::location', Lang.bind(this, function(entry) {
-            this._locationChanged(entry, listbox);
+            this._locationChanged(entry);
         }));
 
         let locationEntry = builder.get_object('location-entry');
         locationEntry.connect('notify::location', Lang.bind(this, function(entry) {
-            this._locationChanged(entry, listbox)
+            this._locationChanged(entry)
         }));
 
-        this.model.fillListbox(listbox);
-
         this.connect('notify::visible', Lang.bind(this, function() {
-            listbox.set_selection_mode(0);
+            this._listbox.set_selection_mode(0);
             locationEntry.grab_focus();
-            listbox.set_selection_mode(1);
+            this._listbox.set_selection_mode(1);
         }));
 
         let autoLocStack = builder.get_object('auto-location-stack');
@@ -349,7 +301,7 @@ const WorldContentView = new Lang.Class({
             GObject.signal_handler_unblock(autoLocSwitch, handlerId);
         }
 
-        listbox.connect('row-activated', Lang.bind(this, function(listbox, row) {
+        this._listbox.connect('row-activated', Lang.bind(this, function(listbox, row) {
             this.hide();
             this.model.rowActivated(listbox, row);
         }));
@@ -357,14 +309,14 @@ const WorldContentView = new Lang.Class({
         this.model.connect('current-location-changed', Lang.bind(this, function(model, location) {
             autoLocStack.set_visible_child_name('auto-location-switch-grid');
             if (location) {
-                this.model.addNewLocation(location, listbox, true);
+                this.model.addNewLocation(location, true);
 
                 GObject.signal_handler_block(autoLocSwitch, handlerId);
                 autoLocSwitch.set_active(true);
                 GObject.signal_handler_unblock(autoLocSwitch, handlerId);
             } else {
                 if (!this.model.addedCurrentLocation)
-                    this.model.showRecent(listbox);
+                    this.model.showRecent(this._listbox);
 
                 GObject.signal_handler_block(autoLocSwitch, handlerId);
                 autoLocSwitch.set_active(false);
@@ -375,8 +327,8 @@ const WorldContentView = new Lang.Class({
         }));
 
         this.model.connect('validate-listbox', Lang.bind(this, function() {
-            listbox.invalidate_filter();
-            let children = listbox.get_children();
+            this._listbox.invalidate_filter();
+            let children = this._listbox.get_children();
             if (children.length == 1) {
                 stackPopover.set_visible_child_name("search-grid");
                 return;
@@ -384,7 +336,11 @@ const WorldContentView = new Lang.Class({
             stackPopover.set_visible_child_name("locations-grid");
         }));
 
-        listbox.set_filter_func(Lang.bind(this, this._filterListbox, this.model));
+        this._listbox.set_filter_func(Lang.bind(this, this._filterListbox, this.model));
+
+        this.model.connect('update-listbox', Lang.bind(this, this._addLocationInternal));
+
+        this.model.fillListbox();
     },
 
     _filterListbox: function(row, model) {
@@ -396,11 +352,74 @@ const WorldContentView = new Lang.Class({
         return true;
     },
 
-    _locationChanged: function(entry, listbox) {
+    _locationChanged: function(entry) {
         if (entry.location) {
-            this.model.addNewLocation(entry.location, listbox, false);
+            this.model.addNewLocation(entry.location, false);
             this.hide();
             entry.location = null;
         }
-    }
+    },
+
+    _addLocationInternal: function(model, location, info, isCurrentLocation) {
+        let grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
+                                  column_spacing: 12,
+                                  margin: 12 });
+
+        let name = location.get_city_name();
+        let locationGrid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
+                                          column_spacing: 12,
+                                          halign: Gtk.Align.START,
+                                          hexpand: true,
+                                          visible: true });
+        let locationLabel = new Gtk.Label({ label: name,
+                                            use_markup: true,
+                                            halign: Gtk.Align.START,
+                                            visible: true });
+        locationGrid.attach(locationLabel, 0, 0, 1, 1);
+        grid.attach(locationGrid, 0, 0, 1, 1);
+
+        let tempLabel = new Gtk.Label({ use_markup: true,
+                                        halign: Gtk.Align.END,
+                                        margin_start: 12,
+                                        visible: true });
+        grid.attach(tempLabel, 1, 0, 1, 1);
+
+        if (isCurrentLocation) {
+            let image = new Gtk.Image({ icon_size: Gtk.IconSize.LARGE_TOOLBAR,
+                                        icon_name: 'mark-location-symbolic',
+                                        use_fallback: true,
+                                        halign: Gtk.Align.START,
+                                        visible: true });
+            locationGrid.attach(image, 1, 0, 1, 1);
+        }
+
+        let image = new Gtk.Image({ icon_size: Gtk.IconSize.LARGE_TOOLBAR,
+                                    use_fallback: true,
+                                    halign: Gtk.Align.END,
+                                    visible: true });
+        grid.attach(image, 2, 0, 1, 1);
+
+        grid.show();
+        if(isCurrentLocation) {
+            if (model.addedCurrentLocation) {
+                let children = this._listbox.get_children();
+                children[0].destroy();
+            }
+            this._listbox.insert(grid, 0);
+        } else {
+            if (model.addedCurrentLocation)
+                this._listbox.insert(grid, 1);
+            else
+                this._listbox.insert(grid, 0);
+        }
+        if (model.numberOfLocations > 5) {
+            let children = this._listbox.get_children();
+            children[children.length-1].destroy();
+        }
+
+        info.connect('updated', Lang.bind(this, function(info) {
+            tempLabel.label = info.get_temp_summary();
+            image.icon_name = info.get_symbolic_icon_name();
+        }));
+    },
 });


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