From f28b9a726fda4ac14cd61d8ee077fc2354110a85 Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Fri, 11 May 2018 22:58:38 +0300 Subject: [PATCH] Search suggestions --- FoxTube/MainPage.xaml | 29 +++++----- FoxTube/MainPage.xaml.cs | 115 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 14 deletions(-) diff --git a/FoxTube/MainPage.xaml b/FoxTube/MainPage.xaml index 88a35d8..7ae3e73 100644 --- a/FoxTube/MainPage.xaml +++ b/FoxTube/MainPage.xaml @@ -108,37 +108,40 @@ Background="Transparent" FontFamily="Segoe MDL2 Assets" Content="" FontSize="20" Foreground="Black"/> - - + + - - + + + + + - + - + - + - + @@ -150,31 +153,31 @@ - + - + - + - + - + diff --git a/FoxTube/MainPage.xaml.cs b/FoxTube/MainPage.xaml.cs index 311beb7..78d6aab 100644 --- a/FoxTube/MainPage.xaml.cs +++ b/FoxTube/MainPage.xaml.cs @@ -11,6 +11,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Shapes; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; @@ -369,7 +370,83 @@ namespace FoxTube async void buildSearchSuggestionsTree(string keyword) { - + searchSuggestionsList.Items.Clear(); + searchStandby.Visibility = Visibility.Visible; + searchSuggestionsList.Visibility = Visibility.Collapsed; + + XmlDocument doc = new XmlDocument(); + await Task.Run(() => + { + doc.Load(string.Format("http://suggestqueries.google.com/complete/search?output=toolbar&hl={0}&q={1}", "en", keyword)); + }); + + for (int k = 0; k < 5; k++) + try + { + searchSuggestionsList.Items.Add(new ListBoxItem() + { + Content = doc["toplevel"].ChildNodes[k]["suggestion"].GetAttribute("data") + }); + } + catch (NullReferenceException) + { + searchSuggestionsList.Items.Clear(); + searchSuggestionsList.Items.Add(new ListBoxItem() + { + Content = "No suggestions found.", + IsEnabled = false, + Foreground = new SolidColorBrush(Colors.Gray) + }); + } + + /*ListBoxItem separator = new ListBoxItem() + { + Padding = new Thickness(0), + IsEnabled = false + }; + StackPanel stack = new StackPanel() { Orientation = Orientation.Horizontal }; + stack.Children.Add(new TextBlock() + { + Foreground = new SolidColorBrush(Colors.Gray), + Text = "Previous requests", + FontSize = 12, + Margin = new Thickness(0), + Padding = new Thickness(0, 0, 5, 0) + }); + stack.Children.Add(new Line() + { + X1 = 0, + X2 = 250, + Y1 = 10, + Y2 = 10, + Stroke = new SolidColorBrush(Colors.Gray), + StrokeThickness = 2 + }); + separator.Content = stack; + searchSuggestionsList.Items.Add(separator); + + if (settings.Values["history0"] != null) + for (int k = 0; k < 5; k++) + try + { + searchSuggestionsList.Items.Add(new ListBoxItem() + { + Content = settings.Values["history" + k] + }); + } + catch (NullReferenceException) + { + break; + } + else searchSuggestionsList.Items.Add(new ListBoxItem() + { + Content = "You have no previous requests.", + Foreground = new SolidColorBrush(Colors.Gray), + IsEnabled = false + });*/ + + searchStandby.Visibility = Visibility.Collapsed; + searchSuggestionsList.Visibility = Visibility.Visible; } private void searchField_LostFocus(object sender, RoutedEventArgs e) @@ -389,6 +466,32 @@ namespace FoxTube topHamburger.SelectedItem = null; bottomHaburger.SelectedItem = null; + /*{ + List history = new List(); + for(int k = 0; k < 5; k++) + try + { + history.Add(settings.Values["history" + k].ToString()); + } + catch(NullReferenceException) + { + break; + } + history.Insert(0, keyword); + if (history.Count > 5) + history.RemoveAt(5); + + for(int k = 0; k < history.Count; k++) + try + { + settings.Values["history" + k] = history[k]; + } + catch (NullReferenceException) + { + settings.Values.Add("history" + k, keyword); + } + }*/ + YouTubeService ytService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", @@ -450,5 +553,15 @@ namespace FoxTube return ""; } } + + private void searchSuggestionsList_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + if(e.AddedItems.Count > 0) + { + ListBoxItem item = e.AddedItems[0] as ListBoxItem; + searchField.Text = item.Content.ToString(); + searchButton_Click(this, null); + } + } } }