protected DownloadViewModel EnqueueDownload(Video video, string filePath) { // Prepare the viewmodel var download = _viewModelFactory.CreateDownloadViewModel(); download.Video = video; download.FilePath = filePath; download.CancellationTokenSource = new CancellationTokenSource(); // Set up progress router var progressRouter = new Progress <double>(p => download.Progress = p); // Get cancellation token var cancellationToken = download.CancellationTokenSource.Token; // Start download _downloadService.DownloadVideoAsync(download.Video.Id, download.FilePath, progressRouter, cancellationToken) .ContinueWith(t => { download.IsFinished = t.IsCompleted && !t.IsCanceled; download.IsCanceled = t.IsCanceled; download.CancellationTokenSource.Dispose(); }); return(download); }
// This is async void on purpose because this is supposed to be always ran in background private async void EnqueueDownload(DownloadViewModel download) { // Cancel an existing download for this file path to prevent writing to the same file Downloads.FirstOrDefault(d => d.FilePath == download.FilePath)?.Cancel(); // Add to list Downloads.Add(download); // Create progress operation download.ProgressOperation = ProgressManager.CreateOperation(); // If download option is not set - get the best download option if (download.DownloadOption == null) { download.DownloadOption = await _downloadService.GetBestDownloadOptionAsync(download.Video.Id, download.Format); } // Download try { await _downloadService.DownloadVideoAsync(download.Video.Id, download.FilePath, download.DownloadOption, download.ProgressOperation, download.CancellationToken); } catch (OperationCanceledException) { // Ignore cancellations } // Mark download as completed download.MarkAsCompleted(); }
public void Start() { if (!CanStart) { return; } IsActive = true; IsSuccessful = false; IsCanceled = false; IsFailed = false; Task.Run(async() => { // Create cancellation token source _cancellationTokenSource = new CancellationTokenSource(); // Create progress operation ProgressOperation = ProgressManager.CreateOperation(); try { // If download option is not set - get the best download option if (DownloadOption == null) { DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format); } await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token); if (_settingsService.ShouldInjectTags) { await _taggingService.InjectTagsAsync(Video, Format, FilePath, _cancellationTokenSource.Token); } if (SubtitleOption != null && SubtitleOption.ClosedCaptionTrackInfos.Any()) { await _downloadService.DownloadSubtitleAsync(SubtitleOption, FilePath); } IsSuccessful = true; } catch (OperationCanceledException) { IsCanceled = true; } catch (Exception ex) { IsFailed = true; FailReason = ex.Message; } finally { IsActive = false; _cancellationTokenSource.Dispose(); ProgressOperation.Dispose(); } }); }
protected DownloadViewModel EnqueueDownload(Video video, string filePath, string format) { // Persist preferences _settingsService.LastFormat = format; // Prepare the viewmodel var download = _viewModelFactory.CreateDownloadViewModel(); download.Video = video; download.FilePath = filePath; download.Format = format; download.CancellationTokenSource = new CancellationTokenSource(); download.StartTime = DateTimeOffset.Now; // Set up progress router var progressRouter = new Progress <double>(p => download.Progress = p); // Get cancellation token var cancellationToken = download.CancellationTokenSource.Token; // Start download _downloadService.DownloadVideoAsync(download.Video.Id, download.FilePath, format, progressRouter, cancellationToken) .ContinueWith(t => { download.IsFinished = t.IsCompleted && !t.IsCanceled; download.IsCanceled = t.IsCanceled; download.EndTime = DateTimeOffset.Now; download.CancellationTokenSource.Dispose(); }); return(download); }
public void Start() { if (!CanStart) { return; } IsActive = true; IsSuccessful = false; IsCanceled = false; IsFailed = false; Task.Run(async() => { // Create cancellation token source _cancellationTokenSource = new CancellationTokenSource(); // Create progress operation ProgressOperation = ProgressManager.CreateOperation(); try { // daca nu sunt setate obtiuni de descarcare se foloseste setarea de baza // descarca video if (DownloadOption == null) { DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format); } await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token); if (_settingsService.ShouldInjectTags) { await _taggingService.InjectTagsAsync(Video, Format, FilePath, _cancellationTokenSource.Token); } IsSuccessful = true; } catch (OperationCanceledException) { IsCanceled = true; } catch (Exception ex) { IsFailed = true; FailReason = ex.Message; } finally { IsActive = false; _cancellationTokenSource.Dispose(); ProgressOperation.Dispose(); } }); }
public void Start() { if (!CanStart) { return; } Task.Run(async() => { // Create cancellation token source _cancellationTokenSource = new CancellationTokenSource(); // Create progress operation ProgressOperation = ProgressManager.CreateOperation(); try { IsSuccessful = false; IsCanceled = false; IsFailed = false; IsActive = true; // If download option is not set - get the best download option if (DownloadOption == null) { DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format); } // Download await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token); IsSuccessful = true; } catch (OperationCanceledException) { IsCanceled = true; } catch (Exception ex) { IsFailed = true; FailReason = ex.Message; } finally { IsActive = false; _cancellationTokenSource.Dispose(); ProgressOperation.Dispose(); } }); }