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

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

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

Description of the change

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

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
Victor Thompson (vthompson) wrote :

LGTM! When we do empty states, we should consider displaying something useful to the user when the API calls are not working and/or there is no stored data.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== 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 @@
671 "geoip": GeoipApi671 "geoip": GeoipApi
672});672});
673673
674var sendRequest = function(message, responseCallback) {674/**
675 // handles the response data675* following WorkerScript handles the data requests against the weather API.
676 var finished = function(result) {676* "message" requires a "params" property with the required params to perform
677 // print result to get data for test json files677* the API call and an "action" property, which will be added also to the response.
678 // print(JSON.stringify(result));678*/
679 //WorkerScript.sendMessage({679
680 responseCallback({680if(typeof WorkerScript != "undefined") {
681 action: message.action,681 WorkerScript.onMessage = function(message) {
682 result: result682 // handles the response data
683 })683 var finished = function(result) {
684 }684 // print result to get data for test json files
685 // handles errors685 // print(JSON.stringify(result));
686 var onError = function(err) {686 WorkerScript.sendMessage({
687 console.log(JSON.stringify(err, null, true));687 action: message.action,
688 //WorkerScript.sendMessage({ 'error': err})688 result: result
689 responseCallback({ 'error': err})
690 }
691 // keep order of locations, sort results
692 var sortDataResults = function(locA, locB) {
693 return locA.db.id - locB.db.id;
694 }
695 // perform the api calls
696 if(message.action === "searchByName") {
697 WeatherApi.search("name", message.params, finished, onError);
698 } else if(message.action === "searchByPoint") {
699 WeatherApi.search("point", message.params, finished, onError);
700 } else if(message.action === "getGeoIp") {
701 WeatherApi.geoLookup(message.params, finished, onError);
702 } else if(message.action === "updateData") {
703 var locLength = message.params.locations.length,
704 locUpdated = 0,
705 result = [],
706 now = new Date().getTime();
707 if(locLength > 0) {
708 message.params.locations.forEach(function(loc) {
709 var updatedHnd = function (newData, cached) {
710 locUpdated += 1;
711 if(cached === true) {
712 newData["save"] = false;
713 } else {
714 newData["save"] = true;
715 newData["updated"] = new Date().getTime();
716 }
717 result.push(newData);
718 if(locUpdated === locLength) {
719 result.sort(sortDataResults);
720 finished(result);
721 }
722 },
723 params = {
724 location:loc.location,
725 db: loc.db,
726 units: 'metric',
727 service: message.params.service,
728 api_key: message.params.api_key,
729 interval: message.params.interval
730 },
731 secsFromLastFetch = (now-loc.updated)/1000;
732 if( message.params.force===true || loc.format !== RESPONSE_DATA_VERSION || secsFromLastFetch > params.interval){
733 // data older than 30min, location is new or data format is deprecated
734 WeatherApi.getLocationData(params, updatedHnd, onError);
735 } else {
736 console.log("["+loc.location.name+"] returning cached data, time from last fetch: "+secsFromLastFetch)
737 updatedHnd(loc, true);
738 }
739 })689 })
740 } else {690 }
741 finished(result);691 // handles errors
692 var onError = function(err) {
693 console.log(JSON.stringify(err, null, true));
694 WorkerScript.sendMessage({ 'error': err})
695 }
696 // keep order of locations, sort results
697 var sortDataResults = function(locA, locB) {
698 return locA.db.id - locB.db.id;
699 }
700 // perform the api calls
701 if(message.action === "searchByName") {
702 WeatherApi.search("name", message.params, finished, onError);
703 } else if(message.action === "searchByPoint") {
704 WeatherApi.search("point", message.params, finished, onError);
705 } else if(message.action === "getGeoIp") {
706 WeatherApi.geoLookup(message.params, finished, onError);
707 } else if(message.action === "updateData") {
708 var locLength = message.params.locations.length,
709 locUpdated = 0,
710 result = [],
711 now = new Date().getTime();
712 if(locLength > 0) {
713 message.params.locations.forEach(function(loc) {
714 var updatedHnd = function (newData, cached) {
715 locUpdated += 1;
716 if(cached === true) {
717 newData["save"] = false;
718 } else {
719 newData["save"] = true;
720 newData["updated"] = new Date().getTime();
721 }
722 result.push(newData);
723 if(locUpdated === locLength) {
724 result.sort(sortDataResults);
725 finished(result);
726 }
727 },
728 params = {
729 location:loc.location,
730 db: loc.db,
731 units: 'metric',
732 service: message.params.service,
733 api_key: message.params.api_key,
734 interval: message.params.interval
735 },
736 secsFromLastFetch = (now-loc.updated)/1000;
737 if( message.params.force===true || loc.format !== RESPONSE_DATA_VERSION || secsFromLastFetch > params.interval){
738 // data older than 30min, location is new or data format is deprecated
739 WeatherApi.getLocationData(params, updatedHnd, onError);
740 } else {
741 console.log("["+loc.location.name+"] returning cached data, time from last fetch: "+secsFromLastFetch)
742 updatedHnd(loc, true);
743 }
744 })
745 } else {
746 finished(result);
747 }
742 }748 }
743 }749 }
744}750}
745751
=== 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 @@
21import Ubuntu.Components 1.121import Ubuntu.Components 1.1
22import "components"22import "components"
23import "data" as Data23import "data" as Data
24import "data/WeatherApi.js" as WeatherApi
25import "data/key.js" as Key24import "data/key.js" as Key
26import "ui"25import "ui"
2726
@@ -57,28 +56,35 @@
57 /*56 /*
58 (re)load the pages on completion57 (re)load the pages on completion
59 */58 */
60 Component.onCompleted: refreshData();59 Component.onCompleted: {
60 storage.getLocations(fillPages);
61 refreshData();
62 }
6163
62 /*64 /*
63 Handle response data from data backend. Checks if a location65 Handle response data from data backend. Checks if a location
64 was updated and has to be stored again.66 was updated and has to be stored again.
65 */67 */
66 function responseDataHandler(messageObject) {68 WorkerScript {
67 if(!messageObject.error) {69 id: lookupWorker
68 if(messageObject.action === "updateData") {70 source: Qt.resolvedUrl("./data/WeatherApi.js")
69 messageObject.result.forEach(function(loc) {71 onMessage: {
70 // replace location data in cache with refreshed values72 if(!messageObject.error) {
71 if(loc["save"] === true) {73 if(messageObject.action === "updateData") {
72 storage.updateLocation(loc.db.id, loc);74 messageObject.result.forEach(function(loc) {
73 }75 // replace location data in cache with refreshed values
74 });76 if(loc["save"] === true) {
75 //print(JSON.stringify(messageObject.result));77 storage.updateLocation(loc.db.id, loc);
76 fillPages(messageObject.result);78 }
79 });
80 //print(JSON.stringify(messageObject.result));
81 fillPages(messageObject.result);
82 }
83 } else {
84 console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
85 // TODO error handling
77 }86 }
78 } else {87 }
79 console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
80 // TODO error handling
81 }
82 }88 }
8389
84 /* Fill the location pages with their data. */90 /* Fill the location pages with their data. */
@@ -98,7 +104,7 @@
98 storage.getLocations(fillPages);104 storage.getLocations(fillPages);
99 } else {105 } else {
100 storage.getLocations(function(locations) {106 storage.getLocations(function(locations) {
101 WeatherApi.sendRequest({107 lookupWorker.sendMessage({
102 action: "updateData",108 action: "updateData",
103 params: {109 params: {
104 locations: locations,110 locations: locations,
@@ -107,7 +113,7 @@
107 api_key: Key.twcKey,113 api_key: Key.twcKey,
108 interval: settings.refreshInterval114 interval: settings.refreshInterval
109 }115 }
110 }, responseDataHandler)116 })
111 });117 });
112 }118 }
113 }119 }
114120
=== 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 @@
122 function loadFromProvider(search) {122 function loadFromProvider(search) {
123 clearModelForLoading()123 clearModelForLoading()
124124
125 WeatherApi.sendRequest({125 lookupWorker.sendMessage({
126 action: "searchByName",126 action: "searchByName",
127 params: {127 params: {
128 name: search,128 name: search,
129 units: "metric"129 units: "metric"
130 }130 }
131 }, searchResponseHandler)131 });
132 }132 }
133133
134 function searchResponseHandler(msgObject) {134
135 if (!msgObject.error) {135 WorkerScript {
136 appendCities(msgObject.result.locations)136 id: lookupWorker
137 } else {137 source: Qt.resolvedUrl("../data/WeatherApi.js")
138 citiesModel.httpError = true138 onMessage: {
139 if (!messageObject.error) {
140 appendCities(messageObject.result.locations)
141 } else {
142 citiesModel.httpError = true
143 }
144
145 citiesModel.loading = false
139 }146 }
140
141 citiesModel.loading = false
142 }147 }
143148
144 ListView {149 ListView {
145150
=== 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 @@
8msgstr ""8msgstr ""
9"Project-Id-Version: ubuntu-weather-app\n"9"Project-Id-Version: ubuntu-weather-app\n"
10"Report-Msgid-Bugs-To: \n"10"Report-Msgid-Bugs-To: \n"
11"POT-Creation-Date: 2015-03-06 15:08+0100\n"11"POT-Creation-Date: 2015-03-14 11:54+0100\n"
12"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"12"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"13"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14"Language-Team: LANGUAGE <LL@li.org>\n"14"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -53,19 +53,19 @@
53msgid "Search city"53msgid "Search city"
54msgstr ""54msgstr ""
5555
56#: ../app/ui/AddLocationPage.qml:24956#: ../app/ui/AddLocationPage.qml:254
57msgid "No city found"57msgid "No city found"
58msgstr ""58msgstr ""
5959
60#: ../app/ui/AddLocationPage.qml:26260#: ../app/ui/AddLocationPage.qml:267
61msgid "Couldn't load weather data, please try later again!"61msgid "Couldn't load weather data, please try later again!"
62msgstr ""62msgstr ""
6363
64#: ../app/ui/AddLocationPage.qml:27264#: ../app/ui/AddLocationPage.qml:277
65msgid "Location already added."65msgid "Location already added."
66msgstr ""66msgstr ""
6767
68#: ../app/ui/AddLocationPage.qml:27568#: ../app/ui/AddLocationPage.qml:280
69msgid "OK"69msgid "OK"
70msgstr ""70msgstr ""
7171

Subscribers

People subscribed via source and target branches

to all changes: