@@ -23,12 +23,12 @@ class Token {
23
23
}
24
24
}
25
25
26
- class SpotifySearchResult {
26
+ class SpotifyTrackSearchResult {
27
27
id : string ;
28
28
name : string ;
29
29
type : string ;
30
30
artists : Array < string > ;
31
- album : Album ;
31
+ album : SpotifyAlbumSearchResult ;
32
32
uri : string ;
33
33
34
34
constructor ( obj ?: any ) {
@@ -37,19 +37,23 @@ class SpotifySearchResult {
37
37
this . type = obj && obj . type || null ;
38
38
this . artists = obj && obj . artists || null ;
39
39
this . album = obj && obj . album || null ;
40
- this . uri = obj && obj . uri || null
40
+ this . uri = obj && obj . uri || null ;
41
41
}
42
42
}
43
43
44
- class Album {
44
+ class SpotifyAlbumSearchResult {
45
45
id : string ;
46
46
name : string ;
47
47
image : string ;
48
+ artists : Array < string > ;
49
+ uri : string ;
48
50
49
51
constructor ( obj ?: any ) {
50
52
this . id = obj && obj . id || null ;
51
53
this . name = obj && obj . name || null ;
52
54
this . image = obj && obj . image || null ;
55
+ this . artists = obj && obj . artists || null ;
56
+ this . uri = obj && obj . uri || null ;
53
57
}
54
58
}
55
59
@@ -66,7 +70,7 @@ export class SpotifyService {
66
70
67
71
}
68
72
69
- search ( query : string ) : Observable < SpotifySearchResult [ ] > {
73
+ searchTracks ( query : string ) : Observable < SpotifyTrackSearchResult [ ] > {
70
74
let params : string = [
71
75
`q=${ query } ` ,
72
76
`type=track`
@@ -81,12 +85,12 @@ export class SpotifyService {
81
85
for ( var i = 0 ; i < item . artists . length ; i ++ ) {
82
86
artists . push ( item . artists [ i ] . name ) ;
83
87
}
84
- var album = new Album ( {
88
+ var album = new SpotifyAlbumSearchResult ( {
85
89
id : item . album . id ,
86
90
name : item . album . name ,
87
- image : item . album . images [ 0 ] . url
91
+ image : item . album . images [ 0 ] . url ,
88
92
} )
89
- return new SpotifySearchResult ( {
93
+ return new SpotifyTrackSearchResult ( {
90
94
id : item . id ,
91
95
name : item . name ,
92
96
type : item . type ,
@@ -98,6 +102,32 @@ export class SpotifyService {
98
102
} ) ;
99
103
}
100
104
105
+ searchAlbums ( query : string ) : Observable < SpotifyAlbumSearchResult [ ] > {
106
+ let params : string = [
107
+ `q=${ query } ` ,
108
+ `type=album`
109
+ ] . join ( '&' ) ;
110
+ let queryUrl : string = `${ this . apiBase } search?${ params } ` ;
111
+
112
+ return Observable . fromPromise ( this . getToken ( ) )
113
+ . flatMap ( token => this . http . get ( queryUrl , { headers : new Headers ( { 'Authorization' : `${ token . token_type } ${ token . access_token } ` } ) } ) )
114
+ . map ( ( response : Response ) => {
115
+ return ( < any > response . json ( ) ) . albums . items . map ( item => {
116
+ var artists = new Array < string > ( ) ;
117
+ for ( var i = 0 ; i < item . artists . length ; i ++ ) {
118
+ artists . push ( item . artists [ i ] . name ) ;
119
+ }
120
+ return new SpotifyAlbumSearchResult ( {
121
+ id : item . id ,
122
+ name : item . name ,
123
+ image : item . images [ 0 ] . url ,
124
+ artists : artists ,
125
+ uri : item . uri
126
+ } ) ;
127
+ } ) ;
128
+ } ) ;
129
+ }
130
+
101
131
getToken ( ) : Promise < Token > {
102
132
let params : string = "grant_type=client_credentials" ;
103
133
let headers = new Headers ( {
@@ -127,7 +157,7 @@ export var spotifyServiceInjectables: Array<any> = [
127
157
] ;
128
158
129
159
@Component ( {
130
- outputs : [ 'loading' , 'results ' ] ,
160
+ outputs : [ 'loading' , 'tracksResults' , 'albumsResults '] ,
131
161
selector : 'spotify-search-box' ,
132
162
template : `
133
163
<p>Enter something in the field and see the asynchronous results!</p>
@@ -137,24 +167,26 @@ export var spotifyServiceInjectables: Array<any> = [
137
167
138
168
export class SpotifySearchBox implements OnInit {
139
169
loading : EventEmitter < boolean > = new EventEmitter < boolean > ( ) ;
140
- results : EventEmitter < SpotifySearchResult [ ] > = new EventEmitter < SpotifySearchResult [ ] > ( ) ;
170
+ tracksResults : EventEmitter < SpotifyTrackSearchResult [ ] > = new EventEmitter < SpotifyTrackSearchResult [ ] > ( ) ;
171
+ albumsResults : EventEmitter < SpotifyAlbumSearchResult [ ] > = new EventEmitter < SpotifyAlbumSearchResult [ ] > ( ) ;
141
172
142
173
constructor ( public spotify : SpotifyService ,
143
174
private el : ElementRef ) {
144
175
}
145
176
146
177
ngOnInit ( ) : void {
178
+ //tracks
147
179
Observable . fromEvent ( this . el . nativeElement , 'keyup' )
148
180
. map ( ( e : any ) => e . target . value )
149
181
. filter ( ( text : string ) => text . length > 1 )
150
182
. debounceTime ( 250 )
151
183
. do ( ( ) => this . loading . next ( true ) )
152
- . map ( ( query : string ) => this . spotify . search ( query ) )
184
+ . map ( ( query : string ) => this . spotify . searchTracks ( query ) )
153
185
. switch ( )
154
186
. subscribe (
155
- ( results : SpotifySearchResult [ ] ) => {
187
+ ( tracksResults : SpotifyTrackSearchResult [ ] ) => {
156
188
this . loading . next ( false ) ;
157
- this . results . next ( results ) ;
189
+ this . tracksResults . next ( tracksResults ) ;
158
190
} ,
159
191
( err : any ) => {
160
192
console . log ( err ) ;
@@ -164,12 +196,34 @@ export class SpotifySearchBox implements OnInit {
164
196
this . loading . next ( false ) ;
165
197
}
166
198
) ;
199
+
200
+ //albums
201
+ Observable . fromEvent ( this . el . nativeElement , 'keyup' )
202
+ . map ( ( e : any ) => e . target . value )
203
+ . filter ( ( text : string ) => text . length > 1 )
204
+ . debounceTime ( 250 )
205
+ . do ( ( ) => this . loading . next ( true ) )
206
+ . map ( ( query : string ) => this . spotify . searchAlbums ( query ) )
207
+ . switch ( )
208
+ . subscribe (
209
+ ( albumsResults : SpotifyAlbumSearchResult [ ] ) => {
210
+ this . loading . next ( false ) ;
211
+ this . albumsResults . next ( albumsResults ) ;
212
+ } ,
213
+ ( err : any ) => {
214
+ console . log ( err ) ;
215
+ this . loading . next ( false ) ;
216
+ } ,
217
+ ( ) => {
218
+ this . loading . next ( false ) ;
219
+ }
220
+ )
167
221
}
168
222
}
169
223
170
224
@Component ( {
171
225
inputs : [ 'result' ] ,
172
- selector : 'spotify-search-result' ,
226
+ selector : 'spotify-tracks- search-result' ,
173
227
template : `
174
228
<ion-card>
175
229
<ion-item>
@@ -195,8 +249,49 @@ export class SpotifySearchBox implements OnInit {
195
249
`
196
250
} )
197
251
198
- export class SpotifySearchResultComponent {
199
- result : SpotifySearchResult ;
252
+ export class SpotifyTracksSearchResultComponent {
253
+ tracksResult : SpotifyTrackSearchResult ;
254
+
255
+ constructor ( public navCtrl : NavController , public discordApi : DiscordApiProvider ) {
256
+
257
+ }
258
+
259
+ Play ( query ) {
260
+ this . discordApi . sendCommand ( this . discordApi . Commands . play , query ) ;
261
+ this . navCtrl . pop ( ) ;
262
+ }
263
+ }
264
+
265
+ @Component ( {
266
+ inputs : [ 'result' ] ,
267
+ selector : 'spotify-albums-search-result' ,
268
+ template : `
269
+ <ion-card>
270
+ <ion-item>
271
+ <ion-avatar item-start>
272
+ <img src="{{result.image}}">
273
+ </ion-avatar>
274
+ <h2>{{result.name}}</h2>
275
+ <!--<p>{{result.artists}}</p>-->
276
+ </ion-item>
277
+ <img src="{{result.image}}">
278
+ <ion-card-content>
279
+ <p>Album: {{result.name}}</p>
280
+ </ion-card-content>
281
+ <ion-row>
282
+ <ion-col>
283
+ <button ion-button icon-left clear small (click)="Play(result.uri)">
284
+ <ion-icon name="play"></ion-icon>
285
+ <div>Play</div>
286
+ </button>
287
+ </ion-col>
288
+ </ion-row>
289
+ </ion-card>
290
+ `
291
+ } )
292
+
293
+ export class SpotifyAlbumsSearchResultComponent {
294
+ albumsResult : SpotifyAlbumSearchResult ;
200
295
201
296
constructor ( public navCtrl : NavController , public discordApi : DiscordApiProvider ) {
202
297
@@ -213,16 +308,23 @@ export class SpotifySearchResultComponent {
213
308
templateUrl : 'spotify.html'
214
309
} )
215
310
export class SpotifyPage {
311
+ type = "" ;
312
+ tracksResults : SpotifyTrackSearchResult [ ] ;
313
+ albumsResults : SpotifyAlbumSearchResult [ ] ;
216
314
217
- constructor ( public navCtrl : NavController , public navParams : NavParams ) { }
315
+ constructor ( public navCtrl : NavController , public navParams : NavParams ) {
316
+ this . type = 'tracks' ;
317
+ }
218
318
219
319
ionViewDidLoad ( ) {
220
320
console . log ( 'ionViewDidLoad SpotifyPage' ) ;
221
321
}
222
322
223
- results : SpotifySearchResult [ ] ;
323
+ updateTracksResults ( results : SpotifyTrackSearchResult [ ] ) : void {
324
+ this . tracksResults = results ;
325
+ }
224
326
225
- updateResults ( results : SpotifySearchResult [ ] ) : void {
226
- this . results = results ;
327
+ updateAlbumsResults ( results : SpotifyAlbumSearchResult [ ] ) : void {
328
+ this . albumsResults = results ;
227
329
}
228
330
}
0 commit comments