From bba4fe4f00f587dae0910c853e45d2ac4d55f325 Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Fri, 2 Nov 2018 10:33:34 +0300 Subject: [PATCH] Development 110218 --- FoxTube.Background/BackgroundProcessor.cs | 65 +++++----- FoxTube/App.xaml.cs | 129 ++++++++++++++++++-- FoxTube/Classes/DownloadAgent.cs | 4 +- FoxTube/Classes/SecretsVault.cs | 113 +++++++++-------- FoxTube/Controls/Advert.xaml | 45 ++++++- FoxTube/Controls/Advert.xaml.cs | 7 ++ FoxTube/Controls/DownloadItem.xaml.cs | 6 +- FoxTube/Package.appxmanifest | 12 ++ FoxTube/Pages/CommentsPage.xaml | 4 +- FoxTube/Pages/CommentsPage.xaml.cs | 2 + FoxTube/Pages/MainPage.xaml.cs | 52 ++++++-- FoxTube/Pages/Settings.xaml.cs | 10 +- FoxTube/Pages/SettingsPages/General.xaml.cs | 15 ++- FoxTube/Pages/VideoGrid.xaml | 7 +- FoxTube/Pages/VideoPage.xaml | 48 +++++++- FoxTube/Pages/VideoPage.xaml.cs | 6 + 16 files changed, 400 insertions(+), 125 deletions(-) diff --git a/FoxTube.Background/BackgroundProcessor.cs b/FoxTube.Background/BackgroundProcessor.cs index 5fa6e5b..12275c1 100644 --- a/FoxTube.Background/BackgroundProcessor.cs +++ b/FoxTube.Background/BackgroundProcessor.cs @@ -2,8 +2,10 @@ using Google.Apis.Services; using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; +using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Text; @@ -34,56 +36,43 @@ namespace FoxTube.Background }); BackgroundTaskDeferral def; - public void Run(IBackgroundTaskInstance taskInstance) + public async void Run(IBackgroundTaskInstance taskInstance) { def = taskInstance.GetDeferral(); - if (settings.Values["lastCheck"] == null) - settings.Values.Add("lastCheck", XmlConvert.ToString(DateTime.Now, "YYYY-MM-DDThh:mm:ss")); - else lastCheck = XmlConvert.ToDateTime(settings.Values["lastCheck"] as string, XmlDateTimeSerializationMode.Unspecified); - - if((bool)settings.Values["newVideoNotification"]) + try + { + if (settings.Values["lastCheck"] == null) + settings.Values.Add("lastCheck", XmlConvert.ToString(DateTime.Now)); + else lastCheck = XmlConvert.ToDateTime(settings.Values["lastCheck"] as string, XmlDateTimeSerializationMode.Unspecified); + } + catch + { + lastCheck = DateTime.Now; + } + + bool[] notificationsSettings = JsonConvert.DeserializeObject(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("notifications.json"))); + if(notificationsSettings[0]) CheckAnnouncements(); - if((bool)settings.Values["devNews"]) + if(notificationsSettings[1]) CheckAccount(); settings.Values["lastCheck"] = XmlConvert.ToString(DateTime.Now, "YYYY-MM-DDThh:mm:ss"); def.Complete(); + ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "Background task complete", DateTime.Now.ToString(), null, null)); } async void CheckAccount() { try { - UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None); - if (credential == null) - return; - - SubscriptionsResource.ListRequest subRequest = new YouTubeService(new BaseClientService.Initializer() - { - HttpClientInitializer = credential, - ApplicationName = "FoxTube" - }).Subscriptions.List("snippet"); - subRequest.Mine = true; - subRequest.MaxResults = 50; - SubscriptionListResponse subResponse = await subRequest.ExecuteAsync(); - Dictionary subs = new Dictionary(); - - foreach (Subscription s in subResponse.Items) - subs.Add(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url); - - string nextToken = subResponse.NextPageToken; - while (nextToken != null) - { - subRequest.PageToken = nextToken; - subResponse = await subRequest.ExecuteAsync(); - nextToken = subResponse.NextPageToken; - - foreach (Subscription s in subResponse.Items) - subs.Add(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url); - } - - foreach (var s in subs) + ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "Checking videos...", DateTime.Now.ToString(), null, null)); + Dictionary subscriptions = JsonConvert.DeserializeObject>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("background.json"))); + Debug.WriteLine("Subscriptions list"); + foreach (var i in subscriptions) + Debug.WriteLine($"{i.Key}: {i.Value}"); + Debug.WriteLine("Subscriptions list end"); + foreach (var s in subscriptions) { SearchResource.ListRequest request = Service.Search.List("snippet"); request.PublishedAfter = lastCheck; @@ -95,9 +84,13 @@ namespace FoxTube.Background foreach (var i in response.Items) ToastNotificationManager.CreateToastNotifier().Show( Notification.GetVideoToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title, i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, s.Value)); + + ToastNotificationManager.CreateToastNotifier().Show(Notification.GetVideoToast(null, s.Key, DateTime.Now.ToString(), s.Key, null, s.Value)); } } catch { } + + ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "New videos checked", DateTime.Now.ToString(), null, null)); } void CheckAnnouncements() diff --git a/FoxTube/App.xaml.cs b/FoxTube/App.xaml.cs index 1749ce0..085c1e2 100644 --- a/FoxTube/App.xaml.cs +++ b/FoxTube/App.xaml.cs @@ -1,11 +1,18 @@ -using System; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; +using System; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; +using System.Linq; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; +using Windows.ApplicationModel.Background; using Windows.ApplicationModel.Core; using Windows.Globalization; using Windows.Storage; +using Windows.System.Power; +using Windows.UI.Notifications; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; @@ -94,12 +101,124 @@ namespace FoxTube } CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true; + ActivateToastBackgoundTask(); + ActivateBackgoundTask(); + } + + public async void ActivateToastBackgoundTask() + { + const string taskName = "FoxtubeToastBackground"; + if (BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals(taskName))) + return; + + var backgroundRequest = await BackgroundExecutionManager.RequestAccessAsync(); + var saverRequest = PowerManager.EnergySaverStatus; + if (backgroundRequest == BackgroundAccessStatus.DeniedBySystemPolicy || backgroundRequest == BackgroundAccessStatus.DeniedByUser) + return; + + BackgroundTaskBuilder builder = new BackgroundTaskBuilder() { Name = taskName }; + builder.SetTrigger(new ToastNotificationActionTrigger()); + + BackgroundTaskRegistration registration = builder.Register(); + } + + public async void ActivateBackgoundTask() + { + const string taskName = "FoxtubeBackgound"; + Debug.WriteLine($"Background task active: {BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals(taskName))}"); + if (BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals(taskName))) + return; + + var backgroundRequest = await BackgroundExecutionManager.RequestAccessAsync(); + var saverRequest = PowerManager.EnergySaverStatus; + if (backgroundRequest == BackgroundAccessStatus.DeniedBySystemPolicy || backgroundRequest == BackgroundAccessStatus.DeniedByUser || saverRequest == EnergySaverStatus.On) + return; + + BackgroundTaskBuilder builder = new BackgroundTaskBuilder() + { + Name = taskName, + IsNetworkRequested = true, + TaskEntryPoint = "FoxTube.Background.BackgroundProcessor" + }; + builder.SetTrigger(new TimeTrigger(15, false)); + + BackgroundTaskRegistration registration = builder.Register(); + Debug.WriteLine($"2. Background task active: {BackgroundTaskRegistration.AllTasks.Any(i => i.Value.Name.Equals(taskName))}"); + } + + protected async override void OnBackgroundActivated(BackgroundActivatedEventArgs args) + { + var deferral = args.TaskInstance.GetDeferral(); + base.OnBackgroundActivated(args); + + if (args.TaskInstance.Task.Name == "FoxtubeToastBackground") + { + var details = args.TaskInstance.TriggerDetails as ToastNotificationActionTriggerDetail; + if (details != null) + { + string[] arguments = details.Argument.Split('|'); + + switch(arguments[0]) + { + case "dcancel": + Debug.WriteLine("Cancel has been required"); + try { Methods.MainPage.Agent.Remove(arguments[1]); } + catch { } + break; + case "later": + try + { + if(!SecretsVault.IsAuthorized) + SecretsVault.CheckAuthorization(false); + if (!SecretsVault.IsAuthorized) + throw new Exception("Not authenticated"); + + PlaylistItem item = new PlaylistItem() + { + Snippet = new PlaylistItemSnippet() + { + ResourceId = new ResourceId() + { + Kind = "youtube#video", + VideoId = arguments[1] + }, + PlaylistId = "WL" + } + }; + + await SecretsVault.Service.PlaylistItems.Insert(item, "snippet").ExecuteAsync(); + } + catch (Exception e) + { + Debug.WriteLine(e.Message); + } + break; + } + } + } + + deferral.Complete(); } protected override void OnActivated(IActivatedEventArgs e) { base.OnActivated(e); + Frame rootFrame = Window.Current.Content as Frame; + + if (rootFrame == null) + { + rootFrame = new Frame(); + rootFrame.NavigationFailed += OnNavigationFailed; + + Window.Current.Content = rootFrame; + } + + if (rootFrame.Content == null) + { + rootFrame.Navigate(typeof(MainPage)); + } + if (e is ToastNotificationActivatedEventArgs) { string[] args = (e as ToastNotificationActivatedEventArgs).Argument.Split('|'); @@ -114,17 +233,9 @@ namespace FoxTube Methods.MainPage.GoToVideo(args[1]); break; - case "dcancel": - Methods.MainPage.Agent.Remove(args[1]); - break; - case "channel": Methods.MainPage.GoToChannel(args[1]); break; - - case "later": - //Add to WL playlist - break; } } diff --git a/FoxTube/Classes/DownloadAgent.cs b/FoxTube/Classes/DownloadAgent.cs index 60ec9d5..8815996 100644 --- a/FoxTube/Classes/DownloadAgent.cs +++ b/FoxTube/Classes/DownloadAgent.cs @@ -25,7 +25,7 @@ namespace FoxTube.Controls { try { - List containers = JsonConvert.DeserializeObject>(await FileIO.ReadTextAsync(await roaming.GetFileAsync("data.json"))); + List containers = JsonConvert.DeserializeObject>(await FileIO.ReadTextAsync(await roaming.GetFileAsync("downloads.json"))); foreach (DownloadItemContainer i in containers) try { items.Add(new DownloadItem(i)); } @@ -67,7 +67,7 @@ namespace FoxTube.Controls containers.Add(i.Container); await FileIO.WriteTextAsync( - await roaming.CreateFileAsync("data.json", CreationCollisionOption.ReplaceExisting), + await roaming.CreateFileAsync("downloads.json", CreationCollisionOption.ReplaceExisting), JsonConvert.SerializeObject(containers)); })); dialog.Commands.Add(new UICommand("No")); diff --git a/FoxTube/Classes/SecretsVault.cs b/FoxTube/Classes/SecretsVault.cs index 2d305b5..09d1e82 100644 --- a/FoxTube/Classes/SecretsVault.cs +++ b/FoxTube/Classes/SecretsVault.cs @@ -9,6 +9,7 @@ using Google.Apis.Auth.OAuth2.Flows; using Google.Apis.Services; using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; +using Newtonsoft.Json; using Windows.Storage; using Windows.UI.Popups; @@ -149,8 +150,8 @@ namespace FoxTube Subscriptions.Remove(s); return true; } - - public static async void Authorize() + + public static async void Authorize(bool retrieveSubs = true) { try { @@ -164,65 +165,71 @@ namespace FoxTube "user", CancellationToken.None); - if (Credential != null) + 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; + + PlaylistItemsResource.ListRequest playlistRequest = Service.PlaylistItems.List("snippet"); + playlistRequest.PlaylistId = UserChannel.ContentDetails.RelatedPlaylists.WatchHistory; + playlistRequest.MaxResults = 50; + PlaylistItemListResponse playlistResponse = await playlistRequest.ExecuteAsync(); + UserHistory.Clear(); + foreach (PlaylistItem i in playlistResponse.Items) + UserHistory.Add(i); + + playlistRequest = Service.PlaylistItems.List("snippet"); + playlistRequest.PlaylistId = UserChannel.ContentDetails.RelatedPlaylists.WatchLater; + playlistRequest.MaxResults = 50; + playlistResponse = await playlistRequest.ExecuteAsync(); + WatchLater.Clear(); + + foreach (PlaylistItem i in playlistResponse.Items) + WatchLater.Add(i); + + string nextToken = playlistResponse.NextPageToken; + while (nextToken != null) { - 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; - - PlaylistItemsResource.ListRequest playlistRequest = Service.PlaylistItems.List("snippet"); - playlistRequest.PlaylistId = UserChannel.ContentDetails.RelatedPlaylists.WatchHistory; - playlistRequest.MaxResults = 50; - PlaylistItemListResponse playlistResponse = await playlistRequest.ExecuteAsync(); - UserHistory.Clear(); - foreach (PlaylistItem i in playlistResponse.Items) - UserHistory.Add(i); - - playlistRequest = Service.PlaylistItems.List("snippet"); - playlistRequest.PlaylistId = UserChannel.ContentDetails.RelatedPlaylists.WatchLater; - playlistRequest.MaxResults = 50; + playlistRequest.PageToken = nextToken; playlistResponse = await playlistRequest.ExecuteAsync(); - WatchLater.Clear(); - foreach (PlaylistItem i in playlistResponse.Items) WatchLater.Add(i); - string nextToken = playlistResponse.NextPageToken; - while (nextToken != null) - { - playlistRequest.PageToken = nextToken; - playlistResponse = await playlistRequest.ExecuteAsync(); - foreach (PlaylistItem i in playlistResponse.Items) - WatchLater.Add(i); + nextToken = playlistResponse.NextPageToken; + } - nextToken = playlistResponse.NextPageToken; - } + SubscriptionsResource.ListRequest subRequest = Service.Subscriptions.List("snippet"); + subRequest.Mine = true; + subRequest.MaxResults = 50; + subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance; + SubscriptionListResponse subResponse = await subRequest.ExecuteAsync(); + Subscriptions.Clear(); - SubscriptionsResource.ListRequest subRequest = Service.Subscriptions.List("snippet"); - subRequest.Mine = true; - subRequest.MaxResults = 50; - subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance; - SubscriptionListResponse subResponse = await subRequest.ExecuteAsync(); - Subscriptions.Clear(); + foreach (Subscription s in subResponse.Items) + Subscriptions.Add(s); + nextToken = subResponse.NextPageToken; + while (nextToken != null) + { + subRequest.PageToken = nextToken; + subResponse = await subRequest.ExecuteAsync(); foreach (Subscription s in subResponse.Items) Subscriptions.Add(s); - - nextToken = subResponse.NextPageToken; - while (nextToken != null) - { - subRequest.PageToken = nextToken; - subResponse = await subRequest.ExecuteAsync(); - foreach (Subscription s in subResponse.Items) - Subscriptions.Add(s); - } } + + Dictionary subs = new Dictionary(); + Subscriptions.ForEach(x => subs.Add(x.Snippet.ResourceId.ChannelId, x.Snippet.Thumbnails.Medium.Url)); + await FileIO.WriteTextAsync( + await ApplicationData.Current.RoamingFolder.CreateFileAsync("background.json", CreationCollisionOption.ReplaceExisting), + JsonConvert.SerializeObject(subs)); } catch { @@ -257,12 +264,12 @@ namespace FoxTube } } - public static void CheckAuthorization() + public static void CheckAuthorization(bool retrieveSubs = true) { if (settings.Values["authorized"] == null || !(bool)settings.Values["authorized"]) IsAuthorized = false; else - Authorize(); + Authorize(retrieveSubs); } public static bool AdsDisabled { get; private set; } = true; @@ -270,7 +277,7 @@ namespace FoxTube public static void CheckAddons() { //TO-DO: Check addons list - bool purchased = true; + bool purchased = false; if(!purchased) { diff --git a/FoxTube/Controls/Advert.xaml b/FoxTube/Controls/Advert.xaml index 3834488..f1eb237 100644 --- a/FoxTube/Controls/Advert.xaml +++ b/FoxTube/Controls/Advert.xaml @@ -2,14 +2,51 @@ x:Class="FoxTube.Controls.Advert" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="using:FoxTube.Controls" + xmlns:ad="using:Microsoft.Advertising.WinRT.UI" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - mc:Ignorable="d" - d:DesignHeight="300" - d:DesignWidth="400"> + mc:Ignorable="d"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FoxTube/Controls/Advert.xaml.cs b/FoxTube/Controls/Advert.xaml.cs index e2872c3..d8a7724 100644 --- a/FoxTube/Controls/Advert.xaml.cs +++ b/FoxTube/Controls/Advert.xaml.cs @@ -19,9 +19,16 @@ namespace FoxTube.Controls { public sealed partial class Advert : UserControl { + public string AdUnitId { get; set; } = "test"; + public string AppId => "3f83fe91-d6be-434d-a0ae-7351c5a997f1"; public Advert() { this.InitializeComponent(); + if (!SecretsVault.AdsDisabled) + Visibility = Visibility.Visible; + else + Visibility = Visibility.Collapsed; + SecretsVault.NotPurchased += () => Visibility = Visibility.Visible; } } } diff --git a/FoxTube/Controls/DownloadItem.xaml.cs b/FoxTube/Controls/DownloadItem.xaml.cs index c595096..1775bd5 100644 --- a/FoxTube/Controls/DownloadItem.xaml.cs +++ b/FoxTube/Controls/DownloadItem.xaml.cs @@ -190,7 +190,11 @@ namespace FoxTube.Controls progressBar.IsIndeterminate = true; cancel.IsEnabled = false; cts.Cancel(); - await Container.File.DeleteAsync(); + try + { + await Container.File.DeleteAsync(); + } + catch { } Methods.MainPage.Agent.Remove(Container.Id); } diff --git a/FoxTube/Package.appxmanifest b/FoxTube/Package.appxmanifest index ab1f30a..a403af9 100644 --- a/FoxTube/Package.appxmanifest +++ b/FoxTube/Package.appxmanifest @@ -25,6 +25,18 @@ + + + + + + + + + + + + diff --git a/FoxTube/Pages/CommentsPage.xaml b/FoxTube/Pages/CommentsPage.xaml index cff7030..4a1544c 100644 --- a/FoxTube/Pages/CommentsPage.xaml +++ b/FoxTube/Pages/CommentsPage.xaml @@ -8,12 +8,12 @@ mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> - + - + diff --git a/FoxTube/Pages/CommentsPage.xaml.cs b/FoxTube/Pages/CommentsPage.xaml.cs index 41f1854..e38d6aa 100644 --- a/FoxTube/Pages/CommentsPage.xaml.cs +++ b/FoxTube/Pages/CommentsPage.xaml.cs @@ -43,6 +43,8 @@ namespace FoxTube.Pages if (!SecretsVault.IsAuthorized) grid.RowDefinitions[0].Height = new GridLength(0); + else + grid.RowDefinitions[0].Height = GridLength.Auto; counter.Text = string.Format("{0:0,0} Comments", video.Statistics.CommentCount); diff --git a/FoxTube/Pages/MainPage.xaml.cs b/FoxTube/Pages/MainPage.xaml.cs index 842fe78..611833e 100644 --- a/FoxTube/Pages/MainPage.xaml.cs +++ b/FoxTube/Pages/MainPage.xaml.cs @@ -24,6 +24,8 @@ using System.Net; using Windows.UI.Popups; using Windows.Networking.Connectivity; using Windows.UI.Core; +using System.IO; +using Newtonsoft.Json; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 @@ -74,7 +76,7 @@ namespace FoxTube if (settings.Values["ver"] == null) settings.Values.Add("ver", $"{ver.Major}.{ver.Minor}"); - //if((string)settings.Values["ver"] != $"{ver.Major}.{ver.Minor}") + if((string)settings.Values["ver"] != $"{ver.Major}.{ver.Minor}") { try { @@ -93,6 +95,16 @@ namespace FoxTube SecretsVault.SubscriptionsChanged += SecretsVault_SubscriptionsChanged; SecretsVault.CheckAuthorization(); SetTitleBar(); + Initialize(); + } + + public async void Initialize() + { + 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)); + Debug.WriteLine(ApplicationData.Current.RoamingFolder.Path); } protected override void OnNavigatedTo(NavigationEventArgs e) @@ -227,7 +239,8 @@ namespace FoxTube } nav.SelectedItem = toHome; - content.Navigate(typeof(Home)); + if(content.Content is Home) + content.Navigate(typeof(Home)); if (videoPlaceholder.Content != null) GoToVideo((videoPlaceholder.Content as VideoPage).videoId); @@ -274,13 +287,12 @@ namespace FoxTube public async void GoToVideo(string id, string playlistId = null) { - Debug.WriteLine($"Video id: {id}; Playlist id: {playlistId}"); + bool cancel = false; try { var connection = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost(); if ((bool)settings.Values["moblieWarning"] && (connection.NetworkCostType == NetworkCostType.Fixed || connection.NetworkCostType == NetworkCostType.Variable)) { - bool cancel = false; MessageDialog dialog = new MessageDialog("You are on metered connection now. Additional charges may apply. Do you want to continue?") { DefaultCommandIndex = 2, @@ -290,16 +302,40 @@ namespace FoxTube dialog.Commands.Add(new UICommand("No", (command) => cancel = true)); dialog.Commands.Add(new UICommand("Add to 'Watch later' playlist", (command) => { - //TO-DO: Adding video to "Watch later" + try + { + if (!SecretsVault.IsAuthorized) + SecretsVault.CheckAuthorization(false); + if (!SecretsVault.IsAuthorized) + throw new Exception("Not authenticated"); + + PlaylistItem item = new PlaylistItem() + { + Snippet = new PlaylistItemSnippet() + { + ResourceId = new ResourceId() + { + Kind = "youtube#video", + VideoId = id + }, + PlaylistId = "WL" + } + }; + + SecretsVault.Service.PlaylistItems.Insert(item, "snippet").Execute(); + } + catch (Exception e) + { + Debug.WriteLine(e.Message); + } cancel = true; })); await dialog.ShowAsync(); - - if (cancel) - return; } } catch { } + if (cancel) + return; nav.IsPaneOpen = false; diff --git a/FoxTube/Pages/Settings.xaml.cs b/FoxTube/Pages/Settings.xaml.cs index 9155168..16cc505 100644 --- a/FoxTube/Pages/Settings.xaml.cs +++ b/FoxTube/Pages/Settings.xaml.cs @@ -28,6 +28,7 @@ namespace FoxTube public sealed partial class Settings : Page { bool inboxLoaded = false; + string inboxId = null; public Settings() { this.InitializeComponent(); @@ -40,9 +41,9 @@ namespace FoxTube { if ((e.Parameter as string).Contains("inbox") || (e.Parameter as string).Contains("changelog")) { - pivot.SelectedIndex = 2; if ((string)e.Parameter != "inbox" && (string)e.Parameter != "changelog") - ((pivot.SelectedItem as PivotItem).Content as Inbox).Open(e.Parameter as string); + inboxId = e.Parameter as string; + pivot.SelectedIndex = 2; } else switch(e.Parameter as string) @@ -63,6 +64,11 @@ namespace FoxTube { (((pivot.Items[2] as PivotItem).Content as ScrollViewer).Content as Inbox).LoadItems(); inboxLoaded = true; + if(inboxId != null) + { + (((pivot.Items[2] as PivotItem).Content as ScrollViewer).Content as Inbox).Open(inboxId as string); + inboxId = null; + } } } } diff --git a/FoxTube/Pages/SettingsPages/General.xaml.cs b/FoxTube/Pages/SettingsPages/General.xaml.cs index 4a448fd..be096a5 100644 --- a/FoxTube/Pages/SettingsPages/General.xaml.cs +++ b/FoxTube/Pages/SettingsPages/General.xaml.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -90,9 +91,13 @@ namespace FoxTube.Pages.SettingsPages settings.Values["videoAutoplay"] = autoplay.IsOn; } - private void notification_IsEnabledChanged(object sender, RoutedEventArgs e) + private async 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)); } private void region_SelectionChanged(object sender, SelectionChangedEventArgs e) @@ -135,9 +140,13 @@ namespace FoxTube.Pages.SettingsPages CoreApplication.Exit(); } - private void devNews_Toggled(object sender, RoutedEventArgs e) + private async 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)); } } } diff --git a/FoxTube/Pages/VideoGrid.xaml b/FoxTube/Pages/VideoGrid.xaml index ba1d7d6..128299c 100644 --- a/FoxTube/Pages/VideoGrid.xaml +++ b/FoxTube/Pages/VideoGrid.xaml @@ -5,8 +5,8 @@ xmlns:local="using:FoxTube" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" - xmlns:ui="using:Microsoft.Advertising.WinRT.UI" + xmlns:ui="using:Microsoft.Toolkit.Uwp.UI.Controls" + xmlns:controls="using:FoxTube.Controls" x:Name="root" mc:Ignorable="d"> @@ -15,7 +15,8 @@ - + + diff --git a/FoxTube/Pages/VideoPage.xaml b/FoxTube/Pages/VideoPage.xaml index 146c2cc..698605a 100644 --- a/FoxTube/Pages/VideoPage.xaml +++ b/FoxTube/Pages/VideoPage.xaml @@ -8,6 +8,7 @@ xmlns:pages="using:FoxTube.Pages" xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls" xmlns:ui="using:Microsoft.Advertising.WinRT.UI" + xmlns:controls1="using:FoxTube.Controls" mc:Ignorable="d"> @@ -106,7 +107,47 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -116,7 +157,10 @@ - + + + + diff --git a/FoxTube/Pages/VideoPage.xaml.cs b/FoxTube/Pages/VideoPage.xaml.cs index 3829e54..ccafc1a 100644 --- a/FoxTube/Pages/VideoPage.xaml.cs +++ b/FoxTube/Pages/VideoPage.xaml.cs @@ -209,6 +209,12 @@ namespace FoxTube.Pages } subscribe.Visibility = Visibility.Visible; } + else + { + download.Visibility = Visibility.Collapsed; + addTo.Visibility = Visibility.Collapsed; + subscribe.Visibility = Visibility.Collapsed; + } ChannelsResource.ListRequest channelRequest = SecretsVault.Service.Channels.List("snippet, statistics"); channelRequest.Id = item.Snippet.ChannelId;