/// <summary> /// Free up memory /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public async void Page_Unloaded(object sender, RoutedEventArgs e) { if (App.isInBackgroundMode) { featuredOffset = 0; newReleasesOffset = 0; await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { MainPivot.SelectionChanged -= MainPivot_SelectionChanged; FeaturedPlaylists.ItemClick -= FeaturedPlaylist_ItemClick; FeaturedPlaylists.ClearValue(XYFocusUpProperty); while (FeaturedPlaylists.Items.Count > 0) { PlaylistHero playlistHero = FeaturedPlaylists.Items.ElementAt(0) as PlaylistHero; FeaturedPlaylists.Items.Remove(playlistHero); playlistHero.Unload(); } FeaturedRefresh.Click -= FeaturedRefresh_Click; FeaturedMore.Click -= FeaturedMore_Click; NewReleasesAlbums.ItemClick -= NewReleasesAlbums_ItemClick; NewReleasesAlbums.ClearValue(XYFocusUpProperty); while (NewReleasesAlbums.Items.Count > 0) { AlbumHero albumHero = NewReleasesAlbums.Items.ElementAt(0) as AlbumHero; NewReleasesAlbums.Items.Remove(albumHero); albumHero.Unload(); } NewReleasesRefresh.Click -= NewReleasesRefresh_Click; NewReleasesMore.Click -= NewReleasesMore_Click; }); } }
/// <summary> /// Refreshes the list of featured playlists /// </summary> /// <param name="sender">The refresh button</param> /// <param name="e">The routed event arguments</param> private async void FeaturedRefresh_Click(object sender, RoutedEventArgs e) { featuredOffset = 0; while (FeaturedPlaylists.Items.Count > 0) { PlaylistHero playlistHero = FeaturedPlaylists.Items.ElementAt(0) as PlaylistHero; playlistHero.Unload(); FeaturedPlaylists.Items.Remove(playlistHero); } await LoadFeaturedPlaylists(); }
/// <summary> /// Loads the featured playlists /// </summary> /// <returns></returns> private async Task LoadFeaturedPlaylists() { long loadingKey = DateTime.Now.Ticks; MainPage.AddLoadingLock(loadingKey); FeaturedMore.IsEnabled = false; FeaturedRefresh.IsEnabled = false; App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, 0, 1, loadingKey); UriBuilder featuredPlaylistsBuilder = new UriBuilder(FEATURED_HREF); List <KeyValuePair <string, string> > queryParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("limit", featuredLimit.ToString()), new KeyValuePair <string, string>("offset", featuredOffset.ToString()) }; featuredPlaylistsBuilder.Query = RequestHandler.ConvertToQueryString(queryParams); string playlistsString = await RequestHandler.SendCliGetRequest(featuredPlaylistsBuilder.Uri.ToString()); JsonObject featuredPlaylistsJson = new JsonObject(); try { featuredPlaylistsJson = JsonObject.Parse(playlistsString); } catch (COMException) { return; } if (featuredPlaylistsJson.TryGetValue("message", out IJsonValue messageJson) && messageJson.ValueType == JsonValueType.String) { FeaturedMessage.Text = messageJson.GetString(); } if (featuredPlaylistsJson.TryGetValue("playlists", out IJsonValue playlistsJson) && playlistsJson.ValueType == JsonValueType.Object) { JsonObject playlists = playlistsJson.GetObject(); if (playlists.TryGetValue("total", out IJsonValue totalJson) && totalJson.ValueType == JsonValueType.Number) { featuredMax = Convert.ToInt32(totalJson.GetNumber()); } if (playlists.TryGetValue("items", out IJsonValue itemsJson) && itemsJson.ValueType == JsonValueType.Array) { JsonArray playlistsArray = itemsJson.GetArray(); foreach (JsonValue playlistJson in playlistsArray) { if (playlistJson.GetObject().TryGetValue("href", out IJsonValue fullHref) && fullHref.ValueType == JsonValueType.String) { string fullPlaylistString = await RequestHandler.SendCliGetRequest(fullHref.GetString()); Playlist playlist = new Playlist(); await playlist.SetInfo(fullPlaylistString); PlaylistHero playlistHero = new PlaylistHero(playlist); FeaturedPlaylists.Items.Add(playlistHero); App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, FeaturedPlaylists.Items.Count, playlistsArray.Count, loadingKey); } } } } FeaturedRefresh.IsEnabled = true; if (featuredOffset + featuredLimit >= featuredMax) { FeaturedMore.Content = "No More"; FeaturedMore.IsEnabled = false; } else { FeaturedMore.Content = "More"; FeaturedMore.IsEnabled = true; } MainPage.RemoveLoadingLock(loadingKey); }