Archived
1
0

DownloadAgent fixes and improvements

This commit is contained in:
Michael Gordeev
2018-07-12 10:57:33 +03:00
parent 71cc12106c
commit 9163fcf2ae
11 changed files with 172 additions and 98 deletions
@@ -6,24 +6,26 @@ using System.Threading.Tasks;
using System.Xml;
using System.IO;
using MyToolkit.Multimedia;
using Windows.Storage;
namespace FoxTube.Controls
{
public class DownloadAgent
{
public List<DownloadItem> Items = new List<DownloadItem>();
public event ObjectEventHandler ListChanged;
private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
XmlDocument doc = new XmlDocument();
string path = $@"{Directory.GetCurrentDirectory()}\DownloadHistory.xml";
public DownloadAgent()
{
if (File.Exists(path))
doc.Load(path);
if (settings.Values["downloadHistory"] != null)
doc.LoadXml(settings.Values["downloadHistory"] as string);
else
{
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
doc.AppendChild(doc.CreateElement("downloads"));
doc.Save(path);
settings.Values.Add("downloadHistory", doc.InnerXml);
}
foreach(XmlElement e in doc["downloads"].ChildNodes)
@@ -48,16 +50,19 @@ namespace FoxTube.Controls
item.DownloadComplete += Item_DownloadComplete;
Items.Add(item);
ListChanged.Invoke(item, new ObjectEventArgs("add"));
}
private void Item_DownloadComplete(object sender, EventArgs e)
private void Item_DownloadComplete(object sender, ObjectEventArgs e)
{
doc["downloads"].AppendChild((e as ObjectEventArgs).Parameters[0] as XmlElement);
doc["downloads"].InnerXml += e.Parameters[0];
settings.Values["downloadHistory"] = doc.InnerXml;
}
private void Item_DownloadCanceled(object sender, EventArgs e)
private void Item_DownloadCanceled(object sender, ObjectEventArgs e)
{
Items.Remove(sender as DownloadItem);
ListChanged.Invoke(sender, new ObjectEventArgs("remove"));
}
}
}
+3 -1
View File
@@ -6,7 +6,9 @@ using System.Threading.Tasks;
namespace FoxTube
{
public class ObjectEventArgs : EventArgs
public delegate void ObjectEventHandler(object sender, ObjectEventArgs args);
public class ObjectEventArgs
{
public List<object> Parameters = new List<object>();
public ObjectEventArgs(params object[] args)
+9 -25
View File
@@ -1,30 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Windows.UI.Xaml.Media;
using Windows.UI;
using Windows.UI.Xaml.Documents;
using System.Net;
using System.Threading;
using Windows.Data.Json;
using Windows.Storage.Streams;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.Auth.OAuth2.Responses;
using Windows.Storage;
using Google.Apis.YouTube.v3.Data;
using Windows.Storage;
namespace FoxTube
{
@@ -32,7 +16,7 @@ namespace FoxTube
{
#region Static Information
public static NetworkCredential EmailCredential => new NetworkCredential("youwillneverknowthisadress@gmail.com", "thisisthepassword12345");
private static ClientSecrets Secrets => new ClientSecrets()
public static ClientSecrets Secrets => new ClientSecrets()
{
ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com",
ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_"
@@ -70,6 +54,7 @@ namespace FoxTube
public Google.Apis.YouTube.v3.Data.Channel channel;
public UserCredential Credential;
public event EventHandler AuthorizationStateChanged;
private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
public async void Authorize()
{
@@ -77,6 +62,9 @@ namespace FoxTube
catch { }
if(Credential != null)
{
if (settings.Values["authorized"] == null)
settings.Values.Add("authorized", true);
else settings.Values["authorized"] = true;
IsLoged = true;
AuthorizationStateChanged.Invoke(this, null);
}
@@ -139,17 +127,13 @@ namespace FoxTube
{
Credential = null;
AuthorizationStateChanged.Invoke(this, null);
settings.Values["authorized"] = false;
}
}
public async void CheckAuthorization()
public void CheckAuthorization()
{
var token = await new AuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer()
{
ClientSecrets = Secrets,
DataStore = null
}).LoadTokenAsync("user", CancellationToken.None);
if (token == null)
if (settings.Values["authorized"] == null || !(bool)settings.Values["authorized"])
IsLoged = false;
else
Authorize();
+4 -5
View File
@@ -31,8 +31,8 @@ namespace FoxTube.Controls
string uri;
string Id;
public event EventHandler DownloadCanceled;
public event EventHandler DownloadComplete;
public event ObjectEventHandler DownloadCanceled;
public event ObjectEventHandler DownloadComplete;
WebClient client = new WebClient();
@@ -91,9 +91,8 @@ namespace FoxTube.Controls
{
progressPanel.Visibility = Visibility.Collapsed;
donePanel.Visibility = Visibility.Visible;
XmlElement node = new XmlDocument().CreateElement("item");
node.InnerXml = $@"<item>
string node = $@"<item>
<title{title.Text}></title>
<snippet>
<quality>{quality.Text.Split(' ')[1]}</quality>
+1 -1
View File
@@ -101,7 +101,7 @@
<Compile Include="Controls\CommentCard.xaml.cs">
<DependentUpon>CommentCard.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\DownloadAgent.cs" />
<Compile Include="Classes\DownloadAgent.cs" />
<Compile Include="Controls\DownloadItem.xaml.cs">
<DependentUpon>DownloadItem.xaml</DependentUpon>
</Compile>
+13 -1
View File
@@ -18,6 +18,7 @@ using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast
namespace FoxTube.Pages
{
@@ -27,15 +28,26 @@ namespace FoxTube.Pages
public sealed partial class Downloads : Page
{
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
DownloadAgent agent = Methods.MainPage.Agent;
public Downloads()
{
this.InitializeComponent();
path.Text = settings.Values["defaultDownload"] as string;
foreach (DownloadItem item in Methods.MainPage.Agent.Items)
agent.ListChanged += UpdateList;
foreach (DownloadItem item in agent.Items)
stack.Children.Add(item);
}
private void UpdateList(object sender, ObjectEventArgs e)
{
if (e.Parameters[0] == "remove")
stack.Children.Remove(sender as DownloadItem);
else if (e.Parameters[0] == "add")
stack.Children.Add(sender as DownloadItem);
}
private async void changePath_Click(object sender, RoutedEventArgs e)
{
FolderPicker picker = new FolderPicker()
-4
View File
@@ -47,10 +47,6 @@ namespace FoxTube
/// </summary>
public enum RightPaneState { Full, Collapsed, Hidden }
public interface IPageNesting
{
void GoToSettings(int pageIndex = 0);
}
public sealed partial class MainPage : Page
{