diff --git a/FoxTube/Classes/ObjectEventArgs.cs b/FoxTube/Classes/ObjectEventArgs.cs index 8f80950..27ac519 100644 --- a/FoxTube/Classes/ObjectEventArgs.cs +++ b/FoxTube/Classes/ObjectEventArgs.cs @@ -6,15 +6,5 @@ using System.Threading.Tasks; namespace FoxTube { - public delegate void ObjectEventHandler(object sender, ObjectEventArgs args); - - public class ObjectEventArgs - { - public List Parameters = new List(); - public ObjectEventArgs(params object[] args) - { - foreach (object a in args) - Parameters.Add(a); - } - } + public delegate void ObjectEventHandler(object sender, params object[] args); } diff --git a/FoxTube/Classes/SecretsVault.cs b/FoxTube/Classes/SecretsVault.cs index e2ad26c..a7478d2 100644 --- a/FoxTube/Classes/SecretsVault.cs +++ b/FoxTube/Classes/SecretsVault.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Net; using System.Threading; - +using System.Threading.Tasks; using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2.Flows; using Google.Apis.Services; @@ -15,6 +15,9 @@ namespace FoxTube public class SecretsVault { #region Static Information + public static event EventHandler AuthorizationStateChanged; + public static event ObjectEventHandler SubscriptionsChanged; + public static NetworkCredential EmailCredential => new NetworkCredential("youwillneverknowthisadress@gmail.com", "thisisthepassword12345"); public static ClientSecrets Secrets => new ClientSecrets() { @@ -52,6 +55,46 @@ namespace FoxTube return NoAuthService; } } + + public static async Task Subscribe(string id) + { + if (!IsAuthorized) + return false; + + var request = Service.Subscriptions.Insert(new Subscription() + { + Snippet = new SubscriptionSnippet() + { + ChannelId = id + } + }, "snippet"); + + Subscription s = await request.ExecuteAsync(); + Subscriptions.Add(s); + SubscriptionsChanged.Invoke(null, "add", s); + return true; + } + + public static async Task Unsubscibe(string id) + { + if (!IsAuthorized) + return false; + + Subscription s = null; + foreach(Subscription i in Subscriptions) + if (i.Snippet.ChannelId == id) + { + s = i; + break; + } + if (s == null) + return false; + SubscriptionsChanged.Invoke(null, "remove", Subscriptions.IndexOf(s)); + await Service.Subscriptions.Delete(s.Id).ExecuteAsync(); //??? + + Subscriptions.Remove(s); + return true; + } #endregion #region Object containers @@ -62,7 +105,6 @@ namespace FoxTube public List later = new List(); public Google.Apis.YouTube.v3.Data.Channel channel; public UserCredential Credential; - public event EventHandler AuthorizationStateChanged; private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; public async void Authorize() diff --git a/FoxTube/Controls/ChannelCard.xaml b/FoxTube/Controls/ChannelCard.xaml index b247145..4fbab61 100644 --- a/FoxTube/Controls/ChannelCard.xaml +++ b/FoxTube/Controls/ChannelCard.xaml @@ -17,28 +17,39 @@ - - + + - + - + + - + - - + + + + + - + + + + + + + Log in to manage your subscriptions + diff --git a/FoxTube/Controls/ChannelCard.xaml.cs b/FoxTube/Controls/ChannelCard.xaml.cs index e14ce94..369cde2 100644 --- a/FoxTube/Controls/ChannelCard.xaml.cs +++ b/FoxTube/Controls/ChannelCard.xaml.cs @@ -1,4 +1,6 @@ -using System; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -11,6 +13,7 @@ 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.Media.Imaging; using Windows.UI.Xaml.Navigation; // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 @@ -19,9 +22,76 @@ namespace FoxTube.Controls { public sealed partial class ChannelCard : UserControl { - public ChannelCard() + string channelId; + Google.Apis.YouTube.v3.Data.Channel item; + + public ChannelCard(string id, string live = "null") { this.InitializeComponent(); + Initialize(id, live); + } + + private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) + { + Height = e.NewSize.Width * 0.75; + } + + public async void Initialize(string id, string live) + { + ChannelsResource.ListRequest request = SecretsVault.NoAuthService.Channels.List("snippet,contentDetails,statistics,liveStreamingDetails"); + request.Id = id; + ChannelListResponse response = await request.ExecuteAsync(); + + item = response.Items[0]; + channelId = id; + + title.Text = item.Snippet.Title; + description.Text = item.Snippet.Description; + + subs.Text = $"{item.Statistics.SubscriberCount} subscribers"; + uploads.Text = $"{item.Statistics.VideoCount} videos"; + + if (live == "live") + liveTag.Visibility = Visibility.Visible; + + if(SecretsVault.IsAuthorized) + { + foreach(Subscription s in SecretsVault.Subscriptions) + { + if(s.Snippet.ChannelId == id) + subscribe.IsChecked = true; + } + subscriptionPane.Visibility = Visibility.Visible; + } + + try + { + avatar.ProfilePicture = new BitmapImage(new Uri(item.Snippet.Thumbnails.Medium.Url)); + cover.Source = new BitmapImage(new Uri(item.BrandingSettings.Image.BannerImageUrl)); + } + catch { } + } + + private void Button_Click(object sender, RoutedEventArgs e) + { + Methods.MainPage.GoToChannel(channelId); + } + + private void Hyperlink_Click(Windows.UI.Xaml.Documents.Hyperlink sender, Windows.UI.Xaml.Documents.HyperlinkClickEventArgs args) + { + SecretsVault.Vault.Authorize(); + } + + private async void subscribe_Click(object sender, RoutedEventArgs e) + { + if (subscribe.IsChecked.Value) + { + if (!await SecretsVault.Subscribe(channelId)) + subscribe.IsChecked = false; + } + else + if (!await SecretsVault.Unsubscibe(channelId)) + subscribe.IsChecked = true; } } }