From efb07fda10c1a78fc92ed865778e2f8b003104c0 Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Sun, 23 Dec 2018 22:22:22 +0300 Subject: [PATCH] 222: Fixed 227: Additional fixes (final) Introduced new settings storing system Related Work Items: #222, #227 --- FoxTube/App.xaml.cs | 1 + FoxTube/Classes/Methods.cs | 16 +++ FoxTube/Classes/SettingsStorage.cs | 12 +- FoxTube/Pages/Home.xaml | 12 +- FoxTube/Pages/Home.xaml.cs | 135 +++----------------- FoxTube/Pages/MainPage.xaml.cs | 10 +- FoxTube/Pages/Search.xaml.cs | 30 +---- FoxTube/Pages/SettingsPages/General.xaml.cs | 109 ++++++---------- 8 files changed, 96 insertions(+), 229 deletions(-) diff --git a/FoxTube/App.xaml.cs b/FoxTube/App.xaml.cs index bfc8da6..e4ee801 100644 --- a/FoxTube/App.xaml.cs +++ b/FoxTube/App.xaml.cs @@ -244,6 +244,7 @@ namespace FoxTube private void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); + SettingsStorage.ExportSettings(); DownloadAgent.QuitPrompt(); deferral.Complete(); } diff --git a/FoxTube/Classes/Methods.cs b/FoxTube/Classes/Methods.cs index b893da2..bd39395 100644 --- a/FoxTube/Classes/Methods.cs +++ b/FoxTube/Classes/Methods.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Text.RegularExpressions; using System.Web; using Windows.ApplicationModel.Core; @@ -33,6 +34,21 @@ namespace FoxTube return new Uri(url); } + public static void ForEach(this IEnumerable array, Action action) + { + array.ToList().ForEach(action); + } + + public static T Find(this IEnumerable array, Predicate match) + { + return array.ToList().Find(match); + } + + public static List FindAll(this IEnumerable array, Predicate match) + { + return array.ToList().FindAll(match); + } + public static string ReplaceInvalidChars(this string str, char newValue) { foreach (char i in Path.GetInvalidFileNameChars()) diff --git a/FoxTube/Classes/SettingsStorage.cs b/FoxTube/Classes/SettingsStorage.cs index fa5437b..6ce1403 100644 --- a/FoxTube/Classes/SettingsStorage.cs +++ b/FoxTube/Classes/SettingsStorage.cs @@ -176,11 +176,19 @@ namespace FoxTube public static async void SaveData() { storage.Values["settings"] = JsonConvert.SerializeObject(settings); + ExportSettings(); + } - bool[] notificationsSettings = new[] { VideoNotifications, DevNotifications }; - await FileIO.WriteTextAsync( + public static async void ExportSettings() + { + try + { + bool[] notificationsSettings = new[] { VideoNotifications, DevNotifications }; + await FileIO.WriteTextAsync( await ApplicationData.Current.RoamingFolder.CreateFileAsync("notifications.json", CreationCollisionOption.ReplaceExisting), JsonConvert.SerializeObject(notificationsSettings)); + } + catch { } } } } diff --git a/FoxTube/Pages/Home.xaml b/FoxTube/Pages/Home.xaml index 46eaa88..4b3dfdc 100644 --- a/FoxTube/Pages/Home.xaml +++ b/FoxTube/Pages/Home.xaml @@ -15,13 +15,10 @@ - + - - - - + @@ -34,10 +31,7 @@ - - - - + diff --git a/FoxTube/Pages/Home.xaml.cs b/FoxTube/Pages/Home.xaml.cs index 50889e3..42574ae 100644 --- a/FoxTube/Pages/Home.xaml.cs +++ b/FoxTube/Pages/Home.xaml.cs @@ -1,68 +1,39 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; 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; -using System.Globalization; - using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; using Windows.Storage; -using Windows.UI.Text; using FoxTube.Controls; using FoxTube.Pages; -using System.Collections.ObjectModel; -using System.Diagnostics; - -// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 +using System.Globalization; namespace FoxTube { /// - /// An empty page that can be used on its own or navigated to within a Frame. + /// Home page /// public sealed partial class Home : Page { - private bool recLoaded = false; private bool trendLoaded = false; - private bool subsLoaded = false; - VideoGrid recGrid, trendGrid, subsGrid; - ShowMore recMore, trendMore, subsMore; + VideoGrid trendGrid; + ShowMore trendMore; LoadingPage loading; - string trendToken, recToken; + string trendToken; Dictionary subsTokens = new Dictionary(); string reg; public Home() { - this.InitializeComponent(); - try - { - reg = (ApplicationData.Current.LocalSettings.Values["region"] as string).ToLower().Remove(0, 3); - } - catch (ArgumentOutOfRangeException) - { - reg = (ApplicationData.Current.LocalSettings.Values["region"] as string).ToUpper(); - } - - recGrid = ((recommended.Content as ScrollViewer).Content as StackPanel).Children[0] as VideoGrid; + InitializeComponent(); + reg = CultureInfo.GetCultures(CultureTypes.AllCultures).Find(i => i.IetfLanguageTag == SettingsStorage.Region).IetfLanguageTag.Remove(0, 3); + trendGrid = ((trending.Content as ScrollViewer).Content as StackPanel).Children[0] as VideoGrid; - subsGrid = ((subscriptions.Content as ScrollViewer).Content as StackPanel).Children[0] as VideoGrid; - - recMore = ((recommended.Content as ScrollViewer).Content as StackPanel).Children[1] as ShowMore; - trendMore = ((trending.Content as ScrollViewer).Content as StackPanel).Children[1] as ShowMore; ; - subsMore = ((subscriptions.Content as ScrollViewer).Content as StackPanel).Children[1] as ShowMore; + trendMore = ((trending.Content as ScrollViewer).Content as StackPanel).Children[1] as ShowMore; loading = grid.Children[2] as LoadingPage; @@ -74,11 +45,6 @@ namespace FoxTube Methods.MainPage.GoToHome(); } - private void RecMore_Clicked() - { - throw new NotImplementedException(); - } - private async void TrendMore_Clicked() { try @@ -99,7 +65,7 @@ namespace FoxTube foreach (Video vid in response.Items) { VideoCard vCard = new VideoCard(vid.Id); - recGrid.Add(vCard); + trendGrid.Add(vCard); } trendMore.Complete(); } @@ -109,45 +75,6 @@ namespace FoxTube } } - private async void SubsMore_Clicked() - { - try - { - List items = new List(); - Dictionary pairs = new Dictionary(); - SearchResource.ListRequest request = SecretsVault.Service.Search.List("snippet"); - request.Type = "video"; - request.MaxResults = 10; - request.Order = SearchResource.ListRequest.OrderEnum.Date; - - foreach (KeyValuePair i in subsTokens) - { - request.ChannelId = i.Key; - request.PageToken = i.Value; - SearchListResponse response = await request.ExecuteAsync(); - foreach (SearchResult result in response.Items) - items.Add(result); - if (response.NextPageToken != null) - pairs.Add(i.Value, response.NextPageToken); - } - if (pairs.Count == 0) - subsMore.Complete(true); - else - subsTokens = pairs; - - items = items.OrderByDescending(x => x.Snippet.PublishedAt).ToList(); - - foreach (SearchResult i in items) - subsGrid.Add(new VideoCard(i.Id.VideoId)); - - subsMore.Complete(); - } - catch - { - subsMore.Complete(true); - } - } - public void Initialize() { if(SecretsVault.IsAuthorized) @@ -164,11 +91,11 @@ namespace FoxTube private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e) { loading.Close(); - if (pivot.SelectedItem == recommended && !recLoaded) + if (pivot.SelectedItem == recommended) LoadRecommendations(); else if (pivot.SelectedItem == trending && !trendLoaded) LoadTrending(); - else if (pivot.SelectedItem == subscriptions && !subsLoaded) + else if (pivot.SelectedItem == subscriptions) LoadSubscriptions(); } @@ -215,63 +142,31 @@ namespace FoxTube try { loading.Refresh(); - - throw new NotImplementedException(); - - loading.Close(); - recLoaded = true; + throw new NotImplementedException("This page has not implemented yet."); } catch (System.Net.Http.HttpRequestException) { - recLoaded = false; loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true); } catch (Exception e) { - recLoaded = false; loading.Error(e.GetType().ToString(), e.Message); } } - async void LoadSubscriptions() + void LoadSubscriptions() { try { loading.Refresh(); - List items = new List(); - SearchResource.ListRequest request = SecretsVault.Service.Search.List("snippet"); - request.Type = "video"; - request.MaxResults = 10; - request.Order = SearchResource.ListRequest.OrderEnum.Date; - - foreach (Subscription i in SecretsVault.Subscriptions) - { - request.ChannelId = i.Snippet.ResourceId.ChannelId; - SearchListResponse response = await request.ExecuteAsync(); - foreach (SearchResult result in response.Items) - items.Add(result); - if (response.NextPageToken != null) - subsTokens.Add(i.Snippet.ResourceId.ChannelId, response.NextPageToken); - } - if (subsTokens.Count == 0) - subsMore.Complete(true); - - items = items.OrderByDescending(x => x.Snippet.PublishedAt).ToList(); - - foreach (SearchResult i in items) - subsGrid.Add(new VideoCard(i.Id.VideoId)); - - loading.Close(); - subsLoaded = true; + throw new NotImplementedException("This page has not implemented yet."); } catch (System.Net.Http.HttpRequestException) { - subsLoaded = false; loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true); } catch (Exception e) { - subsLoaded = false; loading.Error(e.GetType().ToString(), e.Message); } } diff --git a/FoxTube/Pages/MainPage.xaml.cs b/FoxTube/Pages/MainPage.xaml.cs index 7a766ed..d6cfe77 100644 --- a/FoxTube/Pages/MainPage.xaml.cs +++ b/FoxTube/Pages/MainPage.xaml.cs @@ -69,12 +69,6 @@ namespace FoxTube SetTitleBar(); } - protected override void OnNavigatedTo(NavigationEventArgs e) - { - base.OnNavigatedTo(e); - content.Navigate(typeof(Home)); - } - public Video GetCurrentItem() { try { return (videoPlaceholder.Content as VideoPage).item; } @@ -243,10 +237,10 @@ namespace FoxTube await dialog.ShowAsync(); } + content.Navigate(typeof(Home)); + if (videoPlaceholder.Content != null) GoToVideo((videoPlaceholder.Content as VideoPage).videoId); - - nav.SelectedItem = toHome; } private async void feedback_Click(object sender, TappedRoutedEventArgs e) diff --git a/FoxTube/Pages/Search.xaml.cs b/FoxTube/Pages/Search.xaml.cs index b3811b5..32842b2 100644 --- a/FoxTube/Pages/Search.xaml.cs +++ b/FoxTube/Pages/Search.xaml.cs @@ -3,29 +3,17 @@ using FoxTube.Pages; using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; using Windows.Storage; 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; using Windows.System; - -// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 +using System.Globalization; namespace FoxTube { /// - /// An empty page that can be used on its own or navigated to within a Frame. + /// Search page /// public sealed partial class Search : Page { @@ -40,7 +28,7 @@ namespace FoxTube public Search() { - this.InitializeComponent(); + InitializeComponent(); loading = grid.Children[2] as LoadingPage; list = ((grid.Children[0] as ScrollViewer).Content as StackPanel).Children[4] as VideoGrid; more = ((grid.Children[0] as ScrollViewer).Content as StackPanel).Children[5] as ShowMore; @@ -108,14 +96,10 @@ namespace FoxTube } request.Q = arg.Term; request.SafeSearch = (SearchResource.ListRequest.SafeSearchEnum)(int)settings.Values["safeSearch"]; - try - { - request.RelevanceLanguage = settings.Values["region"].ToString().Remove(2).ToLower(); - } - catch (ArgumentOutOfRangeException) - { - request.RelevanceLanguage = settings.Values["region"].ToString().ToLower(); - } + + request.RegionCode = CultureInfo.GetCultures(CultureTypes.AllCultures).Find(i => i.IetfLanguageTag == SettingsStorage.Region).IetfLanguageTag.Remove(0, 3); + request.RelevanceLanguage = CultureInfo.GetCultures(CultureTypes.AllCultures).Find(i => i.IetfLanguageTag == SettingsStorage.Region).TwoLetterISOLanguageName; + request.MaxResults = 48; try { diff --git a/FoxTube/Pages/SettingsPages/General.xaml.cs b/FoxTube/Pages/SettingsPages/General.xaml.cs index d62547b..801adcc 100644 --- a/FoxTube/Pages/SettingsPages/General.xaml.cs +++ b/FoxTube/Pages/SettingsPages/General.xaml.cs @@ -1,49 +1,42 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Globalization; -using System.IO; +using System.Globalization; using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; using Windows.ApplicationModel.Core; -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 Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 namespace FoxTube.Pages.SettingsPages { /// - /// An empty page that can be used on its own or navigated to within a Frame. + /// Settings page with general preferences /// public sealed partial class General : Page { - ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; public General() { - this.InitializeComponent(); + InitializeComponent(); - language.SelectedIndex = (string)settings.Values["language"] == "en-US"? 0 : 1; - quality.SelectedItem = quality.Items.ToList().Find(x => (x as ComboBoxItem).Tag as string == (string)settings.Values["quality"]); + language.SelectedItem = language.Items.Find(i => ((ComboBoxItem)i).Tag.ToString() == SettingsStorage.Language); + CultureInfo.GetCultures(CultureTypes.AllCultures).FindAll(i => !i.IsNeutralCulture).ForEach(i => + { + region.Items.Add(new ComboBoxItem + { + Content = i.DisplayName, + Tag = i.IetfLanguageTag + }); + if (i.IetfLanguageTag == SettingsStorage.Region) + region.SelectedItem = region.Items.Last(); + }); + safeSearch.SelectedIndex = SettingsStorage.SafeSearch; - newVideo.IsOn = (bool)settings.Values["newVideoNotification"]; + quality.SelectedItem = quality.Items.ToList().Find(i => ((ComboBoxItem)i).Tag.ToString() == SettingsStorage.VideoQuality); + mobileWarning.IsOn = SettingsStorage.CheckConnection; + autoplay.IsOn = SettingsStorage.Autoplay; - mobileWarning.IsOn = (bool)settings.Values["moblieWarning"]; - autoplay.IsOn = (bool)settings.Values["videoAutoplay"]; + newVideo.IsOn = SettingsStorage.VideoNotifications; + devNews.IsOn = SettingsStorage.DevNotifications; - safeSearch.SelectedIndex = (int)settings.Values["safeSearch"]; - - switch((int)settings.Values["themeMode"]) + switch(SettingsStorage.Theme) { case 0: light.IsChecked = true; @@ -55,77 +48,63 @@ namespace FoxTube.Pages.SettingsPages system.IsChecked = true; break; } - - foreach (CultureInfo culture in CultureInfo.GetCultures(CultureTypes.AllCultures)) - { - region.Items.Add(culture.DisplayName); - if (culture.Name == (string)settings.Values["region"]) - region.SelectedIndex = region.Items.Count - 1; - } } private void language_SelectionChanged(object sender, SelectionChangedEventArgs e) { - if ((string)settings.Values["language"] != (language.SelectedItem as ComboBoxItem).Tag.ToString()) - { - settings.Values["language"] = (language.SelectedItem as ComboBoxItem).Tag.ToString(); - restartNote.Visibility = Visibility.Visible; - } - else - restartNote.Visibility = Visibility.Collapsed; + if (SettingsStorage.Language == (language.SelectedItem as ComboBoxItem).Tag.ToString()) + return; + + SettingsStorage.Language = (language.SelectedItem as ComboBoxItem).Tag.ToString(); + restartNote.Visibility = Visibility.Visible; } private void quality_SelectionChanged(object sender, SelectionChangedEventArgs e) { - settings.Values["quality"] = (quality.SelectedItem as ComboBoxItem).Tag as string; + SettingsStorage.VideoQuality = (quality.SelectedItem as ComboBoxItem).Tag as string; } private void mobileWarning_Toggled(object sender, RoutedEventArgs e) { - settings.Values["moblieWarning"] = mobileWarning.IsOn; + SettingsStorage.CheckConnection = mobileWarning.IsOn; } private void autoplay_Toggled(object sender, RoutedEventArgs e) { - - settings.Values["videoAutoplay"] = autoplay.IsOn; + SettingsStorage.Autoplay = autoplay.IsOn; } - private async void notification_IsEnabledChanged(object sender, RoutedEventArgs e) + private void notification_IsEnabledChanged(object sender, RoutedEventArgs e) { - settings.Values["newVideoNotification"] = newVideo.IsOn; - bool[] notificationsSettings = new bool[] { (bool)settings.Values["newVideoNotification"], (bool)settings.Values["devNews"] }; - await FileIO.WriteTextAsync( - await ApplicationData.Current.RoamingFolder.CreateFileAsync("notifications.json", CreationCollisionOption.ReplaceExisting), - JsonConvert.SerializeObject(notificationsSettings)); + SettingsStorage.VideoNotifications = newVideo.IsOn; } private void region_SelectionChanged(object sender, SelectionChangedEventArgs e) { - settings.Values["region"] = CultureInfo.GetCultures(CultureTypes.AllCultures)[region.SelectedIndex].Name; + SettingsStorage.Region = CultureInfo.GetCultures(CultureTypes.AllCultures)[region.SelectedIndex].Name; } private void safeSearch_SelectionChanged(object sender, SelectionChangedEventArgs e) { - settings.Values["safeSearch"] = safeSearch.SelectedIndex; + SettingsStorage.SafeSearch = safeSearch.SelectedIndex; } private void RadioButton_Checked(object sender, RoutedEventArgs e) { - if (sender == light && (int)settings.Values["themeMode"] != 0) + if (sender == light && SettingsStorage.Theme != 0) { - settings.Values["themeMode"] = 0; + SettingsStorage.Theme = 0; Methods.MainPage.RequestedTheme = ElementTheme.Light; } - if (sender == dark && (int)settings.Values["themeMode"] != 1) + else if (sender == dark && SettingsStorage.Theme != 1) { - settings.Values["themeMode"] = 1; + SettingsStorage.Theme = 1; Methods.MainPage.RequestedTheme = ElementTheme.Dark; } - if (sender == system && (int)settings.Values["themeMode"] != 2) + else if (sender == system && SettingsStorage.Theme != 2) { - settings.Values["themeMode"] = 2; - Color uiTheme = (new Windows.UI.ViewManagement.UISettings()).GetColorValue(Windows.UI.ViewManagement.UIColorType.Background); + SettingsStorage.Theme = 2; + Color uiTheme = new Windows.UI.ViewManagement.UISettings().GetColorValue(Windows.UI.ViewManagement.UIColorType.Background); if (uiTheme == Colors.Black) Methods.MainPage.RequestedTheme = ElementTheme.Dark; else @@ -139,13 +118,9 @@ namespace FoxTube.Pages.SettingsPages CoreApplication.Exit(); } - private async void devNews_Toggled(object sender, RoutedEventArgs e) + private void devNews_Toggled(object sender, RoutedEventArgs e) { - settings.Values["devnews"] = devNews.IsOn; - bool[] notificationsSettings = new bool[] { (bool)settings.Values["newVideoNotification"], (bool)settings.Values["devNews"] }; - await FileIO.WriteTextAsync( - await ApplicationData.Current.RoamingFolder.CreateFileAsync("notifications.json", CreationCollisionOption.ReplaceExisting), - JsonConvert.SerializeObject(notificationsSettings)); + SettingsStorage.DevNotifications = devNews.IsOn; } } }