// This feels awfully hacky. (and potentially very slow). // For each element in the received JSON, fetch the song object from our previously created "allSongs" list // then fetch the playlist from our "allPlaylists" list // finally, remove the playlist from the list, update it to have the song object attached, and then re-add it to the list (this will change the list order, but that's not particuarly important) // A song which has been "deleted" from a playlist won't be added to it. private void PlaylistSongsReceived(HttpWebRequest request, HttpWebResponse response, String jsonData, Exception error) { JObject allSongsReceived = JObject.Parse(jsonData); foreach (JObject song in allSongsReceived["data"]["items"]) { GMusicPlaylistEntry thisSong = JsonConvert.DeserializeObject <GMusicPlaylistEntry>(song.ToString()); GMusicPlaylist thisPlaylist = allPlaylists.FirstOrDefault(p => p.ID == thisSong.PlaylistID); if (thisPlaylist != null) { // I think as the list contains a reference to the object, it will be updated thisPlaylist.Songs.Add(thisSong); } } JToken nextPageToken = allSongsReceived.GetValue("nextPageToken"); if (nextPageToken != null) { string nextPageTokenString = nextPageToken.ToString(); GetAllPlaylistSongs(nextPageTokenString); return; } log.Log("Playlists fetched!"); if (OnGetAllPlaylistsComplete != null) { OnGetAllPlaylistsComplete(allPlaylists); } }
// This could delete multiple selections at once, but it doesn't private void deleteSong_Click(object sender, EventArgs e) { api.OnDeleteFromPlaylistComplete = OnChangePlaylist; GMusicSong selectedSong = (GMusicSong)playlistSongsBox.SelectedItem; GMusicPlaylist selectedPlaylist = (GMusicPlaylist)playlistListBox.SelectedItem; GMusicPlaylistEntry songEntry = selectedPlaylist.Songs.First(s => s.TrackID == selectedSong.ID); api.DeleteFromPlaylist(new List <GMusicPlaylistEntry> { songEntry }); }