Browse Source

Preferences work

flabbet 4 years ago
parent
commit
82a017fe42

+ 56 - 11
PixiEditor/Models/UserPreferences/PreferencesSettings.cs

@@ -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
@@ -33,8 +73,13 @@ namespace PixiEditor.Models.UserPreferences
 
         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;
         }
     }

+ 1 - 0
PixiEditor/PixiEditor.csproj

@@ -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">

+ 0 - 12
PixiEditor/Properties/Settings.Designer.cs

@@ -22,17 +22,5 @@ namespace PixiEditor.Properties {
                 return defaultInstance;
             }
         }
-        
-        [global::System.Configuration.UserScopedSettingAttribute()]
-        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
-        [global::System.Configuration.DefaultSettingValueAttribute("True")]
-        public bool ShowNewFilePopupOnStartup {
-            get {
-                return ((bool)(this["ShowNewFilePopupOnStartup"]));
-            }
-            set {
-                this["ShowNewFilePopupOnStartup"] = value;
-            }
-        }
     }
 }

+ 2 - 6
PixiEditor/Properties/Settings.settings

@@ -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 - 0
PixiEditor/ViewModels/NewFileMenuViewModel.cs

@@ -13,7 +13,9 @@ namespace PixiEditor.ViewModels
         }
 
         public RelayCommand OkCommand { get; set; }
+
         public RelayCommand CloseCommand { get; set; }
+
         public RelayCommand DragMoveCommand { get; set; }
 
         private void OkButton(object parameter)

+ 5 - 1
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -10,6 +10,7 @@ using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.IO;
+using PixiEditor.Models.UserPreferences;
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
@@ -91,7 +92,10 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             else
             {
-                OpenNewFilePopup(null);
+                if (PreferencesSettings.GetPreference<bool>("ShowNewFilePopupOnStartup"))
+                {
+                    OpenNewFilePopup(null);
+                }
             }
         }
 

+ 5 - 1
PixiEditor/ViewModels/ViewModelMain.cs

@@ -14,6 +14,7 @@ using PixiEditor.Models.Events;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
+using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels.SubViewModels.Main;
 
 namespace PixiEditor.ViewModels
@@ -51,6 +52,7 @@ namespace PixiEditor.ViewModels
         public ColorsViewModel ColorsSubViewModel { get; set; }
 
         public DocumentViewModel DocumentSubViewModel { get; set; }
+
         public MiscViewModel MiscSubViewModel { get; set; }
 
         public BitmapManager BitmapManager { get; set; }
@@ -61,6 +63,8 @@ namespace PixiEditor.ViewModels
 
         public ViewModelMain()
         {
+            PreferencesSettings.Init();
+
             BitmapManager = new BitmapManager();
             BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
             BitmapManager.MouseController.StoppedRecordingChanges += MouseController_StoppedRecordingChanges;
@@ -151,7 +155,7 @@ namespace PixiEditor.ViewModels
         public bool DocumentIsNotNull(object property)
         {
             return BitmapManager.ActiveDocument != null;
-        }        
+        }
 
         private void CloseWindow(object property)
         {

+ 3 - 3
PixiEditorTests/PixiEditorTests.csproj

@@ -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">