Browse Source

Added IPreferences and Service Collection to ViewModelMain

CPKreuz 4 years ago
parent
commit
9645ca513c

+ 2 - 2
PixiEditor/Models/DataHolders/Document/Document.IO.cs

@@ -65,7 +65,7 @@ namespace PixiEditor.Models.DataHolders
                 recentlyOpened.Move(index, 0);
                 recentlyOpened.Move(index, 0);
             }
             }
 
 
-            if (recentlyOpened.Count > PreferencesSettings.GetPreference("maxOpenedRecently", 10))
+            if (recentlyOpened.Count > IPreferences.Current.GetPreference("maxOpenedRecently", 10))
             {
             {
                 for (int i = 4; i < recentlyOpened.Count; i++)
                 for (int i = 4; i < recentlyOpened.Count; i++)
                 {
                 {
@@ -73,7 +73,7 @@ namespace PixiEditor.Models.DataHolders
                 }
                 }
             }
             }
 
 
-            PreferencesSettings.UpdateLocalPreference("RecentlyOpened", recentlyOpened);
+            IPreferences.Current.UpdateLocalPreference("RecentlyOpened", recentlyOpened);
 
 
             XamlAccesibleViewModel.FileSubViewModel.HasRecent = true;
             XamlAccesibleViewModel.FileSubViewModel.HasRecent = true;
         }
         }

+ 2 - 2
PixiEditor/Models/Dialogs/NewFileDialog.cs

