Archived
1
0

Added Subscriptions page and updated subscriptions management

This commit is contained in:
Michael Gordeev
2020-05-14 23:45:11 +03:00
parent 8159d33a43
commit b3212738e8
12 changed files with 284 additions and 67 deletions
+12 -1
View File
@@ -1,10 +1,12 @@
using FoxTube.Utils;
using Google.Apis.YouTube.v3.Data;
using SQLitePCL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Windows.Devices.PointOfService;
using Windows.UI;
@@ -47,7 +49,7 @@ namespace FoxTube
public static string ToHex(this Color color) =>
$"#{color.R:X}{color.G:X}{color.B:X}";
public static Color FromHex(this Color parent, string hex)
public static Color FromHex(string hex)
{
hex = hex.Replace("#", "");
List<byte> values = new List<byte>();
@@ -126,5 +128,14 @@ namespace FoxTube
else
return Math.Round(span.TotalDays / 365) + " " + "years ago";
}
public static async Task<Channel> GetChannel(string channelId, string part)
{
var request = UserManagement.Service.Channels.List(part);
request.Id = channelId;
request.MaxResults = 1;
return (await request.ExecuteAsync()).Items.FirstOrDefault();
}
}
}
+1
View File
@@ -130,6 +130,7 @@
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<ItemGroup>
<Compile Include="Models\Subscription.cs" />
<Compile Include="Services\DownloadsCenter.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Utils\Feedback.cs" />
+16
View File
@@ -0,0 +1,16 @@
using Google.Apis.YouTube.v3.Data;
namespace FoxTube.Models
{
public class Subscription
{
public string Title { get; set; }
public string Description { get; set; }
public string ChannelId { get; set; }
public string SubscriptionId { get; set; }
public ThumbnailDetails Avatar { get; set; }
public ImageSettings Banner { get; set; }
public ulong? SubscribersCount { get; set; }
public ulong VideosCount { get; set; }
}
}
+65 -20
View File
@@ -30,26 +30,24 @@ namespace FoxTube.Models
public async Task<bool> UpdateSubscriptionState(string channelId)
{
if (Subscriptions.Exists(x => x.Snippet.ResourceId.ChannelId == channelId))
if (Subscriptions.Find(i => i.ChannelId == channelId) is Subscription subscription)
{
Subscription s = Subscriptions.Find(x => x.Snippet.ResourceId.ChannelId == channelId);
try { await Service.Subscriptions.Delete(s.Id).ExecuteAsync(); }
try { await Service.Subscriptions.Delete(subscription.SubscriptionId).ExecuteAsync(); }
catch (Exception e)
{
Metrics.SendReport(new Exception("Failed to unsubscribe", e));
return true;
}
UserManagement.SubscriptionsChangedInvoker(this, s);
Subscriptions.Remove(s);
UserManagement.SubscriptionsChangedInvoker(this, subscription);
Subscriptions.Remove(subscription);
SaveSubscriptions();
return false;
}
else
{
var request = Service.Subscriptions.Insert(new Subscription()
var request = Service.Subscriptions.Insert(new Google.Apis.YouTube.v3.Data.Subscription()
{
Snippet = new SubscriptionSnippet()
{
@@ -59,14 +57,27 @@ namespace FoxTube.Models
Kind = "youtube#channel"
}
}
}, "snippet");
}, "id");
Subscription s = await request.ExecuteAsync();
if (s == null)
var subscriptionData = await request.ExecuteAsync();
if (subscriptionData == null)
return false;
Subscriptions.Add(s);
UserManagement.SubscriptionsChangedInvoker(this, s);
Channel channel = await Extensions.GetChannel(subscriptionData.Snippet.ChannelId, "snippet,statistics,brandingSettins");
subscription = new Subscription
{
Title = channel.Snippet.Title,
ChannelId = channel.Id,
SubscriptionId = subscriptionData.Id,
VideosCount = channel.Statistics.VideoCount.Value,
SubscribersCount = channel.Statistics.SubscriberCount,
Avatar = channel.Snippet.Thumbnails,
Banner = channel.BrandingSettings.Image
};
Subscriptions.Add(subscription);
UserManagement.SubscriptionsChangedInvoker(this, subscription);
SaveSubscriptions();
return true;
@@ -76,7 +87,7 @@ namespace FoxTube.Models
private void SaveSubscriptions()
{
Dictionary<string, string> subs = Subscriptions.Select(i =>
new KeyValuePair<string, string>(i.Snippet.ResourceId.ChannelId, i.Snippet.Thumbnails.Default__?.Url))
new KeyValuePair<string, string>(i.ChannelId, i.Avatar.Default__?.Url))
as Dictionary<string, string>;
ApplicationData.Current.RoamingSettings.Values[$"Subscriptions.{UserInfo.Id}"] = JsonConvert.SerializeObject(subs);
@@ -99,9 +110,22 @@ namespace FoxTube.Models
user.UserInfo = await new Oauth2Service(initializer).Userinfo.Get().ExecuteAsync();
var request = user.Service.Channels.List("snippet,contentDetails,brandingSettings");
request.Mine = true;
user.Channel = request.Execute().Items[0];
// TODO: Retrieve history and WL
SubscriptionsResource.ListRequest subRequest = user.Service.Subscriptions.List("snippet");
await user.LoadSubscriptions();
return user;
}
public async Task LoadSubscriptions()
{
Subscriptions.Clear();
SubscriptionsResource.ListRequest subRequest = Service.Subscriptions.List("snippet");
subRequest.Mine = true;
subRequest.MaxResults = 50;
subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance;
@@ -112,16 +136,37 @@ namespace FoxTube.Models
subResponse = await subRequest.ExecuteAsync();
subRequest.PageToken = subResponse.NextPageToken;
user.Subscriptions.AddRange(subResponse.Items);
Subscriptions.AddRange(subResponse.Items.Select(i => new Subscription
{
Title = i.Snippet.Title,
ChannelId = i.Snippet.ResourceId.ChannelId,
SubscriptionId = i.Id,
Avatar = i.Snippet.Thumbnails,
Description = i.Snippet.Description
}));
} while (!string.IsNullOrWhiteSpace(subRequest.PageToken));
var request = user.Service.Channels.List("snippet,contentDetails,brandingSettings");
request.Mine = true;
user.Channel = request.Execute().Items[0];
var request = Service.Channels.List("statistics,brandingSettings");
request.Id = string.Join(',', Subscriptions.Select(i => i.ChannelId));
request.MaxResults = 50;
ChannelListResponse response;
return user;
if (Subscriptions.Count > 0)
do
{
response = await request.ExecuteAsync();
request.PageToken = response.NextPageToken;
Subscriptions.ForEach(i =>
{
Channel channel = response.Items.FirstOrDefault(c => c.Id == i.ChannelId);
i.Banner = channel.BrandingSettings.Image;
i.SubscribersCount = channel.Statistics.HiddenSubscriberCount.Value ? null : channel.Statistics.SubscriberCount;
i.VideosCount = channel.Statistics.VideoCount.Value;
});
}
while (!string.IsNullOrWhiteSpace(request.PageToken));
}
}
}
-1
View File
@@ -9,7 +9,6 @@ using FoxTube.Models;
using YouTube;
using System.Threading;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using Windows.UI.Xaml.Controls;
using FoxTube.Utils;
using YoutubeExplode;