[gnome-weather] Refactor current location handling in the world view



commit 8a01da392a2f23d1ee5bb462c097042108fd2916
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Mon Mar 2 19:15:43 2015 -0800

    Refactor current location handling in the world view
    
    Let's clean up a bit the current location handling.
    Also fixes a couple of regression that were not caught in code
    review but I found in testing, and adds some testing code
    to the background service.

 src/app/window.js   |    1 +
 src/app/world.js    |   42 ++++++++++++++++++++++++++----------------
 src/service/main.js |    9 +++++++++
 src/shared/world.js |   38 ++++++++++++++++++++++++++------------
 4 files changed, 62 insertions(+), 28 deletions(-)
---
diff --git a/src/app/window.js b/src/app/window.js
index 083ddd2..78007b2 100644
--- a/src/app/window.js
+++ b/src/app/window.js
@@ -181,6 +181,7 @@ const MainWindow = new Lang.Class({
             this._cityView.infoPage.timeGrid.show();
         }
 
+        this._worldView.refilter();
         this._stack.set_visible_child(this._cityView);
         this._goToPage(Page.CITY);
     },
diff --git a/src/app/world.js b/src/app/world.js
index ebcac30..0de79d9 100644
--- a/src/app/world.js
+++ b/src/app/world.js
@@ -108,21 +108,13 @@ const WorldContentView = new Lang.Class({
             this._window.showInfo(info, true);
         }));
 
-        let stackPopover = builder.get_object('popover-stack');
-        this.model.connect('revalidate', Lang.bind(this, function() {
-            this._listbox.invalidate_filter();
+        this._stackPopover = builder.get_object('popover-stack');
+        this._listbox.set_filter_func(Lang.bind(this, this._filterListbox));
 
-            let children = this._listbox.get_children();
-            if (children.length == 1)
-                stackPopover.set_visible_child_name("search-grid");
-            else
-                stackPopover.set_visible_child_name("locations-grid");
-        }));
-
-        this._listbox.set_filter_func(Lang.bind(this, this._filterListbox, this.model));
         this.model.connect('location-added', Lang.bind(this, this._onLocationAdded));
         this.model.connect('location-removed', Lang.bind(this, this._onLocationRemoved));
 
