From 1a6447a4aac917e82da86c1c2503921047440d9e Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Fri, 9 Nov 2018 22:27:51 +0300 Subject: [PATCH] History (local) --- FoxTube.Background/BackgroundProcessor.cs | 2 + FoxTube/Classes/SecretsVault.cs | 66 +++++++++++++++++++++-- FoxTube/Controls/VideoCard.xaml | 2 +- FoxTube/Controls/VideoCard.xaml.cs | 8 +-- FoxTube/Pages/History.xaml | 16 +++--- FoxTube/Pages/History.xaml.cs | 42 +++++---------- FoxTube/Pages/MainPage.xaml.cs | 14 ++--- FoxTube/Pages/SettingsPages/Inbox.xaml.cs | 4 +- FoxTube/Pages/VideoPage.xaml.cs | 2 + 9 files changed, 100 insertions(+), 56 deletions(-) diff --git a/FoxTube.Background/BackgroundProcessor.cs b/FoxTube.Background/BackgroundProcessor.cs index 8a3a369..2b2198c 100644 --- a/FoxTube.Background/BackgroundProcessor.cs +++ b/FoxTube.Background/BackgroundProcessor.cs @@ -36,6 +36,8 @@ namespace FoxTube.Background lastCheck = DateTime.Now; } + ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "Background task initialized. Retrieving videos since", lastCheck.ToString(), null, null)); + bool[] notificationsSettings = JsonConvert.DeserializeObject(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("notifications.json"))); if(notificationsSettings[0]) CheckAnnouncements(); diff --git a/FoxTube/Classes/SecretsVault.cs b/FoxTube/Classes/SecretsVault.cs index 9e49c12..5bba87e 100644 --- a/FoxTube/Classes/SecretsVault.cs +++ b/FoxTube/Classes/SecretsVault.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Net; using System.Threading; using System.Threading.Tasks; @@ -38,8 +39,8 @@ namespace FoxTube public static bool IsAuthorized { get; private set; } = false; 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 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() @@ -58,6 +59,37 @@ namespace FoxTube } } + public static async void HistoryAdd(string id) + { + UserHistory.Remove(id); + UserHistory.Add(id); + + if (UserHistory.Count > 100) + UserHistory.RemoveAt(UserHistory.Count - 1); + + await FileIO.WriteTextAsync( + await ApplicationData.Current.RoamingFolder.CreateFileAsync("history.json", CreationCollisionOption.ReplaceExisting), + JsonConvert.SerializeObject(UserHistory)); + } + + public static async void LaterAdd(string id) + { + WatchLater.Add(id); + + await FileIO.WriteTextAsync( + await ApplicationData.Current.RoamingFolder.CreateFileAsync("watchlater.json", CreationCollisionOption.ReplaceExisting), + JsonConvert.SerializeObject(UserHistory)); + } + + public static async void LaterRemove(string id) + { + WatchLater.Remove(id); + + await FileIO.WriteTextAsync( + await ApplicationData.Current.RoamingFolder.CreateFileAsync("watchlater.json", CreationCollisionOption.ReplaceExisting), + JsonConvert.SerializeObject(UserHistory)); + } + public static async Task ChangeSubscriptionState(string id) { if (!IsAuthorized) @@ -177,8 +209,32 @@ namespace FoxTube request.Mine = true; UserChannel = (await request.ExecuteAsync()).Items[0]; AccountId = UserChannel.Id; + + try + { + await ApplicationData.Current.RoamingFolder.GetFileAsync("history.json"); + } + catch (FileNotFoundException) + { + await FileIO.WriteTextAsync( + await ApplicationData.Current.RoamingFolder.CreateFileAsync("history.json", CreationCollisionOption.ReplaceExisting), + JsonConvert.SerializeObject(new List())); + } + try + { + await ApplicationData.Current.RoamingFolder.GetFileAsync("watchlater.json"); + } + catch (FileNotFoundException) + { + await FileIO.WriteTextAsync( + await ApplicationData.Current.RoamingFolder.CreateFileAsync("watchlater.json", CreationCollisionOption.ReplaceExisting), + JsonConvert.SerializeObject(new List())); + } - PlaylistItemsResource.ListRequest playlistRequest = Service.PlaylistItems.List("snippet"); + UserHistory = JsonConvert.DeserializeObject>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("history.json"))); + WatchLater = JsonConvert.DeserializeObject>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("watchlater.json"))); + + /*PlaylistItemsResource.ListRequest playlistRequest = Service.PlaylistItems.List("snippet"); playlistRequest.PlaylistId = UserChannel.ContentDetails.RelatedPlaylists.WatchHistory; playlistRequest.MaxResults = 50; PlaylistItemListResponse playlistResponse = await playlistRequest.ExecuteAsync(); @@ -204,7 +260,7 @@ namespace FoxTube WatchLater.Add(i); nextToken = playlistResponse.NextPageToken; - } + }*/ SubscriptionsResource.ListRequest subRequest = Service.Subscriptions.List("snippet"); subRequest.Mine = true; @@ -216,7 +272,7 @@ namespace FoxTube foreach (Subscription s in subResponse.Items) Subscriptions.Add(s); - nextToken = subResponse.NextPageToken; + string nextToken = subResponse.NextPageToken; while (nextToken != null) { subRequest.PageToken = nextToken; diff --git a/FoxTube/Controls/VideoCard.xaml b/FoxTube/Controls/VideoCard.xaml index 277d740..5780c5b 100644 --- a/FoxTube/Controls/VideoCard.xaml +++ b/FoxTube/Controls/VideoCard.xaml @@ -18,7 +18,7 @@ - + diff --git a/FoxTube/Controls/VideoCard.xaml.cs b/FoxTube/Controls/VideoCard.xaml.cs index 132b6b7..6b9511b 100644 --- a/FoxTube/Controls/VideoCard.xaml.cs +++ b/FoxTube/Controls/VideoCard.xaml.cs @@ -82,12 +82,8 @@ namespace FoxTube.Controls } catch { } - foreach(PlaylistItem i in SecretsVault.UserHistory) - if (i.Snippet.ResourceId.VideoId == id) - { - watched.Visibility = Visibility.Visible; - break; - } + if(SecretsVault.UserHistory.Contains(videoId)) + watched.Visibility = Visibility.Visible; } public async void Button_Click(object sender, RoutedEventArgs e) diff --git a/FoxTube/Pages/History.xaml b/FoxTube/Pages/History.xaml index 5adfa85..f471fb0 100644 --- a/FoxTube/Pages/History.xaml +++ b/FoxTube/Pages/History.xaml @@ -12,16 +12,16 @@ Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> - - - - - + + + + + + - + - - + diff --git a/FoxTube/Pages/History.xaml.cs b/FoxTube/Pages/History.xaml.cs index 3d6ac42..abfb7dc 100644 --- a/FoxTube/Pages/History.xaml.cs +++ b/FoxTube/Pages/History.xaml.cs @@ -1,17 +1,8 @@ using FoxTube.Controls; 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.System; 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 @@ -24,15 +15,19 @@ namespace FoxTube.Pages public sealed partial class History : Page { LoadingPage loading; - ShowMore more; VideoGrid list; public History() { - this.InitializeComponent(); + InitializeComponent(); + } + + protected override void OnNavigatedTo(NavigationEventArgs e) + { + base.OnNavigatedTo(e); + loading = grid.Children[2] as LoadingPage; - more = stack.Children[1] as ShowMore; - list = stack.Children[0] as VideoGrid; + list = scroll.Content as VideoGrid; Initialize(); } @@ -40,24 +35,15 @@ namespace FoxTube.Pages { loading.Refresh(); - more.Complete(true); + list.Clear(); + SecretsVault.UserHistory.ForEach(i => list.Add(new VideoCard(i))); - loading.Block(); + loading.Close(); } - private void ShowMore_Clicked() + private async void toBrowser_Click(object sender, RoutedEventArgs e) { - - } - - private void toBrowser_Click(object sender, RoutedEventArgs e) - { - - } - - private void refresh_Click(object sender, RoutedEventArgs e) - { - + await Launcher.LaunchUriAsync(new Uri("youtube.com/feed/history")); } } } diff --git a/FoxTube/Pages/MainPage.xaml.cs b/FoxTube/Pages/MainPage.xaml.cs index cd1eae4..387497b 100644 --- a/FoxTube/Pages/MainPage.xaml.cs +++ b/FoxTube/Pages/MainPage.xaml.cs @@ -102,11 +102,13 @@ namespace FoxTube 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); + if (await ApplicationData.Current.RoamingFolder.GetFileAsync("notifications.json") != null) + { + 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)); + } } protected override void OnNavigatedTo(NavigationEventArgs e) @@ -128,7 +130,7 @@ namespace FoxTube titleBar.ButtonBackgroundColor = Colors.Red; titleBar.ButtonHoverBackgroundColor = Colors.IndianRed; titleBar.ButtonPressedBackgroundColor = Colors.DarkRed; - titleBar.ButtonInactiveBackgroundColor = Colors.DeepPink; + titleBar.ButtonInactiveBackgroundColor = Colors.DarkRed; titleBar.ForegroundColor = Colors.White; CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = false; diff --git a/FoxTube/Pages/SettingsPages/Inbox.xaml.cs b/FoxTube/Pages/SettingsPages/Inbox.xaml.cs index bf40fe8..70cfad5 100644 --- a/FoxTube/Pages/SettingsPages/Inbox.xaml.cs +++ b/FoxTube/Pages/SettingsPages/Inbox.xaml.cs @@ -52,8 +52,8 @@ namespace FoxTube.Pages.SettingsPages items.Add(new InboxItem( e["header"].InnerText, e["content"].InnerText, - DateTime.Parse(e.GetAttribute("time")), - e.GetAttribute("id") + DateTime.Parse(e["time"].InnerText), + e["id"].InnerText )); items.OrderBy(item => item.TimeStamp); diff --git a/FoxTube/Pages/VideoPage.xaml.cs b/FoxTube/Pages/VideoPage.xaml.cs index ccafc1a..9b405a1 100644 --- a/FoxTube/Pages/VideoPage.xaml.cs +++ b/FoxTube/Pages/VideoPage.xaml.cs @@ -208,6 +208,8 @@ namespace FoxTube.Pages } } subscribe.Visibility = Visibility.Visible; + + SecretsVault.HistoryAdd(videoId); } else {