From adfc893cf601513ceb9c621b9b1e071dcdafb65a Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Sat, 30 Jun 2018 23:06:15 +0300 Subject: [PATCH] Development 3 --- FoxTube/Classes/SecretsVault.cs | 71 ++---- FoxTube/Controls/SuggestionsQueries.xaml | 30 +++ FoxTube/Controls/SuggestionsQueries.xaml.cs | 113 ++++++++++ FoxTube/FoxTube.csproj | 17 +- FoxTube/Pages/MainPage.xaml | 53 ++--- FoxTube/Pages/MainPage.xaml.cs | 231 +++++++++----------- 6 files changed, 311 insertions(+), 204 deletions(-) create mode 100644 FoxTube/Controls/SuggestionsQueries.xaml create mode 100644 FoxTube/Controls/SuggestionsQueries.xaml.cs diff --git a/FoxTube/Classes/SecretsVault.cs b/FoxTube/Classes/SecretsVault.cs index 547d972..bd40783 100644 --- a/FoxTube/Classes/SecretsVault.cs +++ b/FoxTube/Classes/SecretsVault.cs @@ -12,6 +12,10 @@ using Windows.UI; using Windows.UI.Xaml.Documents; using System.Net; using System.Threading; +using Windows.Data.Json; +using Windows.Storage.Streams; +using Windows.Security.Cryptography; +using Windows.Security.Cryptography.Core; using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2.Flows; @@ -19,6 +23,7 @@ using Google.Apis.Services; using Google.Apis.Util.Store; using Google.Apis.YouTube.v3; using Google.Apis.Auth.OAuth2.Responses; +using Windows.Storage; namespace FoxTube { @@ -58,19 +63,27 @@ namespace FoxTube #region Object containers public bool IsLoged = false; - public TokenResponse Token; - private UserCredential Credential; + public UserCredential Credential; public event EventHandler AuthorizationStateChanged; - public SecretsVault() + public async void Authorize() { - CheckAuthorization(); + try { Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None); } + catch { } + if(Credential != null) + { + IsLoged = true; + AuthorizationStateChanged.Invoke(this, null); + } } - public async Task Authorize() + public async void Deauthenticate() { - Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None); - AuthorizationStateChanged.Invoke(this, null); + if(await Credential.RevokeTokenAsync(CancellationToken.None)) + { + Credential = null; + AuthorizationStateChanged.Invoke(this, null); + } } public async void CheckAuthorization() @@ -83,49 +96,7 @@ namespace FoxTube if (token == null) IsLoged = false; else - { - IsLoged = true; - Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None); - AuthorizationStateChanged.Invoke(this, null); - } - } - #endregion - - #region User credentials - private List credentials = new List(); //Test variable simulating actual credentials storage - - public void AddAccount() - { - - - credentials.Add( - new UserCredential( - new AuthorizationCodeFlow( - new GoogleAuthorizationCodeFlow.Initializer() - { - ClientSecrets = Secrets, - DataStore = null //TO-DO: Replace with new PasswordsVaultDataStore() - }), - "userId", - new TokenResponse() - { - AccessToken = "done", - ExpiresInSeconds = 1, - IdToken = "done", - TokenType = "done", - RefreshToken = "done", - Scope = "youtube" - })); - } - - public void DeleteAccount(int index) - { - credentials.RemoveAt(index); - } - - public UserCredential RetrieveAccount(int index) - { - return credentials[index]; + Authorize(); } #endregion } diff --git a/FoxTube/Controls/SuggestionsQueries.xaml b/FoxTube/Controls/SuggestionsQueries.xaml new file mode 100644 index 0000000..80b39b9 --- /dev/null +++ b/FoxTube/Controls/SuggestionsQueries.xaml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FoxTube/Controls/SuggestionsQueries.xaml.cs b/FoxTube/Controls/SuggestionsQueries.xaml.cs new file mode 100644 index 0000000..62ae3e0 --- /dev/null +++ b/FoxTube/Controls/SuggestionsQueries.xaml.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading.Tasks; +using System.Xml; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.Storage; +using Windows.UI; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + +// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 + +namespace FoxTube.Controls +{ + public sealed partial class SuggestionsQueries : UserControl + { + ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; + List lastRequests = new List(); + public SuggestionsQueries() + { + this.InitializeComponent(); + for (int k = 0; k < 5; k++) + if (settings.Values["history" + k] != null) + lastRequests.Add((string)settings.Values["history" + k]); + } + + private void Item_Click(object sender, RoutedEventArgs e) + { + string term = (sender as Button).Content as string; + if (!lastRequests.Contains(term)) + AddToHistory(term); + Methods.MainPage.GoToSearch(term); + } + + public async void BuildList(string keyword) + { + (grid.Children[0] as StackPanel).Visibility = Visibility.Visible; + searchStandby.Visibility = Visibility.Visible; + suggestions.Children.Clear(); + + foreach(string s in lastRequests) + { + Button item = new Button() + { + HorizontalAlignment = HorizontalAlignment.Stretch, + HorizontalContentAlignment = HorizontalAlignment.Left, + Background = new SolidColorBrush(Colors.Transparent), + Content = s + }; + item.Click += Item_Click; + history.Children.Add(item); + } + + XmlDocument doc = new XmlDocument(); + await Task.Run(() => + { + doc.Load(string.Format("http://suggestqueries.google.com/complete/search?output=toolbar&hl={0}&q={1}", + (settings.Values["region"] as string)[0] + (settings.Values["region"] as string)[1], + keyword)); + }); + + if(doc["toplevel"].HasChildNodes) + for (int k = 0; k < 5; k++) + try + { + Button item = new Button() + { + HorizontalAlignment = HorizontalAlignment.Stretch, + HorizontalContentAlignment = HorizontalAlignment.Left, + Background = new SolidColorBrush(Colors.Transparent), + Content = doc["toplevel"].ChildNodes[k]["suggestion"].GetAttribute("data") + }; + item.Click += Item_Click; + suggestions.Children.Add(item); + } catch { break; } + else + suggestions.Children.Add(new Button() + { + Content = "No suggestions found.", + IsEnabled = false, + Foreground = new SolidColorBrush(Colors.Gray), + Background = new SolidColorBrush(Colors.Transparent) + }); + + searchStandby.Visibility = Visibility.Collapsed; + } + + public void AddToHistory(string keyword) + { + lastRequests.Insert(0, keyword); + if (lastRequests.Count > 5) + lastRequests.RemoveAt(5); + + for(int k = 0; k < 5; k++) + try { settings.Values["history" + k] = lastRequests[k]; } + catch { settings.Values.Add("history" + k, lastRequests[k]); } + } + + public void Hide() + { + (grid.Children[0] as StackPanel).Visibility = Visibility.Collapsed; + } + } +} diff --git a/FoxTube/FoxTube.csproj b/FoxTube/FoxTube.csproj index afb80b4..e5a5fbf 100644 --- a/FoxTube/FoxTube.csproj +++ b/FoxTube/FoxTube.csproj @@ -100,6 +100,9 @@ CommentCard.xaml + + SuggestionsQueries.xaml + Channel.xaml @@ -231,6 +234,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -313,19 +320,19 @@ 1.0.1 - 1.34.0 + 1.30.0-beta02 - 1.34.0 + 1.30.0-beta02 - 1.34.0 + 1.30.0-beta02 - 1.34.0.1134 + 1.29.2.994 - 1.34.0.1226 + 1.29.2.1006 6.1.5 diff --git a/FoxTube/Pages/MainPage.xaml b/FoxTube/Pages/MainPage.xaml index bfe1187..fc7db58 100644 --- a/FoxTube/Pages/MainPage.xaml +++ b/FoxTube/Pages/MainPage.xaml @@ -18,11 +18,10 @@ - + + + - +