Skip to content

Commit

Permalink
Preferences work
Browse files Browse the repository at this point in the history
  • Loading branch information
flabbet committed Nov 29, 2020
1 parent 3e73e5f commit 82a017f
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 34 deletions.
67 changes: 56 additions & 11 deletions PixiEditor/Models/UserPreferences/PreferencesSettings.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,67 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection.Metadata;
using Newtonsoft.Json;

namespace PixiEditor.Models.UserPreferences
{
public static class PreferencesSettings
{
public static bool IsLoaded { get; private set; } = false;

public static string PathToUserPreferences { get; } = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PixiEditor",
"user_preferences.json");

public static Dictionary<string, object> Preferences { get; set; } = new Dictionary<string, object>();

public static void Init()
{
if (IsLoaded == false)
{
string dir = Path.GetDirectoryName(PathToUserPreferences);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

if (!File.Exists(PathToUserPreferences))
{
File.WriteAllText(PathToUserPreferences, "{\n}");
}
else
{
string json = File.ReadAllText(PathToUserPreferences);
Preferences = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
}

IsLoaded = true;
}
}

public static void UpdatePreference(string name, object value)
{
Properties.Settings.Default.Reload();
if (Properties.Settings.Default.Properties[name] != null)
if (IsLoaded == false)
{
Properties.Settings.Default.Properties[name].DefaultValue = value;
Init();
}
else

Preferences[name] = value;

Save();
}

public static void Save()
{
if (IsLoaded == false)
{
Properties.Settings.Default.Properties.Add(new SettingsProperty(name) { DefaultValue = value });
Init();
}

Properties.Settings.Default.Save();
File.WriteAllText(PathToUserPreferences, JsonConvert.SerializeObject(Preferences));
}

#nullable enable
Expand All @@ -33,8 +73,13 @@ public static void UpdatePreference(string name, object value)

public static T? GetPreference<T>(string name, T? fallbackValue)
{
return Properties.Settings.Default.Properties[name] != null
? (T)Convert.ChangeType(Properties.Settings.Default.Properties[name].DefaultValue, typeof(T))
if (IsLoaded == false)
{
Init();
}

return Preferences.ContainsKey(name)
? (T)Preferences[name]
: fallbackValue;
}
}
Expand Down
1 change: 1 addition & 0 deletions PixiEditor/PixiEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</PackageReference>
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.8.2" />
<PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="PixiEditor.ColorPicker" Version="1.0.1" />
<PackageReference Include="System.Drawing.Common" Version="5.0.0" />
<PackageReference Include="WriteableBitmapEx">
Expand Down
12 changes: 0 additions & 12 deletions PixiEditor/Properties/Settings.Designer.cs

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

8 changes: 2 additions & 6 deletions PixiEditor/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="PixiEditor.Properties" GeneratedClassName="Settings">
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles />
<Settings>
<Setting Name="ShowNewFilePopupOnStartup" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
<Settings />
</SettingsFile>
2 changes: 2 additions & 0 deletions PixiEditor/ViewModels/NewFileMenuViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public NewFileMenuViewModel()
}

public RelayCommand OkCommand { get; set; }

public RelayCommand CloseCommand { get; set; }

public RelayCommand DragMoveCommand { get; set; }

private void OkButton(object parameter)
Expand Down
6 changes: 5 additions & 1 deletion PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using PixiEditor.Models.Dialogs;
using PixiEditor.Models.Enums;
using PixiEditor.Models.IO;
using PixiEditor.Models.UserPreferences;

namespace PixiEditor.ViewModels.SubViewModels.Main
{
Expand Down Expand Up @@ -91,7 +92,10 @@ private void Owner_OnStartupEvent(object sender, System.EventArgs e)
}
else
{
OpenNewFilePopup(null);
if (PreferencesSettings.GetPreference<bool>("ShowNewFilePopupOnStartup"))
{
OpenNewFilePopup(null);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion PixiEditor/ViewModels/ViewModelMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using PixiEditor.Models.IO;
using PixiEditor.Models.Position;
using PixiEditor.Models.Tools;
using PixiEditor.Models.UserPreferences;
using PixiEditor.ViewModels.SubViewModels.Main;

namespace PixiEditor.ViewModels
Expand Down Expand Up @@ -51,6 +52,7 @@ public class ViewModelMain : ViewModelBase
public ColorsViewModel ColorsSubViewModel { get; set; }

public DocumentViewModel DocumentSubViewModel { get; set; }

public MiscViewModel MiscSubViewModel { get; set; }

public BitmapManager BitmapManager { get; set; }
Expand All @@ -61,6 +63,8 @@ public class ViewModelMain : ViewModelBase

public ViewModelMain()
{
PreferencesSettings.Init();

BitmapManager = new BitmapManager();
BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
Expand Down Expand Up @@ -151,7 +155,7 @@ public void ResetProgramStateValues()
public bool DocumentIsNotNull(object property)
{
return BitmapManager.ActiveDocument != null;
}
}

private void CloseWindow(object property)
{
Expand Down
6 changes: 3 additions & 3 deletions PixiEditorTests/PixiEditorTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

<ItemGroup>
<PackageReference Include="Codecov" Version="1.12.3" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Moq" Version="4.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Include="OpenCover" Version="4.7.922" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down

0 comments on commit 82a017f

Please sign in to comment.