Skip to content

Commit

Permalink
Cleanup (#1086)
Browse files Browse the repository at this point in the history
* Simplify notifying properties

* Fix ReadOnlySpanExtensions.UnEscapedIndexOf ArgumentOutOfRangeException args

* Fix system emoji vendor missing from exception message

* Simplify CacheHandler user prompt code
  • Loading branch information
ScrubN authored Jun 6, 2024
1 parent ef34ccf commit 1f68433
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 145 deletions.
12 changes: 3 additions & 9 deletions TwitchDownloaderCLI/Modes/CacheHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,15 @@ private static void PromptClearCache()
Console.WriteLine("Are you sure you want to clear the cache? This should really only be done if the program isn't working correctly.");
while (true)
{
Console.Write("[Y]es / [N]o: ");
Console.Write("[Y] Yes / [N] No: ");
var userInput = Console.ReadLine()!.Trim().ToLower();
switch (userInput)
{
case "y":
case "ye":
case "yes":
case "y" or "yes":
ClearTempCache();
return;
case "n":
case "no":
case "n" or "no":
return;
default:
Console.Write("Invalid input. ");
continue;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCLI/Modes/RenderChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static ChatRenderOptions GetRenderOptions(ChatRenderArgs inputOptions, I
"twitter" or "twemoji" => EmojiVendor.TwitterTwemoji,
"google" or "notocolor" => EmojiVendor.GoogleNotoColor,
"system" or "none" => EmojiVendor.None,
_ => throw new NotSupportedException("Invalid emoji vendor. Valid values are: 'twitter' / 'twemoji', and 'google' / 'notocolor'")
_ => throw new NotSupportedException("Invalid emoji vendor. Valid values are: 'twitter' / 'twemoji', 'google' / 'notocolor', and 'system' / 'none'")
},
SkipDriveWaiting = inputOptions.SkipDriveWaiting,
EmoteScale = inputOptions.ScaleEmote,
Expand Down
4 changes: 2 additions & 2 deletions TwitchDownloaderCore/Extensions/ReadOnlySpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static int FindCloseQuoteChar(ReadOnlySpan<char> destination, int openQu
public static int UnEscapedIndexOf(this ReadOnlySpan<char> str, char character)
{
if (character is '\\' or '\'' or '\"')
throw new ArgumentOutOfRangeException("Escape characters are not supported.", nameof(character));
throw new ArgumentOutOfRangeException(nameof(character), character, "Escape characters are not supported.");

var firstIndex = str.IndexOf(character);
if (firstIndex == -1)
Expand Down Expand Up @@ -146,7 +146,7 @@ public static int UnEscapedIndexOfAny(this ReadOnlySpan<char> str, ReadOnlySpan<
const string ESCAPE_CHARS = @"\'""";

if (characters.IndexOfAny(ESCAPE_CHARS) != -1)
throw new ArgumentOutOfRangeException("Escape characters are not supported.", nameof(characters));
throw new ArgumentOutOfRangeException(nameof(characters), characters.ToString(), "Escape characters are not supported.");

var firstIndex = str.IndexOfAny(characters);
if (firstIndex == -1)
Expand Down
37 changes: 13 additions & 24 deletions TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -17,24 +18,14 @@ class ChatDownloadTask : ITwitchTask
public int Progress
{
get => _progress;
set
{
if (value == _progress) return;
_progress = value;
OnPropertyChanged();
}
private set => SetField(ref _progress, value);
}

private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
public TwitchTaskStatus Status
{
get => _status;
private set
{
if (value == _status) return;
_status = value;
OnPropertyChanged();
}
private set => SetField(ref _status, value);
}

public ChatDownloadOptions DownloadOptions { get; init; }
Expand All @@ -46,12 +37,7 @@ private set
public TwitchTaskException Exception
{
get => _exception;
private set
{
if (Equals(value, _exception)) return;
_exception = value;
OnPropertyChanged();
}
private set => SetField(ref _exception, value);
}

public string OutputFile => DownloadOptions.Filename;
Expand All @@ -60,12 +46,7 @@ private set
public bool CanCancel
{
get => _canCancel;
private set
{
if (value == _canCancel) return;
_canCancel = value;
OnPropertyChanged();
}
private set => SetField(ref _canCancel, value);
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down Expand Up @@ -147,5 +128,13 @@ 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;
}
}
}
37 changes: 13 additions & 24 deletions TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -17,24 +18,14 @@ class ChatRenderTask : ITwitchTask
public int Progress
{
get => _progress;
set
{
if (value == _progress) return;
_progress = value;
OnPropertyChanged();
}
private set => SetField(ref _progress, value);
}

private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
public TwitchTaskStatus Status
{
get => _status;
private set
{
if (value == _status) return;
_status = value;
OnPropertyChanged();
}
private set => SetField(ref _status, value);
}

public ChatRenderOptions DownloadOptions { get; init; }
Expand All @@ -46,12 +37,7 @@ private set
public TwitchTaskException Exception
{
get => _exception;
private set
{
if (Equals(value, _exception)) return;
_exception = value;
OnPropertyChanged();
}
private set => SetField(ref _exception, value);
}

public string OutputFile => DownloadOptions.OutputFile;
Expand All @@ -60,12 +46,7 @@ private set
public bool CanCancel
{
get => _canCancel;
private set
{
if (value == _canCancel) return;
_canCancel = value;
OnPropertyChanged();
}
private set => SetField(ref _canCancel, value);
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down Expand Up @@ -166,5 +147,13 @@ 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;
}
}
}
37 changes: 13 additions & 24 deletions TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -17,24 +18,14 @@ class ChatUpdateTask : ITwitchTask
public int Progress
{
get => _progress;
set
{
if (value == _progress) return;
_progress = value;
OnPropertyChanged();
}
private set => SetField(ref _progress, value);
}

private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
public TwitchTaskStatus Status
{
get => _status;
private set
{
if (value == _status) return;
_status = value;
OnPropertyChanged();
}
private set => SetField(ref _status, value);
}

public ChatUpdateOptions UpdateOptions { get; init; }
Expand All @@ -46,12 +37,7 @@ private set
public TwitchTaskException Exception
{
get => _exception;
private set
{
if (Equals(value, _exception)) return;
_exception = value;
OnPropertyChanged();
}
private set => SetField(ref _exception, value);
}

public string OutputFile => UpdateOptions.OutputFile;
Expand All @@ -60,12 +46,7 @@ private set
public bool CanCancel
{
get => _canCancel;
private set
{
if (value == _canCancel) return;
_canCancel = value;
OnPropertyChanged();
}
private set => SetField(ref _canCancel, value);
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down Expand Up @@ -148,5 +129,13 @@ 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;
}
}
}
37 changes: 13 additions & 24 deletions TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
Expand All @@ -17,24 +18,14 @@ class ClipDownloadTask : ITwitchTask
public int Progress
{
get => _progress;
set
{
if (value == _progress) return;
_progress = value;
OnPropertyChanged();
}
private set => SetField(ref _progress, value);
}

