using FoxTube.Services; using System; using System.Threading; using System.Threading.Tasks; using Windows.Storage; using YoutubeExplode; using YoutubeExplode.Videos.Streams; namespace FoxTube.Models { public enum DownloadState { Initializing, Downloading, Cancelling } public class DownloadItem : SavedVideo { public DownloadState State { get; private set; } = DownloadState.Initializing; public IProgress DownloadPercentage { get; set; } private CancellationTokenSource CTS { get; set; } = new CancellationTokenSource(); public async Task CommenceDownload(IStreamInfo stream, IStorageFile destination) { Path = destination.Path; YoutubeClient client = new YoutubeClient(UserService.Service.HttpClient); State = DownloadState.Downloading; Task task = client.Videos.Streams.DownloadAsync(stream, Path, DownloadPercentage, CTS.Token); await task.ConfigureAwait(false); if (!task.IsCanceled) return; await destination.DeleteAsync(StorageDeleteOption.PermanentDelete); throw new OperationCanceledException(); } public void Cancel() { State = DownloadState.Cancelling; CTS.Cancel(); } } }