← Back to team overview

ubuntu-touch-coreapps-reviewers team mailing list archive

[Merge] lp:~vthompson/ubuntu-weather-app/reboot-revert-worker into lp:ubuntu-weather-app/reboot

 

Victor Thompson has proposed merging lp:~vthompson/ubuntu-weather-app/reboot-revert-worker into lp:ubuntu-weather-app/reboot.

Commit message:
* Revert WorkerScript, but still initialize app with stored data.

Requested reviews:
  Ubuntu Weather Developers (ubuntu-weather-dev)
Related bugs:
  Bug #1432260 in Ubuntu Weather App: "Crashes while trying to add a new location on N4"
  https://bugs.launchpad.net/ubuntu-weather-app/+bug/1432260

For more details, see:
https://code.launchpad.net/~vthompson/ubuntu-weather-app/reboot-revert-worker/+merge/253050

* Revert WorkerScript, but still initialize app with stored data.
-- 
Your team Ubuntu Weather Developers is requested to review the proposed merge of lp:~vthompson/ubuntu-weather-app/reboot-revert-worker into lp:ubuntu-weather-app/reboot.
=== modified file 'app/data/WeatherApi.js'
--- app/data/WeatherApi.js	2015-03-14 10:55:19 +0000
+++ app/data/WeatherApi.js	2015-03-16 13:47:39 +0000
@@ -671,80 +671,74 @@
     "geoip": GeoipApi
 });
 
