diff --git a/FoxTube.Background/BackgroundProcessor.cs b/FoxTube.Background/BackgroundProcessor.cs index fa39192..5fa6e5b 100644 --- a/FoxTube.Background/BackgroundProcessor.cs +++ b/FoxTube.Background/BackgroundProcessor.cs @@ -16,13 +16,8 @@ using Windows.UI.Notifications; namespace FoxTube.Background { - public delegate void NotificationHandler(object sender, Notification item); - public sealed class BackgroundProcessor : IBackgroundTask { - public static event NotificationHandler NotificationRecieved; - - List Notifications = new List(); private DateTime lastCheck = DateTime.Now; private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; @@ -37,8 +32,6 @@ namespace FoxTube.Background ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", ApplicationName = "FoxTube" }); - - XmlDocument doc = new XmlDocument(); BackgroundTaskDeferral def; public void Run(IBackgroundTaskInstance taskInstance) @@ -47,110 +40,81 @@ namespace FoxTube.Background 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"]) + CheckAnnouncements(); + if((bool)settings.Values["devNews"]) + CheckAccount(); - try - { - doc.LoadXml(settings.Values["notificationsHistory"] as string); - } - catch - { - return; - } - - CheckAnnouncements(); - CheckAccount(); - - SendNSave(); + settings.Values["lastCheck"] = XmlConvert.ToString(DateTime.Now, "YYYY-MM-DDThh:mm:ss"); def.Complete(); } async void CheckAccount() { - 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() + try { - HttpClientInitializer = credential, - ApplicationName = "FoxTube" - }).Subscriptions.List("snippet"); - subRequest.Mine = true; - subRequest.MaxResults = 50; - SubscriptionListResponse subResponse = await subRequest.ExecuteAsync(); - Dictionary subs = new Dictionary(); + UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None); + if (credential == null) + return; - 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; + 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); - } - foreach(var s in subs) - { - SearchResource.ListRequest request = Service.Search.List("snippet"); - request.PublishedAfter = lastCheck; - request.ChannelId = s.Key; - request.Type = "video"; - SearchListResponse response = await request.ExecuteAsync(); + string nextToken = subResponse.NextPageToken; + while (nextToken != null) + { + subRequest.PageToken = nextToken; + subResponse = await subRequest.ExecuteAsync(); + nextToken = subResponse.NextPageToken; - foreach (var i in response.Items) - Notifications.Add(new Notification("video", i.Id.VideoId, - i.Snippet.ChannelTitle, - i.Snippet.Title, - i.Snippet.PublishedAt.Value, - i.Snippet.Thumbnails.Standard.Url, - s.Value)); + foreach (Subscription s in subResponse.Items) + subs.Add(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url); + } + + foreach (var s in subs) + { + SearchResource.ListRequest request = Service.Search.List("snippet"); + request.PublishedAfter = lastCheck; + request.ChannelId = s.Key; + request.Type = "video"; + request.MaxResults = 5; + SearchListResponse response = await request.ExecuteAsync(); + + 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)); + } } + catch { } } void CheckAnnouncements() { - XmlDocument doc = new XmlDocument(); - doc.Load(XmlReader.Create("http://foxgame.hol.es/foxtube-messages.xml")); - if ((XmlConvert.ToDateTimeOffset((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), "YYYY-MM-DDThh:mm:ss") - lastCheck.ToUniversalTime()).TotalSeconds > 0) - Notifications.Add(new Notification("internal", (doc["posts"].FirstChild as XmlElement).GetAttribute("id"), - doc["posts"].FirstChild["header"].InnerText, - doc["posts"].FirstChild["notificationHeader"].InnerText, - XmlConvert.ToDateTimeOffset((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), "YYYY-MM-DDThh:mm:ss"), - (doc["posts"].FirstChild as XmlElement).GetAttribute("image"), (doc["posts"].FirstChild as XmlElement).GetAttribute("avatar"))); - } - - void SendNotification(Notification notification) - { - ToastNotificationManager.CreateToastNotifier().Show(notification.GetToast()); - } - - void SendNSave() - { - foreach (Notification n in Notifications) + try { - NotificationRecieved.Invoke(this, n); - doc["history"].InnerXml += n.GetXml(); + XmlDocument doc = new XmlDocument(); + doc.Load(XmlReader.Create("http://foxgame.hol.es/foxtube-messages.xml")); + if ((XmlConvert.ToDateTimeOffset((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), "YYYY-MM-DDThh:mm:ss") - lastCheck.ToUniversalTime()).TotalSeconds > 0) + ToastNotificationManager.CreateToastNotifier().Show( + Notification.GetInternalToast(doc["posts"].FirstChild["id"].InnerText, + doc["posts"].FirstChild["header"].InnerText, + doc["posts"].FirstChild["content"].InnerText, + doc["posts"].FirstChild["thumbnail"].InnerText, + doc["posts"].FirstChild["avatar"].InnerText)); } - settings.Values.Add("notificationsHistory", doc.InnerXml); - - foreach (Notification n in Notifications) - switch (n.Type) - { - case NotificationType.Video: - if ((bool)settings.Values["newVideoNotification"]) - SendNotification(n); - break; - - case NotificationType.Internal: - SendNotification(n); - break; - } + catch { } } } } diff --git a/FoxTube.Background/Notification.cs b/FoxTube.Background/Notification.cs index a872194..a1f08d0 100644 --- a/FoxTube.Background/Notification.cs +++ b/FoxTube.Background/Notification.cs @@ -1,235 +1,67 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Windows.Data.Xml.Dom; -using Windows.UI; +using Windows.Data.Xml.Dom; using Windows.UI.Notifications; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Media.Imaging; namespace FoxTube.Background { - public enum NotificationType + public static class Notification { - Video, Comment, Post, Internal, Changelog - } - - public sealed class Notification - { - public string Channel { get; set; } - public string Content { get; set; } - public DateTimeOffset TimeStamp { get; set; } - public NotificationType Type { get; set; } - public string Avatar { get; set; } - public string Thumbnail { get; set; } - public string Id { get; set; } - - public Notification(string type, string id, - string channelName, string content, DateTimeOffset date, - string thumbnailUrl, string avatarUrl) - { - Channel = channelName; - Content = content; - TimeStamp = date; - Id = id; - Type = TypeConversion(type); - Avatar = avatarUrl ?? "ms-appx:///Assets/Icons/Profile.png"; - Thumbnail = thumbnailUrl; - } - - public Notification(string xmlSource) - { - System.Xml.XmlDocument d = new System.Xml.XmlDocument(); - d.InnerXml = xmlSource; - System.Xml.XmlElement xml = d["item"]; - - Channel = xml["channelName"].InnerText; - Content = xml["content"].InnerText; - TimeStamp = DateTimeOffset.Parse(xml.GetAttribute("time")); - Id = xml.GetAttribute("id"); - Type = TypeConversion(xml.GetAttribute("type")); - Avatar = xml["images"].GetAttribute("avatar"); - Thumbnail = xml["images"].GetAttribute("thumbnail"); - } - - public string GetXml() - { - return $@" - - {Channel} - {Content} - "; - } - - public Button GetNotification() - { - StackPanel stackPanel = new StackPanel() { Margin = new Thickness(10, 0, 0, 0) }; - - //Channel - switch (Type) - { - case NotificationType.Internal: - stackPanel.Children.Add(new TextBlock() - { - FontSize = 14, - FontStyle = Windows.UI.Text.FontStyle.Italic, - Foreground = new SolidColorBrush(Colors.Gray), - Text = "Developer's message" - }); - break; - - case NotificationType.Changelog: - stackPanel.Children.Add(new TextBlock() - { - FontSize = 14, - FontStyle = Windows.UI.Text.FontStyle.Italic, - Foreground = new SolidColorBrush(Colors.Gray), - Text = "Changelog" - }); - break; - - case NotificationType.Video: - stackPanel.Children.Add(new TextBlock() - { - FontSize = 14, - FontStyle = Windows.UI.Text.FontStyle.Italic, - Foreground = new SolidColorBrush(Colors.Gray), - Text = $"{Channel} uploaded new video" - }); - break; - } - - //Content - stackPanel.Children.Add(new TextBlock() - { - TextWrapping = TextWrapping.WrapWholeWords, - Text = Content, - }); - //Time - stackPanel.Children.Add(new TextBlock() - { - FontSize = 13, - Foreground = new SolidColorBrush(Colors.Gray), - Text = TimeStamp.ToString() - }); - PersonPicture avatar = new PersonPicture() - { - Height = 50, - VerticalAlignment = VerticalAlignment.Top, - ProfilePicture = string.IsNullOrWhiteSpace(Avatar) ? null : new BitmapImage(new Uri(Avatar)) - }; - - Grid grid = new Grid(); - grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50) }); - grid.ColumnDefinitions.Add(new ColumnDefinition()); - - grid.Children.Add(avatar); - Grid.SetColumn(stackPanel, 1); - grid.Children.Add(stackPanel); - - Button item = new Button() - { - HorizontalAlignment = HorizontalAlignment.Stretch, - HorizontalContentAlignment = HorizontalAlignment.Left, - Background = new SolidColorBrush(Colors.Transparent), - Content = grid - }; - - return item; - } - - public ToastNotification GetToast() + public static ToastNotification GetChangelogToast(string version) { XmlDocument template = new XmlDocument(); - switch (Type) - { - case NotificationType.Video: - template.LoadXml($@" - - - - {Content} - {Channel} uploaded a new video - - - - - - - - "); - break; - - case NotificationType.Changelog: - template.LoadXml($@" - - - - - {Content} - {Channel} - - - "); - break; - - case NotificationType.Internal: - template.LoadXml($@" - - - - - {Content} - {Channel} - - - "); - break; - } + template.LoadXml($@" + + + + + Changelog + See what's new in version {version} + + + "); return new ToastNotification(template); } - private string TypeConversion(NotificationType type) + public static ToastNotification GetVideoToast(string id, string channelId, string title, string channel, string thumbnail, string avatar) { - switch(type) - { - case NotificationType.Comment: - return "comment"; - case NotificationType.Post: - return "post"; - case NotificationType.Video: - return "video"; - case NotificationType.Changelog: - return "changelog"; - default: - return "internal"; - } + XmlDocument template = new XmlDocument(); + + template.LoadXml($@" + + + + + {title} + {channel} uploaded a new video + + + + + + + + "); + + return new ToastNotification(template); } - private NotificationType TypeConversion(string type) + public static ToastNotification GetInternalToast(string id, string header, string content, string thumbnail, string avatar) { - switch (type) - { - case "comment": - return NotificationType.Comment; - case "post": - return NotificationType.Post; - case "video": - return NotificationType.Video; - case "changelog": - return NotificationType.Changelog; - default: - return NotificationType.Internal; - } + XmlDocument template = new XmlDocument(); + + template.LoadXml($@" + + + + + {header} + {content} + + + "); + + return new ToastNotification(template); } } } diff --git a/FoxTube/Controls/Advert.xaml b/FoxTube/Controls/Advert.xaml new file mode 100644 index 0000000..3834488 --- /dev/null +++ b/FoxTube/Controls/Advert.xaml @@ -0,0 +1,15 @@ + + + + + + diff --git a/FoxTube/Controls/Advert.xaml.cs b/FoxTube/Controls/Advert.xaml.cs new file mode 100644 index 0000000..e2872c3 --- /dev/null +++ b/FoxTube/Controls/Advert.xaml.cs @@ -0,0 +1,27 @@ +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; + +// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 + +namespace FoxTube.Controls +{ + public sealed partial class Advert : UserControl + { + public Advert() + { + this.InitializeComponent(); + } + } +} diff --git a/FoxTube/FoxTube.csproj b/FoxTube/FoxTube.csproj index 67b00cd..fd5e489 100644 --- a/FoxTube/FoxTube.csproj +++ b/FoxTube/FoxTube.csproj @@ -101,6 +101,9 @@ + + Advert.xaml + ChannelCard.xaml @@ -277,6 +280,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/FoxTube/Notifications/DownloadComplete.xml b/FoxTube/Notifications/DownloadComplete.xml index 0cc68f2..91e9c37 100644 --- a/FoxTube/Notifications/DownloadComplete.xml +++ b/FoxTube/Notifications/DownloadComplete.xml @@ -1 +1 @@ - Download complete Subnautica - SAY GOODBYE TO SUBNAUTICA! We're Back Home! - Subnautica Ending (Full Release Gameplay) \ No newline at end of file + Download complete Subnautica - SAY GOODBYE TO SUBNAUTICA! We're Back Home! - Subnautica Ending (Full Release Gameplay) \ No newline at end of file diff --git a/FoxTube/Notifications/ServerLogSamlpe.xml b/FoxTube/Notifications/ServerLogSamlpe.xml index 1eecfe5..2c9c73f 100644 --- a/FoxTube/Notifications/ServerLogSamlpe.xml +++ b/FoxTube/Notifications/ServerLogSamlpe.xml @@ -1,6 +1,7 @@  - - Short headline for toast notifications + + +
Main headline for full post
Announcement body (beware of special characters)
diff --git a/FoxTube/Pages/MainPage.xaml.cs b/FoxTube/Pages/MainPage.xaml.cs index 1717536..94c642b 100644 --- a/FoxTube/Pages/MainPage.xaml.cs +++ b/FoxTube/Pages/MainPage.xaml.cs @@ -53,8 +53,8 @@ namespace FoxTube if (settings.Values["newVideoNotification"] == null) settings.Values.Add("newVideoNotification", true); - if (settings.Values["newmessagesNotification"] == null) - settings.Values.Add("newmessagesNotification", true); + if (settings.Values["devNews"] == null) + settings.Values.Add("devNews", true); if (settings.Values["moblieWarning"] == null) settings.Values.Add("moblieWarning", false); @@ -70,19 +70,11 @@ namespace FoxTube if (settings.Values["safeSearch"] == null) settings.Values.Add("safeSearch", 0); - if (settings.Values["notificationsHistory"] == null) - { - XmlDocument doc = new XmlDocument(); - doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); - doc.AppendChild(doc.CreateElement("history")); - settings.Values.Add("notificationsHistory", doc.InnerXml); - } - PackageVersion ver = Package.Current.Id.Version; 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}") //Replace for '!=' !!! { try { @@ -90,21 +82,7 @@ namespace FoxTube changelog.Load("http://foxgame.hol.es/foxtube-changelog.xml"); XmlElement e = changelog["items"].ChildNodes[0] as XmlElement; - XmlDocument doc = new XmlDocument(); - doc.LoadXml(settings.Values["notificationsHistory"] as string); - - Background.Notification n = new Background.Notification("changelog", - $"changelog-{e.GetAttribute("version").Replace('.', '-')}", - "Changelog", - $"What's new in version {e.GetAttribute("version")}", - DateTime.Parse(e.GetAttribute("time")), - "http://foxgame.hol.es/FoxTubeAssets/WhatsNewThumb.png", - "http://foxgame.hol.es/FoxTubeAssets/NewsAvatar.png"); - - doc["history"].InnerXml += n.GetXml(); - settings.Values["notificationsHistory"] = doc.InnerXml; - - ToastNotificationManager.CreateToastNotifier().Show(n.GetToast()); + ToastNotificationManager.CreateToastNotifier().Show(FoxTube.Background.Notification.GetChangelogToast(e.GetAttribute("version"))); settings.Values["ver"] = $"{ver.Major}.{ver.Minor}"; } diff --git a/FoxTube/Pages/SettingsPages/General.xaml b/FoxTube/Pages/SettingsPages/General.xaml index cbdfac1..1f9d57f 100644 --- a/FoxTube/Pages/SettingsPages/General.xaml +++ b/FoxTube/Pages/SettingsPages/General.xaml @@ -42,6 +42,7 @@ + diff --git a/FoxTube/Pages/SettingsPages/General.xaml.cs b/FoxTube/Pages/SettingsPages/General.xaml.cs index f95c6bc..4a448fd 100644 --- a/FoxTube/Pages/SettingsPages/General.xaml.cs +++ b/FoxTube/Pages/SettingsPages/General.xaml.cs @@ -134,5 +134,10 @@ namespace FoxTube.Pages.SettingsPages { CoreApplication.Exit(); } + + private void devNews_Toggled(object sender, RoutedEventArgs e) + { + settings.Values["devnews"] = devNews.IsOn; + } } }