@@ -7,9 +7,9 @@ namespace PixiEditor.Models.Dialogs
 {
 {
     public class NewFileDialog : CustomDialog
     public class NewFileDialog : CustomDialog
     {
     {
-        private int height = (int)PreferencesSettings.GetPreference("DefaultNewFileHeight", 16L);
+        private int height = (int)IPreferences.Current.GetPreference("DefaultNewFileHeight", 16L);
 
 
-        private int width = (int)PreferencesSettings.GetPreference("DefaultNewFileWidth", 16L);
+        private int width = (int)IPreferences.Current.GetPreference("DefaultNewFileWidth", 16L);
 
 
         public int Width
         public int Width
         {
         {

+ 32 - 0
PixiEditor/Models/UserPreferences/IPreferences.cs

@@ -0,0 +1,32 @@
+using System;
+using PixiEditor.ViewModels;
+
+namespace PixiEditor.Models.UserPreferences
+{
+    public interface IPreferences
+    {
+        public static IPreferences Current => ViewModelMain.Current.Preferences;
+
+        public void Save();
+
+        public void AddCallback(string setting, Action<object> action);
+
+        public void Init();
+
+        public void Init(string path, string localPath);
+
+        public void UpdatePreference<T>(string name, T value);
+
+        public void UpdateLocalPreference<T>(string name, T value);
+
+#nullable enable
+
+        public T? GetPreference<T>(string name);
+
+        public T? GetPreference<T>(string name, T? fallbackValue);
+
+        public T? GetLocalPreference<T>(string name);
+
+        public T? GetLocalPreference<T>(string name, T? fallbackValue);
+    }
+}

+ 20 - 17
PixiEditor/Models/UserPreferences/PreferencesSettings.cs

@@ -2,27 +2,30 @@
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.IO;
 using System.IO;
 using Newtonsoft.Json;
 using Newtonsoft.Json;
+using PixiEditor.ViewModels;
 
 
 namespace PixiEditor.Models.UserPreferences
 namespace PixiEditor.Models.UserPreferences
 {
 {
-    public static class PreferencesSettings
+    public class PreferencesSettings : IPreferences
     {
     {
-        public static bool IsLoaded { get; private set; } = false;
+        public static IPreferences Current => ViewModelMain.Current.Preferences;
 
 
-        public static string PathToRoamingUserPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.ApplicationData);
+        public bool IsLoaded { get; private set; } = false;
 
 
-        public static string PathToLocalPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.LocalApplicationData);
+        public string PathToRoamingUserPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.ApplicationData);
 
 
-        public static Dictionary<string, object> Preferences { get; set; } = new Dictionary<string, object>();
+        public string PathToLocalPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.LocalApplicationData);
 
 
-        public static Dictionary<string, object> LocalPreferences { get; set; } = new Dictionary<string, object>();
+        public Dictionary<string, object> Preferences { get; set; } = new Dictionary<string, object>();
 
 
-        public static void Init()
+        public Dictionary<string, object> LocalPreferences { get; set; } = new Dictionary<string, object>();
+
+        public void Init()
         {
         {
             Init(PathToRoamingUserPreferences, PathToLocalPreferences);
             Init(PathToRoamingUserPreferences, PathToLocalPreferences);
         }
         }
 
 
-        public static void Init(string path, string localPath)
+        public void Init(string path, string localPath)
         {
         {
             PathToRoamingUserPreferences = path;
             PathToRoamingUserPreferences = path;
             PathToLocalPreferences = localPath;
             PathToLocalPreferences = localPath;
@@ -36,7 +39,7 @@ namespace PixiEditor.Models.UserPreferences
             }
             }
         }
         }
 
 
-        public static void UpdatePreference<T>(string name, T value)
+        public void UpdatePreference<T>(string name, T value)
         {
         {
             if (IsLoaded == false)
             if (IsLoaded == false)
             {
             {
@@ -56,7 +59,7 @@ namespace PixiEditor.Models.UserPreferences
             Save();
             Save();
         }
         }
 
 
-        public static void UpdateLocalPreference<T>(string name, T value)
+        public void UpdateLocalPreference<T>(string name, T value)
         {
         {
             if (IsLoaded == false)
             if (IsLoaded == false)
             {
             {
@@ -76,7 +79,7 @@ namespace PixiEditor.Models.UserPreferences
             Save();
             Save();
         }
         }
 
 
-        public static void Save()
+        public void Save()
         {
         {
             if (IsLoaded == false)
             if (IsLoaded == false)
             {
             {
@@ -87,9 +90,9 @@ namespace PixiEditor.Models.UserPreferences
             File.WriteAllText(PathToLocalPreferences, JsonConvert.SerializeObject(LocalPreferences));
             File.WriteAllText(PathToLocalPreferences, JsonConvert.SerializeObject(LocalPreferences));
         }
         }
 
 
-        public static Dictionary<string, List<Action<object>>> Callbacks { get; set; } = new Dictionary<string, List<Action<object>>>();
+        public Dictionary<string, List<Action<object>>> Callbacks { get; set; } = new Dictionary<string, List<Action<object>>>();
 
 
-        public static void AddCallback(string setting, Action<object> action)
+        public void AddCallback(string setting, Action<object> action)
         {
         {
             if (Callbacks.ContainsKey(setting))
             if (Callbacks.ContainsKey(setting))
             {
             {
@@ -102,12 +105,12 @@ namespace PixiEditor.Models.UserPreferences
 
 
 #nullable enable
 #nullable enable
 
 
-        public static T? GetPreference<T>(string name)
+        public T? GetPreference<T>(string name)
         {
         {
             return GetPreference(name, default(T));
             return GetPreference(name, default(T));
         }
         }
 
 
-        public static T? GetPreference<T>(string name, T? fallbackValue)
+        public T? GetPreference<T>(string name, T? fallbackValue)
         {
         {
             if (IsLoaded == false)
             if (IsLoaded == false)
             {
             {
@@ -119,12 +122,12 @@ namespace PixiEditor.Models.UserPreferences
                 : fallbackValue;
                 : fallbackValue;
         }
         }
 
 
-        public static T? GetLocalPreference<T>(string name)
+        public T? GetLocalPreference<T>(string name)
         {
         {
             return GetPreference(name, default(T));
             return GetPreference(name, default(T));
         }
         }
 
 
-        public static T? GetLocalPreference<T>(string name, T? fallbackValue)
+        public T? GetLocalPreference<T>(string name, T? fallbackValue)
         {
         {
             if (IsLoaded == false)
             if (IsLoaded == false)
             {
             {

+ 1 - 0
PixiEditor/PixiEditor.csproj

@@ -59,6 +59,7 @@
       <Version>1.0.2</Version>
       <Version>1.0.2</Version>
     </PackageReference>
     </PackageReference>
     <PackageReference Include="Extended.Wpf.Toolkit" Version="3.8.2" />
     <PackageReference Include="Extended.Wpf.Toolkit" Version="3.8.2" />
+    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
     <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
     <PackageReference Include="MvvmLightLibs" Version="5.4.1.1" />
     <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
     <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
     <PackageReference Include="PixiEditor.ColorPicker" Version="2.0.0" />
     <PackageReference Include="PixiEditor.ColorPicker" Version="2.0.0" />

+ 8 - 8
PixiEditor/ViewModels/SubViewModels/Main/DiscordViewModel.cs

@@ -30,7 +30,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             }
         }
         }
 
 
-        private bool showDocumentName = PreferencesSettings.GetPreference(nameof(ShowDocumentName), true);
+        private bool showDocumentName = IPreferences.Current.GetPreference(nameof(ShowDocumentName), true);
 
 
         public bool ShowDocumentName
         public bool ShowDocumentName
         {
         {
@@ -45,7 +45,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             }
         }
         }
 
 
-        private bool showDocumentSize = PreferencesSettings.GetPreference(nameof(ShowDocumentSize), true);
+        private bool showDocumentSize = IPreferences.Current.GetPreference(nameof(ShowDocumentSize), true);
 
 
         public bool ShowDocumentSize
         public bool ShowDocumentSize
         {
         {
@@ -60,7 +60,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             }
         }
         }
 
 
-        private bool showLayerCount = PreferencesSettings.GetPreference(nameof(ShowLayerCount), true);
+        private bool showLayerCount = IPreferences.Current.GetPreference(nameof(ShowLayerCount), true);
 
 
         public bool ShowLayerCount
         public bool ShowLayerCount
         {
         {
@@ -81,11 +81,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             Owner.BitmapManager.DocumentChanged += DocumentChanged;
             Owner.BitmapManager.DocumentChanged += DocumentChanged;
             this.clientId = clientId;
             this.clientId = clientId;
 
 
-            Enabled = PreferencesSettings.GetPreference<bool>("EnableRichPresence");
-            PreferencesSettings.AddCallback("EnableRichPresence", x => Enabled = (bool)x);
-            PreferencesSettings.AddCallback(nameof(ShowDocumentName), x => ShowDocumentName = (bool)x);
-            PreferencesSettings.AddCallback(nameof(ShowDocumentSize), x => ShowDocumentSize = (bool)x);
-            PreferencesSettings.AddCallback(nameof(ShowLayerCount), x => ShowLayerCount = (bool)x);
+            Enabled = IPreferences.Current.GetPreference<bool>("EnableRichPresence");
+            IPreferences.Current.AddCallback("EnableRichPresence", x => Enabled = (bool)x);
+            IPreferences.Current.AddCallback(nameof(ShowDocumentName), x => ShowDocumentName = (bool)x);
+            IPreferences.Current.AddCallback(nameof(ShowDocumentSize), x => ShowDocumentSize = (bool)x);
+            IPreferences.Current.AddCallback(nameof(ShowLayerCount), x => ShowLayerCount = (bool)x);
 
 
             AppDomain.CurrentDomain.ProcessExit += (_, _) => Enabled = false;
             AppDomain.CurrentDomain.ProcessExit += (_, _) => Enabled = false;
         }
         }

+ 2 - 2
PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -54,7 +54,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             ExportFileCommand = new RelayCommand(ExportFile, CanSave);
             ExportFileCommand = new RelayCommand(ExportFile, CanSave);
             OpenRecentCommand = new RelayCommand(OpenRecent);
             OpenRecentCommand = new RelayCommand(OpenRecent);
             Owner.OnStartupEvent += Owner_OnStartupEvent;
             Owner.OnStartupEvent += Owner_OnStartupEvent;
-            RecentlyOpened = new ObservableCollection<string>(PreferencesSettings.GetLocalPreference<JArray>(nameof(RecentlyOpened), new JArray()).ToObject<string[]>());
+            RecentlyOpened = new ObservableCollection<string>(IPreferences.Current.GetLocalPreference<JArray>(nameof(RecentlyOpened), new JArray()).ToObject<string[]>());
 
 
             if (RecentlyOpened.Count > 0)
             if (RecentlyOpened.Count > 0)
             {
             {
@@ -138,7 +138,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             }
             }
             else
             else
             {
             {
-                if (PreferencesSettings.GetPreference("ShowNewFilePopupOnStartup", true))
+                if (IPreferences.Current.GetPreference("ShowNewFilePopupOnStartup", true))
                 {
                 {
                     OpenNewFilePopup(null);
                     OpenNewFilePopup(null);
                 }
                 }

+ 1 - 1
PixiEditor/ViewModels/SubViewModels/Main/UpdateViewModel.cs

@@ -86,7 +86,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
         private async void Owner_OnStartupEvent(object sender, EventArgs e)
         private async void Owner_OnStartupEvent(object sender, EventArgs e)
         {
         {
-            if (PreferencesSettings.GetPreference("CheckUpdatesOnStartup", true))
+            if (IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true))
             {
             {
                 await CheckForUpdate();
                 await CheckForUpdate();
             }
             }

+ 3 - 3
PixiEditor/ViewModels/SubViewModels/UserPreferences/SettingsGroup.cs

@@ -8,14 +8,14 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
     {
     {
         protected static T GetPreference<T>(string name)
         protected static T GetPreference<T>(string name)
         {
         {
-            return PreferencesSettings.GetPreference<T>(name);
+            return IPreferences.Current.GetPreference<T>(name);
         }
         }
 
 
 #nullable enable
 #nullable enable
 
 
         protected static T? GetPreference<T>(string name, T? fallbackValue)
         protected static T? GetPreference<T>(string name, T? fallbackValue)
         {
         {
-            return PreferencesSettings.GetPreference(name, fallbackValue);
+            return IPreferences.Current.GetPreference(name, fallbackValue);
         }
         }
 
 
 #nullable disable
 #nullable disable
@@ -23,7 +23,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
         protected void RaiseAndUpdatePreference<T>(string name, T value)
         protected void RaiseAndUpdatePreference<T>(string name, T value)
         {
         {
             RaisePropertyChanged(name);
             RaisePropertyChanged(name);
-            PreferencesSettings.UpdatePreference(name, value);
+            IPreferences.Current.UpdatePreference(name, value);
         }
         }
     }
     }
 }
 }

+ 5 - 5
PixiEditor/ViewModels/SubViewModels/UserPreferences/SettingsViewModel.cs

@@ -8,7 +8,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
     {
     {
         public class FileSettings : SettingsGroup
         public class FileSettings : SettingsGroup
         {
         {
-            private bool showNewFilePopupOnStartup = PreferencesSettings.GetPreference("ShowNewFilePopupOnStartup", true);
+            private bool showNewFilePopupOnStartup = IPreferences.Current.GetPreference("ShowNewFilePopupOnStartup", true);
 
 
             public bool ShowNewFilePopupOnStartup
             public bool ShowNewFilePopupOnStartup
             {
             {
@@ -21,7 +21,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
                 }
                 }
             }
             }
 
 
-            private long defaultNewFileWidth = (int)PreferencesSettings.GetPreference("DefaultNewFileWidth", 16L);
+            private long defaultNewFileWidth = (int)IPreferences.Current.GetPreference("DefaultNewFileWidth", 16L);
 
 
             public long DefaultNewFileWidth
             public long DefaultNewFileWidth
             {
             {
@@ -34,7 +34,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
                 }
                 }
             }
             }
 
 
-            private long defaultNewFileHeight = (int)PreferencesSettings.GetPreference("DefaultNewFileHeight", 16L);
+            private long defaultNewFileHeight = (int)IPreferences.Current.GetPreference("DefaultNewFileHeight", 16L);
 
 
             public long DefaultNewFileHeight
             public long DefaultNewFileHeight
             {
             {
@@ -47,7 +47,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
                 }
                 }
             }
             }
 
 
-            private int maxOpenedRecently = (int)PreferencesSettings.GetPreference(nameof(MaxOpenedRecently), 10);
+            private int maxOpenedRecently = (int)IPreferences.Current.GetPreference(nameof(MaxOpenedRecently), 10);
 
 
             public int MaxOpenedRecently
             public int MaxOpenedRecently
             {
             {
@@ -62,7 +62,7 @@ namespace PixiEditor.ViewModels.SubViewModels.UserPreferences
 
 
         public class UpdateSettings : SettingsGroup
         public class UpdateSettings : SettingsGroup
         {
         {
-            private bool checkUpdatesOnStartup = PreferencesSettings.GetPreference("CheckUpdatesOnStartup", true);
+            private bool checkUpdatesOnStartup = IPreferences.Current.GetPreference("CheckUpdatesOnStartup", true);
 
 
             public bool CheckUpdatesOnStartup
             public bool CheckUpdatesOnStartup
             {
             {

+ 9 - 3
PixiEditor/ViewModels/ViewModelMain.cs

@@ -4,6 +4,7 @@ using System.ComponentModel;
 using System.Linq;
 using System.Linq;
 using System.Windows;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
+using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Controllers.Shortcuts;
 using PixiEditor.Models.Controllers.Shortcuts;
@@ -64,9 +65,15 @@ namespace PixiEditor.ViewModels
 
 
         public ShortcutController ShortcutController { get; set; }
         public ShortcutController ShortcutController { get; set; }
 
 
-        public ViewModelMain()
+        public IPreferences Preferences { get; set; }
+
+        public ViewModelMain(IServiceProvider services)
         {
         {
-            PreferencesSettings.Init();
+            Current = this;
+
+            Preferences = services.GetRequiredService<IPreferences>();
+
+            Preferences.Init();
 
 
             BitmapManager = new BitmapManager();
             BitmapManager = new BitmapManager();
             BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
             BitmapManager.BitmapOperations.BitmapChanged += BitmapUtility_BitmapChanged;
@@ -141,7 +148,6 @@ namespace PixiEditor.ViewModels
                 }
                 }
             };
             };
             BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;
             BitmapManager.PrimaryColor = ColorsSubViewModel.PrimaryColor;
-            Current = this;
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 2 - 2
PixiEditor/Views/MainWindow.xaml

@@ -14,7 +14,7 @@
         xmlns:avalonDockTheme="clr-namespace:PixiEditor.Styles.AvalonDock"
         xmlns:avalonDockTheme="clr-namespace:PixiEditor.Styles.AvalonDock"
         mc:Ignorable="d" WindowStyle="None" Initialized="MainWindow_Initialized"
         mc:Ignorable="d" WindowStyle="None" Initialized="MainWindow_Initialized"
         Title="PixiEditor" Name="mainWindow" Height="1000" Width="1600" Background="{StaticResource MainColor}"
         Title="PixiEditor" Name="mainWindow" Height="1000" Width="1600" Background="{StaticResource MainColor}"
-        WindowStartupLocation="CenterScreen" WindowState="Maximized" DataContext="{DynamicResource ViewModelMain}">
+        WindowStartupLocation="CenterScreen" WindowState="Maximized">
     <WindowChrome.WindowChrome>
     <WindowChrome.WindowChrome>
         <WindowChrome CaptionHeight="32"
         <WindowChrome CaptionHeight="32"
                       ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}" />
                       ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}" />
@@ -22,7 +22,7 @@
 
 
     <Window.Resources>
     <Window.Resources>
         <ResourceDictionary>
         <ResourceDictionary>
-            <vm:ViewModelMain x:Key="ViewModelMain" />
+            <!--<vm:ViewModelMain x:Key="ViewModelMain" />-->
             <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
             <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
             <converters:BoolToIntConverter x:Key="BoolToIntConverter" />
             <converters:BoolToIntConverter x:Key="BoolToIntConverter" />
             <converters:FloatNormalizeConverter x:Key="FloatNormalizeConverter" />
             <converters:FloatNormalizeConverter x:Key="FloatNormalizeConverter" />

+ 8 - 0
PixiEditor/Views/MainWindow.xaml.cs

@@ -5,9 +5,11 @@ using System.IO;
 using System.Reflection;
 using System.Reflection;
 using System.Windows;
 using System.Windows;
 using System.Windows.Input;
 using System.Windows.Input;
+using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Processes;
 using PixiEditor.Models.Processes;
+using PixiEditor.Models.UserPreferences;
 using PixiEditor.UpdateModule;
 using PixiEditor.UpdateModule;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 
 
@@ -23,6 +25,12 @@ namespace PixiEditor
         public MainWindow()
         public MainWindow()
         {
         {
             InitializeComponent();
             InitializeComponent();
+
+            IServiceCollection services = new ServiceCollection()
+                .AddSingleton<IPreferences>(new PreferencesSettings());
+
+            DataContext = new ViewModelMain(services.BuildServiceProvider());
+
             StateChanged += MainWindowStateChangeRaised;
             StateChanged += MainWindowStateChangeRaised;
             MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
             MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
             viewModel = (ViewModelMain)DataContext;
             viewModel = (ViewModelMain)DataContext;

+ 56 - 0
PixiEditorTests/Mocks/PreferenceSettingsMock.cs

@@ -0,0 +1,56 @@
+using System;
+using PixiEditor.Models.UserPreferences;
+
+namespace PixiEditorTests.Mocks
+{
+    public class PreferenceSettingsMock : IPreferences
+    {
+        public void AddCallback(string setting, Action<object> action)
+        {
+        }
+
+#nullable enable
+
+        public T? GetLocalPreference<T>(string name)
+        {
+            return default;
+        }
+
+        public T? GetLocalPreference<T>(string name, T? fallbackValue)
+        {
+            return fallbackValue;
+        }
+
+        public T? GetPreference<T>(string name)
+        {
+            return default;
+        }
+
+        public T? GetPreference<T>(string name, T? fallbackValue)
+        {
+            return fallbackValue;
+        }
+
+#nullable disable
+
+        public void Init()
+        {
+        }
+
+        public void Init(string path, string localPath)
+        {
+        }
+
+        public void Save()
+        {
+        }
+
+        public void UpdateLocalPreference<T>(string name, T value)
+        {
+        }
+
+        public void UpdatePreference<T>(string name, T value)
+        {
+        }
+    }
+}

+ 4 - 2
PixiEditorTests/ModelsTests/ToolsTests/ZoomToolTests.cs

@@ -1,4 +1,6 @@
-using PixiEditor.Models.Tools.Tools;
+using Microsoft.Extensions.DependencyInjection;
+using PixiEditor.Models.Tools.Tools;
+using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 using Xunit;
 using Xunit;
 
 
@@ -10,7 +12,7 @@ namespace PixiEditorTests.ModelsTests.ToolsTests
         [StaFact]
         [StaFact]
         public void TestThatZoomSetsActiveDocumentZoomPercentage()
         public void TestThatZoomSetsActiveDocumentZoomPercentage()
         {
         {
-            ViewModelMain vm = new ViewModelMain();
+            ViewModelMain vm = new ViewModelMain(new ServiceCollection().AddSingleton<IPreferences>(new Mocks.PreferenceSettingsMock()).BuildServiceProvider());
             vm.BitmapManager.ActiveDocument = new PixiEditor.Models.DataHolders.Document(10, 10);
             vm.BitmapManager.ActiveDocument = new PixiEditor.Models.DataHolders.Document(10, 10);
             ZoomTool zoomTool = new ZoomTool();
             ZoomTool zoomTool = new ZoomTool();
             double zoom = 110;
             double zoom = 110;

+ 2 - 0
PixiEditorTests/ModelsTests/UserPreferencesTests/PreferencesSettingsTests.cs

@@ -12,6 +12,8 @@ namespace PixiEditorTests.ModelsTests.UserPreferencesTests
 
 
         public static string PathToLocalPreferencesFile { get; } = Path.Join("PixiEditor", "local_test_preferences.json");
         public static string PathToLocalPreferencesFile { get; } = Path.Join("PixiEditor", "local_test_preferences.json");
 
 
+        public static readonly PreferencesSettings PreferencesSettings = new PreferencesSettings();
+
         public PreferencesSettingsTests()
         public PreferencesSettingsTests()
         {
         {
             PreferencesSettings.Init(PathToPreferencesFile, PathToLocalPreferencesFile);
             PreferencesSettings.Init(PathToPreferencesFile, PathToLocalPreferencesFile);

+ 1 - 0
PixiEditorTests/PixiEditorTests.csproj

@@ -23,6 +23,7 @@
       <PrivateAssets>all</PrivateAssets>
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
     </PackageReference>
     </PackageReference>
+    <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
     <PackageReference Include="Moq" Version="4.16.0" />
     <PackageReference Include="Moq" Version="4.16.0" />
     <PackageReference Include="OpenCover" Version="4.7.922" />
     <PackageReference Include="OpenCover" Version="4.7.922" />

+ 24 - 12
PixiEditorTests/ViewModelsTests/ViewModelMainTests.cs

@@ -1,12 +1,15 @@
-using System.IO;
+using System;
+using System.IO;
 using System.Windows.Input;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media;
+using Microsoft.Extensions.DependencyInjection;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.IO;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Position;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools;
 using PixiEditor.Models.Tools.Tools;
 using PixiEditor.Models.Tools.Tools;
+using PixiEditor.Models.UserPreferences;
 using PixiEditor.ViewModels;
 using PixiEditor.ViewModels;
 using Xunit;
 using Xunit;
 
 
@@ -15,10 +18,19 @@ namespace PixiEditorTests.ViewModelsTests
     [Collection("Application collection")]
     [Collection("Application collection")]
     public class ViewModelMainTests
     public class ViewModelMainTests
     {
     {
+        public static IServiceProvider Services;
+
+        public ViewModelMainTests()
+        {
+            Services = new ServiceCollection()
+                .AddSingleton<IPreferences>(new Mocks.PreferenceSettingsMock())
+                .BuildServiceProvider();
+        }
+
         [StaFact]
         [StaFact]
         public void TestThatConstructorSetsUpControllersCorrectly()
         public void TestThatConstructorSetsUpControllersCorrectly()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
 
 
             Assert.NotNull(viewModel.ChangesController);
             Assert.NotNull(viewModel.ChangesController);
             Assert.NotNull(viewModel.ShortcutController);
             Assert.NotNull(viewModel.ShortcutController);
@@ -30,7 +42,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatSwapColorsCommandSwapsColors()
         public void TestThatSwapColorsCommandSwapsColors()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
 
 
             viewModel.ColorsSubViewModel.PrimaryColor = Colors.Black;
             viewModel.ColorsSubViewModel.PrimaryColor = Colors.Black;
             viewModel.ColorsSubViewModel.SecondaryColor = Colors.White;
             viewModel.ColorsSubViewModel.SecondaryColor = Colors.White;
@@ -44,7 +56,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatNewDocumentCreatesNewDocumentWithBaseLayer()
         public void TestThatNewDocumentCreatesNewDocumentWithBaseLayer()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
 
 
             viewModel.FileSubViewModel.NewDocument(5, 5);
             viewModel.FileSubViewModel.NewDocument(5, 5);
 
 
@@ -55,7 +67,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatMouseMoveCommandUpdatesCurrentCoordinates()
         public void TestThatMouseMoveCommandUpdatesCurrentCoordinates()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
             viewModel.BitmapManager.ActiveDocument = new Document(10, 10);
             viewModel.BitmapManager.ActiveDocument = new Document(10, 10);
 
 
             Assert.Equal(new Coordinates(0, 0), MousePositionConverter.CurrentCoordinates);
             Assert.Equal(new Coordinates(0, 0), MousePositionConverter.CurrentCoordinates);
@@ -71,7 +83,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatSelectToolCommandSelectsNewTool()
         public void TestThatSelectToolCommandSelectsNewTool()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
 
 
             Assert.Equal(typeof(MoveTool), viewModel.BitmapManager.SelectedTool.GetType());
             Assert.Equal(typeof(MoveTool), viewModel.BitmapManager.SelectedTool.GetType());
 
 
@@ -83,7 +95,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatMouseUpCommandStopsRecordingMouseMovements()
         public void TestThatMouseUpCommandStopsRecordingMouseMovements()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
 
 
             viewModel.BitmapManager.MouseController.StartRecordingMouseMovementChanges(true);
             viewModel.BitmapManager.MouseController.StartRecordingMouseMovementChanges(true);
 
 
@@ -97,7 +109,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatNewLayerCommandCreatesNewLayer()
         public void TestThatNewLayerCommandCreatesNewLayer()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
 
 
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
 
 
@@ -111,7 +123,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatSaveDocumentCommandSavesFile()
         public void TestThatSaveDocumentCommandSavesFile()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
             string fileName = "testFile.pixi";
             string fileName = "testFile.pixi";
 
 
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1)
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1)
@@ -129,7 +141,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatAddSwatchAddsNonDuplicateSwatch()
         public void TestThatAddSwatchAddsNonDuplicateSwatch()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
 
 
             viewModel.ColorsSubViewModel.AddSwatch(Colors.Green);
             viewModel.ColorsSubViewModel.AddSwatch(Colors.Green);
@@ -149,7 +161,7 @@ namespace PixiEditorTests.ViewModelsTests
         [InlineData(120, 150)]
         [InlineData(120, 150)]
         public void TestThatSelectAllCommandSelectsWholeDocument(int docWidth, int docHeight)
         public void TestThatSelectAllCommandSelectsWholeDocument(int docWidth, int docHeight)
         {
         {
-            ViewModelMain viewModel = new ViewModelMain
+            ViewModelMain viewModel = new ViewModelMain(Services)
             {
             {
                 BitmapManager = { ActiveDocument = new Document(docWidth, docHeight) }
                 BitmapManager = { ActiveDocument = new Document(docWidth, docHeight) }
             };
             };
@@ -165,7 +177,7 @@ namespace PixiEditorTests.ViewModelsTests
         [StaFact]
         [StaFact]
         public void TestThatDocumentIsNotNullReturnsTrue()
         public void TestThatDocumentIsNotNullReturnsTrue()
         {
         {
-            ViewModelMain viewModel = new ViewModelMain();
+            ViewModelMain viewModel = new ViewModelMain(Services);
 
 
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);
             viewModel.BitmapManager.ActiveDocument = new Document(1, 1);