Core refactoring (app doesn't work)
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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<double> 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(UserManagement.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user