Skip to content

Commit

Permalink
Use compile-time serialization for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed May 22, 2024
1 parent 0c6e7eb commit 0b5daaa
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 21 deletions.
91 changes: 74 additions & 17 deletions YoutubeDownloader/Services/SettingsService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Text.Json;
Expand All @@ -13,48 +12,100 @@

namespace YoutubeDownloader.Services;

// Can't use [ObservableProperty] here because System.Text.Json's source generator doesn't see
// the generated properties.
[INotifyPropertyChanged]
public partial class SettingsService()
: SettingsBase(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings.dat"))
: SettingsBase(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings.dat"),
SerializerContext.Default
)
{
[ObservableProperty]
private bool _isUkraineSupportMessageEnabled = true;
public bool IsUkraineSupportMessageEnabled
{
get => _isUkraineSupportMessageEnabled;
set => SetProperty(ref _isUkraineSupportMessageEnabled, value);
}

[ObservableProperty]
private ThemeVariant _theme;
public ThemeVariant Theme
{
get => _theme;
set => SetProperty(ref _theme, value);
}

[ObservableProperty]
private bool _isAutoUpdateEnabled = true;
public bool IsAutoUpdateEnabled
{
get => _isAutoUpdateEnabled;
set => SetProperty(ref _isAutoUpdateEnabled, value);
}

[ObservableProperty]
private bool _isAuthPersisted = true;
public bool IsAuthPersisted
{
get => _isAuthPersisted;
set => SetProperty(ref _isAuthPersisted, value);
}

[ObservableProperty]
private bool _shouldInjectSubtitles = true;
public bool ShouldInjectSubtitles
{
get => _shouldInjectSubtitles;
set => SetProperty(ref _shouldInjectSubtitles, value);
}

[ObservableProperty]
private bool _shouldInjectTags = true;
public bool ShouldInjectTags
{
get => _shouldInjectTags;
set => SetProperty(ref _shouldInjectTags, value);
}

[ObservableProperty]
private bool _shouldSkipExistingFiles;
public bool ShouldSkipExistingFiles
{
get => _shouldSkipExistingFiles;
set => SetProperty(ref _shouldSkipExistingFiles, value);
}

[ObservableProperty]
private string _fileNameTemplate = "$title";
public string FileNameTemplate
{
get => _fileNameTemplate;
set => SetProperty(ref _fileNameTemplate, value);
}

[ObservableProperty]
private int _parallelLimit = 2;
public int ParallelLimit
{
get => _parallelLimit;
set => SetProperty(ref _parallelLimit, value);
}

[ObservableProperty]
private IReadOnlyList<Cookie>? _lastAuthCookies;
public IReadOnlyList<Cookie>? LastAuthCookies
{
get => _lastAuthCookies;
set => SetProperty(ref _lastAuthCookies, value);
}

[ObservableProperty]
[property: JsonConverter(typeof(ContainerJsonConverter))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(Container))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(ContainerJsonConverter))]
private Container _lastContainer = Container.Mp4;

[ObservableProperty]
[JsonConverter(typeof(ContainerJsonConverter))]
public Container LastContainer
{
get => _lastContainer;
set => SetProperty(ref _lastContainer, value);
}

private VideoQualityPreference _lastVideoQualityPreference = VideoQualityPreference.Highest;
public VideoQualityPreference LastVideoQualityPreference
{
get => _lastVideoQualityPreference;
set => SetProperty(ref _lastVideoQualityPreference, value);
}

public override void Save()
{
Expand Down Expand Up @@ -117,3 +168,9 @@ JsonSerializerOptions options
}
}
}

public partial class SettingsService
{
[JsonSerializable(typeof(SettingsService))]
private partial class SerializerContext : JsonSerializerContext;
}
11 changes: 7 additions & 4 deletions YoutubeDownloader/YoutubeDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationIcon>..\favicon.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<PublishTrimmed>true</PublishTrimmed>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
<NoWarn>$(NoWarn);IL2104</NoWarn>
</PropertyGroup>

Expand All @@ -19,6 +16,12 @@
<NoWarn>$(NoWarn);IL2036</NoWarn>
</PropertyGroup>

<!-- Required by Avalonia.WebView -->
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
</PropertyGroup>

<!-- Avalonia.WebView is completely incompatible with trimming -->
<ItemGroup>
<TrimmerRootAssembly Include="Avalonia.WebView" />
Expand All @@ -43,7 +46,7 @@
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.10" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.10" />
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.10" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Cogwheel" Version="2.0.4" />
<PackageReference Include="Cogwheel" Version="2.1.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
<PackageReference Include="Deorcify" Version="1.0.2" PrivateAssets="all" />
Expand Down

0 comments on commit 0b5daaa

Please sign in to comment.