/// <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 void 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 20 if (localArtists.Count == recommendResponses) { int i = App.ViewModel.Recommendations.Count - 1; while (i > 20) { App.ViewModel.Recommendations.RemoveAt(i); i--; } } return; } 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) { 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[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) { ShowNokiaMusicApiError(); } HideProgressIndicator("FetchRecommendations()"); recommendResponses++; FetchRecommendations(); }); }, localArtists[recommendResponses].Id); ShowProgressIndicator("FetchRecommendations()"); }