private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
public TwitchTaskStatus Status
{
get => _status;
private set
{
if (value == _status) return;
_status = value;
OnPropertyChanged();
}
private set => SetField(ref _status, value);
}

public ClipDownloadOptions DownloadOptions { get; init; }
Expand All @@ -46,12 +37,7 @@ private set
public TwitchTaskException Exception
{
get => _exception;
private set
{
if (Equals(value, _exception)) return;
_exception = value;
OnPropertyChanged();
}
private set => SetField(ref _exception, value);
}

public string OutputFile => DownloadOptions.Filename;
Expand All @@ -60,12 +46,7 @@ private set
public bool CanCancel
{
get => _canCancel;
private set
{
if (value == _canCancel) return;
_canCancel = value;
OnPropertyChanged();
}
private set => SetField(ref _canCancel, value);
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down Expand Up @@ -147,5 +128,13 @@ 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;
}
}
}
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum TwitchTaskStatus
public interface ITwitchTask : INotifyPropertyChanged
{
TaskData Info { get; set; }
int Progress { get; set; }
int Progress { get; }
TwitchTaskStatus Status { get; }
CancellationTokenSource TokenSource { get; set; }
ITwitchTask DependantTask { get; set; }
Expand Down
Loading

0 comments on commit 1f68433

Please sign in to comment.