Skip to content

Commit add110e

Browse files
committed
🐎 General cleanup
1 parent 294537f commit add110e

File tree

7 files changed

+108
-59
lines changed

7 files changed

+108
-59
lines changed

YoutubeDl.Wpf/Models/BackendInstance.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.Globalization;
9+
using System.IO;
910
using System.Linq;
1011
using System.Reactive.Concurrency;
1112
using System.Text;
@@ -220,7 +221,7 @@ public void GenerateDownloadArguments()
220221

221222
if (_settings.UseCustomPath)
222223
{
223-
outputTemplate = $@"{_settings.DownloadPath}\{outputTemplate}";
224+
outputTemplate = $@"{_settings.DownloadPath}{Path.DirectorySeparatorChar}{outputTemplate}";
224225
}
225226

226227
if (_settings.UseCustomOutputTemplate || _settings.UseCustomPath)

YoutubeDl.Wpf/Models/ObservableSettings.cs

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace YoutubeDl.Wpf.Models;
1010

1111
public class ObservableSettings : ReactiveObject
1212
{
13+
public Settings AppSettings { get; }
14+
1315
public BaseTheme AppColorMode { get; set; }
1416

1517
[Reactive]
@@ -96,6 +98,7 @@ public class ObservableSettings : ReactiveObject
9698

9799
public ObservableSettings(Settings settings)
98100
{
101+
AppSettings = settings;
99102
AppColorMode = settings.AppColorMode;
100103
Backend = settings.Backend;
101104
BackendPath = settings.BackendPath;
@@ -122,31 +125,31 @@ public ObservableSettings(Settings settings)
122125
DownloadPathHistory = new(settings.DownloadPathHistory);
123126
}
124127

125-
public void UpdateSettings(Settings settings)
128+
public void UpdateAppSettings()
126129
{
127-
settings.AppColorMode = AppColorMode;
128-
settings.Backend = Backend;
129-
settings.BackendPath = BackendPath;
130-
settings.BackendGlobalArguments = BackendGlobalArguments.ToArray();
131-
settings.BackendDownloadArguments = BackendDownloadArguments.ToArray();
132-
settings.BackendAutoUpdate = BackendAutoUpdate;
133-
settings.BackendLastUpdateCheck = BackendLastUpdateCheck;
134-
settings.FfmpegPath = FfmpegPath;
135-
settings.Proxy = Proxy;
136-
settings.LoggingMaxEntries = LoggingMaxEntries;
137-
settings.SelectedPreset = SelectedPreset;
138-
settings.SelectedPresetText = SelectedPresetText;
139-
settings.CustomPresets = CustomPresets.ToArray();
140-
settings.AddMetadata = AddMetadata;
141-
settings.DownloadThumbnail = DownloadThumbnail;
142-
settings.DownloadSubtitles = DownloadSubtitles;
143-
settings.DownloadSubtitlesAllLanguages = DownloadSubtitlesAllLanguages;
144-
settings.DownloadAutoGeneratedSubtitles = DownloadAutoGeneratedSubtitles;
145-
settings.DownloadPlaylist = DownloadPlaylist;
146-
settings.UseCustomOutputTemplate = UseCustomOutputTemplate;
147-
settings.CustomOutputTemplate = CustomOutputTemplate;
148-
settings.UseCustomPath = UseCustomPath;
149-
settings.DownloadPath = DownloadPath;
150-
settings.DownloadPathHistory = DownloadPathHistory.ToArray();
130+
AppSettings.AppColorMode = AppColorMode;
131+
AppSettings.Backend = Backend;
132+
AppSettings.BackendPath = BackendPath;
133+
AppSettings.BackendGlobalArguments = BackendGlobalArguments.ToArray();
134+
AppSettings.BackendDownloadArguments = BackendDownloadArguments.ToArray();
135+
AppSettings.BackendAutoUpdate = BackendAutoUpdate;
136+
AppSettings.BackendLastUpdateCheck = BackendLastUpdateCheck;
137+
AppSettings.FfmpegPath = FfmpegPath;
138+
AppSettings.Proxy = Proxy;
139+
// AppSettings.LoggingMaxEntries is managed by the validation handler.
140+
AppSettings.SelectedPreset = SelectedPreset;
141+
AppSettings.SelectedPresetText = SelectedPresetText;
142+
AppSettings.CustomPresets = CustomPresets.ToArray();
143+
AppSettings.AddMetadata = AddMetadata;
144+
AppSettings.DownloadThumbnail = DownloadThumbnail;
145+
AppSettings.DownloadSubtitles = DownloadSubtitles;
146+
AppSettings.DownloadSubtitlesAllLanguages = DownloadSubtitlesAllLanguages;
147+
AppSettings.DownloadAutoGeneratedSubtitles = DownloadAutoGeneratedSubtitles;
148+
AppSettings.DownloadPlaylist = DownloadPlaylist;
149+
AppSettings.UseCustomOutputTemplate = UseCustomOutputTemplate;
150+
AppSettings.CustomOutputTemplate = CustomOutputTemplate;
151+
AppSettings.UseCustomPath = UseCustomPath;
152+
AppSettings.DownloadPath = DownloadPath;
153+
AppSettings.DownloadPathHistory = DownloadPathHistory.ToArray();
151154
}
152155
}

