171 lines
5.2 KiB
C#
171 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Windows.UI.Xaml;
|
|
using Windows.UI.Xaml.Controls;
|
|
using Google.Apis.YouTube.v3;
|
|
using Google.Apis.YouTube.v3.Data;
|
|
using FoxTube.Controls;
|
|
using FoxTube.Pages;
|
|
|
|
namespace FoxTube
|
|
{
|
|
/// <summary>
|
|
/// Home page
|
|
/// </summary>
|
|
public sealed partial class Home : Page
|
|
{
|
|
// TODO: Refactor home page
|
|
private bool trendLoaded = false;
|
|
|
|
VideoGrid trendGrid;
|
|
ShowMore trendMore;
|
|
LoadingPage loading;
|
|
|
|
string trendToken;
|
|
Dictionary<string, string> subsTokens = new Dictionary<string, string>();
|
|
|
|
public Home()
|
|
{
|
|
InitializeComponent();
|
|
|
|
trendGrid = ((trending.Content as ScrollViewer).Content as StackPanel).Children[0] as VideoGrid;
|
|
trendMore = ((trending.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 async void TrendMore_Clicked()
|
|
{
|
|
try
|
|
{
|
|
VideosResource.ListRequest request = SecretsVault.Service.Videos.List("id");
|
|
request.MaxResults = 25;
|
|
request.PageToken = trendToken;
|
|
|
|
request.Chart = VideosResource.ListRequest.ChartEnum.MostPopular;
|
|
request.RegionCode = SettingsStorage.Region;
|
|
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);
|
|
}
|
|
trendMore.Complete();
|
|
}
|
|
catch
|
|
{
|
|
trendMore.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)
|
|
LoadRecommendations();
|
|
else if (pivot.SelectedItem == trending && !trendLoaded)
|
|
LoadTrending();
|
|
else if (pivot.SelectedItem == subscriptions)
|
|
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 = SettingsStorage.Region;
|
|
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("This page has not implemented yet.");
|
|
}
|
|
catch (System.Net.Http.HttpRequestException)
|
|
{
|
|
loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
loading.Error(e.GetType().ToString(), e.Message);
|
|
}
|
|
}
|
|
|
|
void LoadSubscriptions()
|
|
{
|
|
try
|
|
{
|
|
loading.Refresh();
|
|
throw new NotImplementedException("This page has not implemented yet.");
|
|
}
|
|
catch (System.Net.Http.HttpRequestException)
|
|
{
|
|
loading.Error("System.Net.Http.HttpRequestException", "Unable to connect to Google servers.", true);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
loading.Error(e.GetType().ToString(), e.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|