91fd3f6ab9
Related Work Items: #212
280 lines
9.6 KiB
C#
280 lines
9.6 KiB
C#
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;
|
|
using System.Globalization;
|
|
|
|
using Google.Apis.YouTube.v3;
|
|
using Google.Apis.YouTube.v3.Data;
|
|
using Windows.Storage;
|
|
using Windows.UI.Text;
|
|
using FoxTube.Controls;
|
|
using FoxTube.Pages;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
|
|
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
|
|
|
|
namespace FoxTube
|
|
{
|
|
/// <summary>
|
|
/// An empty page that can be used on its own or navigated to within a Frame.
|
|
/// </summary>
|
|
public sealed partial class Home : Page
|
|
{
|
|
private bool recLoaded = false;
|
|
private bool trendLoaded = false;
|
|
private bool subsLoaded = false;
|
|
|
|
VideoGrid recGrid, trendGrid, subsGrid;
|
|
ShowMore recMore, trendMore, subsMore;
|
|
LoadingPage loading;
|
|
|
|
string trendToken, recToken;
|
|
Dictionary<string, string> subsTokens = new Dictionary<string, string>();
|
|
string reg;
|
|
|
|
public Home()
|
|
{
|
|
this.InitializeComponent();
|
|
try
|
|
{
|
|
reg = (ApplicationData.Current.LocalSettings.Values["region"] as string).ToLower().Remove(0, 3);
|
|
}
|
|
catch (ArgumentOutOfRangeException)
|
|
{
|
|
reg = (ApplicationData.Current.LocalSettings.Values["region"] as string).ToUpper();
|
|
}
|
|
|
|
recGrid = ((recommended.Content as ScrollViewer).Content as StackPanel).Children[0] as VideoGrid;
|
|
trendGrid = ((trending.Content as ScrollViewer).Content as StackPanel).Children[0] as VideoGrid;
|
|
subsGrid = ((subscriptions.Content as ScrollViewer).Content as StackPanel).Children[0] as VideoGrid;
|
|
|
|
recMore = ((recommended.Content as ScrollViewer).Content as StackPanel).Children[1] as ShowMore;
|
|
trendMore = ((trending.Content as ScrollViewer).Content as StackPanel).Children[1] as ShowMore; ;
|
|
subsMore = ((subscriptions.Content as ScrollViewer).Content as StackPanel).Children[1] as ShowMore;
|
|
|
|
loading = grid.Children[2] as LoadingPage;
|
|
|
|
Initialize();
|
|
}
|
|
|
|
private void refreshPage(object sender, RoutedEventArgs e)
|
|
{
|
|
Methods.MainPage.GoToHome();
|
|
}
|
|
|
|
private void RecMore_Clicked()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private async void TrendMore_Clicked()
|
|
{
|
|
try
|
|
{
|
|
VideosResource.ListRequest request = SecretsVault.Service.Videos.List("id");
|
|
request.MaxResults = 48;
|
|
request.PageToken = trendToken;
|
|
|
|
request.Chart = VideosResource.ListRequest.ChartEnum.MostPopular;
|
|
request.RegionCode = reg;
|
|
VideoListResponse response = await request.ExecuteAsync();
|
|
|
|
if (!string.IsNullOrWhiteSpace(response.NextPageToken))
|
|
trendToken = response.NextPageToken;
|
|
else
|
|
trendMore.Complete(true);
|
|
|
|
foreach (Video vid in response.Items)
|
|
{
|
|
VideoCard vCard = new VideoCard(vid.Id);
|
|
recGrid.Add(vCard);
|
|
}
|
|
trendMore.Complete();
|
|
}
|
|
catch
|
|
{
|
|
trendMore.Complete(true);
|
|
}
|
|
}
|
|
|
|
private async void SubsMore_Clicked()
|
|
{
|
|
try
|
|
{
|
|
List<SearchResult> items = new List<SearchResult>();
|
|
Dictionary<string, string> pairs = new Dictionary<string, string>();
|
|
SearchResource.ListRequest request = SecretsVault.Service.Search.List("snippet");
|
|
request.Type = "video";
|
|
request.MaxResults = 10;
|
|
request.Order = SearchResource.ListRequest.OrderEnum.Date;
|
|
|
|
foreach (KeyValuePair<string, string> i in subsTokens)
|
|
{
|
|
request.ChannelId = i.Key;
|
|
request.PageToken = i.Value;
|
|
SearchListResponse response = await request.ExecuteAsync();
|
|
foreach (SearchResult result in response.Items)
|
|
items.Add(result);
|
|
if (response.NextPageToken != null)
|
|
pairs.Add(i.Value, response.NextPageToken);
|
|
}
|
|
if (pairs.Count == 0)
|
|
subsMore.Complete(true);
|
|
else
|
|
subsTokens = pairs;
|
|
|
|
items = items.OrderByDescending(x => x.Snippet.PublishedAt).ToList();
|
|
|
|
foreach (SearchResult i in items)
|
|
subsGrid.Add(new VideoCard(i.Id.VideoId));
|
|
|
|
subsMore.Complete();
|
|
}
|
|
catch
|
|
{
|
|
subsMore.Complete(true);
|
|
}
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
if(SecretsVault.IsAuthorized)
|
|
LoadRecommendations();
|
|
else
|
|
{
|
|
pivot.Items.Remove(recommended);
|
|
pivot.Items.Remove(subscriptions);
|
|
|
|
LoadTrending();
|
|
}
|
|
}
|
|
|
|
private void pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
loading.Close();
|
|
if (pivot.SelectedItem == recommended && !recLoaded)
|
|
LoadRecommendations();
|
|
else if (pivot.SelectedItem == trending && !trendLoaded)
|
|
LoadTrending();
|
|
else if (pivot.SelectedItem == subscriptions && !subsLoaded)
|
|
LoadSubscriptions();
|
|
}
|
|
|
|
async void LoadTrending()
|
|
{
|
|
try
|
|
{
|
|
loading.Refresh();
|
|
VideosResource.ListRequest request = SecretsVault.Service.Videos.List("id");
|
|
request.MaxResults = 48;
|
|
|
|
request.Chart = VideosResource.ListRequest.ChartEnum.MostPopular;
|
|
request.RegionCode = reg;
|
|
VideoListResponse response = await request.ExecuteAsync();
|
|
|
|
if (!string.IsNullOrWhiteSpace(response.NextPageToken))
|
|
trendToken = response.NextPageToken;
|
|
else
|
|
trendMore.Complete(true);
|
|
|
|
foreach (Video vid in response.Items)
|
|
{
|
|
VideoCard vCard = new VideoCard(vid.Id);
|
|
trendGrid.Add(vCard);
|
|
}
|
|
|
|
loading.Close();
|
|
trendLoaded = true;
|
|
}
|
|
catch (System.Net.Http.HttpRequestException)
|
|
{
|
|
trendLoaded = false;
|
|
loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
trendLoaded = false;
|
|
loading.Error(e.GetType().ToString(), e.Message);
|
|
}
|
|
}
|
|
|
|
void LoadRecommendations()
|
|
{
|
|
try
|
|
{
|
|
loading.Refresh();
|
|
|
|
throw new NotImplementedException();
|
|
|
|
loading.Close();
|
|
recLoaded = true;
|
|
}
|
|
catch (System.Net.Http.HttpRequestException)
|
|
{
|
|
recLoaded = false;
|
|
loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
recLoaded = false;
|
|
loading.Error(e.GetType().ToString(), e.Message);
|
|
}
|
|
}
|
|
|
|
async void LoadSubscriptions()
|
|
{
|
|
try
|
|
{
|
|
loading.Refresh();
|
|
List<SearchResult> items = new List<SearchResult>();
|
|
SearchResource.ListRequest request = SecretsVault.Service.Search.List("snippet");
|
|
request.Type = "video";
|
|
request.MaxResults = 10;
|
|
request.Order = SearchResource.ListRequest.OrderEnum.Date;
|
|
|
|
foreach (Subscription i in SecretsVault.Subscriptions)
|
|
{
|
|
request.ChannelId = i.Snippet.ResourceId.ChannelId;
|
|
SearchListResponse response = await request.ExecuteAsync();
|
|
foreach (SearchResult result in response.Items)
|
|
items.Add(result);
|
|
if (response.NextPageToken != null)
|
|
subsTokens.Add(i.Snippet.ResourceId.ChannelId, response.NextPageToken);
|
|
}
|
|
if (subsTokens.Count == 0)
|
|
subsMore.Complete(true);
|
|
|
|
items = items.OrderByDescending(x => x.Snippet.PublishedAt).ToList();
|
|
|
|
foreach (SearchResult i in items)
|
|
subsGrid.Add(new VideoCard(i.Id.VideoId));
|
|
|
|
loading.Close();
|
|
subsLoaded = true;
|
|
}
|
|
catch (System.Net.Http.HttpRequestException)
|
|
{
|
|
subsLoaded = false;
|
|
loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
subsLoaded = false;
|
|
loading.Error(e.GetType().ToString(), e.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|