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

Proposed by Victor Thompson
Status: Merged
Approved by: Martin Borho
Approved revision: 23
Merged at revision: 22
Proposed branch: lp:~vthompson/ubuntu-weather-app/reboot-revert-worker
Merge into: lp:ubuntu-weather-app
Diff against target: 301 lines (+99/-113)
4 files modified
app/data/WeatherApi.js (+67/-73)
app/ubuntu-weather-app.qml (+18/-21)
app/ui/AddLocationPage.qml (+9/-14)
po/com.ubuntu.weather.pot (+5/-5)
To merge this branch: bzr merge lp:~vthompson/ubuntu-weather-app/reboot-revert-worker
Reviewer Review Type Date Requested Status
Martin Borho Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Review via email: mp+253050@code.launchpad.net

This proposal supersedes a proposal from 2015-03-16.

Commit message

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

Description of the change

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

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Martin Borho (martin-borho) wrote :

Thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'app/data/WeatherApi.js'
2--- app/data/WeatherApi.js 2015-03-14 10:55:19 +0000
3+++ app/data/WeatherApi.js 2015-03-16 13:47:39 +0000
4@@ -671,80 +671,74 @@
5 "geoip": GeoipApi
6 });
7
8-/**
9-* following WorkerScript handles the data requests against the weather API.
10-* "message" requires a "params" property with the required params to perform
11-* the API call and an "action" property, which will be added also to the response.
12-*/
13-
14-if(typeof WorkerScript != "undefined") {
15- WorkerScript.onMessage = function(message) {
16- // handles the response data
17- var finished = function(result) {
18- // print result to get data for test json files
19- // print(JSON.stringify(result));
20- WorkerScript.sendMessage({
21- action: message.action,
22- result: result
23+var sendRequest = function(message, responseCallback) {
24+ // handles the response data
25+ var finished = function(result) {
26+ // print result to get data for test json files
27+ // print(JSON.stringify(result));
28+ //WorkerScript.sendMessage({
29+ responseCallback({
30+ action: message.action,
31+ result: result
32+ })
33+ }
34+ // handles errors
35+ var onError = function(err) {
36+ console.log(JSON.stringify(err, null, true));
37+ //WorkerScript.sendMessage({ 'error': err})
38+ responseCallback({ 'error': err})
39+ }
40+ // keep order of locations, sort results
41+ var sortDataResults = function(locA, locB) {
42+ return locA.db.id - locB.db.id;
43+ }
44+ // perform the api calls
45+ if(message.action === "searchByName") {
46+ WeatherApi.search("name", message.params, finished, onError);
47+ } else if(message.action === "searchByPoint") {
48+ WeatherApi.search("point", message.params, finished, onError);
49+ } else if(message.action === "getGeoIp") {
50+ WeatherApi.geoLookup(message.params, finished, onError);
51+ } else if(message.action === "updateData") {
52+ var locLength = message.params.locations.length,
53+ locUpdated = 0,
54+ result = [],
55+ now = new Date().getTime();
56+ if(locLength > 0) {
57+ message.params.locations.forEach(function(loc) {
58+ var updatedHnd = function (newData, cached) {
59+ locUpdated += 1;
60+ if(cached === true) {
61+ newData["save"] = false;
62+ } else {
63+ newData["save"] = true;
64+ newData["updated"] = new Date().getTime();
65+ }
66+ result.push(newData);
67+ if(locUpdated === locLength) {
68+ result.sort(sortDataResults);
69+ finished(result);
70+ }
71+ },
72+ params = {
73+ location:loc.location,
74+ db: loc.db,
75+ units: 'metric',
76+ service: message.params.service,
77+ api_key: message.params.api_key,
78+ interval: message.params.interval
79+ },
80+ secsFromLastFetch = (now-loc.updated)/1000;
81+ if( message.params.force===true || loc.format !== RESPONSE_DATA_VERSION || secsFromLastFetch > params.interval){
82+ // data older than 30min, location is new or data format is deprecated
83+ WeatherApi.getLocationData(params, updatedHnd, onError);
84+ } else {
85+ console.log("["+loc.location.name+"] returning cached data, time from last fetch: "+secsFromLastFetch)
86+ updatedHnd(loc, true);
87+ }
88 })
89- }
90- // handles errors
91- var onError = function(err) {
92- console.log(JSON.stringify(err, null, true));
93- WorkerScript.sendMessage({ 'error': err})
94- }
95- // keep order of locations, sort results
96- var sortDataResults = function(locA, locB) {
97- return locA.db.id - locB.db.id;
98- }
99- // perform the api calls
100- if(message.action === "searchByName") {
101- WeatherApi.search("name", message.params, finished, onError);
102- } else if(message.action === "searchByPoint") {
103- WeatherApi.search("point", message.params, finished, onError);
104- } else if(message.action === "getGeoIp") {
105- WeatherApi.geoLookup(message.params, finished, onError);
106- } else if(message.action === "updateData") {
107- var locLength = message.params.locations.length,
108- locUpdated = 0,
109- result = [],
110- now = new Date().getTime();
111- if(locLength > 0) {
112- message.params.locations.forEach(function(loc) {
113- var updatedHnd = function (newData, cached) {
114- locUpdated += 1;
115- if(cached === true) {
116- newData["save"] = false;
117- } else {
118- newData["save"] = true;
119- newData["updated"] = new Date().getTime();
120- }
121- result.push(newData);
122- if(locUpdated === locLength) {
123- result.sort(sortDataResults);
124- finished(result);
125- }
126- },
127- params = {
128- location:loc.location,
129- db: loc.db,
130- units: 'metric',
131- service: message.params.service,
132- api_key: message.params.api_key,
133- interval: message.params.interval
134- },
135- secsFromLastFetch = (now-loc.updated)/1000;
136- if( message.params.force===true || loc.format !== RESPONSE_DATA_VERSION || secsFromLastFetch > params.interval){
137- // data older than 30min, location is new or data format is deprecated
138- WeatherApi.getLocationData(params, updatedHnd, onError);
139- } else {
140- console.log("["+loc.location.name+"] returning cached data, time from last fetch: "+secsFromLastFetch)
141- updatedHnd(loc, true);
142- }
143- })
144- } else {
145- finished(result);
146- }
147+ } else {
148+ finished(result);
149 }
150 }
151 }
152
153=== modified file 'app/ubuntu-weather-app.qml'
154--- app/ubuntu-weather-app.qml 2015-03-14 15:17:33 +0000
155+++ app/ubuntu-weather-app.qml 2015-03-16 13:47:39 +0000
156@@ -21,6 +21,7 @@
157 import Ubuntu.Components 1.1
158 import "components"
159 import "data" as Data
160+import "data/WeatherApi.js" as WeatherApi
161 import "data/key.js" as Key
162 import "ui"
163
164@@ -65,26 +66,22 @@
165 Handle response data from data backend. Checks if a location
166 was updated and has to be stored again.
167 */
168- WorkerScript {
169- id: lookupWorker
170- source: Qt.resolvedUrl("./data/WeatherApi.js")
171- onMessage: {
172- if(!messageObject.error) {
173- if(messageObject.action === "updateData") {
174- messageObject.result.forEach(function(loc) {
175- // replace location data in cache with refreshed values
176- if(loc["save"] === true) {
177- storage.updateLocation(loc.db.id, loc);
178- }
179- });
180- //print(JSON.stringify(messageObject.result));
181- fillPages(messageObject.result);
182- }
183- } else {
184- console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
185- // TODO error handling
186+ function responseDataHandler(messageObject) {
187+ if(!messageObject.error) {
188+ if(messageObject.action === "updateData") {
189+ messageObject.result.forEach(function(loc) {
190+ // replace location data in cache with refreshed values
191+ if(loc["save"] === true) {
192+ storage.updateLocation(loc.db.id, loc);
193+ }
194+ });
195+ //print(JSON.stringify(messageObject.result));
196+ fillPages(messageObject.result);
197 }
198- }
199+ } else {
200+ console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
201+ // TODO error handling
202+ }
203 }
204
205 /* Fill the location pages with their data. */
206@@ -104,7 +101,7 @@
207 storage.getLocations(fillPages);
208 } else {
209 storage.getLocations(function(locations) {
210- lookupWorker.sendMessage({
211+ WeatherApi.sendRequest({
212 action: "updateData",
213 params: {
214 locations: locations,
215@@ -113,7 +110,7 @@
216 api_key: Key.twcKey,
217 interval: settings.refreshInterval
218 }
219- })
220+ }, responseDataHandler)
221 });
222 }
223 }
224
225=== modified file 'app/ui/AddLocationPage.qml'
226--- app/ui/AddLocationPage.qml 2015-03-14 10:55:19 +0000
227+++ app/ui/AddLocationPage.qml 2015-03-16 13:47:39 +0000
228@@ -122,28 +122,23 @@
229 function loadFromProvider(search) {
230 clearModelForLoading()
231
232- lookupWorker.sendMessage({
233+ WeatherApi.sendRequest({
234 action: "searchByName",
235 params: {
236 name: search,
237 units: "metric"
238 }
239- });
240+ }, searchResponseHandler)
241 }
242
243-
244- WorkerScript {
245- id: lookupWorker
246- source: Qt.resolvedUrl("../data/WeatherApi.js")
247- onMessage: {
248- if (!messageObject.error) {
249- appendCities(messageObject.result.locations)
250- } else {
251- citiesModel.httpError = true
252- }
253-
254- citiesModel.loading = false
255+ function searchResponseHandler(msgObject) {
256+ if (!msgObject.error) {
257+ appendCities(msgObject.result.locations)
258+ } else {
259+ citiesModel.httpError = true
260 }
261+
262+ citiesModel.loading = false
263 }
264
265 ListView {
266
267=== modified file 'po/com.ubuntu.weather.pot'
268--- po/com.ubuntu.weather.pot 2015-03-14 10:55:19 +0000
269+++ po/com.ubuntu.weather.pot 2015-03-16 13:47:39 +0000
270@@ -8,7 +8,7 @@
271 msgstr ""
272 "Project-Id-Version: ubuntu-weather-app\n"
273 "Report-Msgid-Bugs-To: \n"
274-"POT-Creation-Date: 2015-03-14 11:54+0100\n"
275+"POT-Creation-Date: 2015-03-06 15:08+0100\n"
276 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
277 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
278 "Language-Team: LANGUAGE <LL@li.org>\n"
279@@ -53,19 +53,19 @@
280 msgid "Search city"
281 msgstr ""
282
283-#: ../app/ui/AddLocationPage.qml:254
284+#: ../app/ui/AddLocationPage.qml:249
285 msgid "No city found"
286 msgstr ""
287
288-#: ../app/ui/AddLocationPage.qml:267
289+#: ../app/ui/AddLocationPage.qml:262
290 msgid "Couldn't load weather data, please try later again!"
291 msgstr ""
292
293-#: ../app/ui/AddLocationPage.qml:277
294+#: ../app/ui/AddLocationPage.qml:272
295 msgid "Location already added."
296 msgstr ""
297
298-#: ../app/ui/AddLocationPage.qml:280
299+#: ../app/ui/AddLocationPage.qml:275
300 msgid "OK"
301 msgstr ""
302

Subscribers

People subscribed via source and target branches

to all changes: