-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpotifyMusicSearchScreen.qml
More file actions
412 lines (351 loc) · 10.2 KB
/
SpotifyMusicSearchScreen.qml
File metadata and controls
412 lines (351 loc) · 10.2 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import QtQuick 2.1
import BasicUIControls 1.0
import qb.components 1.0
Screen {
id: favoritesScreen
screenTitle: "Zoek in Spotify";
property int albumCount : 0
property int trackCount : 0
property int playlistCount : 0
property variant searchResults : []
property variant displayResults : []
property bool albumSelected : false
property bool trackSelected : false
property bool playlistSelected : false
onHidden: {
screenStateController.screenColorDimmedIsReachable = true;
}
onCustomButtonClicked: {
if (app.messageScreen) app.messageScreen.show();
}
onShown: {
addCustomTopRightButton("Audiobericht");
screenStateController.screenColorDimmedIsReachable = false;
pageThrobber.visible = false;
}
//Required to use the Sonos HTTP API and to start every request in the functions.
function simpleSynchronous(request) {
pageThrobber.visible = true;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", request, true);
xmlhttp.timeout = 1500;
xmlhttp.send();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
pageThrobber.visible = false;
if (typeof(functie) !== 'undefined') {
functie(parameter);
}
}
}
}
}
function saveSearchTextLabel(text) {
if (text) {
searchTextLabel.inputText = text;
searchMusic();
}
}
//if the connection is not available or is still loading a page throbber will be visible in the scrollable list.
Throbber {
id: pageThrobber
visible: true
z: isNxt ? 25 : 20
anchors {
horizontalCenter: parent.horizontalCenter
verticalCenter: parent.verticalCenter
}
}
//this text shows the counter above the scrollable list.
Text {
id: chooseText
text: "Zoek op naam artiest/album/titel van de track:"
font.pixelSize: isNxt ? 20 : 16
font.family: qfont.regular.name
font.bold: true
wrapMode: Text.WordWrap
anchors {
top: parent.top
topMargin: isNxt ? 20 : 16
left: parent.left
leftMargin: isNxt ? 20 : 16 }
}
EditTextLabel4421 {
id: searchTextLabel
width: isNxt ? 550 : 440
height: isNxt ? 44 : 35
leftTextAvailableWidth: isNxt ? 150 : 120
leftText: "Zoeken op:"
x: isNxt ? 38 : 30
y: 10
anchors {
top: chooseText.bottom
left: chooseText.left
}
onClicked: {
qkeyboard.open("Zoek naar muziek:", searchTextLabel.inputText, saveSearchTextLabel)
}
}
StandardButton {
id: searchResultsAlbums
width: isNxt ? 175 : 140
radius: 5
text: "Albums (" + albumCount + ")"
fontPixelSize: isNxt ? 20 : 16
color: colors.background
anchors {
top: searchTextLabel.bottom
topMargin: isNxt ? 30 : 24
left: searchTextLabel.left
}
visible: (albumCount > 0)
onClicked: {
var tmpResults = [];
searchResultsSimpleList.removeAll();
for (var i = 0; i < albumCount ; i++) {
tmpResults.push({"name": searchResults["albums"]["items"][i]["name"] + " (" + searchResults["albums"]["items"][i]["artists"][0]["name"] + ")" , "uri":searchResults["albums"]["items"][i]["uri"]});
searchResultsSimpleList.addDevice(i);
}
displayResults = tmpResults;
searchResultsSimpleList.refreshView();
searchResultsSimpleList.scrollToPage(0);
trackSelected = false
albumSelected = true
playlistSelected = false
}
}
Text {
id: albumSelectedText
text: "-->"
font.pixelSize: isNxt ? 30 : 24
font.family: qfont.regular.name
font.bold: true
visible: albumSelected
wrapMode: Text.WordWrap
anchors {
top: searchResultsAlbums.top
left: searchResultsAlbums.right
leftMargin : isNxt ? 15 : 12
}
}
StandardButton {
id: searchResultsTracks
width: isNxt ? 175 : 140
radius: 5
text: "Tracks (" + trackCount + ")"
fontPixelSize: isNxt ? 20 : 16
color: colors.background
anchors {
top: searchResultsAlbums.bottom
topMargin: isNxt ? 20 : 16
left: searchResultsAlbums.left
}
visible: (trackCount > 0)
onClicked: {
var tmpResults = [];
searchResultsSimpleList.removeAll();
for (var i = 0; i < trackCount ; i++) {
tmpResults.push({"name": searchResults["tracks"]["items"][i]["name"] + " (" + searchResults["tracks"]["items"][i]["artists"][0]["name"] + ")" , "uri":searchResults["tracks"]["items"][i]["uri"]});
searchResultsSimpleList.addDevice(i);
}
displayResults = tmpResults;
searchResultsSimpleList.refreshView();
searchResultsSimpleList.scrollToPage(0);
trackSelected = true
albumSelected = false
playlistSelected = false
}
}
Text {
id: trackSelectedText
text: "-->"
font.pixelSize: isNxt ? 30 : 24
font.family: qfont.regular.name
font.bold: true
visible: trackSelected
wrapMode: Text.WordWrap
anchors {
top: searchResultsTracks.top
left: searchResultsTracks.right
leftMargin : isNxt ? 15 : 12
}
}
StandardButton {
id: searchResultsPlaylists
width: isNxt ? 175 : 140
radius: 5
text: "Playlists (" + playlistCount + ")"
fontPixelSize: isNxt ? 20 : 16
color: colors.background
anchors {
top: searchResultsTracks.bottom
topMargin: isNxt ? 20 : 16
left: searchResultsTracks.left
}
visible: (playlistCount > 0)
onClicked: {
var tmpResults = [];
searchResultsSimpleList.removeAll();
for (var i = 0; i < playlistCount ; i++) {
tmpResults.push({"name": searchResults["albums"]["items"][i]["name"] , "uri":searchResults["albums"]["items"][i]["uri"]});
searchResultsSimpleList.addDevice(i);
}
displayResults = tmpResults;
searchResultsSimpleList.refreshView();
searchResultsSimpleList.scrollToPage(0);
trackSelected = false
albumSelected = false
playlistSelected = true
}
}
Text {
id: playlistSelectedText
text: "-->"
font.pixelSize: isNxt ? 30 : 24
font.family: qfont.regular.name
font.bold: true
visible: playlistSelected
wrapMode: Text.WordWrap
anchors {
top: searchResultsPlaylists.top
left: searchResultsPlaylists.right
leftMargin : isNxt ? 15 : 12
}
}
Text {
id: maxText
text: "Maximaal 20 resultaten\nper categorie"
font.pixelSize: isNxt ? 20 : 16
font.family: qfont.regular.name
font.bold: true
visible: ((albumCount == 20) || (trackCount == 20) || (playlistCount == 20))
wrapMode: Text.WordWrap
anchors {
top: searchResultsPlaylists.bottom
topMargin: isNxt ? 15 : 12
left: searchResultsPlaylists.left
}
width: 450
}
StandardButton {
id: playAllTracks
width: isNxt ? 175 : 140
radius: 5
text: "Play all tracks"
fontPixelSize: isNxt ? 20 : 16
color: colors.background
anchors {
bottom: searchResultsSimpleList.bottom
left: searchResultsTracks.left
}
visible: trackSelected
onClicked: {
simpleSynchronous("http://"+app.connectionPath+"/"+app.sonosName+"/clearqueue");
for (var i = 0; i < trackCount ; i++) {
simpleSynchronous("http://"+app.connectionPath+"/"+app.sonosName+"/spotify/queue/" + searchResults["tracks"]["items"][i]["uri"]);
}
simpleSynchronous("http://"+app.connectionPath+"/"+app.sonosName+"/play")
if (app.mediaScreen) app.mediaScreen.show();
}
}
//Property's for the scrollable list it self.
ScrollableSimpleList {
id: searchResultsSimpleList
width: isNxt ? 750 : 600
height: isNxt ? 420 : 336
itemsPerPage: 7
delegate: serachResultsDelegate
anchors {
top: searchTextLabel.bottom
topMargin: isNxt ? 15 : 12
left: albumSelectedText.right
leftMargin: isNxt ? 10 : 8
}
visible: ( (albumCount > 0) || (trackCount > 0) || (playlistCount > 0 ))
Throbber {
id: throbberPL
visible: false
anchors {
horizontalCenter: parent.horizontalCenter
horizontalCenterOffset: isNxt ? -30 : -25
verticalCenter: parent.verticalCenter
}
}
}
Component {
id: serachResultsDelegate
//to make the list clickable
Item {
width: isNxt ? 750 : 600
height: isNxt ? 50 : 40
StandardButton {
id: playlistButtom
radius: 5
text: displayResults[item]['name']
width: isNxt ? 650 : 520
anchors {
top: parent.top
}
onClicked: {
simpleSynchronous("http://"+app.connectionPath+"/"+app.sonosName+"/clearqueue");
simpleSynchronous("http://"+app.connectionPath+"/"+app.sonosName+"/spotify/now/" + displayResults[item]['uri']);
if (!trackSelected) app.mediaScreen.show();
}
}
}
}
//Fill the file: FavoriteslistItemsJS with only the item "Name" and also manage a little bit the scrollable list (with refreshing it). This has also the counter function.
function searchMusic() {
if (app.spotifyStatus == "configured") {
var xmlhttpSpot = new XMLHttpRequest();
xmlhttpSpot.onreadystatechange=function() {
if (xmlhttpSpot.readyState == 4) {
if (xmlhttpSpot.status == 200) {
searchResults = JSON.parse(xmlhttpSpot.responseText);
searchResultsSimpleList.removeAll();
// get search results counters for albums, tracks and playlists
trackCount = 0;
if (searchResults["tracks"]) {
if (searchResults["tracks"]["items"]) {
trackCount = searchResults["tracks"]["items"].length
}
}
albumCount = 0;
if (searchResults["albums"]) {
if (searchResults["albums"]["items"]) {
albumCount = searchResults["albums"]["items"].length
}
}
playlistCount = 0;
if (searchResults["playlists"]) {
if (searchResults["playlists"]["items"]) {
playlistCount = searchResults["playlists"]["items"].length
}
}
// fill displayResults with tracks
if (trackCount > 0) {
var tmpResults = [];
for (var i = 0; i < trackCount ; i++) {
tmpResults.push({"name": searchResults["tracks"]["items"][i]["name"] + " (" + searchResults["tracks"]["items"][i]["artists"][0]["name"] + ")" , "uri":searchResults["tracks"]["items"][i]["uri"]});
searchResultsSimpleList.addDevice(i);
}
displayResults = tmpResults;
searchResultsSimpleList.refreshView();
if (searchResultsSimpleList.currentPage == -1) {
searchResultsSimpleList.scrollToPage(0);
}
trackSelected = true
albumSelected = false
playlistSelected = false
}
pageThrobber.visible = false;
}
}
}
}
xmlhttpSpot.open("GET", "https://api.spotify.com/v1/search?q=" + searchTextLabel.inputText + "&type=track,album,playlist,artist");
xmlhttpSpot.setRequestHeader("Authorization", 'Bearer ' + app.spotifyToken["access_token"]);
xmlhttpSpot.send();
}
}