-/**
-*  following WorkerScript handles the data requests against the weather API.
-*  "message" requires a "params" property with the required params to perform
-*  the API call and an "action" property, which will be added also to the response.
-*/
-
-if(typeof WorkerScript != "undefined") {
-    WorkerScript.onMessage = function(message) {
-        // handles the response data
-        var finished = function(result) {
-            // print result to get data for test json files
-            // print(JSON.stringify(result));
-            WorkerScript.sendMessage({
-                action: message.action,
-                result: result
+var sendRequest = function(message, responseCallback) {
+    // handles the response data
+    var finished = function(result) {
+        // print result to get data for test json files
+        // print(JSON.stringify(result));
+        //WorkerScript.sendMessage({
+        responseCallback({
+            action: message.action,
+            result: result
+        })
+    }
+    // handles errors
+    var onError = function(err) {
+        console.log(JSON.stringify(err, null, true));
+        //WorkerScript.sendMessage({ 'error': err})
+        responseCallback({ 'error': err})
+    }
+    // keep order of locations, sort results
+    var sortDataResults = function(locA, locB) {
+        return locA.db.id - locB.db.id;
+    }
+    // perform the api calls
+    if(message.action === "searchByName") {
+        WeatherApi.search("name", message.params, finished, onError);
+    } else if(message.action === "searchByPoint") {
+        WeatherApi.search("point", message.params, finished, onError);
+    } else if(message.action === "getGeoIp") {
+        WeatherApi.geoLookup(message.params, finished, onError);
+    } else if(message.action === "updateData") {
+        var locLength = message.params.locations.length,
+            locUpdated = 0,
+            result = [],
+            now = new Date().getTime();
+        if(locLength > 0) {
+            message.params.locations.forEach(function(loc) {
+                var updatedHnd = function (newData, cached) {
+                        locUpdated += 1;
+                        if(cached === true) {
+                            newData["save"] = false;
+                        } else {
+                            newData["save"] = true;
+                            newData["updated"] =  new Date().getTime();
+                        }
+                        result.push(newData);
+                        if(locUpdated === locLength) {
+                            result.sort(sortDataResults);
+                            finished(result);
+                        }
+                    },
+                    params = {
+                        location:loc.location,
+                        db: loc.db,
+                        units: 'metric',
+                        service: message.params.service,
+                        api_key: message.params.api_key,
+                        interval: message.params.interval
+                    },
+                    secsFromLastFetch = (now-loc.updated)/1000;
+                if( message.params.force===true || loc.format !== RESPONSE_DATA_VERSION || secsFromLastFetch > params.interval){
+                    // data older than 30min, location is new or data format is deprecated
+                    WeatherApi.getLocationData(params, updatedHnd, onError);
+                } else {
+                    console.log("["+loc.location.name+"] returning cached data, time from last fetch: "+secsFromLastFetch)
+                    updatedHnd(loc, true);
+                }
             })
-        }
-        // handles errors
-        var onError = function(err) {
-            console.log(JSON.stringify(err, null, true));
-            WorkerScript.sendMessage({ 'error': err})
-        }
-        // keep order of locations, sort results
-        var sortDataResults = function(locA, locB) {
-            return locA.db.id - locB.db.id;
-        }
-        // perform the api calls
-        if(message.action === "searchByName") {
-            WeatherApi.search("name", message.params, finished, onError);
-        } else if(message.action === "searchByPoint") {
-            WeatherApi.search("point", message.params, finished, onError);
-        } else if(message.action === "getGeoIp") {
-            WeatherApi.geoLookup(message.params, finished, onError);
-        } else if(message.action === "updateData") {
-            var locLength = message.params.locations.length,
-                locUpdated = 0,
-                result = [],
-                now = new Date().getTime();
-            if(locLength > 0) {
-                message.params.locations.forEach(function(loc) {
-                    var updatedHnd = function (newData, cached) {
-                            locUpdated += 1;
-                            if(cached === true) {
-                                newData["save"] = false;
-                            } else {
-                                newData["save"] = true;
-                                newData["updated"] =  new Date().getTime();
-                            }
-                            result.push(newData);
-                            if(locUpdated === locLength) {
-                                result.sort(sortDataResults);
-                                finished(result);
-                            }
-                        },
-                        params = {
-                            location:loc.location,
-                            db: loc.db,
-                            units: 'metric',
-                            service: message.params.service,
-                            api_key: message.params.api_key,
-                            interval: message.params.interval
-                        },
-                        secsFromLastFetch = (now-loc.updated)/1000;
-                    if( message.params.force===true || loc.format !== RESPONSE_DATA_VERSION || secsFromLastFetch > params.interval){
-                        // data older than 30min, location is new or data format is deprecated
-                        WeatherApi.getLocationData(params, updatedHnd, onError);
-                    } else {
-                        console.log("["+loc.location.name+"] returning cached data, time from last fetch: "+secsFromLastFetch)
-                        updatedHnd(loc, true);
-                    }
-                })
-            } else {
-                finished(result);
-            }
+        } else {
+            finished(result);
         }
     }
 }

=== modified file 'app/ubuntu-weather-app.qml'
--- app/ubuntu-weather-app.qml	2015-03-14 15:17:33 +0000
+++ app/ubuntu-weather-app.qml	2015-03-16 13:47:39 +0000
@@ -21,6 +21,7 @@
 import Ubuntu.Components 1.1
 import "components"
 import "data" as Data
+import "data/WeatherApi.js" as WeatherApi
 import "data/key.js" as Key
 import "ui"
 
@@ -65,26 +66,22 @@
       Handle response data from data backend. Checks if a location
       was updated and has to be stored again.
     */
-    WorkerScript {
-         id: lookupWorker
-         source: Qt.resolvedUrl("./data/WeatherApi.js")
-         onMessage: {
-             if(!messageObject.error) {
-                 if(messageObject.action === "updateData") {
-                     messageObject.result.forEach(function(loc) {
-                         // replace location data in cache with refreshed values
-                         if(loc["save"] === true) {
-                             storage.updateLocation(loc.db.id, loc);
-                         }
-                     });
-                     //print(JSON.stringify(messageObject.result));
-                     fillPages(messageObject.result);
-                 }
-             } else {
-                 console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
-                 // TODO error handling
+    function responseDataHandler(messageObject) {
+         if(!messageObject.error) {
+             if(messageObject.action === "updateData") {
+                 messageObject.result.forEach(function(loc) {
+                     // replace location data in cache with refreshed values
+                     if(loc["save"] === true) {
+                         storage.updateLocation(loc.db.id, loc);
+                     }
+                 });
+                 //print(JSON.stringify(messageObject.result));
+                 fillPages(messageObject.result);
              }
-        }
+         } else {
+             console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
+             // TODO error handling
+         }
      }
 
     /* Fill the location pages with their data. */
@@ -104,7 +101,7 @@
             storage.getLocations(fillPages);
         } else {
             storage.getLocations(function(locations) {
-                 lookupWorker.sendMessage({
+                WeatherApi.sendRequest({
                     action: "updateData",
                     params: {
                         locations: locations,
@@ -113,7 +110,7 @@
                         api_key: Key.twcKey,
                         interval: settings.refreshInterval
                     }
-                })
+                }, responseDataHandler)
             });
         }
     }

=== modified file 'app/ui/AddLocationPage.qml'
--- app/ui/AddLocationPage.qml	2015-03-14 10:55:19 +0000
+++ app/ui/AddLocationPage.qml	2015-03-16 13:47:39 +0000
@@ -122,28 +122,23 @@
     function loadFromProvider(search) {
         clearModelForLoading()
 
-        lookupWorker.sendMessage({
+        WeatherApi.sendRequest({
                                    action: "searchByName",
                                    params: {
                                        name: search,
                                        units: "metric"
                                    }
-                               });
+                               }, searchResponseHandler)
     }
 
-
-    WorkerScript {
-        id: lookupWorker
-        source: Qt.resolvedUrl("../data/WeatherApi.js")
-        onMessage: {
-            if (!messageObject.error) {
-                appendCities(messageObject.result.locations)
-            } else {
-                citiesModel.httpError = true
-            }
-
-            citiesModel.loading = false
+    function searchResponseHandler(msgObject) {
+        if (!msgObject.error) {
+            appendCities(msgObject.result.locations)
+        } else {
+            citiesModel.httpError = true
         }
+
+        citiesModel.loading = false
     }
 
     ListView {

=== modified file 'po/com.ubuntu.weather.pot'
--- po/com.ubuntu.weather.pot	2015-03-14 10:55:19 +0000
+++ po/com.ubuntu.weather.pot	2015-03-16 13:47:39 +0000
@@ -8,7 +8,7 @@
 msgstr ""
 "Project-Id-Version: ubuntu-weather-app\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2015-03-14 11:54+0100\n"
+"POT-Creation-Date: 2015-03-06 15:08+0100\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@xxxxxx>\n"
@@ -53,19 +53,19 @@
 msgid "Search city"
 msgstr ""
 
-#: ../app/ui/AddLocationPage.qml:254
+#: ../app/ui/AddLocationPage.qml:249
 msgid "No city found"
 msgstr ""
 
-#: ../app/ui/AddLocationPage.qml:267
+#: ../app/ui/AddLocationPage.qml:262
 msgid "Couldn't load weather data, please try later again!"
 msgstr ""
 
-#: ../app/ui/AddLocationPage.qml:277
+#: ../app/ui/AddLocationPage.qml:272
 msgid "Location already added."
 msgstr ""
 
-#: ../app/ui/AddLocationPage.qml:280
+#: ../app/ui/AddLocationPage.qml:275
 msgid "OK"
 msgstr ""
 


References