namespace VideoBrowser.ViewModels { using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Threading; using VideoBrowser.Common; using VideoBrowser.Controls.CefSharpBrowser.Models; using VideoBrowser.Core; using VideoBrowser.Helpers; using VideoBrowser.Models; using VideoBrowser.Resources; /// /// Defines the . /// public class DownloadQueueViewModel { #region Constructors /// /// Initializes a new instance of the class. /// /// The downloadItemModels. internal DownloadQueueViewModel(ObservableCollection downloadItemModels) { this.DownloadItemModels = downloadItemModels; this.OperationCollectionView = CollectionViewSource.GetDefaultView(this.DownloadItemModels); this.RemoveOperationCommand = new RelayCommand(this.OnRemoveOperation); } #endregion Constructors #region Properties /// /// Gets the DownloadItemModels. /// public ObservableCollection DownloadItemModels { get; } /// /// Gets or sets the Icon. /// public Geometry Icon { get; set; } = Icons.Download; /// /// Gets the OperationCollectionView. /// public ICollectionView OperationCollectionView { get; } /// /// Gets the RemoveOperationCommand. /// public ICommand RemoveOperationCommand { get; } /// /// Gets or sets the ShowMessageAsync. /// public Action ShowMessageAsync { get; set; } #endregion Properties #region Methods /// /// The Download. /// /// The operation. public void Download(Operation operation) { var operationModel = new OperationModel(operation) { PauseDownloadAction = this.OnPauseDownloadCalled, CancelDownloadAction = this.OnCancelDownloadCalled }; var element = (DispatcherObject)this.OperationCollectionView; element.InvokeAsync(() => { if (!this.DownloadItemModels.Contains(operationModel)) { this.DownloadItemModels.Insert(0, operationModel); DownloadQueueHandler.Add(operation); } else { var output = Path.GetFileName(operationModel.Operation.Output); this.ShowMessageAsync("Download Canceled", $"The video/audio {output} is already downloaded"); } }); } /// /// The OnCancelDownloadCalled. /// /// The model. internal void OnCancelDownloadCalled(DownloadItemModel model) { model.Dispose(); this.DownloadItemModels.Remove(model); } /// /// The OnPauseDownloadCalled. /// /// The model. internal void OnPauseDownloadCalled(DownloadItemModel model) { if (!(model is OperationModel operationModel)) { return; } var operation = operationModel.Operation; var status = operation.Status; if (status != OperationStatus.Paused && status != OperationStatus.Queued && status != OperationStatus.Working) { return; } if (status == OperationStatus.Paused) { operation.Resume(); model.PauseText = "Pause"; } else { operation.Pause(); model.PauseText = "Resume"; } } /// /// The OnRemoveVideo. /// /// The obj. private void OnRemoveOperation(object obj) { } #endregion Methods } }