Skip to content

Commit

Permalink
Replace ITwitchTask with abstract base class (#1197)
Browse files Browse the repository at this point in the history
This greatly reduces the amount of duplicate code
  • Loading branch information
ScrubN authored Aug 27, 2024
1 parent b2dc445 commit 9713246
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 623 deletions.
28 changes: 14 additions & 14 deletions TwitchDownloaderWPF/PageQueue.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace TwitchDownloaderWPF
public partial class PageQueue : Page
{
public static readonly object taskLock = new object();
public static ObservableCollection<ITwitchTask> taskList { get; } = new ObservableCollection<ITwitchTask>();
public static ObservableCollection<TwitchTask> taskList { get; } = new();
private static readonly BackgroundWorker taskManager = new BackgroundWorker();

public PageQueue()
Expand Down Expand Up @@ -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;
}
Expand All @@ -208,15 +208,15 @@ 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;
}

CancelTask(task);
}

private static void CancelTask(ITwitchTask task)
private static void CancelTask(TwitchTask task)
{
if (task.CanCancel)
{
Expand All @@ -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;
}
Expand All @@ -236,15 +236,15 @@ 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;
}

ShowTaskException(task);
}

private static void ShowTaskException(ITwitchTask task)
private static void ShowTaskException(TwitchTask task)
{
var taskException = task.Exception;

Expand All @@ -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;
}
Expand All @@ -274,15 +274,15 @@ 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;
}

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)
{
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -318,15 +318,15 @@ 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;
}

RetryTask(task);
}

private static void RetryTask(ITwitchTask task)
private static void RetryTask(TwitchTask task)
{
if (task.CanReinitialize)
{
Expand Down
120 changes: 7 additions & 113 deletions TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -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)
{
Expand Down Expand Up @@ -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<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
}
}
Loading

0 comments on commit 9713246

Please sign in to comment.