/// <summary> /// Retrieves top artists (10 most popular) from Nokia Music API. /// </summary> public void GetTopArtists() { if (!initialized) { return; } client.GetTopArtists((ListResponse <Artist> response) => { Deployment.Current.Dispatcher.BeginInvoke(() => { // Use results if (response != null && response.Result != null && response.Result.Count > 0) { App.ViewModel.TopArtists.Clear(); // Insert a place holder for title text App.ViewModel.TopArtists.Add(new ArtistModel() { Name = "TitlePlaceholderwho's hot", ItemHeight = "110", ItemWidth = "450" }); foreach (Artist a in response.Result) { App.ViewModel.TopArtists.Add(new ArtistModel() { Name = a.Name, Country = CountryCodes.CountryNameFromTwoLetter(a.Country), Genres = a.Genres[0].Name, Thumb100Uri = a.Thumb100Uri, Thumb200Uri = a.Thumb200Uri, Thumb320Uri = a.Thumb320Uri, Id = a.Id, ItemWidth = "205", ItemHeight = "205" }); } } if (response != null && response.Error != null) { ShowNokiaMusicApiError(); } HideProgressIndicator("GetTopArtists()"); }); }); ShowProgressIndicator("GetTopArtists()"); }
/// <summary> /// Retrieves similar artists for a selected artist from Nokia Music API. /// </summary> /// <param name="id">Id of the artist.</param> public void GetSimilarArtists(string id) { if (!initialized) { return; } App.ViewModel.SimilarForArtist.Clear(); App.ViewModel.NoSimilarVisibility = Visibility.Visible; client.GetSimilarArtists((ListResponse <Artist> response) => { Deployment.Current.Dispatcher.BeginInvoke(() => { // Use results if (response != null && response.Result != null && response.Result.Count > 0) { foreach (Artist a in response.Result) { App.ViewModel.SimilarForArtist.Add(new ArtistModel() { Name = a.Name, Country = CountryCodes.CountryNameFromTwoLetter(a.Country), Genres = a.Genres[0].Name, Thumb100Uri = a.Thumb100Uri, Thumb200Uri = a.Thumb200Uri, Thumb320Uri = a.Thumb320Uri, Id = a.Id, ItemWidth = "205", ItemHeight = "205" }); } } if (App.ViewModel.SimilarForArtist.Count() > 0) { App.ViewModel.NoSimilarVisibility = Visibility.Collapsed; } if (response != null && response.Error != null) { ShowNokiaMusicApiError(); } HideProgressIndicator("GetSimilarArtists()"); }); }, id); }
/// <summary> /// Retrieves top artists (10 most popular) from MixRadio API. /// </summary> public async void GetTopArtists() { if (!initialized) { return; } ShowProgressIndicator("GetTopArtists()"); ListResponse <Artist> response = await client.GetTopArtistsAsync(); if (response != null && response.Result != null && response.Result.Count > 0) { App.ViewModel.TopArtists.Clear(); // Insert a place holder for title text App.ViewModel.TopArtists.Add(new ArtistModel() { Name = "TitlePlaceholderwho's hot", ItemHeight = "110", ItemWidth = "450" }); foreach (Artist a in response.Result) { App.ViewModel.TopArtists.Add(new ArtistModel() { Name = a.Name, Country = CountryCodes.CountryNameFromTwoLetter(a.Country), Genres = a.Genres[0].Name, Thumb100Uri = a.Thumb100Uri, Thumb200Uri = a.Thumb200Uri, Thumb320Uri = a.Thumb320Uri, Id = a.Id, ItemWidth = "205", ItemHeight = "205" }); } } if (response != null && response.Error != null) { ShowMixRadioApiError(); } HideProgressIndicator("GetTopArtists()"); }
/// <summary> /// Retrieves information (id, genre, thumbnail, etc.) for local artists. /// This method initiates a chain of requests, which /// 1. requests artist information for each of the local artists /// one after another. /// 2. Initiates recommendations searching. /// </summary> public void GetArtistInfoForLocalAudio() { if (!initialized || localAudioResponses >= App.ViewModel.LocalAudio.Count) { return; } ArtistModel m = App.ViewModel.LocalAudio[localAudioResponses]; client.SearchArtists((ListResponse <Artist> response) => { Deployment.Current.Dispatcher.BeginInvoke(() => { // Use results if (response != null && response.Result != null && response.Result.Count > 0) { m.Id = response.Result[0].Id; m.Country = CountryCodes.CountryNameFromTwoLetter(response.Result[0].Country); m.Genres = response.Result[0].Genres[0].Name; int itemHeight = Int32.Parse(m.ItemHeight); m.Thumb100Uri = response.Result[0].Thumb100Uri; m.Thumb200Uri = response.Result[0].Thumb200Uri; m.Thumb320Uri = response.Result[0].Thumb320Uri; localArtists.Add(response.Result[0]); } if (response != null && response.Error != null) { ShowNokiaMusicApiError(); } HideProgressIndicator("GetArtistInfoForLocalAudio()"); localAudioResponses++; GetArtistInfoForLocalAudio(); if (localAudioResponses == App.ViewModel.LocalAudio.Count) { // Request recommendations after receiving info for all local artists FetchRecommendations(); } }); }, m.Name); ShowProgressIndicator("GetArtistInfoForLocalAudio()"); }
/// <summary> /// Retrieves information (id, genre, thumbnail, etc.) for local artists. /// This method initiates a chain of requests, which /// 1. requests artist information for each of the local artists /// one after another. /// 2. Initiates recommendations searching. /// </summary> public async void GetArtistInfoForLocalAudio() { if (!initialized || localAudioResponses >= App.ViewModel.LocalAudio.Count) { return; } ArtistModel m = App.ViewModel.LocalAudio[localAudioResponses]; ShowProgressIndicator("GetArtistInfoForLocalAudio()"); ListResponse <Artist> response = await client.SearchArtistsAsync(m.Name); if (response != null && response.Result != null && response.Result.Count > 0) { m.Id = response.Result[0].Id; m.Country = CountryCodes.CountryNameFromTwoLetter(response.Result[0].Country); if (response.Result[0].Genres != null) { m.Genres = response.Result[0].Genres[0].Name; } int itemHeight = Int32.Parse(m.ItemHeight); m.Thumb100Uri = response.Result[0].Thumb100Uri; m.Thumb200Uri = response.Result[0].Thumb200Uri; m.Thumb320Uri = response.Result[0].Thumb320Uri; localArtists.Add(response.Result[0]); } if (response != null && response.Error != null) { ShowMixRadioApiError(); } HideProgressIndicator("GetArtistInfoForLocalAudio()"); localAudioResponses++; GetArtistInfoForLocalAudio(); if (localAudioResponses == App.ViewModel.LocalAudio.Count) { // Request recommendations after receiving info for all local artists await FetchRecommendations(); } }
/// <summary> /// Retrieves similar artists for a selected artist from MixRadio API. /// </summary> /// <param name="id">Id of the artist.</param> public async void GetSimilarArtists(string id) { if (!initialized) { return; } App.ViewModel.SimilarForArtist.Clear(); App.ViewModel.NoSimilarVisibility = Visibility.Visible; ListResponse <Artist> response = await client.GetSimilarArtistsAsync(id); if (response != null && response.Result != null && response.Result.Count > 0) { foreach (Artist a in response.Result) { App.ViewModel.SimilarForArtist.Add(new ArtistModel() { Name = a.Name, Country = CountryCodes.CountryNameFromTwoLetter(a.Country), Genres = a.Genres[0].Name, Thumb100Uri = a.Thumb100Uri, Thumb200Uri = a.Thumb200Uri, Thumb320Uri = a.Thumb320Uri, Id = a.Id, ItemWidth = "205", ItemHeight = "205" }); } } if (App.ViewModel.SimilarForArtist.Count() > 0) { App.ViewModel.NoSimilarVisibility = Visibility.Collapsed; } if (response != null && response.Error != null) { ShowMixRadioApiError(); } HideProgressIndicator("GetSimilarArtists()"); }
/// <summary> /// Retrieves top artists of a selected genre from MixRadio API. /// </summary> /// <param name="id">Id of the genre.</param> public async void GetTopArtistsForGenre(string id) { if (!initialized) { return; } ShowProgressIndicator("GetTopArtistsForGenre()"); ListResponse <Artist> response = await client.GetTopArtistsForGenreAsync(id); if (response != null && response.Result != null && response.Result.Count > 0) { App.ViewModel.TopArtistsForGenre.Clear(); foreach (Artist a in response.Result) { App.ViewModel.TopArtistsForGenre.Add(new ArtistModel() { Name = a.Name, Country = CountryCodes.CountryNameFromTwoLetter(a.Country), Genres = a.Genres[0].Name, Thumb100Uri = a.Thumb100Uri, Thumb200Uri = a.Thumb200Uri, Thumb320Uri = a.Thumb320Uri, Id = a.Id, ItemWidth = "205", ItemHeight = "205" }); } } if (response != null && response.Error != null) { ShowMixRadioApiError(); } HideProgressIndicator("GetTopArtistsForGenre()"); }
/// <summary> /// Builds the recommended artists lists. /// This method initiates a chain of requests, which requests similar /// artists information for each of the local artists one after another. /// </summary> private async Task FetchRecommendations() { if (!initialized || localArtists.Count <= recommendResponses) { if (initialized && App.ViewModel.Recommendations.Count <= 0) { App.ViewModel.NoRecommendedVisibility = Visibility.Visible; } else { App.ViewModel.NoRecommendedVisibility = Visibility.Collapsed; } // limit the number of recommended artists to 10 if (localArtists.Count == recommendResponses) { int i = App.ViewModel.Recommendations.Count - 1; while (i > 20) { App.ViewModel.Recommendations.RemoveAt(i); i--; } } return; } ShowProgressIndicator("FetchRecommendations()"); ListResponse <Artist> response = await client.GetSimilarArtistsAsync(localArtists[recommendResponses].Id); if (response != null && response.Result != null && response.Result.Count > 0) { foreach (Artist a in response.Result) { bool handled = false; // Don't recommend artists already stored in device. if (App.ViewModel.IsLocalArtist(a.Name)) { handled = true; } // Check if the artist has already been recommended -> add some weight // to recommendation. if (!handled) { for (int i = 0; i < App.ViewModel.Recommendations.Count; i++) { if (App.ViewModel.Recommendations[i].Name == a.Name) { handled = true; App.ViewModel.Recommendations[i].SimilarArtistCount++; // position according to weight if (i > 1) { int j = 1; for (j = i - 1; j > 1; j--) { if (App.ViewModel.Recommendations[j].SimilarArtistCount >= App.ViewModel.Recommendations[i].SimilarArtistCount) { j++; // This item (j) has been ranked higher or equal - correct index is one more break; } } if (i > j) { ArtistModel artist = App.ViewModel.Recommendations[i]; App.ViewModel.Recommendations.RemoveAt(i); App.ViewModel.Recommendations.Insert(j, artist); break; } } } } } // If the artist is not present in the device and has not yet been // recommended, do it now. if (!handled) { App.ViewModel.Recommendations.Add(new ArtistModel() { Name = a.Name, Country = CountryCodes.CountryNameFromTwoLetter(a.Country), Genres = a.Genres == null ? null : a.Genres[0].Name, Thumb100Uri = a.Thumb100Uri, Thumb200Uri = a.Thumb200Uri, Thumb320Uri = a.Thumb320Uri, Id = a.Id, SimilarArtistCount = 1, ItemWidth = "205", ItemHeight = "205" }); } } } if (response != null && response.Error != null) { ShowMixRadioApiError(); } HideProgressIndicator("FetchRecommendations()"); recommendResponses++; await FetchRecommendations(); }