Skip to content

Commit d36a0f1

Browse files
committed
Cleanup (lay295#1086)
* Simplify notifying properties * Fix ReadOnlySpanExtensions.UnEscapedIndexOf ArgumentOutOfRangeException args * Fix system emoji vendor missing from exception message * Simplify CacheHandler user prompt code
1 parent 69e480c commit d36a0f1

File tree

10 files changed

+82
-145
lines changed

10 files changed

+82
-145
lines changed

TwitchDownloaderCLI/Modes/CacheHandler.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,15 @@ private static void PromptClearCache()
2727
Console.WriteLine("Are you sure you want to clear the cache? This should really only be done if the program isn't working correctly.");
2828
while (true)
2929
{
30-
Console.Write("[Y]es / [N]o: ");
30+
Console.Write("[Y] Yes / [N] No: ");
3131
var userInput = Console.ReadLine()!.Trim().ToLower();
3232
switch (userInput)
3333
{
34-
case "y":
35-
case "ye":
36-
case "yes":
34+
case "y" or "yes":
3735
ClearTempCache();
3836
return;
39-
case "n":
40-
case "no":
37+
case "n" or "no":
4138
return;
42-
default:
43-
Console.Write("Invalid input. ");
44-
continue;
4539
}
4640
}
4741
}

TwitchDownloaderCLI/Modes/RenderChat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static ChatRenderOptions GetRenderOptions(ChatRenderArgs inputOptions, I
7777
"twitter" or "twemoji" => EmojiVendor.TwitterTwemoji,
7878
"google" or "notocolor" => EmojiVendor.GoogleNotoColor,
7979
"system" or "none" => EmojiVendor.None,
80-
_ => throw new NotSupportedException("Invalid emoji vendor. Valid values are: 'twitter' / 'twemoji', and 'google' / 'notocolor'")
80+
_ => throw new NotSupportedException("Invalid emoji vendor. Valid values are: 'twitter' / 'twemoji', 'google' / 'notocolor', and 'system' / 'none'")
8181
},
8282
SkipDriveWaiting = inputOptions.SkipDriveWaiting,
8383
EmoteScale = inputOptions.ScaleEmote,

TwitchDownloaderCore/Extensions/ReadOnlySpanExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static int FindCloseQuoteChar(ReadOnlySpan<char> destination, int openQu
9595
public static int UnEscapedIndexOf(this ReadOnlySpan<char> str, char character)
9696
{
9797
if (character is '\\' or '\'' or '\"')
98-
throw new ArgumentOutOfRangeException("Escape characters are not supported.", nameof(character));
98+
throw new ArgumentOutOfRangeException(nameof(character), character, "Escape characters are not supported.");
9999

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

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

151151
var firstIndex = str.IndexOfAny(characters);
152152
if (firstIndex == -1)

TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Runtime.CompilerServices;
45
using System.Threading;
@@ -17,24 +18,14 @@ class ChatDownloadTask : ITwitchTask
1718
public int Progress
1819
{
1920
get => _progress;
20-
set
21-
{
22-
if (value == _progress) return;
23-
_progress = value;
24-
OnPropertyChanged();
25-
}
21+
private set => SetField(ref _progress, value);
2622
}
2723

2824
private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
2925
public TwitchTaskStatus Status
3026
{
3127
get => _status;
32-
private set
33-
{
34-
if (value == _status) return;
35-
_status = value;
36-
OnPropertyChanged();
37-
}
28+
private set => SetField(ref _status, value);
3829
}
3930

4031
public ChatDownloadOptions DownloadOptions { get; init; }
@@ -46,12 +37,7 @@ private set
4637
public TwitchTaskException Exception
4738
{
4839
get => _exception;
49-
private set
50-
{
51-
if (Equals(value, _exception)) return;
52-
_exception = value;
53-
OnPropertyChanged();
54-
}
40+
private set => SetField(ref _exception, value);
5541
}
5642

5743
public string OutputFile => DownloadOptions.Filename;
@@ -60,12 +46,7 @@ private set
6046
public bool CanCancel
6147
{
6248
get => _canCancel;
63-
private set
64-
{
65-
if (value == _canCancel) return;
66-
_canCancel = value;
67-
OnPropertyChanged();
68-
}
49+
private set => SetField(ref _canCancel, value);
6950
}
7051

7152
public event PropertyChangedEventHandler PropertyChanged;
@@ -147,5 +128,13 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
147128
{
148129
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
149130
}
131+
132+
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
133+
{
134+
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
135+
field = value;
136+
OnPropertyChanged(propertyName);
137+
return true;
138+
}
150139
}
151140
}

TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Runtime.CompilerServices;
45
using System.Threading;
@@ -17,24 +18,14 @@ class ChatRenderTask : ITwitchTask
1718
public int Progress
1819
{
1920
get => _progress;
20-
set
21-
{
22-
if (value == _progress) return;
23-
_progress = value;
24-
OnPropertyChanged();
25-
}
21+
private set => SetField(ref _progress, value);
2622
}
2723

2824
private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
2925
public TwitchTaskStatus Status
3026
{
3127
get => _status;
32-
private set
33-
{
34-
if (value == _status) return;
35-
_status = value;
36-
OnPropertyChanged();
37-
}
28+
private set => SetField(ref _status, value);
3829
}
3930

