~ahayzen/ubuntu-weather-app/reboot-use-settings-api

« back to all changes in this revision

Viewing changes to app/ubuntu-weather-app.qml

  • Committer: Tarmac
  • Author(s): Martin Borho
  • Date: 2015-02-08 20:04:27 UTC
  • mfrom: (6.1.3 reboot-real-data)
  • Revision ID: tarmac-20150208200427-wjnyzv0k018uhqck
Filled static prototype components with real weather data.

Approved by Victor Thompson, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
    useDeprecatedToolbar: false
42
42
    anchorToKeyboard: true
43
43
 
 
44
    /*
 
45
      List of locations and their data, accessible through index
 
46
    */
 
47
    property var locationsList: []
 
48
 
 
49
    /*
 
50
      Index of Location before a refresh, to go back after
 
51
    */
 
52
    property int indexAtRefresh: -1
 
53
 
 
54
    /*
 
55
      Set default values for settings here
 
56
    */
 
57
    property var settings: {
 
58
        "units": Qt.locale().measurementSystem === Locale.MetricSystem ? "metric" : "imperial",
 
59
        "wind_units": Qt.locale().measurementSystem === Locale.MetricSystem ? "kmh" : "mph",
 
60
        "precip_units": Qt.locale().measurementSystem === Locale.MetricSystem ? "mm" : "in",
 
61
        "service": "weatherchannel"
 
62
    }
 
63
 
 
64
    /*
 
65
      Scale symbols and labels.
 
66
    */
 
67
    property string tempScale
 
68
    property string speedScale
 
69
    property string precipScale
 
70
    property string tempUnits
 
71
    property string windUnits
 
72
    property string precipUnits
 
73
 
 
74
    /*
 
75
      After reading the settings from storage and updating the default
 
76
      settings with the user selected ones, (re)load pages!
 
77
    */
44
78
    Component.onCompleted: {
45
 
        storage.getLocations(function(locations) {
46
 
            WeatherApi.sendRequest({
47
 
                action: "updateData",
48
 
                params: {
49
 
                    locations:locations,
50
 
                    force:false,
51
 
                    service: "weatherchannel",
52
 
                    api_key: Key.twcKey
53
 
                }
54
 
            }, responseDataHandler)
 
79
        storage.getSettings(function(storedSettings) {
 
80
            for(var settingName in storedSettings) {
 
81
                settings[settingName] = storedSettings[settingName];
 
82
            }
 
83
            setScalesAndLabels();
 
84
            refreshData();
55
85
        })
56
86
    }
57
87
 
 
88
    function setScalesAndLabels() {
 
89
        // set scales
 
90
        tempScale = String("°") + ((settings["units"] === "imperial") ? "F" : "C")
 
91
        speedScale = ((settings["wind_units"] === "mph") ? "mph" : "km/h")
 
92
        precipScale = ((settings["precip_units"] === "in") ? "in" : "mm")
 
93
        tempUnits = ((settings["units"] === 'imperial') ? 'imperial' : 'metric')
 
94
        windUnits = ((settings["wind_units"] === 'mph') ? 'imperial' : 'metric')
 
95
        precipUnits = ((settings["precip_units"] === 'in') ? 'imperial' : 'metric')
 
96
    }
 
97
 
 
98
    /*
 
99
      Handle response data from data backend. Checks if a location
 
100
      was updated and has to be stored again.
 
101
    */
58
102
    function responseDataHandler(messageObject) {
59
103
         if(!messageObject.error) {
60
104
             if(messageObject.action === "updateData") {
65
109
                     }
66
110
                 });
67
111
                 //print(JSON.stringify(messageObject.result));
68
 
                 //buildTabs(messageObject.result);
 
112
                 fillPages(messageObject.result);
69
113
             }
70
114
         } else {
71
115
             console.log(messageObject.error.msg+" / "+messageObject.error.request.url)
73
117
         }
74
118
     }
75
119
 
 
120
    /* Fill the location pages with their data. */
 
121
    function fillPages(locations) {
 
122
        locationsList = locations;
 
123
        // refactor this when Location are in a ListView!
 
124
        homePage.renderData();
 
125
    }
 
126
 
 
127
    /*
 
128
      Refresh data, either directly from storage or by checking against
 
129
      API instead.
 
130
    */
 
131
    function refreshData(from_storage, force_refresh) {
 
132
        if(from_storage === true && force_refresh !== true) {
 
133
            storage.getLocations(fillPages);
 
134
        } else {
 
135
            storage.getLocations(function(locations) {
 
136
                WeatherApi.sendRequest({
 
137
                    action: "updateData",
 
138
                    params: {
 
139
                        locations:locations,
 
140
                        force:force_refresh,
 
141
                        service:settings["service"],
 
142
                        api_key: Key.twcKey
 
143
                    }
 
144
                }, responseDataHandler)
 
145
            });
 
146
        }
 
147
    }
 
148
 
76
149
    Data.Storage {
77
150
        id: storage
78
151
    }