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/MainPage.xaml.cs
T
2018-07-27 17:07:51 +03:00

648 lines
24 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;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Diagnostics;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using System.Xml;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System.Threading.Tasks;
using System.Threading;
using Google.Apis.Util.Store;
using System.Globalization;
using Windows.ApplicationModel.Core;
using Windows.System;
using Windows.UI.Xaml.Documents;
using Google.Apis.Oauth2.v2;
using Google.Apis.Oauth2.v2.Data;
using FoxTube.Controls;
using FoxTube.Pages;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace FoxTube
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public enum RightPaneState { Full, Collapsed, Hidden }
public sealed partial class MainPage : Page
{
public DownloadAgent Agent = new DownloadAgent();
RightPaneState paneState = RightPaneState.Full;
bool isForcedCollapsed = false;
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
NotificationsCenter notificationsCenter = new NotificationsCenter();
SuggestionsQueries suggestions = new SuggestionsQueries();
public MainPage()
{
this.InitializeComponent();
if(settings.Values["language"] == null)
settings.Values.Add("language", 0);
if (settings.Values["quality"] == null)
settings.Values.Add("quality", 1);
if (settings.Values["newVideoNotification"] == null)
settings.Values.Add("newVideoNotification", true);
if (settings.Values["notifications"] == null)
settings.Values.Add("notifications", true);
if (settings.Values["newmessagesNotification"] == null)
settings.Values.Add("newmessagesNotification", true);
if (settings.Values["moblieWarning"] == null)
settings.Values.Add("moblieWarning", false);
if (settings.Values["videoAutoplay"] == null)
settings.Values.Add("videoAutoplay", false);
if (settings.Values["volume"] == null)
settings.Values.Add("volume", 100);
if (settings.Values["region"] == null)
settings.Values.Add("region", CultureInfo.CurrentCulture.Name);
if (settings.Values["safeSearch"] == null)
settings.Values.Add("safeSearch", 0);
if (settings.Values["defaultDownload"] == null)
settings.Values.Add("defaultDownload", Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\DownloadedVideos");
notificationPane.Child = notificationsCenter;
SecretsVault.AuthorizationStateChanged += Vault_AuthorizationStateChanged;
SecretsVault.SubscriptionsChanged += SecretsVault_SubscriptionsChanged;
SecretsVault.CheckAuthorization();
if(!SecretsVault.IsAuthorized)
content.Navigate(typeof(Home));
}
private void SecretsVault_SubscriptionsChanged(object sender, params object[] args)
{
if ((string)args[0] == "add" && subscriptionsList.Items.Count < 10)
{
Subscription s = args[1] as Subscription;
StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal };
panel.Children.Add(new PersonPicture()
{
Height = 25,
Margin = new Thickness(0, 0, 17, 0),
ProfilePicture = new BitmapImage(new Uri(s.Snippet.Thumbnails.Medium.Url))
});
panel.Children.Add(new TextBlock()
{
VerticalAlignment = VerticalAlignment.Center,
Text = s.Snippet.Title
});
subscriptionsList.Items.Add(new ListBoxItem() { Content = panel });
}
else if ((string)args[0] == "remove" && (int)args[1] < 10)
subscriptionsList.Items.RemoveAt((int)args[1]);
}
private async void Vault_AuthorizationStateChanged(object sender, EventArgs e)
{
if(SecretsVault.IsAuthorized)
{
account.Visibility = Visibility.Collapsed;
try
{
Userinfoplus info = await new Oauth2Service(SecretsVault.Initializer).Userinfo.Get().ExecuteAsync();
ToolTipService.SetToolTip(avatar, new ToolTip() { Content = info.Name });
((avatar.Content as Grid).Children[1] as PersonPicture).ProfilePicture = new BitmapImage(new Uri(info.Picture));
}
catch { }
avatar.Visibility = Visibility.Visible;
toHistory.Visibility = Visibility.Visible;
toLiked.Visibility = Visibility.Visible;
toLater.Visibility = Visibility.Visible;
toSubscriptions.Visibility = Visibility.Visible;
toChannel.Visibility = Visibility.Visible;
if (SecretsVault.Subscriptions.Count > 0)
{
subscriptionsList.Visibility = Visibility.Visible;
int l = SecretsVault.Subscriptions.Count;
int n = 10;
for(int k = 0; k < l && k < n; k++)
try
{
Subscription s = SecretsVault.Subscriptions[k];
StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal };
panel.Children.Add(new PersonPicture()
{
Height = 25,
Margin = new Thickness(0, 0, 17, 0),
ProfilePicture = new BitmapImage(new Uri(s.Snippet.Thumbnails.Medium.Url))
});
panel.Children.Add(new TextBlock()
{
VerticalAlignment = VerticalAlignment.Center,
Text = s.Snippet.Title
});
subscriptionsList.Items.Add(new ListBoxItem() { Content = panel });
}
catch { n++; }
}
}
else
{
account.Visibility = Visibility.Visible;
avatar.Visibility = Visibility.Collapsed;
toHistory.Visibility = Visibility.Collapsed;
toLiked.Visibility = Visibility.Collapsed;
toLater.Visibility = Visibility.Collapsed;
toSubscriptions.Visibility = Visibility.Collapsed;
toChannel.Visibility = Visibility.Collapsed;
subscriptionsList.Visibility = Visibility.Collapsed;
for(int k = 1; k < subscriptionsList.Items.Count; k++)
subscriptionsList.Items.RemoveAt(k);
}
content.CacheSize = 0;
content.Navigate(typeof(Home));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
SetTitleBar();
}
private void SetTitleBar()
{
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.BackgroundColor = titleBar.ButtonBackgroundColor = Colors.Red;
titleBar.ForegroundColor = titleBar.ButtonForegroundColor = Colors.White;
titleBar.ButtonHoverBackgroundColor = Colors.IndianRed;
titleBar.ButtonPressedBackgroundColor = Colors.DarkRed;
CoreApplicationViewTitleBar coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = false;
}
private void menuButton_Click(object sender, RoutedEventArgs e)
{
menu.IsPaneOpen = !menu.IsPaneOpen;
}
public void GotNotification()
{
notificationMenu.Content = "";
}
private void HamburgerSelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
if (sender == mainList)
{
subscriptionsList.SelectedItem = null;
categoriesList.SelectedItem = null;
serviceList.SelectedItem = null;
MainListSelected();
}
else if (sender == subscriptionsList)
{
mainList.SelectedItem = null;
categoriesList.SelectedItem = null;
serviceList.SelectedItem = null;
SubscriptionSelected();
}
else if (sender == categoriesList)
{
subscriptionsList.SelectedItem = null;
mainList.SelectedItem = null;
serviceList.SelectedItem = null;
FeaturedSelected();
}
else if (sender == serviceList)
{
subscriptionsList.SelectedItem = null;
mainList.SelectedItem = null;
categoriesList.SelectedItem = null;
ServiceListSelected();
}
else
{
mainList.SelectedItem = null;
subscriptionsList.SelectedItem = null;
categoriesList.SelectedItem = null;
serviceList.SelectedItem = null;
}
} catch { }
}
void MainListSelected()
{
if (mainList.SelectedItem == null)
return;
object s = mainList.SelectedItem;
if (s == toHistory)
content.Navigate(typeof(Settings));
else if (s == toLiked)
content.Navigate(typeof(PlaylistPage));
else if (s == toLater)
content.Navigate(typeof(Settings));
else if (s == toSubscriptions)
content.Navigate(typeof(Settings));
else if (s == toDownloads)
content.Navigate(typeof(Downloads));
else
content.Navigate(typeof(Home));
}
void ServiceListSelected()
{
if (serviceList.SelectedItem == null)
return;
object s = serviceList.SelectedItem;
if (s == toChannel)
content.Navigate(typeof(Channel), SecretsVault.AccountId);
else if (s == toRemoveAds)
content.Navigate(typeof(Settings), "adblock");
else
content.Navigate(typeof(Settings));
}
void SubscriptionSelected()
{
if (subscriptionsList.SelectedItem == null)
return;
content.Navigate(typeof(Channel), SecretsVault.Subscriptions[subscriptionsList.SelectedIndex - 1].Snippet.ChannelId);
}
void FeaturedSelected()
{
if (serviceList.SelectedItem == null)
return;
switch(serviceList.SelectedIndex)
{
case 0:
content.Navigate(typeof(Channel), "UC-9-kyTW8ZkZNDHQJ6FgpwQ");
break;
case 1:
content.Navigate(typeof(Channel), "UCEgdi0XIXXZ-qJOFPf4JSKw");
break;
case 2:
content.Navigate(typeof(Channel), "UCOpNcN46UbXVtpKMrmU4Abg");
break;
case 3:
content.Navigate(typeof(Channel), "UCYfdidRxbB8Qhf0Nx7ioOYw");
break;
case 4:
content.Navigate(typeof(Channel), "UC4R8DWoMoI7CAwX8_LjQHig");
break;
case 5:
content.Navigate(typeof(Channel), "UC8iNz9uwDGfomRnnKKbOhOQ");
break;
case 6:
content.Navigate(typeof(Channel), "UCzuqhhs6NWbgTzMuM09WKDQ");
break;
}
}
/*private void MenuSelectionChanged()
{
if(topHamburger.SelectedIndex == 0)
{
content.Navigate(typeof(Home));
headerText.Text = "Home";
menu.DisplayMode = SplitViewDisplayMode.CompactInline;
isForcedCollapsed = false;
menu.IsPaneOpen = true;
}
else if (topHamburger.SelectedIndex == 1)
{
//content.Navigate(typeof(Video));
//headerText.Text = "Video";
//menu.DisplayMode = SplitViewDisplayMode.CompactOverlay;
//menu.IsPaneOpen = false;
//isForcedCollapsed = true;
}
else if (bottomHaburger.SelectedIndex == 4)
{
content.Navigate(typeof(Settings));
headerText.Text = "Settings";
menu.DisplayMode = SplitViewDisplayMode.CompactOverlay;
menu.IsPaneOpen = false;
isForcedCollapsed = true;
}
else if (bottomHaburger.SelectedIndex == 1)
{
content.Navigate(typeof(Channel));
headerText.Text = "Channel overview";
menu.DisplayMode = SplitViewDisplayMode.CompactOverlay;
menu.IsPaneOpen = false;
isForcedCollapsed = true;
}
else if (bottomHaburger.SelectedIndex == 3)
{
if(content.SourcePageType == typeof(Settings))
bottomHaburger.SelectedIndex = 3;
else if (content.SourcePageType == typeof(Home))
topHamburger.SelectedIndex = 0;
else if (content.SourcePageType == typeof(Channel))
bottomHaburger.SelectedIndex = 1;
}
}*/
public void notificationMenu_Click(object sender, RoutedEventArgs e)
{
notificationMenu.Content = "";
notificationPane.IsOpen = !notificationPane.IsOpen;
}
private void feedback_Click(object sender, RoutedEventArgs e)
{
content.Navigate(typeof(Settings), "feedback");
}
public void PreDefineFeedback(bool isProblem, string meta)
{
content.Navigate(typeof(Settings), $"feedback&isProblem={isProblem}&meta={meta}");
}
private void menu_PaneClosed(SplitView sender, object args)
{
subsTitle.Visibility = Visibility.Collapsed;
catTitle.Visibility = Visibility.Collapsed;
}
private void menu_PaneOpened(SplitView sender, object args)
{
try
{
subsTitle.Visibility = Visibility.Visible;
catTitle.Visibility = Visibility.Visible;
}
catch { }
}
private async void createAccount_Click(object sender, RoutedEventArgs e)
{
await Launcher.LaunchUriAsync(new Uri("https://accounts.google.com/signup/v2/webcreateaccount?ManageAccount&flowName=GlifWebSignIn&flowEntry=SignUp"));
}
private void signIn_Click(object sender, RoutedEventArgs e)
{
SecretsVault.Authorize();
}
private void myChannel_Click(object sender, RoutedEventArgs e)
{
content.Navigate(typeof(Channel), SecretsVault.UserChannel.Id);
}
private void logout_Click(object sender, RoutedEventArgs e)
{
SecretsVault.Deauthenticate();
}
private void searchField_TextChanged(object sender, TextChangedEventArgs e)
{
if (searchField.Text.Length > 2)
suggestions.BuildList(searchField.Text);
else
suggestions.Hide();
}
private void searchField_GotFocus(object sender, RoutedEventArgs e)
{
suggestions.Hide();
if (Window.Current.Bounds.Width >= 500)
{
searchSuggestions.Child = suggestions;
searchSuggestions.IsOpen = !searchSuggestions.IsOpen;
}
else
popupPlaceholder.Content = suggestions;
}
private void searchButton_Click(object sender, RoutedEventArgs e)
{
if(!string.IsNullOrWhiteSpace(searchField.Text))
{
if (!(content.Content is Search))
content.Navigate(typeof(Search), searchField.Text);
else if ((content.Content as Search).Term != searchField.Text)
(content.Content as Search).Initialize(searchField.Text);
}
}
public void GoToSearch(string keyword)
{
searchField.Text = keyword;
searchButton_Click(this, null);
}
/*private async void StartSearch(string keyword)
{
content.Navigate(typeof(Search));
HamburgerSelectionChanged(null, null);
YouTubeService ytService = SecretsVault.IsAuthorized ? SecretsVault.Service : SecretsVault.NoAuthService;
var searchListRequest = ytService.Search.List("snippet");
searchListRequest.Q = keyword;
searchListRequest.SafeSearch = (SearchResource.ListRequest.SafeSearchEnum)(int)settings.Values["safeSearch"];
searchListRequest.MaxResults = 25;
try
{
searchListRequest.RelevanceLanguage = settings.Values["region"].ToString().Remove(2).ToLower();
}
catch(ArgumentOutOfRangeException)
{
searchListRequest.RelevanceLanguage = settings.Values["region"].ToString().ToLower();
}
var response = await searchListRequest.ExecuteAsync();
Search s = content.Content as Search;
s.SetResults(keyword, (int)response.PageInfo.TotalResults);
Debug.WriteLine("building items tree...");
foreach (SearchResult result in response.Items)
s.AddItem(result);
s.ring.IsActive = false;
s.content.Visibility = Visibility.Visible;
Debug.WriteLine("done");
}*/
private void searchField_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
searchButton_Click(this, null);
content.Focus(FocusState.Pointer);
searchSuggestions.IsOpen = false;
}
}
private void searchSuggestionsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems.Count > 0)
{
ListBoxItem item = e.AddedItems[0] as ListBoxItem;
searchField.Text = item.Content.ToString();
searchButton_Click(this, null);
}
}
public void GoToChannel(string id)
{
MinimizeVideo();
headerText.Text = "Channel overview";
content.Navigate(typeof(Channel), id);
}
public void GoToVideo(string id)
{
headerText.Text = "Video";
menu.DisplayMode = SplitViewDisplayMode.CompactOverlay;
menu.IsPaneOpen = false;
isForcedCollapsed = true;
videoPlaceholder.Content = null;
videoPlaceholder.Width = double.NaN;
videoPlaceholder.Height = double.NaN;
videoPlaceholder.VerticalAlignment = VerticalAlignment.Stretch;
videoPlaceholder.HorizontalAlignment = HorizontalAlignment.Stretch;
videoPlaceholder.Margin = new Thickness(0);
videoPlaceholder.Navigate(typeof(Video), id);
}
public void GoToDeveloper(string id)
{
}
public void GoToPlaylist(string id)
{
MinimizeVideo();
headerText.Text = "Playlist overview";
content.Navigate(typeof(PlaylistPage), id);
}
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (videoPlaceholder.Content != null)
(videoPlaceholder.Content as Video).player.UpdateSize();
if(isForcedCollapsed)
{
if (e.NewSize.Width >= 600 && paneState != RightPaneState.Collapsed)
{
menu.DisplayMode = SplitViewDisplayMode.CompactOverlay;
menu.IsPaneOpen = false;
paneState = RightPaneState.Collapsed;
}
else if (e.NewSize.Width < 600 && paneState != RightPaneState.Hidden)
{
menu.DisplayMode = SplitViewDisplayMode.Overlay;
menu.IsPaneOpen = false;
paneState = RightPaneState.Hidden;
}
}
else
{
if (e.NewSize.Width >= 1000 && paneState != RightPaneState.Full)
{
menu.DisplayMode = SplitViewDisplayMode.CompactInline;
menu.IsPaneOpen = true;
paneState = RightPaneState.Full;
}
else if(e.NewSize.Width >= 600 && e.NewSize.Width < 1000 && paneState != RightPaneState.Collapsed)
{
menu.DisplayMode = SplitViewDisplayMode.CompactOverlay;
menu.IsPaneOpen = false;
paneState = RightPaneState.Collapsed;
}
else if(e.NewSize.Width < 600 && paneState != RightPaneState.Hidden)
{
menu.DisplayMode = SplitViewDisplayMode.Overlay;
menu.IsPaneOpen = false;
paneState = RightPaneState.Hidden;
}
}
}
public void MinimizeVideo()
{
videoPlaceholder.Width = 432;
videoPlaceholder.Height = 243;
videoPlaceholder.VerticalAlignment = VerticalAlignment.Bottom;
videoPlaceholder.HorizontalAlignment = HorizontalAlignment.Right;
videoPlaceholder.Margin = new Thickness(0, 0, 25, 50);
}
public void MaximizeVideo()
{
videoPlaceholder.Width = double.NaN;
videoPlaceholder.Height = double.NaN;
videoPlaceholder.VerticalAlignment = VerticalAlignment.Stretch;
videoPlaceholder.HorizontalAlignment = HorizontalAlignment.Stretch;
videoPlaceholder.Margin = new Thickness(0);
}
public void Fullscreen(bool on)
{
if(on)
{
grid.RowDefinitions[0].Height = new GridLength(0);
menu.CompactPaneLength = 0;
/*menu.DisplayMode = SplitViewDisplayMode.Overlay;
menu.IsPaneOpen = false;*/
isForcedCollapsed = true;
}
else
{
grid.RowDefinitions[0].Height = new GridLength(50);
menu.CompactPaneLength = 50;
//menu.DisplayMode = SplitViewDisplayMode.CompactOverlay;
}
}
public void CloseVideo()
{
if (ApplicationView.GetForCurrentView().IsFullScreenMode)
{
ApplicationView.GetForCurrentView().ExitFullScreenMode();
Fullscreen(false);
}
videoPlaceholder.Content = null;
MaximizeVideo();
}
}
}