[gnome-weather] Refactor adding and displaying of locations



commit 0742534d5bd1b85bdd197ff764c47076292e549a
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Sat Feb 28 20:41:16 2015 +0100

    Refactor adding and displaying of locations
    
    Remove the 'show-info' signal and all display related methods
    from the shared world model. And have explicit calls to the showInfo
    method on the window object used instead.
    
    This fixes a bug where adding a location to the shared model would
    show the new location on all open windows. Also make sure to only display
    a new current location when we are not displaying another location,
    or when we are displaying the previous current location.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=740414

 src/app/main.js     |    3 +-
 src/app/window.js   |   33 ++++++++++++++++---------
 src/app/world.js    |   21 ++++++++++------
 src/shared/world.js |   67 ++++++++++++++------------------------------------
 4 files changed, 55 insertions(+), 69 deletions(-)
---
diff --git a/src/app/main.js b/src/app/main.js
index fe85976..653c41e 100644
--- a/src/app/main.js
+++ b/src/app/main.js
@@ -60,7 +60,8 @@ const Application = new Lang.Class({
         let location = this.world.deserialize(parameter.deep_unpack());
         let win = this._createWindow();
 
-        this.model.addNewLocation(location, false);
+        let info = this.model.addNewLocation(location, false);
+        win.showInfo(info, false);
     },
 
     _initAppMenu: function() {
diff --git a/src/app/window.js b/src/app/window.js
index 062f696..083ddd2 100644
--- a/src/app/window.js
+++ b/src/app/window.js
@@ -41,7 +41,7 @@ const MainWindow = new Lang.Class({
         this.parent(params);
 
         this._world = this.application.world;
-        this._currentInfo = null;
+        this.currentInfo = null;
         this._currentPage = Page.SEARCH;
         this._pageWidgets = [[],[]];
 
@@ -63,16 +63,10 @@ const MainWindow = new Lang.Class({
         this._header.title = title;
         this._header.subtitle = subtitle;
 
-        this._worldView = new WorldView.WorldContentView(this.application, { visible: true });
+        this._worldView = new WorldView.WorldContentView(this.application, this, { visible: true });
         this._worldView.hide();
 
         this._model = this._worldView.model;
-        this._model.connect('show-info', Lang.bind(this, function(model, info) {
-            if (info)
-                this.showInfo(info);
-            else
-                this.showSearch(info);
-        }));
 
         let initialGrid = builder.get_object('initial-grid');
 
@@ -103,7 +97,7 @@ const MainWindow = new Lang.Class({
 
         let autoLocation = this.application.currentLocationController.autoLocation;
         if (!autoLocation)
-            this._model.showRecent();
+            this.showInfo(this._model.getRecent(), false);
     },
 
     update: function() {
@@ -111,8 +105,10 @@ const MainWindow = new Lang.Class({
     },
 
     _initialLocationChanged: function(entry) {
-        if (entry.location)
-            this._model.addNewLocation(entry.location, false);
+        if (entry.location) {
+            let info = this._model.addNewLocation(entry.location, false);
+            this.showInfo(info, false);
+        }
     },
 
     _getTitle: function() {
@@ -156,7 +152,20 @@ const MainWindow = new Lang.Class({
         this._goToPage(Page.SEARCH);
     },
 
-    showInfo: function(info) {
+    showInfo: function(info, isCurrentLocation) {
+        if (!info)
+            return;
+
+        /*
+         * Only show location updates if we have no loaded info or if we are
+         * currently showing the previous current location.
+         */
+        if (isCurrentLocation) {
+            if (this._cityView.info && !this._cityView.info._isCurrentLocation)
+                return;
+        }
+
+        this.currentInfo = info;
         this._cityView.info = info;
         this._cityView.disconnectClock();
 
diff --git a/src/app/world.js b/src/app/world.js
index 5d92895..ebcac30 100644
--- a/src/app/world.js
+++ b/src/app/world.js
@@ -31,7 +31,7 @@ const WorldContentView = new Lang.Class({
     Name: 'WorldContentView',
     Extends: Gtk.Popover,
 
-    _init: function(application, params) {
+    _init: function(application, window, params) {
         params = Params.fill(params, { hexpand: false, vexpand: false });
         this.parent(params);
 
@@ -44,6 +44,7 @@ const WorldContentView = new Lang.Class({
         this.add(grid);
 
         this.model = application.model;
+        this._window = window;
 
         this._listbox = builder.get_object('locations-list-box');
         this._listbox.set_header_func(function (row, previous) {
@@ -86,12 +87,13 @@ const WorldContentView = new Lang.Class({
 
         this._listbox.connect('row-activated', Lang.bind(this, function(listbox, row) {
             this.hide();
-            this.model.showInfo(row._info, row._isCurrentLocation);
+            this.model.moveLocationToFront(row._info);
+            this._window.showInfo(row._info, false);
         }));
 
-        this.model.connect('current-location-changed', Lang.bind(this, function(model, location) {
+        this.model.connect('current-location-changed', Lang.bind(this, function(model, info) {
             autoLocStack.visible_child_name = 'auto-location-switch-grid';
-            if (location) {
+            if (info.location) {
                 GObject.signal_handler_block(autoLocSwitch, handlerId);
                 autoLocSwitch.active = true;
                 GObject.signal_handler_unblock(autoLocSwitch, handlerId);
@@ -102,6 +104,8 @@ const WorldContentView = new Lang.Class({
 
                 autoLocSwitch.sensitive = false;
             }
+
+            this._window.showInfo(info, true);
         }));
 
         let stackPopover = builder.get_object('popover-stack');
@@ -128,14 +132,15 @@ const WorldContentView = new Lang.Class({
         }
     },
 
-    _filterListbox: function(row, model) {
-        return model.currentlyLoadedInfo == null ||
-            row._info != model.currentlyLoadedInfo;
+    _filterListbox: function(row, window) {
+        return window.currentInfo == null ||
+            row._info != window.currentInfo;
     },
 
     _locationChanged: function(entry) {
         if (entry.location) {
-            this.model.addNewLocation(entry.location, false);
+            let info = this.model.addNewLocation(entry.location, false);
+            this._window.showInfo(info, false);
             this.hide();
             entry.location = null;
         }
diff --git a/src/shared/world.js b/src/shared/world.js
index 4153668..3ed13ce 100644
--- a/src/shared/world.js
+++ b/src/shared/world.js
@@ -28,8 +28,7 @@ const WorldModel = new Lang.Class({
     Name: 'WorldModel',
     Extends: GObject.Object,
     Signals: {
-        'show-info': { param_types: [ GWeather.Info ] },
-        'current-location-changed': { param_types: [ GWeather.Location ] },
+        'current-location-changed': { param_types: [ GWeather.Info ] },
         'revalidate': { param_types: [ GWeather.Location ] },
         'location-added': { param_types: [ GWeather.Info, GObject.Boolean ] },
         'location-removed': { param_types: [ GWeather.Info ] }
@@ -50,7 +49,6 @@ const WorldModel = new Lang.Class({
 
         this._infoList = [];
 
-        this.currentlyLoadedInfo = null;
         this.addedCurrentLocation = false;
     },
 
@@ -68,37 +66,16 @@ const WorldModel = new Lang.Class({
 
     currentLocationChanged: function(location) {
         if (location) {
-            this.addNewLocation(location, true);
-        } else {
-            if (!this.addedCurrentLocation)
-                this.showRecent();
+            let info = this.addNewLocation(location, true);
+            this.emit('current-location-changed', info);
         }
-
-        this.emit('current-location-changed', location);
-    },
-
-    showInfo: function(info, isCurrentLocation) {
-        if (info != null &&
-            this._infoList[0] != info &&
-            this._infoList.length > 1) {
-            this._moveLocationToFront(info, isCurrentLocation);
-        }
-
-        this._showInfoInternal(info);
     },
 
-    _showInfoInternal: function(info) {
-        this.currentlyLoadedInfo = info;
-        this.emit('show-info', info);
-        if (info != null)
-            this.emit('revalidate', info.location);
-    },
-
-    showRecent: function(listbox) {
+    getRecent: function() {
         if (this._infoList.length > 0)
-            this.showInfo(this._infoList[0], false);
+            return this._infoList[0];
         else
-            this.showInfo(null, false);
+            return null;
     },
 
     load: function () {
@@ -116,9 +93,6 @@ const WorldModel = new Lang.Class({
 
             info = this._addLocationInternal(location, false);
         }
-
-        if (info)
-            this._showAddedLocation(info, false);
     },
 
     _updateLoadingCount: function(delta) {
@@ -154,17 +128,19 @@ const WorldModel = new Lang.Class({
             for (let info of this._infoList) {
                 let location = info.location;
                 if (location.equal(newLocation)) {
-                    this.showInfo(info, false);
-                    return;
+                    this.moveLocationToFront(info);
+                    return info;
                 }
             }
         }
 
         let info = this._addLocationInternal(newLocation, isCurrentLocation);
-        this._showAddedLocation(info, isCurrentLocation);
+        this.emit('revalidate', info.location);
 
         if (!isCurrentLocation)
             this._queueSaveSettings();
+
+        return info;
     },
 
     _queueSaveSettings: function() {
@@ -201,9 +177,12 @@ const WorldModel = new Lang.Class({
         this._saveSettingsInternal();
     },
 
-    _moveLocationToFront: function(info, isCurrentLocation) {
+    moveLocationToFront: function(info) {
+        if (this._infoList[0] == info || this._infoList.length == 0)
+            return;
+
         this._removeLocationInternal(info, true);
-        this._addInfoInternal(info, isCurrentLocation);
+        this._addInfoInternal(info, info._isCurrentLocation);
 
         // mark info as a manually chosen location so that we
         // save it
@@ -242,22 +221,14 @@ const WorldModel = new Lang.Class({
         this._infoList.unshift(info);
         this.updateInfo(info);
 
+        if (isCurrentLocation)
+            this.addedCurrentLocation = true;
+
         this.emit('location-added', info, isCurrentLocation);
 
         if (this._infoList.length > 5) {
             let oldInfo = this._infoList.pop();
             this._removeLocationInternal(oldInfo);
         }
-    },
-
-    _showAddedLocation: function(info, isCurrentLocation) {
-        if (isCurrentLocation) {
-            if(!this.addedCurrentLocation)
-                this._showInfoInternal(info);
-
-            this.addedCurrentLocation = true;
-        } else {
-            this._showInfoInternal(info);
-        }
     }
 });


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