← Back to team overview

ubuntu-touch-coreapps-reviewers team mailing list archive

[Merge] lp:~martin-borho/ubuntu-weather-app/reboot-worker into lp:ubuntu-weather-app/reboot

 

Martin Borho has proposed merging lp:~martin-borho/ubuntu-weather-app/reboot-worker into lp:ubuntu-weather-app/reboot.

Commit message:
To prevent a blank app at startup:

* reimplemented worker calls for data requests
* starts up the app with stored data and spins off a refresh request in a worker


Requested reviews:
  Ubuntu Weather Developers (ubuntu-weather-dev)

For more details, see:
https://code.launchpad.net/~martin-borho/ubuntu-weather-app/reboot-worker/+merge/252972

To prevent a blank app at startup:

* reimplemented worker calls for data requests
* starts up the app with stored data and spins off a refresh request in a worker

See https://code.launchpad.net/~vthompson/ubuntu-weather-app/reboot-get-initial-data-from-storage/+merge/252962
-- 
Your team Ubuntu Weather Developers is requested to review the proposed merge of lp:~martin-borho/ubuntu-weather-app/reboot-worker into lp:ubuntu-weather-app/reboot.
=== modified file 'app/data/WeatherApi.js'
--- app/data/WeatherApi.js	2015-03-08 17:23:12 +0000
+++ app/data/WeatherApi.js	2015-03-14 15:20:24 +0000
@@ -671,74 +671,80 @@
     "geoip": GeoipApi
 });
 
-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);
-                }
+/**
+*  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
             })
-        } else {
-            finished(result);
+        }
+        // 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);
+            }
         }
     }
 }

=== modified file 'app/ubuntu-weather-app.qml'
--- app/ubuntu-weather-app.qml	2015-03-08 17:23:58 +0000
+++ app/ubuntu-weather-app.qml	2015-03-14 15:20:24 +0000
@@ -21,7 +21,6 @@
 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"
 
@@ -57,28 +56,35 @@
     /*
       (re)load the pages on completion
     */
-    Component.onCompleted: refreshData();
+    Component.onCompleted: {
+        storage.getLocations(fillPages);
+        refreshData();
+    }
 
     /*
       Handle response data from data backend. Checks if a location
       was updated and has to be stored again.
     */
-    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);
+    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
              }
-         } else {
-             console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
-             // TODO error handling
-         }
+        }
      }
 
     /* Fill the location pages with their data. */
@@ -98,7 +104,7 @@
             storage.getLocations(fillPages);
         } else {
             storage.getLocations(function(locations) {
-                WeatherApi.sendRequest({
+                 lookupWorker.sendMessage({
                     action: "updateData",
                     params: {
                         locations: locations,
@@ -107,7 +113,7 @@
                         api_key: Key.twcKey,
                         interval: settings.refreshInterval
                     }
-                }, responseDataHandler)
+                })
             });
         }
     }

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

=== modified file 'po/com.ubuntu.weather.pot'
--- po/com.ubuntu.weather.pot	2015-03-06 14:15:29 +0000
+++ po/com.ubuntu.weather.pot	2015-03-14 15:20:24 +0000
@@ -8,7 +8,7 @@
 msgstr ""
 "Project-Id-Version: ubuntu-weather-app\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2015-03-06 15:08+0100\n"
+"POT-Creation-Date: 2015-03-14 11:54+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:249
+#: ../app/ui/AddLocationPage.qml:254
 msgid "No city found"
 msgstr ""
 
-#: ../app/ui/AddLocationPage.qml:262
+#: ../app/ui/AddLocationPage.qml:267
 msgid "Couldn't load weather data, please try later again!"
 msgstr ""
 
-#: ../app/ui/AddLocationPage.qml:272
+#: ../app/ui/AddLocationPage.qml:277
 msgid "Location already added."
 msgstr ""
 
-#: ../app/ui/AddLocationPage.qml:275
+#: ../app/ui/AddLocationPage.qml:280
 msgid "OK"
 msgstr ""
 


Follow ups