1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
* Copyright (C) 2015-2016 Canonical Ltd
*
* This file is part of Ubuntu Weather App
*
* Ubuntu Weather App is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Ubuntu Weather App is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.4
import Ubuntu.Components 1.3
import Ubuntu.Components.Popups 1.3
import "../components"
import "../data/CitiesList.js" as Cities
import "../data/WeatherApi.js" as WeatherApi
Page {
id: addLocationPage
objectName: "addLocationPage"
visible: false
property bool searching: citiesModel.loading || searchTimer.running
header: standardHeader
PageHeader {
id: standardHeader
title: i18n.tr("Select a city")
/*
Flickable is set to null to stop page header from hiding since the fast
scroll component hides top anchor margin is incorrect.
*/
flickable: null
visible: addLocationPage.header === standardHeader
trailingActionBar {
actions: [
Action {
iconName: "search"
objectName: "search"
text: i18n.tr("City")
onTriggered: {
addLocationPage.header = searchHeader
searchComponentLoader.sourceComponent = searchComponent
searchComponentLoader.item.forceActiveFocus()
}
}
]
objectName: "addLocationTrailingActionBar"
}
}
PageHeader {
id: searchHeader
visible: addLocationPage.header === searchHeader
leadingActionBar {
actions: [
Action {
iconName: "back"
objectName: "pagestack_back_action_button"
text: i18n.tr("Back")
onTriggered: {
locationList.forceActiveFocus()
searchComponentLoader.item.text = ""
addLocationPage.header = standardHeader
searchComponentLoader.sourceComponent = undefined
}
}
]
}
contents: Loader {
id: searchComponentLoader
anchors {
left: parent ? parent.left : undefined
right: parent ? parent.right : undefined
verticalCenter: parent ? parent.verticalCenter : undefined
}
}
}
// Outside component so property can bind to for autopilot
Timer {
id: searchTimer
interval: 250
onTriggered: {
if (searchComponentLoader.item) { // check component exists
if (searchComponentLoader.item.text.trim() === "") {
loadEmpty()
} else {
loadFromProvider(searchComponentLoader.item.text)
}
} else {
loadEmpty()
}
}
}
Component {
id: searchComponent
TextField {
id: searchField
objectName: "searchField"
inputMethodHints: Qt.ImhNoPredictiveText
placeholderText: i18n.tr("Search city")
hasClearButton: true
onTextChanged: searchTimer.restart()
}
}
// Builds a area label for the location, depending on the uniqueness of name, adminName1 and country
function buildAreaLabel(loc, a1Counts) {
var label = "";
label += ((loc.adminName1) ? loc.adminName1.replace(/ Region$/,''):"");
if (loc.adminName2 && a1Counts[loc.name+loc.adminName1] > 1) {
// even name and adminName1 are multiple, add adminName2
label += ", "+loc.adminName2;
}
label += ((label !== "") ? ", " : "") + loc.countryName
return label;
}
function appendCities(list) {
var a1Counts = {};
// count occurrences of name+adminName1 and name+country
list.forEach(function(loc) {
a1Counts[loc.name+loc.adminName1] = (!a1Counts[loc.name+loc.adminName1]) ? 1 : a1Counts[loc.name+loc.adminName1]+1;
});
// add locations to listmodel
list.forEach(function(loc) {
loc.areaLabel = buildAreaLabel(loc, a1Counts)
citiesModel.append(loc);
})
}
function clearModelForLoading() {
citiesModel.clear()
citiesModel.loading = true
citiesModel.httpError = false
}
function loadEmpty() {
clearModelForLoading()
appendCities(Cities.preList)
citiesModel.loading = false
}
function loadFromProvider(search) {
clearModelForLoading()
WeatherApi.sendRequest({
action: "searchByName",
params: {
name: search,
units: "metric"
}
}, searchResponseHandler)
}
function searchResponseHandler(msgObject) {
if (!msgObject.error) {
appendCities(msgObject.result.locations)
} else {
citiesModel.httpError = true
}
citiesModel.loading = false
}
ListView {
id: locationList
anchors {
top: addLocationPage.header.bottom
topMargin: units.gu(2)
left: parent.left
right: parent.right
rightMargin: fastScroll.showing ? fastScroll.width - units.gu(1) : 0
bottom: parent.bottom
}
objectName: "locationList"
clip: true
currentIndex: -1
function getSectionText(index) {
return citiesModel.get(index).name.substring(0,1)
}
onFlickStarted: {
forceActiveFocus()
}
section.property: "name"
section.criteria: ViewSection.FirstCharacter
section.labelPositioning: ViewSection.InlineLabels
section.delegate: ListItem {
height: headerText.implicitHeight + units.gu(1)
Label {
id: headerText
text: section
anchors { left: parent.left; right: parent.right; margins: units.gu(2) }
font.weight: Font.DemiBold
}
}
model: ListModel {
id: citiesModel
property bool loading: true
property bool httpError: false
onRowsAboutToBeInserted: loading = false
}
delegate: ListItem {
divider.visible: false
objectName: "addLocation" + index
height: listDelegateLayout.height
ListItemLayout {
id: listDelegateLayout
title.text: name
subtitle.text: areaLabel
subtitle.textSize: Label.Small
}
onClicked: {
if (storage.addLocation(citiesModel.get(index))) {
mainPageStack.pop()
} else {
PopupUtils.open(locationExistsComponent, addLocationPage)
}
}
}
Component.onCompleted: loadEmpty()
Behavior on anchors.rightMargin {
UbuntuNumberAnimation {}
}
}
FastScroll {
id: fastScroll
listView: locationList
enabled: (locationList.contentHeight > (locationList.height * 2)) &&
(locationList.height >= minimumHeight)
anchors {
top: locationList.top
topMargin: units.gu(1.5)
bottom: locationList.bottom
right: parent.right
}
}
ActivityIndicator {
anchors {
centerIn: parent
}
running: visible
visible: citiesModel.loading
}
Label {
id: noCity
objectName: "noCity"
anchors {
centerIn: parent
}
text: i18n.tr("No city found")
visible: citiesModel.count === 0 && !citiesModel.loading
}
Label {
id: httpFail
anchors {
left: parent.left
margins: units.gu(1)
right: parent.right
top: noCity.bottom
}
horizontalAlignment: Text.AlignHCenter
text: i18n.tr("Couldn't load weather data, please try later again!")
visible: citiesModel.httpError
wrapMode: Text.WordWrap
}
Component {
id: locationExistsComponent
Dialog {
id: locationExists
title: i18n.tr("Location already added.")
Button {
text: i18n.tr("OK")
onClicked: PopupUtils.close(locationExists)
}
}
}
}
|