From 8432271b328a1357716f297bc882ab3fcd6b04d8 Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Fri, 21 Dec 2018 17:34:26 +0300 Subject: [PATCH] -Fixed Unspecified error on App.e.g.cs -Refactoring --- ...{ObjectEventArgs.cs => SearchPaameters.cs} | 12 -- FoxTube/Classes/SecretsVault.cs | 165 ++++------------ FoxTube/Classes/SettingsStorage.cs | 180 ++++++++++++++++++ FoxTube/Controls/ChannelCard.xaml.cs | 2 +- FoxTube/FoxTube.csproj | 3 +- FoxTube/Pages/MainPage.xaml | 2 +- FoxTube/Pages/MainPage.xaml.cs | 41 +--- 7 files changed, 229 insertions(+), 176 deletions(-) rename FoxTube/Classes/{ObjectEventArgs.cs => SearchPaameters.cs} (95%) create mode 100644 FoxTube/Classes/SettingsStorage.cs diff --git a/FoxTube/Classes/ObjectEventArgs.cs b/FoxTube/Classes/SearchPaameters.cs similarity index 95% rename from FoxTube/Classes/ObjectEventArgs.cs rename to FoxTube/Classes/SearchPaameters.cs index 23cd9d0..78dab5c 100644 --- a/FoxTube/Classes/ObjectEventArgs.cs +++ b/FoxTube/Classes/SearchPaameters.cs @@ -11,18 +11,6 @@ namespace FoxTube public delegate void ObjectEventHandler(object sender, params object[] args); - /*public class SearchParameters - { - public string ChannelId { get; set; } - public string Term { get; set; } - - public SearchParameters(string channelId, string term) - { - ChannelId = channelId; - Term = term; - } - }*/ - public class SearchParameters { public class Filters diff --git a/FoxTube/Classes/SecretsVault.cs b/FoxTube/Classes/SecretsVault.cs index 5b6a58e..4067f5b 100644 --- a/FoxTube/Classes/SecretsVault.cs +++ b/FoxTube/Classes/SecretsVault.cs @@ -16,76 +16,49 @@ using Windows.UI.Popups; namespace FoxTube { - /*public class HistoryItem - { - public string Id { get; set; } - public double LeftOn { get; set; } - - public HistoryItem(string id, TimeSpan elapsed, TimeSpan total) - { - Id = id; - LeftOn = elapsed.TotalSeconds / total.TotalSeconds * 100; - } - - public HistoryItem(string id) - { - Id = id; - LeftOn = 0; - } - }*/ - public static class SecretsVault { - public static event EventHandler AuthorizationStateChanged; + #region Properties + //Events + public static event Event AuthorizationStateChanged; public static event ObjectEventHandler SubscriptionsChanged; + public static event Event NotPurchased; //Rising when app finds out that it's not a PRO version - private static ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; - public static NetworkCredential EmailCredential => new NetworkCredential("youwillneverknowthisadress@gmail.com", "thisisthepassword12345"); - public static ClientSecrets Secrets => new ClientSecrets() + //Private properties + private static ClientSecrets Secrets => new ClientSecrets() { ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com", ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_" }; - private static UserCredential Credential; + private static YouTubeService NoAuthService => new YouTubeService(new BaseClientService.Initializer() + { + ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", + ApplicationName = "FoxTube" + }); public static BaseClientService.Initializer Initializer => new BaseClientService.Initializer() { HttpClientInitializer = Credential, ApplicationName = "FoxTube" }; + public static YouTubeService Service => IsAuthorized ? new YouTubeService(Initializer) : NoAuthService; + public static bool AdsDisabled { get; private set; } = true; - public static string AccountId { get; private set; } - public static bool IsAuthorized { get; private set; } = false; + //User info + public static bool IsAuthorized => Credential != null; + private static UserCredential Credential { get; set; } + public static string AccountId => UserChannel?.Id; public static Channel UserChannel { get; private set; } - /*public static List WatchLater { get; private set; } = new List(); - public static List UserHistory { get; private set; } = new List();*/ - public static List Subscriptions { get; private set; } = new List(); - public static YouTubeService NoAuthService => new YouTubeService(new BaseClientService.Initializer() - { - ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", - ApplicationName = "FoxTube" - }); - public static YouTubeService Service - { - get - { - if (IsAuthorized) - return new YouTubeService(Initializer); - else - return NoAuthService; - } - } - - /*public static void HistoryAdd(string id, TimeSpan elapsed, TimeSpan total) - { - UserHistory.Remove(UserHistory.Find(x => x.Id == id)); - UserHistory.Add(new HistoryItem(id, elapsed, total)); - - if (UserHistory.Count > 100) - UserHistory.RemoveAt(UserHistory.Count - 1); - }*/ + public static List Subscriptions { get; } = new List(); + #endregion + #region Methods + /// + /// Subscribes or unsibscribes authorized user from the channel + /// + /// The ID of channel which has to be added/removed + /// Returns 'true' if channel is in subscriptions now; 'false' if it's not public static async Task ChangeSubscriptionState(string id) { if (!IsAuthorized) @@ -125,62 +98,10 @@ namespace FoxTube } } - public static async Task Subscribe(string id) - { - if (!IsAuthorized) - return false; - - var request = Service.Subscriptions.Insert(new Subscription() - { - Snippet = new SubscriptionSnippet() - { - ResourceId = new ResourceId() - { - ChannelId = id, - Kind = "youtube#channel" - } - } - }, "snippet"); - - Subscription s = await request.ExecuteAsync(); - if (s == null) - return false; - Subscriptions.Add(s); - SubscriptionsChanged.Invoke(null, "add", s); - - SaveSubscriptions(); - return true; - } - - public static async Task Unsubscibe(string id) - { - if (!IsAuthorized) - return false; - - Subscription s = null; - foreach(Subscription i in Subscriptions) - if (i.Snippet.ResourceId.ChannelId == id) - { - s = i; - break; - } - if (s == null) - return false; - - try - { - await Service.Subscriptions.Delete(s.Id).ExecuteAsync(); - } - catch - { - return false; - } - - SubscriptionsChanged.Invoke(null, "remove", Subscriptions.IndexOf(s)); - Subscriptions.Remove(s); - return true; - } - + /// + /// Prompts to add an Youtube account and retrieves its info when successful + /// + /// public static async void Authorize(bool retrieveSubs = true) { try @@ -194,19 +115,17 @@ namespace FoxTube }, "user", CancellationToken.None); - + if (Credential == null || !retrieveSubs) return; if (settings.Values["authorized"] == null) settings.Values.Add("authorized", true); else settings.Values["authorized"] = true; - IsAuthorized = true; var request = Service.Channels.List("snippet,contentDetails"); request.Mine = true; UserChannel = (await request.ExecuteAsync()).Items[0]; - AccountId = UserChannel.Id; /*try { @@ -265,20 +184,20 @@ namespace FoxTube subRequest.Mine = true; subRequest.MaxResults = 50; subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance; - SubscriptionListResponse subResponse = await subRequest.ExecuteAsync(); + SubscriptionListResponse subResponse; + string nextToken = null; Subscriptions.Clear(); - foreach (Subscription s in subResponse.Items) - Subscriptions.Add(s); - - string nextToken = subResponse.NextPageToken; - while (nextToken != null) + do { subRequest.PageToken = nextToken; subResponse = await subRequest.ExecuteAsync(); foreach (Subscription s in subResponse.Items) Subscriptions.Add(s); - } + nextToken = subResponse.NextPageToken; + + } while (!string.IsNullOrWhiteSpace(nextToken)); + SaveSubscriptions(); } catch @@ -317,21 +236,17 @@ namespace FoxTube if(await Credential.RevokeTokenAsync(CancellationToken.None)) { Credential = null; - IsAuthorized = false; - AuthorizationStateChanged.Invoke(null, null); + AuthorizationStateChanged.Invoke(); settings.Values["authorized"] = false; } } public static void CheckAuthorization(bool retrieveSubs = true) { - if (settings.Values["authorized"] == null || !(bool)settings.Values["authorized"]) - IsAuthorized = false; - else + if (settings.Values["authorized"] != null && (bool)settings.Values["authorized"]) Authorize(retrieveSubs); } - public static bool AdsDisabled { get; private set; } = true; public static void CheckAddons() { @@ -346,6 +261,6 @@ namespace FoxTube } } - public static event Event NotPurchased; + #endregion } } diff --git a/FoxTube/Classes/SettingsStorage.cs b/FoxTube/Classes/SettingsStorage.cs new file mode 100644 index 0000000..0abec86 --- /dev/null +++ b/FoxTube/Classes/SettingsStorage.cs @@ -0,0 +1,180 @@ +using Newtonsoft.Json; +using System; +using System.Globalization; +using System.Linq; +using Windows.ApplicationModel; +using Windows.Storage; + +namespace FoxTube.Classes +{ + public static class SettingsStorage + { + public static string VideoQuality + { + get { return (string)settings[0]; } + set + { + settings[0] = value; + SaveData(); + } + } + public static string RememberedQuality + { + get { return (string)settings[1]; } + set + { + settings[1] = value; + SaveData(); + } + } + + public static bool VideoNotifications + { + get { return (bool)settings[2]; } + set + { + settings[2] = value; + SaveData(); + } + } + public static bool DevNotifications + { + get { return (bool)settings[3]; } + set + { + settings[3] = value; + SaveData(); + } + } + + public static bool CheckConnection + { + get { return (bool)settings[4]; } + set + { + settings[4] = value; + SaveData(); + } + } + public static bool Autoplay + { + get { return (bool)settings[5]; } + set + { + settings[5] = value; + SaveData(); + } + } + public static int Volume + { + get { return (int)settings[6]; } + set + { + settings[6] = value; + SaveData(); + } + } + + public static string Language + { + get { return (string)settings[7]; } + set + { + settings[7] = value; + SaveData(); + } + } + public static string Region + { + get { return (string)settings[8]; } + set + { + settings[8] = value; + SaveData(); + } + } + public static int SafeSearch + { + get { return (int)settings[9]; } + set + { + settings[9] = value; + SaveData(); + } + } + + public static int Theme + { + get { return (int)settings[10]; } + set + { + settings[10] = value; + SaveData(); + } + } + public static bool HasAccount + { + get { return (bool)settings[11]; } + set + { + settings[11] = value; + SaveData(); + } + } + + public static string Version + { + get + { + if (storage.Values["ver"] == null) + { + PackageVersion ver = Package.Current.Id.Version; + return $"{ver.Major}.{ver.Minor}"; + } + else return (string)storage.Values["version"]; + } + set + { + storage.Values["version"] = value; + } + } + + //Settings storage + private static ApplicationDataContainer storage = ApplicationData.Current.LocalSettings; + + //Predefined preferences + private static object[] settings = new object[] + { + "remember", + "1080p", + + true, + true, + + true, + true, + 100, + + (new[] { "ua", "ru", "by", "kz", "kg", "md", "lv", "ee" }).Contains(CultureInfo.InstalledUICulture.TwoLetterISOLanguageName) ? "ru-RU" : "en-US", + CultureInfo.CurrentCulture.Name, + 0, + + 2, + false + }; + + public static void LoadData() + { + try + { + settings = JsonConvert.DeserializeObject(storage.Values["settings"] as string); + } + catch (NullReferenceException) { } + } + + public static void SaveData() + { + storage.Values["settings"] = JsonConvert.SerializeObject(settings); + } + } +} diff --git a/FoxTube/Controls/ChannelCard.xaml.cs b/FoxTube/Controls/ChannelCard.xaml.cs index 7ccd966..327417d 100644 --- a/FoxTube/Controls/ChannelCard.xaml.cs +++ b/FoxTube/Controls/ChannelCard.xaml.cs @@ -39,7 +39,7 @@ namespace FoxTube.Controls public async void Initialize(string id, string live) { - ChannelsResource.ListRequest request = SecretsVault.NoAuthService.Channels.List("snippet,statistics,brandingSettings"); + ChannelsResource.ListRequest request = SecretsVault.Service.Channels.List("snippet,statistics,brandingSettings"); request.Id = id; ChannelListResponse response = await request.ExecuteAsync(); diff --git a/FoxTube/FoxTube.csproj b/FoxTube/FoxTube.csproj index 5772f30..56515db 100644 --- a/FoxTube/FoxTube.csproj +++ b/FoxTube/FoxTube.csproj @@ -100,7 +100,8 @@ - + + Advert.xaml diff --git a/FoxTube/Pages/MainPage.xaml b/FoxTube/Pages/MainPage.xaml index 8d206ce..81118d2 100644 --- a/FoxTube/Pages/MainPage.xaml +++ b/FoxTube/Pages/MainPage.xaml @@ -28,7 +28,7 @@ - + diff --git a/FoxTube/Pages/MainPage.xaml.cs b/FoxTube/Pages/MainPage.xaml.cs index cff7741..0d260f3 100644 --- a/FoxTube/Pages/MainPage.xaml.cs +++ b/FoxTube/Pages/MainPage.xaml.cs @@ -39,37 +39,11 @@ namespace FoxTube public enum Sender { Menu, Frame, None } public sealed partial class MainPage : Page - { - ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; - + { Sender s = Sender.None; public MainPage() { this.InitializeComponent(); - - if (settings.Values["quality"] == null) - settings.Values.Add("quality", "remember"); - if (settings.Values["rememberedQuality"] == null) - settings.Values.Add("rememberedQuality", "1080p"); - - if (settings.Values["newVideoNotification"] == null) - settings.Values.Add("newVideoNotification", true); - if (settings.Values["devNews"] == null) - settings.Values.Add("devNews", true); - - if (settings.Values["moblieWarning"] == null) - settings.Values.Add("moblieWarning", false); - if (settings.Values["videoAutoplay"] == null) - settings.Values.Add("videoAutoplay", true); - if (settings.Values["themeMode"] == null) - settings.Values.Add("themeMode", 2); - if (settings.Values["volume"] == null) - settings.Values.Add("volume", 100); - - if (settings.Values["region"] == null) - settings.Values.Add("region", CultureInfo.CurrentCulture.Name); - if (settings.Values["safeSearch"] == null) - settings.Values.Add("safeSearch", 0); PackageVersion ver = Package.Current.Id.Version; if (settings.Values["ver"] == null) @@ -90,9 +64,7 @@ namespace FoxTube catch { } } - content.Navigate(typeof(Home)); - - SecretsVault.AuthorizationStateChanged += Vault_AuthorizationStateChanged; + SecretsVault.AuthorizationStateChanged += AuthorizationStateChanged; SecretsVault.SubscriptionsChanged += SecretsVault_SubscriptionsChanged; SecretsVault.NotPurchased += () => removeAds.Visibility = Visibility.Visible; SecretsVault.CheckAuthorization(); @@ -165,7 +137,7 @@ namespace FoxTube nav.MenuItems.RemoveAt((int)args[1] + 9); } - private async void Vault_AuthorizationStateChanged(object sender, EventArgs e) + private async void AuthorizationStateChanged() { if(SecretsVault.IsAuthorized) { @@ -186,7 +158,6 @@ namespace FoxTube toHistory.Visibility = Visibility.Visible; toLiked.Visibility = Visibility.Visible; toLater.Visibility = Visibility.Visible; - toDownloads.Visibility = Visibility.Visible; subsHeader.Visibility = Visibility.Visible; if (SecretsVault.Subscriptions.Count > 0) @@ -229,15 +200,13 @@ namespace FoxTube toHistory.Visibility = Visibility.Collapsed; toLiked.Visibility = Visibility.Collapsed; toLater.Visibility = Visibility.Collapsed; - toDownloads.Visibility = Visibility.Collapsed; subsHeader.Visibility = Visibility.Collapsed; subsHeader.Visibility = Visibility.Collapsed; for(int k = 9; k < nav.MenuItems.Count; k++) nav.MenuItems.RemoveAt(k); } - - nav.SelectedItem = toHome; + content.Navigate(typeof(Home)); if (videoPlaceholder.Content != null) @@ -261,7 +230,7 @@ namespace FoxTube private void myChannel_Click(object sender, RoutedEventArgs e) { - content.Navigate(typeof(Channel), SecretsVault.UserChannel.Id); + GoToChannel(SecretsVault.AccountId); } private void logout_Click(object sender, RoutedEventArgs e)