-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPageViewModel.cs
440 lines (346 loc) · 15.1 KB
/
MainPageViewModel.cs
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using KKBOX.OpenAPI;
using KKBOX.OpenAPI.ServiceModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace OpenAPI.App
{
public partial class MainPageViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string ClientId { get; set; } = "";
public string ClientSecret { get; set; } = "";
public KKBOXAPI OpenAPI { get; private set; }
private bool isEnabled;
public bool IsEanbled
{
get { return isEnabled; }
set
{
isEnabled = value;
RaisePropertyChanged(nameof(IsEanbled));
}
}
public MainPageViewModel()
{
OpenAPI = new KKBOXAPI();
}
/// <summary>
/// Process OAuth authorized.
/// </summary>
public async Task GetAccessToken(string accessToken)
{
if (string.IsNullOrEmpty(accessToken))
{
var result = await KKBOXOAuth.SignInAsync(ClientId, ClientSecret);
if (result?.Staus == APIStatus.Success)
{
Debug.WriteLine($"access token: {result.Content.AccessToken}");
Debug.WriteLine($"expires in: {result.Content.ExipresIn}");
OpenAPI.AccessToken = result.Content.AccessToken;
this.IsEanbled = true;
}
}
else
{
OpenAPI.AccessToken = accessToken;
this.IsEanbled = true;
}
}
public async Task GetAlbumAndTracks()
{
string albumId = "WpTPGzNLeutVFHcFq6";
// Get album information
var albumResult = await OpenAPI.GetAlbumAsync(albumId);
var album = albumResult.Content;
Debug.WriteLine($"album: {album.Name}, {album.Id}");
PagingData tracksPaging = null;
// Get tracks of the album
var tracksResult = await OpenAPI.GetTracksOfAlbumAsync(album.Id, tracksPaging?.Limit, tracksPaging?.Offset);
var tracks = tracksResult.Content;
tracksPaging = tracks.Paging;
tracksPaging.Offset = tracks.Paging.NextOffset();
foreach (var track in tracks.Data)
{
Debug.WriteLine($"song: {track.Name}, {track.Id}");
}
if (tracksPaging.IsEnd() == false)
{
// Loop to get all tracks of the album
}
}
public async Task GetArtistAndAlbumTracksAndTopTracks()
{
string artistId = "8q3_xzjl89Yakn_7GB";
// Get artist information
var artistResult = await OpenAPI.GetArtistAsync(artistId);
var artist = artistResult.Content;
Debug.WriteLine($"artist: {artist.Name}, {artist.Id}");
PagingData albumsPaging = null;
// Get albums of the artist
var albumsResult = await OpenAPI.GetAlbumsOfArtistAsync(artist.Id, albumsPaging?.Limit, albumsPaging?.Offset);
var albums = albumsResult.Content;
albumsPaging = albums.Paging;
albumsPaging.Offset = albums.Paging.NextOffset();
foreach (var album in albums.Data)
{
Debug.WriteLine($"album: {album.Name}, {album.Id}");
}
if (albumsPaging.IsEnd())
{
// Loop to get all albums of the artist
}
PagingData topTracksPaging = null;
// Get top tracks of the artist
var topTracksResult = await OpenAPI.GetTopTracksOfArtistAsync(artist.Id, topTracksPaging?.Limit, topTracksPaging?.Offset);
var topTracks = topTracksResult.Content;
topTracksPaging = topTracks.Paging;
topTracksPaging.Offset = topTracks.Paging.NextOffset();
foreach (var track in topTracks.Data)
{
Debug.WriteLine($"top song: {track.Name}, {track.Id}");
}
if (topTracksPaging.IsEnd())
{
// Loop to get all top tracks of the artist
}
}
public async Task GetChartsAndPlaylistTracks()
{
var chartPlaylistsResult = await OpenAPI.GetChartListAsync();
var chartPlaylists = chartPlaylistsResult.Content;
foreach (var item in chartPlaylists.Data)
{
Debug.WriteLine($"chart playlist: {item.Title}, {item.Id}");
PagingData tracksPaging = null;
// Get playlist information
var playlist = await OpenAPI.GetPlaylistOfChartAsync(item.Id);
// Get tracks of the playlist
var tracksResult = await OpenAPI.GetPlaylistTracksOfChartAsync(item.Id, tracksPaging?.Limit, tracksPaging?.Offset);
var tracks = tracksResult.Content;
tracksPaging = tracks.Paging;
tracksPaging.Offset = tracks.Paging.NextOffset();
foreach (var track in tracks.Data)
{
Debug.WriteLine($"song: {track.Name}, {track.Id}");
}
if (tracksPaging.IsEnd())
{
// Loop to get all tracks of the playlist
}
}
}
public async Task GetFeaturedPlaylists()
{
PagingData paging = null;
// Get featured playlists
var featuredPlaylistsResult = await OpenAPI.GetFeaturedPlaylistsAsync(paging?.Limit, paging?.Offset);
var featuredPlaylists = featuredPlaylistsResult.Content;
foreach (var item in featuredPlaylists.Data)
{
Debug.WriteLine($"featured playlist: {item.Title}, {item.Id}");
// Get playlist information
var playlist = await OpenAPI.GetPlaylistOfFeaturedPlaylistsAsync(item.Id);
PagingData tracksPaging = null;
// Get tracks of the feature playlist
var tracksResult = await OpenAPI.GetPlaylistTracksOfFeaturedPlaylistsAsync(item.Id, tracksPaging?.Limit, tracksPaging?.Offset);
var tracks = tracksResult.Content;
tracksPaging = tracks.Paging;
tracksPaging.Offset = tracks.Paging.NextOffset();
foreach (var track in tracks.Data)
{
Debug.WriteLine($"song: {track.Name}, {track.Id}");
}
if (tracksPaging.IsEnd())
{
// Loop to get all tracks of the playlist
}
}
paging = featuredPlaylists.Paging;
paging.Offset = featuredPlaylists.Paging.NextOffset();
if (paging.IsEnd())
{
// Loop to get all feature playlists
}
}
public async Task GetFeaturedCategroyPlaylists()
{
// List of featured playlist categories.
var categoryListResult = await OpenAPI.GetFeaturedPlaylistCategoriesAsync();
var categoryList = categoryListResult.Content;
foreach (var item in categoryList.Data)
{
Debug.WriteLine($"featured playlist category: {item.Title}, {item.Id}");
// Get A Featured Playlist Category
var detailCategoryResult = await OpenAPI.GetFeaturedPlaylistCategoryAsync(item.Id);
var detailCategory = detailCategoryResult.Content;
foreach (var playlist in detailCategory.Playlists.Data)
{
Debug.WriteLine($"playlist of category: {playlist.Title}, {playlist.Id}");
}
if (detailCategory.Playlists.Paging.IsEnd())
{
continue;
}
PagingData playlistPaging = detailCategory.Playlists.Paging;
playlistPaging.Offset = detailCategory.Playlists.Paging.NextOffset();
var nextPlaylistResult = await OpenAPI.GetPlaylistsOfFeaturedPlaylistCategoryAsync(item.Id, playlistPaging.Limit, playlistPaging.Offset);
var nextPlaylist = nextPlaylistResult.Content;
foreach (var playlist in nextPlaylist.Data)
{
Debug.WriteLine($"playlist of category: {playlist.Title}, {playlist.Id}");
}
playlistPaging = nextPlaylist.Paging;
playlistPaging.Offset = playlistPaging.Limit + playlistPaging.Offset;
if (string.IsNullOrEmpty(nextPlaylist.Paging.Next))
{
break;
}
}
}
public async Task GetMoodStationList()
{
// List of mood stations.
var moonListResult = await OpenAPI.GetMoodStationListAsync();
var moonList = moonListResult.Content;
foreach (var mood in moonList.Data)
{
Debug.WriteLine($"mood: {mood.Title}, {mood.Id}");
// To retrieve information of the mood station with {station_id}.
var stationResult = await OpenAPI.GetMoodStationAsync(mood.Id);
var station = stationResult.Content;
foreach (var item in station.Tracks.Data)
{
Debug.WriteLine($"mood track: {item.Name}, {item.Id}");
}
}
}
public async Task GetGenreStationList()
{
// List of genre stations.
var genreListResult = await OpenAPI.GetGenreStationListAsync();
var genreList = genreListResult.Content;
foreach (var genre in genreList.Data)
{
Debug.WriteLine($"genre: {genre.Name}, {genre.Id}");
// To retrieve information of the genre station with {station_id}.
var stationResult = await OpenAPI.GetGenreStationAsync(genre.Id);
var station = stationResult.Content;
foreach (var item in station.Tracks.Data)
{
Debug.WriteLine($"genre track: {item.Name}, {item.Id}");
}
}
}
public async Task GetNewReleaseList()
{
PagingData categoryPaging = null;
// List of new release categories.
var newReleaseCategoryResult = await OpenAPI.GetNewReleaseCategoriesAsync(categoryPaging?.Limit, categoryPaging?.Offset);
var newReleaseCategory = newReleaseCategoryResult.Content;
foreach (var categoryItem in newReleaseCategory.Data)
{
Debug.WriteLine($"new release category: {categoryItem.Title}, {categoryItem.Id}");
// To retrieve information of the new release category with {category_id}.
var categoryDetailResult = await OpenAPI.GetNewReleaseCategoryAsync(categoryItem.Id);
var categoryDetail = categoryDetailResult.Content;
PagingData albumPaging = categoryDetail.Albums.Paging;
albumPaging.Offset = albumPaging.NextOffset();
foreach (var album in categoryDetail.Albums.Data)
{
Debug.WriteLine($"album of the new release: {album.Name}, {album.Id} : {categoryDetail.Title}");
}
if (albumPaging.IsEnd())
{
continue;
}
var albumsResult = await OpenAPI.GetAlbumsOfNewReleaseCategoryAsync(categoryItem.Id, albumPaging?.Limit, albumPaging?.Offset);
var albums = albumsResult.Content;
foreach (var album in albums.Data)
{
Debug.WriteLine($"album of the new release: {album.Name}, {album.Id} : {categoryDetail.Title}");
}
albumPaging = albums.Paging;
albumPaging.Offset = albumPaging.Offset + albumPaging.Limit;
if (string.IsNullOrEmpty(albums.Paging.Next))
{
// Loop to get all albums of the category
}
}
categoryPaging = newReleaseCategory.Paging;
categoryPaging.Offset = categoryPaging.Offset + categoryPaging.Limit;
if (string.IsNullOrEmpty(newReleaseCategory.Paging.Next))
{
// List of new release categories.
}
}
public async Task GetSearchResult()
{
string keyword = "祝我幸福";
PagingData paging = null;
SearchType[] searchType = { SearchType.album, SearchType.artist, SearchType.playlist, SearchType.track };
var searchResult = await OpenAPI.SearchAsync(keyword, paging?.Limit, paging?.Offset, searchType);
var result = searchResult.Content;
// get tracks
result.Tracks.Data.ForEach(x =>
{
Debug.WriteLine($"name: {x.Name}, album: {x.Album.Name}");
});
paging = result.Tracks.Paging;
paging.Offset = result.Tracks.Paging.NextOffset();
while (true)
{
if (string.IsNullOrEmpty(paging.Next))
{
break;
}
var nextResult = await OpenAPI.SearchAsync(keyword, paging?.Limit, paging.Offset, SearchType.track);
var next = nextResult.Content;
next.Tracks.Data.ForEach(x =>
{
Debug.WriteLine($"name: {x.Name}, album: {x.Album.Name}");
});
paging = next.Tracks.Paging;
paging.Offset = paging.NextOffset();
}
}
public async Task GetTrack()
{
var trackResult = await OpenAPI.GetTrackAsync("-rMBKj4tJ3FgYZK_5n");
var track = trackResult.Content;
Debug.WriteLine($"song: {track.Name}, {track.Id}");
}
public async Task GetNewHitsPlaylists()
{
PagingData paging = new PagingData
{
Offset = 0,
Limit = 10,
};
var playlistsResult = await OpenAPI.GetNewHitsPlaylistsAsync(paging.Limit, paging.Offset);
var playlists = playlistsResult.Content;
foreach (var item in playlists.Data)
{
Debug.WriteLine($"new hits playlist: {item.Title}");
var detailPlaylistResult = await OpenAPI.GetPlaylistOfNewHitsPlaylistsAsync(item.Id);
var detailPlaylist = detailPlaylistResult.Content;
foreach (var trackItem in detailPlaylist.Tracks.Data)
{
Debug.WriteLine($"private playlist track: {trackItem.Id}, {trackItem.Name}");
}
}
}
public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
try
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
catch
{
// nothing
}
}
}
}