Added Subscriptions page and updated subscriptions management
This commit is contained in:
+65
-20
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user