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
Michael Gordeev 8432271b32 -Fixed Unspecified error on App.e.g.cs
-Refactoring
2018-12-21 17:34:26 +03:00

678 lines
27 KiB
C#

using System;
using System.Collections.Generic;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;
using System.Diagnostics;
using Windows.UI.Notifications;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using System.Xml;
using Google.Apis.YouTube.v3.Data;
using System.Globalization;
using Windows.ApplicationModel.Core;
using Windows.System;
using Google.Apis.Oauth2.v2;
using Google.Apis.Oauth2.v2.Data;
using FoxTube.Controls;
using FoxTube.Pages;
using Windows.ApplicationModel;
using System.Net;
using Windows.UI.Popups;
using Windows.Networking.Connectivity;
using Windows.UI.Core;
using System.IO;
using Newtonsoft.Json;
using Google.Apis.YouTube.v3;
// 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 Sender { Menu, Frame, None }
public sealed partial class MainPage : Page
{
Sender s = Sender.None;
public MainPage()
{
this.InitializeComponent();
PackageVersion ver = Package.Current.Id.Version;
if (settings.Values["ver"] == null)
settings.Values.Add("ver", $"{ver.Major}.{ver.Minor}");
if((string)settings.Values["ver"] != $"{ver.Major}.{ver.Minor}")
{
try
{
XmlDocument changelog = new XmlDocument();
changelog.Load("http://foxgame-studio.000webhostapp.com/foxtube-changelog.xml");
XmlElement e = changelog["items"].ChildNodes[0] as XmlElement;
ToastNotificationManager.CreateToastNotifier().Show(FoxTube.Background.Notification.GetChangelogToast(e.GetAttribute("version")));
settings.Values["ver"] = $"{ver.Major}.{ver.Minor}";
}
catch { }
}
SecretsVault.AuthorizationStateChanged += AuthorizationStateChanged;
SecretsVault.SubscriptionsChanged += SecretsVault_SubscriptionsChanged;
SecretsVault.NotPurchased += () => removeAds.Visibility = Visibility.Visible;
SecretsVault.CheckAuthorization();
SecretsVault.CheckAddons();
DownloadAgent.Initialize();
SetTitleBar();
Initialize();
}
public async void Initialize()
{
bool[] notificationsSettings = new bool[] { (bool)settings.Values["newVideoNotification"], (bool)settings.Values["devNews"] };
await FileIO.WriteTextAsync(
await ApplicationData.Current.RoamingFolder.CreateFileAsync("notifications.json", CreationCollisionOption.ReplaceExisting),
JsonConvert.SerializeObject(notificationsSettings));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
}
public Video GetCurrentItem()
{
try { return (videoPlaceholder.Content as VideoPage).item; }
catch { return null; }
}
public void SetTitleBar()
{
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.BackgroundColor = Colors.Red;
titleBar.ButtonBackgroundColor = Colors.Red;
titleBar.ButtonHoverBackgroundColor = Colors.IndianRed;
titleBar.ButtonPressedBackgroundColor = Colors.DarkRed;
titleBar.ButtonInactiveBackgroundColor = Colors.Black;
titleBar.ForegroundColor = Colors.White;
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = false;
}
private void SecretsVault_SubscriptionsChanged(object sender, params object[] args)
{
if ((string)args[0] == "add" && nav.MenuItems.Count < 19)
{
Subscription s = args[1] as Subscription;
StackPanel panel = new StackPanel()
{
Orientation = Orientation.Horizontal,
Padding = new Thickness(5)
};
panel.Children.Add(new PersonPicture()
{
Height = 20,
Margin = new Thickness(-5, 0, 15, 0),
ProfilePicture = new BitmapImage(new Uri(s.Snippet.Thumbnails.Medium.Url))
});
panel.Children.Add(new TextBlock() { Text = s.Snippet.Title });
nav.MenuItems.Add(new NavigationViewItem()
{
Content = panel,
Name = (nav.MenuItems.Count - 9).ToString(),
Padding = new Thickness(-5)
});
}
else if ((string)args[0] == "remove" && (int)args[1] < 10)
nav.MenuItems.RemoveAt((int)args[1] + 9);
}
private async void AuthorizationStateChanged()
{
if(SecretsVault.IsAuthorized)
{
account.Visibility = Visibility.Collapsed;
try
{
Userinfoplus info = await new Oauth2Service(SecretsVault.Initializer).Userinfo.Get().ExecuteAsync();
myName.Text = info.Name;
((avatar.Content as StackPanel).Children[0] as PersonPicture).ProfilePicture = new BitmapImage(new Uri(info.Picture));
}
catch { }
avatar.Visibility = Visibility.Visible;
toChannel.Visibility = Visibility.Visible;
toSubscriptions.Visibility = Visibility.Visible;
libHeader.Visibility = Visibility.Visible;
toHistory.Visibility = Visibility.Visible;
toLiked.Visibility = Visibility.Visible;
toLater.Visibility = Visibility.Visible;
subsHeader.Visibility = Visibility.Visible;
if (SecretsVault.Subscriptions.Count > 0)
{
subsHeader.Visibility = Visibility.Visible;
for(int k = 0; k < SecretsVault.Subscriptions.Count && k < 10; k++)
try
{
Subscription s = SecretsVault.Subscriptions[k];
StackPanel panel = new StackPanel()
{
Orientation = Orientation.Horizontal,
Padding = new Thickness(5)
};
panel.Children.Add(new PersonPicture()
{
Height = 20,
Margin = new Thickness(-5, 0, 15, 0),
ProfilePicture = new BitmapImage(new Uri(s.Snippet.Thumbnails.Medium.Url))
});
panel.Children.Add(new TextBlock() { Text = s.Snippet.Title });
nav.MenuItems.Add(new NavigationViewItem()
{
Content = panel,
Name = k.ToString(),
Padding = new Thickness(-5)
});
}
catch { continue; }
}
}
else
{
account.Visibility = Visibility.Visible;
avatar.Visibility = Visibility.Collapsed;
toChannel.Visibility = Visibility.Collapsed;
toSubscriptions.Visibility = Visibility.Collapsed;
libHeader.Visibility = Visibility.Collapsed;
toHistory.Visibility = Visibility.Collapsed;
toLiked.Visibility = Visibility.Collapsed;
toLater.Visibility = Visibility.Collapsed;
subsHeader.Visibility = Visibility.Collapsed;
subsHeader.Visibility = Visibility.Collapsed;
for(int k = 9; k < nav.MenuItems.Count; k++)
nav.MenuItems.RemoveAt(k);
}
content.Navigate(typeof(Home));
if (videoPlaceholder.Content != null)
GoToVideo((videoPlaceholder.Content as VideoPage).videoId);
}
private async void feedback_Click(object sender, TappedRoutedEventArgs e)
{
await Launcher.LaunchUriAsync(new Uri("feedback-hub:"));
}
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)
{
GoToChannel(SecretsVault.AccountId);
}
private void logout_Click(object sender, RoutedEventArgs e)
{
SecretsVault.Deauthenticate();
}
public void GoToSearch(SearchParameters args)
{
nav.IsPaneOpen = false;
MinimizeAsInitializer();
content.Navigate(typeof(Search), args);
}
public void GoToChannel(string id)
{
Debug.WriteLine($"Channel id: {id}");
MinimizeAsInitializer();
content.Navigate(typeof(ChannelPage), id);
}
public async void GoToVideo(string id, string playlistId = null)
{
bool cancel = false;
try
{
var connection = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();
if ((bool)settings.Values["moblieWarning"] && (connection.NetworkCostType == NetworkCostType.Fixed || connection.NetworkCostType == NetworkCostType.Variable))
{
MessageDialog dialog = new MessageDialog("You are on metered connection now. Additional charges may apply. Do you want to continue?")
{
DefaultCommandIndex = 2,
CancelCommandIndex = 1
};
dialog.Commands.Add(new UICommand("Yes"));
dialog.Commands.Add(new UICommand("No", (command) => cancel = true));
dialog.Commands.Add(new UICommand("Add to 'Watch later' playlist", (command) =>
{
try
{
if (!SecretsVault.IsAuthorized)
SecretsVault.CheckAuthorization(false);
if (!SecretsVault.IsAuthorized)
throw new Exception("Not authenticated");
PlaylistItem item = new PlaylistItem()
{
Snippet = new PlaylistItemSnippet()
{
ResourceId = new ResourceId()
{
Kind = "youtube#video",
VideoId = id
},
PlaylistId = "WL"
}
};
SecretsVault.Service.PlaylistItems.Insert(item, "snippet").Execute();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
cancel = true;
}));
await dialog.ShowAsync();
}
}
catch { }
if (cancel)
return;
videoPlaceholder.Content = null;
MaximizeVideo();
Fullscreen(false);
videoPlaceholder.Navigate(typeof(VideoPage), new string[2] { id, playlistId });
}
public void GoToDeveloper(string id)
{
MinimizeAsInitializer();
content.Navigate(typeof(Settings), $"inbox&{id}");
}
public void GoToPlaylist(string id)
{
Debug.WriteLine($"Playlist id: {id}");
MinimizeAsInitializer();
content.Navigate(typeof(PlaylistPage), id);
}
public void GoToDownloads()
{
content.Navigate(typeof(Downloads));
}
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (videoPlaceholder.Content != null)
(videoPlaceholder.Content as VideoPage).player.UpdateSize();
}
public void MinimizeAsInitializer()
{
if(videoPlaceholder.Content != null)
{
if ((videoPlaceholder.Content as VideoPage).loading.State != LoadingState.Loaded)
CloseVideo();
else
{
try { (videoPlaceholder.Content as VideoPage).player.minimize_Click(this, null); }
catch { }
}
}
}
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);
if (content.CanGoBack)
nav.IsBackEnabled = true;
else
nav.IsBackEnabled = false;
}
public void MaximizeVideo()
{
videoPlaceholder.Width = double.NaN;
videoPlaceholder.Height = double.NaN;
videoPlaceholder.VerticalAlignment = VerticalAlignment.Stretch;
videoPlaceholder.HorizontalAlignment = HorizontalAlignment.Stretch;
videoPlaceholder.Margin = new Thickness(0);
nav.IsBackEnabled = true;
}
public void Fullscreen(bool on)
{
if (on)
{
nav.CompactModeThresholdWidth = short.MaxValue;
nav.ExpandedModeThresholdWidth = short.MaxValue;
nav.OpenPaneLength = 0;
nav.CompactPaneLength = 0;
nav.Margin = new Thickness(0, -45, 0, 0);
}
else
{
nav.CompactModeThresholdWidth = 641;
nav.ExpandedModeThresholdWidth = 1008;
nav.Margin = new Thickness(0);
if (videoPlaceholder.Content != null && nav.IsPaneOpen)
nav.IsPaneOpen = false;
else
{
nav.OpenPaneLength = 300;
nav.CompactPaneLength = 40;
}
SetTitleBar();
}
}
public void CloseVideo()
{
if (ApplicationView.GetForCurrentView().IsFullScreenMode)
{
ApplicationView.GetForCurrentView().ExitFullScreenMode();
Fullscreen(false);
}
(videoPlaceholder.Content as VideoPage).player.pointerCaptured = false;
videoPlaceholder.Content = null;
Window.Current.CoreWindow.PointerCursor = new CoreCursor(CoreCursorType.Arrow, 0);
MaximizeVideo();
if (content.CanGoBack)
nav.IsBackEnabled = true;
else
nav.IsBackEnabled = false;
}
private void search_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if(!string.IsNullOrWhiteSpace(search.Text))
GoToSearch(new SearchParameters(search.Text));
}
private void search_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (search.Text.Length > 2 && args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load($"http://suggestqueries.google.com/complete/search?ds=yt&client=toolbar&q={search.Text}");
List<string> suggestions = new List<string>();
for (int i = 0; i < doc["toplevel"].ChildNodes.Count && i < 5; i++)
suggestions.Add(doc["toplevel"].ChildNodes[i]["suggestion"].GetAttribute("data"));
search.ItemsSource = suggestions;
}
catch (WebException)
{
search.ItemsSource = new List<string>();
}
}
}
private void nav_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
try
{
if (s == Sender.None)
{
s = Sender.Menu;
if (args.IsSettingsSelected)
content.Navigate(typeof(Settings));
else
{
if (args.SelectedItem == toHome)
content.Navigate(typeof(Home));
else if (args.SelectedItem == toHistory)
content.Navigate(typeof(History));
else if (args.SelectedItem == toLiked)
content.Navigate(typeof(PlaylistPage), SecretsVault.UserChannel.ContentDetails.RelatedPlaylists.Likes);
else if (args.SelectedItem == toLater)
content.Navigate(typeof(PlaylistPage), SecretsVault.UserChannel.ContentDetails.RelatedPlaylists.WatchLater);
else if (args.SelectedItem == toSubscriptions)
content.Navigate(typeof(Subscriptions));
else if (args.SelectedItem == toDownloads)
content.Navigate(typeof(Downloads));
else if (args.SelectedItem == toChannel)
content.Navigate(typeof(ChannelPage), SecretsVault.UserChannel.Id);
else
content.Navigate(typeof(ChannelPage), SecretsVault.Subscriptions[Convert.ToInt32((args.SelectedItem as NavigationViewItem).Name)].Snippet.ResourceId.ChannelId);
}
}
else
s = Sender.None;
}
catch { }
}
public void content_Navigated(object sender, NavigationEventArgs e)
{
Dictionary<Type, Action> switchCase = new Dictionary<Type, Action>()
{
{ typeof(Settings), () => nav.Header = "Settings" },
{ typeof(ChannelPage), () => nav.Header = "Channel" },
{ typeof(PlaylistPage), () => nav.Header = "Playlist" },
{ typeof(Search), () => nav.Header = "Search" },
{ typeof(Subscriptions), () => nav.Header = "Subscriptions" },
{ typeof(History), () => nav.Header = "History" },
{ typeof(Home), () => nav.Header = "Home" },
{ typeof(Downloads), () => nav.Header = "Downloads" }
};
try { switchCase[e.SourcePageType](); }
catch { }
if (s == Sender.None)
{
s = Sender.Frame;
Dictionary<Type, Action> navCase = new Dictionary<Type, Action>()
{
{ typeof(Settings), () =>
{
if(nav.SelectedItem != nav.SettingsItem)
nav.SelectedItem = nav.SettingsItem;
else
s = Sender.None;
} },
{ typeof(ChannelPage), () =>
{
if(sender.GetType() != typeof(ChannelPage))
{
Methods.NeedToResponse = true;
s = Sender.None;
return;
}
Methods.NeedToResponse = false;
if(SecretsVault.IsAuthorized)
{
if((content.Content as ChannelPage).channelId == SecretsVault.UserChannel.Id)
{
if(nav.SelectedItem != toChannel)
nav.SelectedItem = toChannel;
else
s = Sender.None;
}
else
{
bool found = false;
for(int k = 0; k < SecretsVault.Subscriptions.Count && k < 10; k++)
if(SecretsVault.Subscriptions[k].Snippet.ResourceId.ChannelId == (content.Content as ChannelPage).channelId)
{
if(nav.SelectedItem != nav.MenuItems[k + 9])
nav.SelectedItem = nav.MenuItems[k + 9];
else
s = Sender.None;
found = true;
break;
}
if(!found)
{
nav.SelectedItem = null;
}
}
}
else
nav.SelectedItem = null;
} },
{ typeof(PlaylistPage), () =>
{
if(sender.GetType() != typeof(PlaylistPage))
{
Methods.NeedToResponse = true;
s = Sender.None;
return;
}
Methods.NeedToResponse = false;
if((content.Content as PlaylistPage).playlistId == SecretsVault.UserChannel.ContentDetails.RelatedPlaylists.Likes)
{
if(nav.SelectedItem != toLiked)
nav.SelectedItem = toLiked;
else
s = Sender.None;
}
else if ((content.Content as PlaylistPage).playlistId == SecretsVault.UserChannel.ContentDetails.RelatedPlaylists.WatchLater)
{
if(nav.SelectedItem != toLater)
nav.SelectedItem = toLater;
else
s = Sender.None;
}
else
{
nav.SelectedItem = null;
}
} },
{ typeof(Search), () => nav.SelectedItem = null },
{typeof(Subscriptions), () =>
{
if(nav.SelectedItem != toSubscriptions)
nav.SelectedItem = toSubscriptions;
else
s = Sender.None;
} },
{ typeof(History), () =>
{
if(nav.SelectedItem != toHistory)
nav.SelectedItem = toHistory;
else
s = Sender.None;
} },
{ typeof(Home), () =>
{
if(nav.SelectedItem != toHome)
nav.SelectedItem = toHome;
else
s = Sender.None;
} },
{ typeof(Downloads), () =>
{
if(nav.SelectedItem != toDownloads)
nav.SelectedItem = toDownloads;
else
s = Sender.None;
} }
};
try { navCase[content.SourcePageType](); }
catch
{
nav.SelectedItem = null;
}
}
else
s = Sender.None;
if (content.CanGoBack)
nav.IsBackEnabled = true;
else
nav.IsBackEnabled = false;
if (content.SourcePageType == typeof(Home) || content.SourcePageType == typeof(Settings) || content.SourcePageType == typeof(Subscriptions))
{
nav.ExpandedModeThresholdWidth = 1008;
if (nav.DisplayMode == NavigationViewDisplayMode.Expanded)
nav.IsPaneOpen = true;
}
else
nav.ExpandedModeThresholdWidth = int.MaxValue;
if (videoPlaceholder.Content != null && videoPlaceholder.HorizontalAlignment == HorizontalAlignment.Stretch)
MinimizeAsInitializer();
}
private void toChannel_Click(object sender, RoutedEventArgs e)
{
GoToChannel(SecretsVault.UserChannel.Id);
}
private void nav_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
{
if (videoPlaceholder.Content != null)
{
if ((videoPlaceholder.Content as VideoPage).loading.State != LoadingState.Loaded)
CloseVideo();
else if (videoPlaceholder.HorizontalAlignment == HorizontalAlignment.Stretch)
MinimizeAsInitializer();
else
content.GoBack();
}
else
content.GoBack();
}
private void Page_PreviewKeyUp(object sender, KeyRoutedEventArgs e)
{
if(videoPlaceholder.Content != null && FocusManager.GetFocusedElement().GetType() != typeof(TextBox))
{
e.Handled = true;
(videoPlaceholder.Content as VideoPage).player.KeyUpPressed(sender, e);
}
}
private void openContext(object sender, TappedRoutedEventArgs e)
{
((NavigationViewItem)sender).ContextFlyout.ShowAt((NavigationViewItem)sender);
}
}
}