diff --git a/FoxTube/App.xaml.cs b/FoxTube/App.xaml.cs index 0bfc754..8290dd6 100644 --- a/FoxTube/App.xaml.cs +++ b/FoxTube/App.xaml.cs @@ -12,6 +12,7 @@ using Windows.ApplicationModel.Background; using Windows.ApplicationModel.Core; using Windows.Globalization; using Windows.Storage; +using Windows.System; using Windows.System.Power; using Windows.UI.Notifications; using Windows.UI.Popups; @@ -161,11 +162,6 @@ namespace FoxTube switch(arguments[0]) { - case "dcancel": - Debug.WriteLine("Cancel has been required"); - try { Methods.MainPage.Agent.Remove(arguments[1]); } - catch { } - break; case "later": try { @@ -194,6 +190,9 @@ namespace FoxTube Debug.WriteLine(e.Message); } break; + case "download": + await Launcher.LaunchFileAsync(await StorageFile.GetFileFromPathAsync(arguments[1])); + break; } } } @@ -237,6 +236,12 @@ namespace FoxTube case "channel": Methods.MainPage.GoToChannel(args[1]); break; + case "download": + Methods.MainPage.GoToDownloads(); + break; + case "dcancel": + DownloadAgent.Remove(args[1]); + break; } } @@ -273,8 +278,7 @@ namespace FoxTube await ApplicationData.Current.RoamingFolder.CreateFileAsync("watchlater.json", CreationCollisionOption.ReplaceExisting), JsonConvert.SerializeObject(SecretsVault.WatchLater));*/ //Saving downloads - Methods.MainPage.Agent.QuitPrompt(); - ToastNotificationManager.CreateToastNotifier().Show(Background.Notification.GetInternalToast(null, "All data saved", "", null, null)); + DownloadAgent.QuitPrompt(); deferral.Complete(); } } diff --git a/FoxTube/Classes/DownloadAgent.cs b/FoxTube/Classes/DownloadAgent.cs index 6fb413b..cd60ce3 100644 --- a/FoxTube/Classes/DownloadAgent.cs +++ b/FoxTube/Classes/DownloadAgent.cs @@ -1,70 +1,76 @@ using System; using System.Collections.Generic; -using System.IO; using Windows.Storage; using FoxTube.Classes; using Newtonsoft.Json; -using Windows.UI.Popups; using YoutubeExplode.Models.MediaStreams; using Google.Apis.YouTube.v3.Data; -using Windows.Foundation; +using System.Diagnostics; +using FoxTube.Controls; -namespace FoxTube.Controls +namespace FoxTube { - public class DownloadAgent + public static class DownloadAgent { - public List items = new List(); - StorageFolder roaming = ApplicationData.Current.RoamingFolder; - public IAsyncOperation prompt; - - public DownloadAgent() - { - Initialize(); - Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += (s, a) => QuitPrompt(); - } + public static List items = new List(); + private static ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; + public static StorageFolder Downloads { get; set; } - public async void Initialize() + public static async void Initialize() { + Downloads = await KnownFolders.VideosLibrary.CreateFolderAsync("FoxTube", CreationCollisionOption.OpenIfExists); try { - List containers = JsonConvert.DeserializeObject>(await FileIO.ReadTextAsync(await roaming.GetFileAsync("downloads.json"))); - - foreach (DownloadItemContainer i in containers) - try { items.Add(new DownloadItem(i)); } - catch (FileNotFoundException) { } + List containers = JsonConvert.DeserializeObject>((string)settings.Values["downloads"]); + containers.ForEach(i => items.Add(new DownloadItem(i))); } catch { } } - public void Add(MediaStreamInfo info, Video meta, string qualty) + public static void Add(MediaStreamInfo info, Video meta, string qualty) { - items.Add(new DownloadItem(info, meta, qualty)); + items.Insert(0, new DownloadItem(info, meta, qualty)); } - public void Remove(string id) + public static void CancelItem(string id) + { + DownloadItem item = items.Find(x => x.Container.Id == id); + if (item == null || !item.InProgress) + return; + + item.CancelPrompt(); + } + + public static void Remove(string id) { DownloadItem item = items.Find(x => x.Container.Id == id); if (item == null) return; + if (item.InProgress) - item.cancel_Click(this, null); - else + item.Cancel(); + else items.Remove(item); } - public async void QuitPrompt() + public static void QuitPrompt() { - foreach (DownloadItem i in items.FindAll(x => x.InProgress)) + foreach (DownloadItem i in items.FindAll(i => i.InProgress)) i.Cancel(); - items.RemoveAll(x => x.InProgress); List containers = new List(); - foreach (DownloadItem i in items) - containers.Add(i.Container); + items.ForEach(i => containers.Add(i.Container)); - await FileIO.WriteTextAsync( - await roaming.CreateFileAsync("downloads.json", CreationCollisionOption.ReplaceExisting), - JsonConvert.SerializeObject(containers)); + string data = JsonConvert.SerializeObject(containers); + + try + { + settings.Values["downloads"] = data; + } + catch + { + settings.Values.Add("downloads", data); + } } } } diff --git a/FoxTube/Classes/DownloadItemContainer.cs b/FoxTube/Classes/DownloadItemContainer.cs index 05dcdbb..b857c75 100644 --- a/FoxTube/Classes/DownloadItemContainer.cs +++ b/FoxTube/Classes/DownloadItemContainer.cs @@ -8,10 +8,10 @@ namespace FoxTube.Classes public string Title { get; set; } public string Channel { get; set; } public string Id { get; set; } - public Uri Path => File.Path.ToUri(); + public string Name { get; set; } + public string Extension { get; set; } public Uri Thumbnail { get; set; } public string Quality { get; set; } public TimeSpan Duration { get; set; } - public StorageFile File { get; set; } } } diff --git a/FoxTube/Controls/DownloadItem.xaml b/FoxTube/Controls/DownloadItem.xaml index 34b5a96..23eff25 100644 --- a/FoxTube/Controls/DownloadItem.xaml +++ b/FoxTube/Controls/DownloadItem.xaml @@ -20,7 +20,7 @@ - + @@ -47,7 +47,7 @@ - + + + + + + + + + + + diff --git a/FoxTube/Package.appxmanifest b/FoxTube/Package.appxmanifest index a403af9..3a8f43c 100644 --- a/FoxTube/Package.appxmanifest +++ b/FoxTube/Package.appxmanifest @@ -1,5 +1,5 @@  - + @@ -41,5 +41,7 @@ + + \ No newline at end of file diff --git a/FoxTube/Pages/Downloads.xaml b/FoxTube/Pages/Downloads.xaml index aa5ccd0..0a97c37 100644 --- a/FoxTube/Pages/Downloads.xaml +++ b/FoxTube/Pages/Downloads.xaml @@ -9,9 +9,29 @@ mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> - - - - - + + + + + + + + + + + + +