/// <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 new releases albums /// </summary> /// <param name="sender">The refresh button</param> /// <param name="e">The routed event arguments</param> private async void NewReleasesRefresh_Click(object sender, RoutedEventArgs e) { newReleasesOffset = 0; while (NewReleasesAlbums.Items.Count > 0) { AlbumHero albumHero = NewReleasesAlbums.Items.ElementAt(0) as AlbumHero; albumHero.Unload(); NewReleasesAlbums.Items.Remove(albumHero); } await LoadNewReleases(); }
/// <summary> /// Loads the New Releases playlists /// </summary> private async Task LoadNewReleases() { NewReleasesMore.IsEnabled = false; NewReleasesRefresh.IsEnabled = false; long loadingKey = DateTime.Now.Ticks; MainPage.AddLoadingLock(loadingKey); App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, 0, 1, loadingKey); UriBuilder newReleasesPlaylistsBuilder = new UriBuilder(NEW_RELEASES_HREF); List <KeyValuePair <string, string> > queryParams = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("limit", newReleasesLimit.ToString()), new KeyValuePair <string, string>("offset", newReleasesOffset.ToString()) }; newReleasesPlaylistsBuilder.Query = RequestHandler.ConvertToQueryString(queryParams); string playlistsString = await RequestHandler.SendCliGetRequest(newReleasesPlaylistsBuilder.Uri.ToString()); JsonObject newReleasesPlaylistsJson = new JsonObject(); try { newReleasesPlaylistsJson = JsonObject.Parse(playlistsString); } catch (COMException) { return; } if (newReleasesPlaylistsJson.TryGetValue("albums", out IJsonValue albums) && albums.ValueType == JsonValueType.Object) { JsonObject alubms = albums.GetObject(); if (alubms.TryGetValue("total", out IJsonValue totalJson) && totalJson.ValueType == JsonValueType.Number) { newReleasesMax = Convert.ToInt32(totalJson.GetNumber()); } if (alubms.TryGetValue("items", out IJsonValue itemsJson) && itemsJson.ValueType == JsonValueType.Array) { JsonArray albumsArray = itemsJson.GetArray(); foreach (JsonValue albumJson in albumsArray) { if (albumJson.GetObject().TryGetValue("href", out IJsonValue fullHref) && fullHref.ValueType == JsonValueType.String) { string fullAlbumString = await RequestHandler.SendCliGetRequest(fullHref.GetString()); Album alubm = new Album(); await alubm.SetInfo(fullAlbumString); AlbumHero albumHero = new AlbumHero(alubm); NewReleasesAlbums.Items.Add(albumHero); App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, NewReleasesAlbums.Items.Count, albumsArray.Count, loadingKey); } } } } NewReleasesRefresh.IsEnabled = true; if (newReleasesOffset + newReleasesLimit >= newReleasesMax) { NewReleasesMore.Content = "No More"; NewReleasesMore.IsEnabled = false; } else { NewReleasesMore.Content = "More"; NewReleasesMore.IsEnabled = true; } MainPage.RemoveLoadingLock(loadingKey); }