Skip to content

Commit

Permalink
Added support for saving settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dusrdev committed Oct 18, 2022
1 parent 8120d07 commit 68e461d
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 4 deletions.
18 changes: 18 additions & 0 deletions PdfTool/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="PdfTool.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<PdfTool.Properties.Settings>
<setting name="ImageConversionMode" serializeAs="String">
<value>0</value>
</setting>
<setting name="PdfAction" serializeAs="String">
<value>0</value>
</setting>
</PdfTool.Properties.Settings>
</userSettings>
</configuration>
9 changes: 9 additions & 0 deletions PdfTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
_settings = new AppSettings();
SyncronizeFromSettings();
_imageConverter = new ImageToPdfConverter(_settings);
_merger = new PdfMerger(_settings);
_splitter = new PdfSplitter();
}

/// <summary>
/// Syncronizes UI with settings
/// </summary>
private void SyncronizeFromSettings() {
SliderImageConvertMode.Value = (int)_settings.ConversionMode;
SliderMode.Value = (int)_settings.Action;
}

/// <summary>
/// Merges multiple files to one file
/// </summary>
Expand Down
28 changes: 25 additions & 3 deletions PdfTool/Models/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
namespace PdfTool.Models;

internal sealed record AppSettings {
public ImageConversionMode ConversionMode { get; set; }
internal sealed class AppSettings {
private ImageConversionMode _conversionMode;
private PdfAction _action;

public PdfAction Action { get; set; }
public ImageConversionMode ConversionMode {
get => _conversionMode;
set {
_conversionMode = value;
Properties.Settings.Default[nameof(ImageConversionMode)] = (int)value;
Properties.Settings.Default.Save();
}
}

public PdfAction Action {
get => _action;
set {
_action = value;
Properties.Settings.Default[nameof(PdfAction)] = (int)value;
Properties.Settings.Default.Save();
}
}

public string MergedFilename { get; set; } = "Merged";

public AppSettings() {
_conversionMode = (ImageConversionMode)Properties.Settings.Default[nameof(ImageConversionMode)];
_action = (PdfAction)Properties.Settings.Default[nameof(PdfAction)];
}
}
1 change: 0 additions & 1 deletion PdfTool/Models/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// <summary>
/// A result container with message and status
/// </summary>
/// <typeparam name="T">Type of inner value</typeparam>
internal readonly record struct Result {
/// <summary>
/// Whether the operation was successful
Expand Down
15 changes: 15 additions & 0 deletions PdfTool/PdfTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>

</Project>
50 changes: 50 additions & 0 deletions PdfTool/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions PdfTool/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="PdfTool.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="ImageConversionMode" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="PdfAction" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>

0 comments on commit 68e461d

Please sign in to comment.