4031
public ChatRenderOptions DownloadOptions { get; init; }
@@ -46,12 +37,7 @@ private set
4637
public TwitchTaskException Exception
4738
{
4839
get => _exception;
49-
private set
50-
{
51-
if (Equals(value, _exception)) return;
52-
_exception = value;
53-
OnPropertyChanged();
54-
}
40+
private set => SetField(ref _exception, value);
5541
}
5642

5743
public string OutputFile => DownloadOptions.OutputFile;
@@ -60,12 +46,7 @@ private set
6046
public bool CanCancel
6147
{
6248
get => _canCancel;
63-
private set
64-
{
65-
if (value == _canCancel) return;
66-
_canCancel = value;
67-
OnPropertyChanged();
68-
}
49+
private set => SetField(ref _canCancel, value);
6950
}
7051

7152
public event PropertyChangedEventHandler PropertyChanged;
@@ -166,5 +147,13 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
166147
{
167148
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
168149
}
150+
151+
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
152+
{
153+
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
154+
field = value;
155+
OnPropertyChanged(propertyName);
156+
return true;
157+
}
169158
}
170159
}

TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Runtime.CompilerServices;
45
using System.Threading;
@@ -17,24 +18,14 @@ class ChatUpdateTask : ITwitchTask
1718
public int Progress
1819
{
1920
get => _progress;
20-
set
21-
{
22-
if (value == _progress) return;
23-
_progress = value;
24-
OnPropertyChanged();
25-
}
21+
private set => SetField(ref _progress, value);
2622
}
2723

2824
private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
2925
public TwitchTaskStatus Status
3026
{
3127
get => _status;
32-
private set
33-
{
34-
if (value == _status) return;
35-
_status = value;
36-
OnPropertyChanged();
37-
}
28+
private set => SetField(ref _status, value);
3829
}
3930

4031
public ChatUpdateOptions UpdateOptions { get; init; }
@@ -46,12 +37,7 @@ private set
4637
public TwitchTaskException Exception
4738
{
4839
get => _exception;
49-
private set
50-
{
51-
if (Equals(value, _exception)) return;
52-
_exception = value;
53-
OnPropertyChanged();
54-
}
40+
private set => SetField(ref _exception, value);
5541
}
5642

5743
public string OutputFile => UpdateOptions.OutputFile;
@@ -60,12 +46,7 @@ private set
6046
public bool CanCancel
6147
{
6248
get => _canCancel;
63-
private set
64-
{
65-
if (value == _canCancel) return;
66-
_canCancel = value;
67-
OnPropertyChanged();
68-
}
49+
private set => SetField(ref _canCancel, value);
6950
}
7051

7152
public event PropertyChangedEventHandler PropertyChanged;
@@ -148,5 +129,13 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
148129
{
149130
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
150131
}
132+
133+
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
134+
{
135+
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
136+
field = value;
137+
OnPropertyChanged(propertyName);
138+
return true;
139+
}
151140
}
152141
}

TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.ComponentModel;
34
using System.Runtime.CompilerServices;
45
using System.Threading;
@@ -17,24 +18,14 @@ class ClipDownloadTask : ITwitchTask
1718
public int Progress
1819
{
1920
get => _progress;
20-
set
21-
{
22-
if (value == _progress) return;
23-
_progress = value;
24-
OnPropertyChanged();
25-
}
21+
private set => SetField(ref _progress, value);
2622
}
2723

2824
private TwitchTaskStatus _status = TwitchTaskStatus.Ready;
2925
public TwitchTaskStatus Status
3026
{
3127
get => _status;
32-
private set
33-
{
34-
if (value == _status) return;
35-
_status = value;
36-
OnPropertyChanged();
37-
}
28+
private set => SetField(ref _status, value);
3829
}
3930

4031
public ClipDownloadOptions DownloadOptions { get; init; }
@@ -46,12 +37,7 @@ private set
4637
public TwitchTaskException Exception
4738
{
4839
get => _exception;
49-
private set
50-
{
51-
if (Equals(value, _exception)) return;
52-
_exception = value;
53-
OnPropertyChanged();
54-
}
40+
private set => SetField(ref _exception, value);
5541
}
5642

5743
public string OutputFile => DownloadOptions.Filename;
@@ -60,12 +46,7 @@ private set
6046
public bool CanCancel
6147
{
6248
get => _canCancel;
63-
private set
64-
{
65-
if (value == _canCancel) return;
66-
_canCancel = value;
67-
OnPropertyChanged();
68-
}
49+
private set => SetField(ref _canCancel, value);
6950
}
7051

7152
public event PropertyChangedEventHandler PropertyChanged;
@@ -147,5 +128,13 @@ private void OnPropertyChanged([CallerMemberName] string propertyName = null)
147128
{
148129
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
149130
}
131+
132+
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
133+
{
134+
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
135+
field = value;
136+
OnPropertyChanged(propertyName);
137+
return true;
138+
}
150139
}
151140
}

TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public enum TwitchTaskStatus
1818
public interface ITwitchTask : INotifyPropertyChanged
1919
{
2020
TaskData Info { get; set; }
21-
int Progress { get; set; }
21+
int Progress { get; }
2222
TwitchTaskStatus Status { get; }
2323
CancellationTokenSource TokenSource { get; set; }
2424
ITwitchTask DependantTask { get; set; }

0 commit comments

Comments
 (0)