Пример #1
0
        /// <summary>
        /// Search for the selected item in Spotify
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            string feedbackMessage = "";

            if (Feedback == null)
            {
                return;
            }
            Feedback.Text                     = "";
            feedbackMessage                   = "";
            PlaylistHeader.Visibility         = Visibility.Collapsed;
            TracklistHeader.Visibility        = Visibility.Collapsed;
            AlbumlistHeader.Visibility        = Visibility.Collapsed;
            ResultsHeaderContainer.Visibility = Visibility.Collapsed;
            if (SearchBox.Text == "")
            {
                feedbackMessage = "Please enter text to search for (I can't read your mind...yet)";
            }
            else
            {
                searchSave     = SearchBox.Text;
                searchTypeSave = SearchType.SelectedIndex;
                MainPanel.SetValue(MarginProperty, new Thickness(0, 20, 0, 0));
                RelativePanel.SetAlignTopWithPanel(SearchBox, true);
                ComboBoxItem selected         = SearchType.SelectedValue as ComboBoxItem;
                String       selectedString   = selected.Content.ToString().ToLower();
                UriBuilder   searchUriBuilder = new UriBuilder(SEARCH_URL);
                List <KeyValuePair <string, string> > queryParams = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("type", selectedString),
                    new KeyValuePair <string, string>("limit", "10"),
                    new KeyValuePair <string, string>("q", SearchBox.Text.Replace(" ", "+"))
                };
                string queryParamsString = RequestHandler.ConvertToQueryString(queryParams);
                searchUriBuilder.Query = queryParamsString;
                string searchResultString = await RequestHandler.SendCliGetRequest(searchUriBuilder.Uri.ToString());

                JsonObject searchResultJson = new JsonObject();
                try
                {
                    searchResultJson = JsonObject.Parse(searchResultString);
                }
                catch (COMException)
                {
                    return;
                }

                ClearResults();
                Results.Visibility = Visibility.Visible;

                // playlists
                if (selectedString == "playlist")
                {
                    if (searchResultJson.TryGetValue("playlists", out IJsonValue playlistsJson) && playlistsJson.ValueType == JsonValueType.Object)
                    {
                        JsonObject playlists = playlistsJson.GetObject();
                        if (playlists.TryGetValue("items", out IJsonValue itemsJson) && itemsJson.ValueType == JsonValueType.Array)
                        {
                            JsonArray playlistsArray = itemsJson.GetArray();
                            if (playlistsArray.Count == 0)
                            {
                                feedbackMessage = "No playlists found.";
                            }
                            else
                            {
                                ResultsHeaderContainer.Visibility = Visibility.Visible;
                                PlaylistHeader.Visibility         = Visibility.Visible;
                                long loadingKey = DateTime.Now.Ticks;
                                MainPage.AddLoadingLock(loadingKey);
                                App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, 0, playlistsArray.Count, loadingKey);
                                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);

                                        PlaylistList playlistList = new PlaylistList(playlist);
                                        try
                                        {
                                            if (!App.isInBackgroundMode)
                                            {
                                                Results.Items.Add(playlistList);
                                                if (Results.Items.IndexOf(playlistList) % 2 == 1)
                                                {
                                                    playlistList.TurnOffOpaqueBackground();
                                                }
                                            }
                                        }
                                        catch (COMException) { }
                                        App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, Results.Items.Count, playlistsArray.Count, loadingKey);
                                    }
                                }
                                MainPage.RemoveLoadingLock(loadingKey);
                            }
                        }
                    }
                }

                // track
                else if (selectedString == "track")
                {
                    if (searchResultJson.TryGetValue("tracks", out IJsonValue tracksJson) && tracksJson.ValueType == JsonValueType.Object)
                    {
                        JsonObject tracks = tracksJson.GetObject();
                        if (tracks.TryGetValue("items", out IJsonValue itemsJson) && itemsJson.ValueType == JsonValueType.Array)
                        {
                            JsonArray tracksArray = itemsJson.GetArray();
                            if (tracksArray.Count == 0)
                            {
                                feedbackMessage = "No tracks found.";
                            }
                            else
                            {
                                ResultsHeaderContainer.Visibility = Visibility.Visible;
                                TracklistHeader.Visibility        = Visibility.Visible;
                                long loadingKey = DateTime.Now.Ticks;
                                MainPage.AddLoadingLock(loadingKey);
                                App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, 0, tracksArray.Count, loadingKey);
                                foreach (JsonValue trackJson in tracksArray)
                                {
                                    Track track = new Track();
                                    await track.SetInfoDirect(trackJson.Stringify());

                                    TrackList trackList = new TrackList(track);
                                    try
                                    {
                                        if (!App.isInBackgroundMode)
                                        {
                                            Results.Items.Add(trackList);
                                            if (Results.Items.IndexOf(trackList) % 2 == 1)
                                            {
                                                trackList.TurnOffOpaqueBackground();
                                            }
                                        }
                                    }
                                    catch (COMException) { }
                                    App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, Results.Items.Count, tracksArray.Count, loadingKey);
                                }
                                MainPage.RemoveLoadingLock(loadingKey);
                            }
                        }
                    }
                }

                // album
                else if (selectedString == "album")
                {
                    if (searchResultJson.TryGetValue("albums", out IJsonValue albumsJson) && albumsJson.ValueType == JsonValueType.Object)
                    {
                        JsonObject albums = albumsJson.GetObject();
                        if (albums.TryGetValue("items", out IJsonValue itemsJson) && itemsJson.ValueType == JsonValueType.Array)
                        {
                            JsonArray albumsArray = itemsJson.GetArray();
                            if (albumsArray.Count == 0)
                            {
                                feedbackMessage = "No albums found.";
                            }
                            else
                            {
                                ResultsHeaderContainer.Visibility = Visibility.Visible;
                                AlbumlistHeader.Visibility        = Visibility.Visible;
                                long loadingKey = DateTime.Now.Ticks;
                                MainPage.AddLoadingLock(loadingKey);
                                App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, 0, albumsArray.Count, loadingKey);
                                foreach (JsonValue albumJson in albumsArray)
                                {
                                    Album album = new Album();
                                    await album.SetInfo(albumJson.Stringify());

                                    AlbumList albumList = new AlbumList(album);
                                    try
                                    {
                                        if (!App.isInBackgroundMode)
                                        {
                                            Results.Items.Add(albumList);
                                            if (Results.Items.IndexOf(albumList) % 2 == 1)
                                            {
                                                albumList.TurnOffOpaqueBackground();
                                            }
                                        }
                                    }
                                    catch (COMException) { }
                                    App.mainPage.SetLoadingProgress(PlaybackSource.Spotify, Results.Items.Count, albumsArray.Count, loadingKey);
                                }
                                MainPage.RemoveLoadingLock(loadingKey);
                            }
                        }
                    }
                }
            }
            Feedback.Text = feedbackMessage;
            if (feedbackMessage == "")
            {
                Feedback.Visibility = Visibility.Collapsed;
            }
            else
            {
                Feedback.Visibility = Visibility.Visible;
            }
        }