Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube/Pages/PlaylistPage.xaml.cs
T
Michael Gordeev 91fd3f6ab9 Refreshing fixed
Related Work Items: #212
2018-12-23 14:24:16 +03:00

179 lines
6.5 KiB
C#

using FoxTube.Controls;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Streams;
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.Media.Imaging;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
namespace FoxTube.Pages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class PlaylistPage : Page
{
public string playlistId;
Playlist item;
LoadingPage loading;
VideoGrid list;
public PlaylistPage()
{
this.InitializeComponent();
loading = grid.Children[2] as LoadingPage;
list = ((grid.Children[0] as ScrollViewer).Content as Grid).Children[1] as VideoGrid;
loading.RefreshPage += refresh_Click;
DataTransferManager.GetForCurrentView().DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(Share);
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.Parameter == null)
loading.Error("NullReferenceException", "Unable to initialize page. Playlist ID is not stated.");
else
Initialize(e.Parameter as string);
}
public async void Initialize(string id)
{
loading.Refresh();
try
{
playlistId = id;
if (Methods.NeedToResponse)
Methods.MainPage.content_Navigated(this, null);
PlaylistsResource.ListRequest request = SecretsVault.Service.Playlists.List("snippet,contentDetails");
request.Id = id;
item = (await request.ExecuteAsync()).Items[0];
title.Text = item.Snippet.Title;
info.Text = $"{item.ContentDetails.ItemCount} videos";
description.Text = item.Snippet.Description;
channelName.Text = item.Snippet.ChannelTitle;
try
{
thumbnail.Source = new BitmapImage(new Uri(item.Snippet.Thumbnails.Medium.Url));
ChannelsResource.ListRequest channelRequest = SecretsVault.Service.Channels.List("snippet");
channelRequest.Id = item.Snippet.ChannelId;
Channel channel = (await channelRequest.ExecuteAsync()).Items[0];
avatar.ProfilePicture = new BitmapImage(new Uri(channel.Snippet.Thumbnails.Medium.Url));
}
catch { }
PlaylistItemsResource.ListRequest listRequest = SecretsVault.Service.PlaylistItems.List("contentDetails");
listRequest.PlaylistId = id;
listRequest.MaxResults = 50;
PlaylistItemListResponse response = await listRequest.ExecuteAsync();
list.Clear();
foreach (PlaylistItem i in response.Items)
list.Add(new VideoCard(i.ContentDetails.VideoId, playlistId));
while (response.NextPageToken != null)
{
listRequest.PageToken = response.NextPageToken;
response = await listRequest.ExecuteAsync();
foreach (PlaylistItem i in response.Items)
list.Add(new VideoCard(i.ContentDetails.VideoId, playlistId));
}
loading.Close();
}
catch (System.Net.Http.HttpRequestException)
{
loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true);
}
catch
{
loading.Error();
}
}
private void toChannel_Click(object sender, RoutedEventArgs e)
{
Methods.MainPage.GoToChannel(item.Snippet.ChannelId);
}
private async void inBrowser_Click(object sender, RoutedEventArgs e)
{
await Launcher.LaunchUriAsync(new Uri($"https://www.youtube.com/playlist?list={item.Id}"));
}
private void refresh_Click(object sender, RoutedEventArgs e)
{
Methods.MainPage.GoToPlaylist(playlistId);
}
private void share_Click(object sender, RoutedEventArgs e)
{
DataTransferManager.ShowShareUI();
}
private async void Share(DataTransferManager sender, DataRequestedEventArgs args)
{
DataRequest request = args.Request;
request.Data.Properties.Title = item.Snippet.Title;
request.Data.Properties.Description = "Sharing a playlist";
// Handle errors
//request.FailWithDisplayText("Something unexpected could happen.");
// Plain text
request.Data.SetText(item.Snippet.Title + "\n" + "#YouTube #FoxTube #SharedWithFoxTube");
// Uniform Resource Identifiers (URIs)
request.Data.SetWebLink(new Uri($"https://www.youtube.com/playlist?list={item.Id}"));
// HTML
//request.Data.SetHtmlFormat("<b>Bold Text</b>");
// Because we are making async calls in the DataRequested event handler,
// we need to get the deferral first.
DataRequestDeferral deferral = request.GetDeferral();
// Make sure we always call Complete on the deferral.
try
{
StorageFile thumbnailFile = await Package.Current.InstalledLocation.GetFileAsync(item.Snippet.Thumbnails.Medium.Url);
request.Data.Properties.Thumbnail = RandomAccessStreamReference.CreateFromFile(thumbnailFile);
StorageFile imageFile = await Package.Current.InstalledLocation.GetFileAsync(item.Snippet.Thumbnails.Medium.Url);
// Bitmaps
request.Data.SetBitmap(RandomAccessStreamReference.CreateFromFile(imageFile));
}
finally
{
deferral.Complete();
}
}
}
}