YoutubeDl.Wpf/Models/Preset.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
#pragma warning disable IDE0079
2-
// STJ source generation actually supports record types with constructor parameters.
3-
// See discussions at https://github.com/dotnet/runtime/issues/58770.
4-
// Analyzer fix: https://github.com/dotnet/runtime/pull/68064.
5-
// Warning suppression is needed for .NET 6.
6-
#pragma warning disable SYSLIB1037
7-
#pragma warning restore IDE0079
8-
9-
using System.Collections.Generic;
1+
using System.Collections.Generic;
102

113
namespace YoutubeDl.Wpf.Models;
124

@@ -18,7 +10,7 @@ public record Preset(
1810
bool IsPredefined = false,
1911
params string[] ExtraArgs)
2012
{
21-
public string DisplayName => Name ?? FormatArg ?? ContainerArg ?? "Auto";
13+
public string DisplayName => Name ?? FormatArg ?? ContainerArg ?? "unnamed";
2214

2315
public IEnumerable<string> ToArgs()
2416
{
@@ -48,7 +40,9 @@ public IEnumerable<string> ToArgs()
4840
}
4941
}
5042

51-
public static readonly Preset Auto = new(IsPredefined: true);
43+
public const string AutoName = "Auto";
44+
45+
public static readonly Preset Auto = new(AutoName, IsPredefined: true);
5246

5347
public static readonly Preset[] PredefinedPresets =
5448
{

YoutubeDl.Wpf/Models/QueuedTextBoxSink.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace YoutubeDl.Wpf.Models;
1111
public class QueuedTextBoxSink : ReactiveObject, ILogEventSink
1212
{
1313
private readonly object _locker = new();
14-
private readonly ObservableSettings _settings;
14+
private readonly Settings _settings;
1515
private readonly Queue<string> _queuedLogMessages;
1616
private readonly IFormatProvider? _formatProvider;
1717
private int _contentLength;
1818

1919
[Reactive]
2020
public string Content { get; set; } = "";
2121

22-
public QueuedTextBoxSink(ObservableSettings settings, IFormatProvider? formatProvider = null)
22+
public QueuedTextBoxSink(Settings settings, IFormatProvider? formatProvider = null)
2323
{
2424
_settings = settings;
2525
_queuedLogMessages = new(settings.LoggingMaxEntries);

YoutubeDl.Wpf/Models/Settings.cs

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using MaterialDesignThemes.Wpf;
22
using System;
3+
using System.ComponentModel;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using YoutubeDl.Wpf.Utils;
@@ -14,6 +15,12 @@ public class Settings
1415
/// </summary>
1516
public const int DefaultVersion = 1;
1617

18+
/// <summary>
19+
/// Defines the default limit on the number of log messages
20+
/// displayed in the logs view.
21+
/// </summary>
22+
public const int DefaultLoggingMaxEntries = 1024;
23+
1724
/// <summary>
1825
/// Defines the default custom output filename template.
1926
/// We use yt-dlp's default as the default custom value.
@@ -44,11 +51,11 @@ public class Settings
4451

4552
public string Proxy { get; set; } = "";
4653

47-
public int LoggingMaxEntries { get; set; } = 1024;
54+
public int LoggingMaxEntries { get; set; } = DefaultLoggingMaxEntries;
4855

4956
public Preset? SelectedPreset { get; set; } = Preset.Auto;
5057

51-
public string SelectedPresetText { get; set; } = "Auto";
58+
public string SelectedPresetText { get; set; } = Preset.AutoName;
5259

5360
public Preset[] CustomPresets { get; set; } = Array.Empty<Preset>();
5461

@@ -82,7 +89,8 @@ public class Settings
8289
public static async Task<Settings> LoadAsync(CancellationToken cancellationToken = default)
8390
{
8491
var settings = await FileHelper.LoadFromJsonFileAsync("Settings.json", SettingsJsonSerializerContext.Default.Settings, cancellationToken).ConfigureAwait(false);
85-
settings.UpdateSettings();
92+
settings.Update();
93+
settings.Validate();
8694
return settings;
8795
}
8896

@@ -95,22 +103,59 @@ public Task SaveAsync(CancellationToken cancellationToken = default)
95103
=> FileHelper.SaveToJsonFileAsync("Settings.json", this, SettingsJsonSerializerContext.Default.Settings, cancellationToken);
96104

97105
/// <summary>
98-
/// Updates the loaded settings to the latest version.
106+
/// Updates settings to the latest version.
99107
/// </summary>
100-
/// <exception cref="Exception">
101-
/// The loaded settings have a higher version number than supported.
108+
/// <exception cref="NotSupportedException">
109+
/// The version number is not supported.
102110
/// </exception>
103-
public void UpdateSettings()
111+
private void Update()
104112
{
105113
switch (Version)
106114
{
107-
case 0: // nothing to do
115+
case 0:
108116
Version++;
109-
goto case 1; // go to the next update path
110-
case DefaultVersion: // current version
117+
goto case 1;
118+
case DefaultVersion:
111119
return;
112120
default:
113-
throw new Exception($"Settings version {Version} is newer than supported.");
121+
throw new NotSupportedException($"Settings version {Version} is not supported.");
122+
}
123+
}
124+
125+
/// <summary>
126+
/// Validates that all properties have valid values.
127+
/// </summary>
128+
/// <exception cref="InvalidEnumArgumentException">
129+
/// One of the enum properties have an invalid value.
130+
/// </exception>
131+
/// <exception cref="ArgumentOutOfRangeException">
132+
/// One of the integer values is out of the allowed range.
133+
/// </exception>
134+
private void Validate()
135+
{
136+
switch (AppColorMode)
137+
{
138+
case BaseTheme.Inherit:
139+
case BaseTheme.Light:
140+
case BaseTheme.Dark:
141+
break;
142+
default:
143+
throw new InvalidEnumArgumentException(nameof(AppColorMode), (int)AppColorMode, typeof(BaseTheme));
144+
}
145+
146+
switch (Backend)
147+
{
148+
case BackendTypes.None:
149+
case BackendTypes.Ytdl:
150+
case BackendTypes.Ytdlp:
151+
break;
152+
default:
153+
throw new InvalidEnumArgumentException(nameof(Backend), (int)Backend, typeof(BackendTypes));
154+
}
155+
156+
if (LoggingMaxEntries <= 0)
157+
{
158+
throw new ArgumentOutOfRangeException(nameof(LoggingMaxEntries), LoggingMaxEntries, "Max log entries must be positive.");
114159
}
115160
}
116161
}

YoutubeDl.Wpf/ViewModels/MainWindowViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
4545
_snackbarMessageQueue = snackbarMessageQueue;
4646

4747
// Configure logging.
48-
var queuedTextBoxsink = new QueuedTextBoxSink(_observableSettings);
48+
var queuedTextBoxsink = new QueuedTextBoxSink(_settings);
4949
var logger = new LoggerConfiguration()
5050
.WriteTo.Sink(queuedTextBoxsink)
5151
.CreateLogger();
@@ -64,11 +64,11 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue)
6464
SaveSettingsAsyncCommand = ReactiveCommand.CreateFromTask<CancelEventArgs?, bool>(SaveSettingsAsync);
6565
}
6666

