diff --git a/TwitchDownloaderWPF/PageQueue.xaml.cs b/TwitchDownloaderWPF/PageQueue.xaml.cs index 70cbe723..47ce873f 100644 --- a/TwitchDownloaderWPF/PageQueue.xaml.cs +++ b/TwitchDownloaderWPF/PageQueue.xaml.cs @@ -17,7 +17,7 @@ namespace TwitchDownloaderWPF public partial class PageQueue : Page { public static readonly object taskLock = new object(); - public static ObservableCollection taskList { get; } = new ObservableCollection(); + public static ObservableCollection taskList { get; } = new(); private static readonly BackgroundWorker taskManager = new BackgroundWorker(); public PageQueue() @@ -198,7 +198,7 @@ private void btnClips_Click(object sender, RoutedEventArgs e) private void BtnCancelTask_Click(object sender, RoutedEventArgs e) { - if (sender is not Button { DataContext: ITwitchTask task }) + if (sender is not Button { DataContext: TwitchTask task }) { return; } @@ -208,7 +208,7 @@ private void BtnCancelTask_Click(object sender, RoutedEventArgs e) private void MenuItemCancelTask_Click(object sender, RoutedEventArgs e) { - if (sender is not MenuItem { DataContext: ITwitchTask task }) + if (sender is not MenuItem { DataContext: TwitchTask task }) { return; } @@ -216,7 +216,7 @@ private void MenuItemCancelTask_Click(object sender, RoutedEventArgs e) CancelTask(task); } - private static void CancelTask(ITwitchTask task) + private static void CancelTask(TwitchTask task) { if (task.CanCancel) { @@ -226,7 +226,7 @@ private static void CancelTask(ITwitchTask task) private void BtnTaskError_Click(object sender, RoutedEventArgs e) { - if (sender is not Button { DataContext: ITwitchTask task }) + if (sender is not Button { DataContext: TwitchTask task }) { return; } @@ -236,7 +236,7 @@ private void BtnTaskError_Click(object sender, RoutedEventArgs e) private void MenuItemTaskError_Click(object sender, RoutedEventArgs e) { - if (sender is not MenuItem { DataContext: ITwitchTask task }) + if (sender is not MenuItem { DataContext: TwitchTask task }) { return; } @@ -244,7 +244,7 @@ private void MenuItemTaskError_Click(object sender, RoutedEventArgs e) ShowTaskException(task); } - private static void ShowTaskException(ITwitchTask task) + private static void ShowTaskException(TwitchTask task) { var taskException = task.Exception; @@ -264,7 +264,7 @@ private static void ShowTaskException(ITwitchTask task) private void BtnRemoveTask_Click(object sender, RoutedEventArgs e) { - if (sender is not Button { DataContext: ITwitchTask task }) + if (sender is not Button { DataContext: TwitchTask task }) { return; } @@ -274,7 +274,7 @@ private void BtnRemoveTask_Click(object sender, RoutedEventArgs e) private void MenuItemRemoveTask_Click(object sender, RoutedEventArgs e) { - if (sender is not MenuItem { DataContext: ITwitchTask task }) + if (sender is not MenuItem { DataContext: TwitchTask task }) { return; } @@ -282,7 +282,7 @@ private void MenuItemRemoveTask_Click(object sender, RoutedEventArgs e) RemoveTask(task); } - private static void RemoveTask(ITwitchTask task) + private static void RemoveTask(TwitchTask task) { if (task.CanRun() || task.Status is TwitchTaskStatus.Running or TwitchTaskStatus.Waiting) { @@ -298,7 +298,7 @@ private static void RemoveTask(ITwitchTask task) private void MenuItemOpenTaskFolder_Click(object sender, RoutedEventArgs e) { - if (sender is not MenuItem { DataContext: ITwitchTask task }) + if (sender is not MenuItem { DataContext: TwitchTask task }) { return; } @@ -308,7 +308,7 @@ private void MenuItemOpenTaskFolder_Click(object sender, RoutedEventArgs e) private void BtnRetryTask_Click(object sender, RoutedEventArgs e) { - if (sender is not Button { DataContext: ITwitchTask task }) + if (sender is not Button { DataContext: TwitchTask task }) { return; } @@ -318,7 +318,7 @@ private void BtnRetryTask_Click(object sender, RoutedEventArgs e) private void MenuItemTaskRetry_Click(object sender, RoutedEventArgs e) { - if (sender is not MenuItem { DataContext: ITwitchTask task }) + if (sender is not MenuItem { DataContext: TwitchTask task }) { return; } @@ -326,7 +326,7 @@ private void MenuItemTaskRetry_Click(object sender, RoutedEventArgs e) RetryTask(task); } - private static void RetryTask(ITwitchTask task) + private static void RetryTask(TwitchTask task) { if (task.CanReinitialize) { diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs index ecec89c3..f1872c63 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using TwitchDownloaderCore; @@ -10,87 +7,13 @@ namespace TwitchDownloaderWPF.TwitchTasks { - internal class ChatDownloadTask : ITwitchTask + internal class ChatDownloadTask : TwitchTask { - public TaskData Info { get; } = new(); - - private int _progress; - public int Progress - { - get => _progress; - private set => SetField(ref _progress, value); - } - - private TwitchTaskStatus _status = TwitchTaskStatus.Ready; - public TwitchTaskStatus Status - { - get => _status; - private set => SetField(ref _status, value); - } - - private string _displayStatus; - public string DisplayStatus - { - get => _displayStatus; - private set => SetField(ref _displayStatus, value); - } - - private string _statusImage; - public string StatusImage - { - get => _statusImage; - private set => SetField(ref _statusImage, value); - } - public ChatDownloadOptions DownloadOptions { get; init; } - public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; init; } - public string TaskType { get; } = Translations.Strings.ChatDownload; - - private Exception _exception; - public Exception Exception - { - get => _exception; - private set => SetField(ref _exception, value); - } - - public string OutputFile => DownloadOptions.Filename; - - private bool _canCancel; - public bool CanCancel - { - get => _canCancel; - private set => SetField(ref _canCancel, value); - } - - private bool _canReinitialize; - public bool CanReinitialize - { - get => _canReinitialize; - private set => SetField(ref _canReinitialize, value); - } - - public event PropertyChangedEventHandler PropertyChanged; + public override string TaskType { get; } = Translations.Strings.ChatDownload; + public override string OutputFile => DownloadOptions.Filename; - public void Cancel() - { - if (!CanCancel) - { - return; - } - - TokenSource.Cancel(); - - if (Status == TwitchTaskStatus.Running) - { - ChangeStatus(TwitchTaskStatus.Stopping); - return; - } - - ChangeStatus(TwitchTaskStatus.Canceled); - } - - public void Reinitialize() + public override void Reinitialize() { Progress = 0; TokenSource = new CancellationTokenSource(); @@ -99,28 +22,12 @@ public void Reinitialize() ChangeStatus(TwitchTaskStatus.Ready); } - public bool CanRun() + public override bool CanRun() { return Status == TwitchTaskStatus.Ready; } - public void ChangeStatus(TwitchTaskStatus newStatus) - { - Status = newStatus; - DisplayStatus = newStatus.ToString(); - - CanCancel = newStatus is not TwitchTaskStatus.Canceled and not TwitchTaskStatus.Failed and not TwitchTaskStatus.Finished and not TwitchTaskStatus.Stopping; - - StatusImage = newStatus switch - { - TwitchTaskStatus.Running => "Images/ppOverheat.gif", - TwitchTaskStatus.Ready or TwitchTaskStatus.Waiting => "Images/ppHop.gif", - TwitchTaskStatus.Stopping => "Images/ppStretch.gif", - _ => null - }; - } - - public async Task RunAsync() + public override async Task RunAsync() { if (TokenSource.IsCancellationRequested) { @@ -161,18 +68,5 @@ public async Task RunAsync() TokenSource.Dispose(); GC.Collect(-1, GCCollectionMode.Default, false); } - - private void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - private bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) - { - if (EqualityComparer.Default.Equals(field, value)) return false; - field = value; - OnPropertyChanged(propertyName); - return true; - } } -} +} \ No newline at end of file diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs index 13db3a4c..87e2a81f 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using TwitchDownloaderCore; @@ -10,87 +7,13 @@ namespace TwitchDownloaderWPF.TwitchTasks { - internal class ChatRenderTask : ITwitchTask + internal class ChatRenderTask : TwitchTask { - public TaskData Info { get; } = new(); - - private int _progress; - public int Progress - { - get => _progress; - private set => SetField(ref _progress, value); - } - - private TwitchTaskStatus _status = TwitchTaskStatus.Ready; - public TwitchTaskStatus Status - { - get => _status; - private set => SetField(ref _status, value); - } - - private string _displayStatus; - public string DisplayStatus - { - get => _displayStatus; - private set => SetField(ref _displayStatus, value); - } - - private string _statusImage; - public string StatusImage - { - get => _statusImage; - private set => SetField(ref _statusImage, value); - } - public ChatRenderOptions DownloadOptions { get; init; } - public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; init; } - public string TaskType { get; } = Translations.Strings.ChatRender; - - private Exception _exception; - public Exception Exception - { - get => _exception; - private set => SetField(ref _exception, value); - } - - public string OutputFile => DownloadOptions.OutputFile; - - private bool _canCancel; - public bool CanCancel - { - get => _canCancel; - private set => SetField(ref _canCancel, value); - } - - private bool _canReinitialize; - public bool CanReinitialize - { - get => _canReinitialize; - private set => SetField(ref _canReinitialize, value); - } - - public event PropertyChangedEventHandler PropertyChanged; - - public void Cancel() - { - if (!CanCancel) - { - return; - } - - TokenSource.Cancel(); - - if (Status == TwitchTaskStatus.Running) - { - ChangeStatus(TwitchTaskStatus.Stopping); - return; - } + public override string TaskType { get; } = Translations.Strings.ChatRender; + public override string OutputFile => DownloadOptions.OutputFile; - ChangeStatus(TwitchTaskStatus.Canceled); - } - - public void Reinitialize() + public override void Reinitialize() { Progress = 0; TokenSource = new CancellationTokenSource(); @@ -99,21 +22,20 @@ public void Reinitialize() ChangeStatus(DependantTask is null ? TwitchTaskStatus.Ready : TwitchTaskStatus.Waiting); } - public bool CanRun() + public override bool CanRun() { if (DependantTask == null) { - if (Status == TwitchTaskStatus.Ready) - { - return true; - } + return Status == TwitchTaskStatus.Ready; } - else if (Status == TwitchTaskStatus.Waiting) + + if (Status == TwitchTaskStatus.Waiting) { if (DependantTask.Status == TwitchTaskStatus.Finished) { return true; } + if (DependantTask.Status is TwitchTaskStatus.Failed or TwitchTaskStatus.Canceled) { ChangeStatus(TwitchTaskStatus.Canceled); @@ -121,26 +43,11 @@ public bool CanRun() return false; } } - return false; - } - - public void ChangeStatus(TwitchTaskStatus newStatus) - { - Status = newStatus; - DisplayStatus = newStatus.ToString(); - CanCancel = newStatus is not TwitchTaskStatus.Canceled and not TwitchTaskStatus.Failed and not TwitchTaskStatus.Finished and not TwitchTaskStatus.Stopping; - - StatusImage = newStatus switch - { - TwitchTaskStatus.Running => "Images/ppOverheat.gif", - TwitchTaskStatus.Ready or TwitchTaskStatus.Waiting => "Images/ppHop.gif", - TwitchTaskStatus.Stopping => "Images/ppStretch.gif", - _ => null - }; + return false; } - public async Task RunAsync() + public override async Task RunAsync() { if (TokenSource.IsCancellationRequested) { @@ -183,18 +90,5 @@ public async Task RunAsync() TokenSource.Dispose(); GC.Collect(-1, GCCollectionMode.Default, false); } - - private void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - private bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) - { - if (EqualityComparer.Default.Equals(field, value)) return false; - field = value; - OnPropertyChanged(propertyName); - return true; - } } -} +} \ No newline at end of file diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs index d5072417..21102639 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using TwitchDownloaderCore; @@ -10,87 +7,13 @@ namespace TwitchDownloaderWPF.TwitchTasks { - internal class ChatUpdateTask : ITwitchTask + internal class ChatUpdateTask : TwitchTask { - public TaskData Info { get; } = new(); - - private int _progress; - public int Progress - { - get => _progress; - private set => SetField(ref _progress, value); - } - - private TwitchTaskStatus _status = TwitchTaskStatus.Ready; - public TwitchTaskStatus Status - { - get => _status; - private set => SetField(ref _status, value); - } - - private string _displayStatus; - public string DisplayStatus - { - get => _displayStatus; - private set => SetField(ref _displayStatus, value); - } - - private string _statusImage; - public string StatusImage - { - get => _statusImage; - private set => SetField(ref _statusImage, value); - } - public ChatUpdateOptions UpdateOptions { get; init; } - public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; init; } - public string TaskType { get; } = Translations.Strings.ChatUpdate; - - private Exception _exception; - public Exception Exception - { - get => _exception; - private set => SetField(ref _exception, value); - } - - public string OutputFile => UpdateOptions.OutputFile; - - private bool _canCancel; - public bool CanCancel - { - get => _canCancel; - private set => SetField(ref _canCancel, value); - } - - private bool _canReinitialize; - public bool CanReinitialize - { - get => _canReinitialize; - private set => SetField(ref _canReinitialize, value); - } - - public event PropertyChangedEventHandler PropertyChanged; + public override string TaskType { get; } = Translations.Strings.ChatUpdate; + public override string OutputFile => UpdateOptions.OutputFile; - public void Cancel() - { - if (!CanCancel) - { - return; - } - - TokenSource.Cancel(); - - if (Status == TwitchTaskStatus.Running) - { - ChangeStatus(TwitchTaskStatus.Stopping); - return; - } - - ChangeStatus(TwitchTaskStatus.Canceled); - } - - public void Reinitialize() + public override void Reinitialize() { Progress = 0; TokenSource = new CancellationTokenSource(); @@ -99,28 +22,12 @@ public void Reinitialize() ChangeStatus(TwitchTaskStatus.Ready); } - public bool CanRun() + public override bool CanRun() { return Status == TwitchTaskStatus.Ready; } - public void ChangeStatus(TwitchTaskStatus newStatus) - { - Status = newStatus; - DisplayStatus = newStatus.ToString(); - - CanCancel = newStatus is not TwitchTaskStatus.Canceled and not TwitchTaskStatus.Failed and not TwitchTaskStatus.Finished and not TwitchTaskStatus.Stopping; - - StatusImage = newStatus switch - { - TwitchTaskStatus.Running => "Images/ppOverheat.gif", - TwitchTaskStatus.Ready or TwitchTaskStatus.Waiting => "Images/ppHop.gif", - TwitchTaskStatus.Stopping => "Images/ppStretch.gif", - _ => null - }; - } - - public async Task RunAsync() + public override async Task RunAsync() { if (TokenSource.IsCancellationRequested) { @@ -162,18 +69,5 @@ public async Task RunAsync() TokenSource.Dispose(); GC.Collect(-1, GCCollectionMode.Default, false); } - - private void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - private bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) - { - if (EqualityComparer.Default.Equals(field, value)) return false; - field = value; - OnPropertyChanged(propertyName); - return true; - } } -} +} \ No newline at end of file diff --git a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs index c130bbe9..bee5baf3 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using TwitchDownloaderCore; @@ -10,87 +7,13 @@ namespace TwitchDownloaderWPF.TwitchTasks { - internal class ClipDownloadTask : ITwitchTask + internal class ClipDownloadTask : TwitchTask { - public TaskData Info { get; } = new(); - - private int _progress; - public int Progress - { - get => _progress; - private set => SetField(ref _progress, value); - } - - private TwitchTaskStatus _status = TwitchTaskStatus.Ready; - public TwitchTaskStatus Status - { - get => _status; - private set => SetField(ref _status, value); - } - - private string _displayStatus; - public string DisplayStatus - { - get => _displayStatus; - private set => SetField(ref _displayStatus, value); - } - - private string _statusImage; - public string StatusImage - { - get => _statusImage; - private set => SetField(ref _statusImage, value); - } - public ClipDownloadOptions DownloadOptions { get; init; } - public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; init; } - public string TaskType { get; } = Translations.Strings.ClipDownload; - - private Exception _exception; - public Exception Exception - { - get => _exception; - private set => SetField(ref _exception, value); - } - - public string OutputFile => DownloadOptions.Filename; - - private bool _canCancel; - public bool CanCancel - { - get => _canCancel; - private set => SetField(ref _canCancel, value); - } - - private bool _canReinitialize; - public bool CanReinitialize - { - get => _canReinitialize; - private set => SetField(ref _canReinitialize, value); - } - - public event PropertyChangedEventHandler PropertyChanged; + public override string TaskType { get; } = Translations.Strings.ClipDownload; + public override string OutputFile => DownloadOptions.Filename; - public void Cancel() - { - if (!CanCancel) - { - return; - } - - TokenSource.Cancel(); - - if (Status == TwitchTaskStatus.Running) - { - ChangeStatus(TwitchTaskStatus.Stopping); - return; - } - - ChangeStatus(TwitchTaskStatus.Canceled); - } - - public void Reinitialize() + public override void Reinitialize() { Progress = 0; TokenSource = new CancellationTokenSource(); @@ -99,28 +22,12 @@ public void Reinitialize() ChangeStatus(TwitchTaskStatus.Ready); } - public bool CanRun() + public override bool CanRun() { return Status == TwitchTaskStatus.Ready; } - public void ChangeStatus(TwitchTaskStatus newStatus) - { - Status = newStatus; - DisplayStatus = newStatus.ToString(); - - CanCancel = newStatus is not TwitchTaskStatus.Canceled and not TwitchTaskStatus.Failed and not TwitchTaskStatus.Finished and not TwitchTaskStatus.Stopping; - - StatusImage = newStatus switch - { - TwitchTaskStatus.Running => "Images/ppOverheat.gif", - TwitchTaskStatus.Ready or TwitchTaskStatus.Waiting => "Images/ppHop.gif", - TwitchTaskStatus.Stopping => "Images/ppStretch.gif", - _ => null - }; - } - - public async Task RunAsync() + public override async Task RunAsync() { if (TokenSource.IsCancellationRequested) { @@ -161,18 +68,5 @@ public async Task RunAsync() TokenSource.Dispose(); GC.Collect(-1, GCCollectionMode.Default, false); } - - private void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - private bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) - { - if (EqualityComparer.Default.Equals(field, value)) return false; - field = value; - OnPropertyChanged(propertyName); - return true; - } } -} +} \ No newline at end of file diff --git a/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs b/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs deleted file mode 100644 index 349e69ff..00000000 --- a/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.ComponentModel; -using System.Threading; -using System.Threading.Tasks; - -namespace TwitchDownloaderWPF.TwitchTasks -{ - public enum TwitchTaskStatus - { - Waiting, - Ready, - Running, - Failed, - Finished, - Stopping, - Canceled - } - - public interface ITwitchTask : INotifyPropertyChanged - { - TaskData Info { get; } - int Progress { get; } - TwitchTaskStatus Status { get; } - string DisplayStatus { get; } - string StatusImage { get; } - CancellationTokenSource TokenSource { get; } - ITwitchTask DependantTask { get; init; } - string TaskType { get; } - Exception Exception { get; } - string OutputFile { get; } - bool CanCancel { get; } - bool CanReinitialize { get; } - - Task RunAsync(); - void Cancel(); - bool CanRun(); - void Reinitialize(); - } -} diff --git a/TwitchDownloaderWPF/TwitchTasks/TwitchTask.cs b/TwitchDownloaderWPF/TwitchTasks/TwitchTask.cs new file mode 100644 index 00000000..84243c50 --- /dev/null +++ b/TwitchDownloaderWPF/TwitchTasks/TwitchTask.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; + +namespace TwitchDownloaderWPF.TwitchTasks +{ + public abstract class TwitchTask : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + public TaskData Info { get; } = new(); + + private int _progress; + public int Progress + { + get => _progress; + protected set => SetField(ref _progress, value); + } + + private TwitchTaskStatus _status = TwitchTaskStatus.Ready; + public TwitchTaskStatus Status + { + get => _status; + private set => SetField(ref _status, value); + } + + private string _displayStatus; + public string DisplayStatus + { + get => _displayStatus; + protected set => SetField(ref _displayStatus, value); + } + + private string _statusImage; + public string StatusImage + { + get => _statusImage; + private set => SetField(ref _statusImage, value); + } + + protected CancellationTokenSource TokenSource { get; set; } = new(); + public TwitchTask DependantTask { get; init; } + public abstract string TaskType { get; } + + private Exception _exception; + public Exception Exception + { + get => _exception; + protected set => SetField(ref _exception, value); + } + + public abstract string OutputFile { get; } + + private bool _canCancel; + public bool CanCancel + { + get => _canCancel; + protected set => SetField(ref _canCancel, value); + } + + private bool _canReinitialize; + public bool CanReinitialize + { + get => _canReinitialize; + protected set => SetField(ref _canReinitialize, value); + } + + public void Cancel() + { + if (!CanCancel) + return; + + TokenSource.Cancel(); + + ChangeStatus(Status is TwitchTaskStatus.Running ? TwitchTaskStatus.Stopping : TwitchTaskStatus.Canceled); + } + + public abstract void Reinitialize(); + + public abstract bool CanRun(); + + public abstract Task RunAsync(); + + public void ChangeStatus(TwitchTaskStatus newStatus) + { + Status = newStatus; + DisplayStatus = newStatus.ToString(); + + CanCancel = newStatus is not TwitchTaskStatus.Canceled and not TwitchTaskStatus.Failed and not TwitchTaskStatus.Finished and not TwitchTaskStatus.Stopping; + + StatusImage = newStatus switch + { + TwitchTaskStatus.Running => "Images/ppOverheat.gif", + TwitchTaskStatus.Ready or TwitchTaskStatus.Waiting => "Images/ppHop.gif", + TwitchTaskStatus.Stopping => "Images/ppStretch.gif", + _ => null + }; + } + + private void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + private bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) + { + if (EqualityComparer.Default.Equals(field, value)) + return false; + + field = value; + OnPropertyChanged(propertyName); + return true; + } + } +} \ No newline at end of file diff --git a/TwitchDownloaderWPF/TwitchTasks/TwitchTaskStatus.cs b/TwitchDownloaderWPF/TwitchTasks/TwitchTaskStatus.cs new file mode 100644 index 00000000..ebe216b3 --- /dev/null +++ b/TwitchDownloaderWPF/TwitchTasks/TwitchTaskStatus.cs @@ -0,0 +1,13 @@ +namespace TwitchDownloaderWPF.TwitchTasks +{ + public enum TwitchTaskStatus + { + Waiting, + Ready, + Running, + Failed, + Finished, + Stopping, + Canceled + } +} diff --git a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs index e80bc5a2..5967719f 100644 --- a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using TwitchDownloaderCore; @@ -10,87 +7,13 @@ namespace TwitchDownloaderWPF.TwitchTasks { - internal class VodDownloadTask : ITwitchTask + internal class VodDownloadTask : TwitchTask { - public TaskData Info { get; } = new(); - - private int _progress; - public int Progress - { - get => _progress; - private set => SetField(ref _progress, value); - } - - private TwitchTaskStatus _status = TwitchTaskStatus.Ready; - public TwitchTaskStatus Status - { - get => _status; - private set => SetField(ref _status, value); - } - - private string _displayStatus; - public string DisplayStatus - { - get => _displayStatus; - private set => SetField(ref _displayStatus, value); - } - - private string _statusImage; - public string StatusImage - { - get => _statusImage; - private set => SetField(ref _statusImage, value); - } - public VideoDownloadOptions DownloadOptions { get; init; } - public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; init; } - public string TaskType { get; } = Translations.Strings.VodDownload; - - private Exception _exception; - public Exception Exception - { - get => _exception; - private set => SetField(ref _exception, value); - } - - public string OutputFile => DownloadOptions.Filename; - - private bool _canCancel; - public bool CanCancel - { - get => _canCancel; - private set => SetField(ref _canCancel, value); - } - - private bool _canReinitialize; - public bool CanReinitialize - { - get => _canReinitialize; - private set => SetField(ref _canReinitialize, value); - } - - public event PropertyChangedEventHandler PropertyChanged; + public override string TaskType { get; } = Translations.Strings.VodDownload; + public override string OutputFile => DownloadOptions.Filename; - public void Cancel() - { - if (!CanCancel) - { - return; - } - - TokenSource.Cancel(); - - if (Status == TwitchTaskStatus.Running) - { - ChangeStatus(TwitchTaskStatus.Stopping); - return; - } - - ChangeStatus(TwitchTaskStatus.Canceled); - } - - public void Reinitialize() + public override void Reinitialize() { Progress = 0; TokenSource = new CancellationTokenSource(); @@ -99,28 +22,12 @@ public void Reinitialize() ChangeStatus(TwitchTaskStatus.Ready); } - public bool CanRun() + public override bool CanRun() { return Status == TwitchTaskStatus.Ready; } - public void ChangeStatus(TwitchTaskStatus newStatus) - { - Status = newStatus; - DisplayStatus = newStatus.ToString(); - - CanCancel = newStatus is not TwitchTaskStatus.Canceled and not TwitchTaskStatus.Failed and not TwitchTaskStatus.Finished and not TwitchTaskStatus.Stopping; - - StatusImage = newStatus switch - { - TwitchTaskStatus.Running => "Images/ppOverheat.gif", - TwitchTaskStatus.Ready or TwitchTaskStatus.Waiting => "Images/ppHop.gif", - TwitchTaskStatus.Stopping => "Images/ppStretch.gif", - _ => null - }; - } - - public async Task RunAsync() + public override async Task RunAsync() { if (TokenSource.IsCancellationRequested) { @@ -161,18 +68,5 @@ public async Task RunAsync() TokenSource.Dispose(); GC.Collect(-1, GCCollectionMode.Default, false); } - - private void OnPropertyChanged([CallerMemberName] string propertyName = null) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - - private bool SetField(ref T field, T value, [CallerMemberName] string propertyName = null) - { - if (EqualityComparer.Default.Equals(field, value)) return false; - field = value; - OnPropertyChanged(propertyName); - return true; - } } -} +} \ No newline at end of file