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/Classes/DownloadAgent.cs
T
Michael Gordeev f637c18e22 Merged PR 17: Version 0.2.181021
- Closed captions fixes
- Merged to YoutubeExplode instead of MyToolkit for video sources
- Video playback fixes
- Internal links support
- Subscription button fixed
- Comments threads fixes and improvements (highlighting author's and user's names)
- General bug fixes
2018-10-20 22:03:04 +00:00

73 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using Windows.Storage;
using FoxTube.Classes;
using Newtonsoft.Json;
using Windows.UI.Popups;
namespace FoxTube.Controls
{
public class DownloadAgent
{
public List<DownloadItem> items = new List<DownloadItem>();
StorageFolder roaming = ApplicationData.Current.RoamingFolder;
public DownloadAgent()
{
Initialize();
Windows.UI.Core.Preview.SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += (s, a) => QuitPrompt();
}
public async void Initialize()
{
try
{
List<DownloadItemContainer> containers = JsonConvert.DeserializeObject<List<DownloadItemContainer>>(await FileIO.ReadTextAsync(await roaming.GetFileAsync("data.json")));
foreach (DownloadItemContainer i in containers)
try { items.Add(new DownloadItem(i)); }
catch (FileNotFoundException) { }
}
catch { }
}
public void Add(string url)
{
items.Add(new DownloadItem(url));
}
private void Item_DownloadCanceled(object sender, params object[] e)
{
items.Remove(sender as DownloadItem);
}
public async void QuitPrompt()
{
if(items.Find(x => x.InProgress) != null)
{
MessageDialog dialog = new MessageDialog("You have some unfinished downloads. Quitting now will erase all downloaded data. Are you sure to continue?", "Some downloads are still pending");
dialog.Commands.Add(new UICommand("Yes", async (command) =>
{
foreach (DownloadItem i in items.FindAll(x => x.InProgress))
i.Cancel();
items.RemoveAll(x => x.InProgress);
List<DownloadItemContainer> containers = new List<DownloadItemContainer>();
foreach (DownloadItem i in items)
containers.Add(i.Container);
await FileIO.WriteTextAsync(
await roaming.CreateFileAsync("data.json", CreationCollisionOption.ReplaceExisting),
JsonConvert.SerializeObject(containers));
}));
dialog.Commands.Add(new UICommand("No"));
dialog.DefaultCommandIndex = 1;
await dialog.ShowAsync();
}
}
}
}