67-
public void ControlDialog(bool open) => IsDialogOpen = open;
67+
private void ControlDialog(bool open) => IsDialogOpen = open;
6868

69-
public async Task<bool> SaveSettingsAsync(CancelEventArgs? cancelEventArgs = null, CancellationToken cancellationToken = default)
69+
private async Task<bool> SaveSettingsAsync(CancelEventArgs? cancelEventArgs = null, CancellationToken cancellationToken = default)
7070
{
71-
_observableSettings.UpdateSettings(_settings);
71+
_observableSettings.UpdateAppSettings();
7272

7373
try
7474
{

YoutubeDl.Wpf/ViewModels/SettingsViewModel.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,17 @@ public SettingsViewModel(ObservableSettings settings, BackendService backendServ
9191
.Subscribe(_ => _snackbarMessageQueue.Enqueue("Warning: Invalid proxy URL"));
9292

9393
this.WhenAnyValue(x => x.SharedSettings.LoggingMaxEntries)
94-
.Where(loggingMaxEntries => loggingMaxEntries <= 0)
95-
.Subscribe(_ =>
94+
.Subscribe(loggingMaxEntries =>
9695
{
97-
_snackbarMessageQueue.Enqueue("Warning: Max log entries must be greater than 0.");
98-
SharedSettings.LoggingMaxEntries = 1024;
96+
if (loggingMaxEntries > 0)
97+
{
98+
SharedSettings.AppSettings.LoggingMaxEntries = loggingMaxEntries;
99+
}
100+
else
101+
{
102+
_snackbarMessageQueue.Enqueue("Warning: Max log entries must be positive.");
103+
SharedSettings.LoggingMaxEntries = SharedSettings.AppSettings.LoggingMaxEntries;
104+
}
99105
});
100106

101107
// Guess the backend type from binary name.
@@ -117,7 +123,7 @@ public SettingsViewModel(ObservableSettings settings, BackendService backendServ
117123
BrowseDlBinaryCommand = ReactiveCommand.Create(BrowseDlBinary);
118124
UpdateBackendCommand = ReactiveCommand.Create(_backendService.UpdateBackend, canUpdateBackend);
119125
BrowseFfmpegBinaryCommand = ReactiveCommand.Create(BrowseFfmpegBinary);
120-
OpenUri = ReactiveCommand.Create<string>(uri => WpfHelper.OpenUri(uri));
126+
OpenUri = ReactiveCommand.Create<string>(WpfHelper.OpenUri);
121127
}
122128

123129
private void ChangeColorMode(BaseTheme colorMode)

0 commit comments

Comments
 (0)