+        this._currentLocationAdded = false;
         if (this.model.length > 0) {
             this.model.getAll().forEach(Lang.bind(this, function(info) {
                 this._onLocationAdded(this.model, info, info._isCurrentLocation);
@@ -132,9 +124,20 @@ const WorldContentView = new Lang.Class({
         }
     },
 
-    _filterListbox: function(row, window) {
-        return window.currentInfo == null ||
-            row._info != window.currentInfo;
+    refilter: function() {
+        this._listbox.invalidate_filter();
+    },
+
+    _syncStackPopover: function() {
+        if (this.model.length == 1)
+            this._stackPopover.set_visible_child_name("search-grid");
+        else
+            this._stackPopover.set_visible_child_name("locations-grid");
+    },
+
+    _filterListbox: function(row) {
+        return this._window.currentInfo == null ||
+            row._info != this._window.currentInfo;
     },
 
     _locationChanged: function(entry) {
@@ -194,15 +197,16 @@ const WorldContentView = new Lang.Class({
         row._isCurrentLocation = isCurrentLocation;
 
         if (isCurrentLocation) {
-            if (model.addedCurrentLocation) {
+            if (this._currentLocationAdded) {
                 let row0 = this._listbox.get_row_at_index(0);
                 if (row0)
                     row0.destroy();
             }
 
+            this._currentLocationAdded = true;
             this._listbox.insert(row, 0);
         } else {
-            if (model.addedCurrentLocation)
+            if (this._currentLocationAdded)
                 this._listbox.insert(row, 1);
             else
                 this._listbox.insert(row, 0);
@@ -215,6 +219,8 @@ const WorldContentView = new Lang.Class({
             tempLabel.label = info.get_temp_summary();
             image.icon_name = info.get_symbolic_icon_name();
         }));
+
+        this._syncStackPopover();
     },
 
     _onLocationRemoved: function(model, info) {
@@ -231,5 +237,9 @@ const WorldContentView = new Lang.Class({
             info.disconnect(info._updatedId);
             info._updatedId = 0;
         }
+        if (info._isCurrentLocation)
+            this._currentLocationAdded = false;
+
+        this._syncStackPopover();
     },
 });
diff --git a/src/service/main.js b/src/service/main.js
index ff67308..bf09bc1 100644
--- a/src/service/main.js
+++ b/src/service/main.js
@@ -49,6 +49,9 @@ const BackgroundService = new Lang.Class({
         GLib.set_application_name(_("Weather"));
 
         this._searchProvider = new SearchProvider.SearchProvider(this);
+
+        if (!pkg.moduledir.startsWith('resource://'))
+            this.debug = true;
     },
 
     _onQuit: function() {
@@ -79,6 +82,12 @@ const BackgroundService = new Lang.Class({
         this.model = new World.WorldModel(this.world, false);
         this.model.load();
 
+        if (this.debug) {
+            this.model.getAll().forEach(function(info) {
+                log(info.location.get_city_name());
+            });
+        }
+
         Util.initActions(this,
                          [{ name: 'quit',
                             activate: this._onQuit }]);
diff --git a/src/shared/world.js b/src/shared/world.js
index 3ed13ce..b8bdfa1 100644
--- a/src/shared/world.js
+++ b/src/shared/world.js
@@ -29,7 +29,6 @@ const WorldModel = new Lang.Class({
     Extends: GObject.Object,
     Signals: {
         '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 ] }
     },
@@ -47,28 +46,41 @@ const WorldModel = new Lang.Class({
 
         this._loadingCount = 0;
 
+        this._currentLocationInfo = null;
         this._infoList = [];
-
-        this.addedCurrentLocation = false;
     },
 
     get length() {
-        return this._infoList.length;
+        return this._infoList.length + (this._currentLocationInfo ? 1 : 0);
     },
 
     getAll: function() {
-        return [].concat(this._infoList);
+        if (this._currentLocationInfo)
+            return [this._currentLocationInfo].concat(this._infoList);
+        else
+            return [].concat(this._infoList);
     },
 
     getAtIndex: function(index) {
+        if (this._currentLocationInfo) {
+            if (index == 0)
+                return this._currentLocationInfo;
+            else
+                index--;
+        }
+
         return this._infoList[index];
     },
 
     currentLocationChanged: function(location) {
-        if (location) {
-            let info = this.addNewLocation(location, true);
-            this.emit('current-location-changed', info);
-        }
+        if (!location)
+            return;
+
+        if (this._currentLocationInfo)
+            this._removeLocationInternal(this._currentLocationInfo, false);
+
+        let info = this.addNewLocation(location, true);
+        this.emit('current-location-changed', info);
     },
 
     getRecent: function() {
@@ -135,7 +147,6 @@ const WorldModel = new Lang.Class({
         }
 
         let info = this._addLocationInternal(newLocation, isCurrentLocation);
-        this.emit('revalidate', info.location);
 
         if (!isCurrentLocation)
             this._queueSaveSettings();
@@ -178,7 +189,7 @@ const WorldModel = new Lang.Class({
     },
 
     moveLocationToFront: function(info) {
-        if (this._infoList[0] == info || this._infoList.length == 0)
+        if (this._infoList.length == 0 || this._infoList[0] == info)
             return;
 
         this._removeLocationInternal(info, true);
@@ -198,6 +209,9 @@ const WorldModel = new Lang.Class({
             this._updateLoadingCount(-1);
         }
 
+        if (oldInfo == this._currentLocationInfo)
+            this._currentLocationInfo = null;
+
         for (let i = 0; i < this._infoList.length; i++) {
             if (this._infoList[i] == oldInfo) {
                 this._infoList.splice(i, 1);
@@ -222,7 +236,7 @@ const WorldModel = new Lang.Class({
         this.updateInfo(info);
 
         if (isCurrentLocation)
-            this.addedCurrentLocation = true;
+            this._currentLocationInfo = info;
 
         this.emit('location-added', info, isCurrentLocation);
 


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