Browse Source

Changed user preference path to roaming and added local user preference

CPKreuz 4 years ago
parent
commit
d2f923aeb1

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

@@ -67,8 +67,7 @@ namespace PixiEditor.Models.DataHolders
                 }
             }
 
-            UserPreferences.PreferencesSettings.UpdatePreference("RecentlyOpened", recentlyOpened);
+            UserPreferences.PreferencesSettings.UpdateLocalPreference("RecentlyOpened", recentlyOpened);
         }
-
     }
 }

+ 85 - 25
PixiEditor/Models/UserPreferences/PreferencesSettings.cs

@@ -9,38 +9,28 @@ namespace PixiEditor.Models.UserPreferences
     {
         public static bool IsLoaded { get; private set; } = false;
 
-        public static string PathToUserPreferences { get; private set; } = Path.Join(
-            Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
-            "PixiEditor",
-            "user_preferences.json");
+        public static string PathToRoamingUserPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.ApplicationData);
+
+        public static string PathToLocalPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.LocalApplicationData);
 
         public static Dictionary<string, object> Preferences { get; set; } = new Dictionary<string, object>();
 
+        public static Dictionary<string, object> LocalPreferences { get; set; } = new Dictionary<string, object>();
+
         public static void Init()
         {
-            Init(PathToUserPreferences);
+            Init(PathToRoamingUserPreferences, PathToLocalPreferences);
         }
 
-        public static void Init(string path)
+        public static void Init(string path, string localPath)
         {
-            PathToUserPreferences = path;
+            PathToRoamingUserPreferences = path;
+            PathToLocalPreferences = localPath;
+
             if (IsLoaded == false)
             {
-                string dir = Path.GetDirectoryName(path);
-                if (!Directory.Exists(dir))
-                {
-                    Directory.CreateDirectory(dir);
-                }
-
-                if (!File.Exists(path))
-                {
-                    File.WriteAllText(path, "{\n}");
-                }
-                else
-                {
-                    string json = File.ReadAllText(path);
-                    Preferences = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
-                }
+                Preferences = InitPath(path);
+                LocalPreferences = InitPath(localPath);
 
                 IsLoaded = true;
             }
@@ -66,17 +56,36 @@ namespace PixiEditor.Models.UserPreferences
             Save();
         }
 
-        public static void Save()
+        public static void UpdateLocalPreference<T>(string name, T value)
         {
             if (IsLoaded == false)
             {
                 Init();
             }
 
-            File.WriteAllText(PathToUserPreferences, JsonConvert.SerializeObject(Preferences));
+            LocalPreferences[name] = value;
+
+            if (Callbacks.ContainsKey(name))
+            {
+                foreach (var action in Callbacks[name])
+                {
+                    action.Invoke(value);
+                }
+            }
+
+            Save();
         }
 
-#nullable enable
+        public static void Save()
+        {
+            if (IsLoaded == false)
+            {
+                Init();
+            }
+
+            File.WriteAllText(PathToRoamingUserPreferences, JsonConvert.SerializeObject(Preferences));
+            File.WriteAllText(PathToLocalPreferences, JsonConvert.SerializeObject(LocalPreferences));
+        }
 
         public static Dictionary<string, List<Action<object>>> Callbacks { get; set; } = new Dictionary<string, List<Action<object>>>();
 
@@ -91,6 +100,8 @@ namespace PixiEditor.Models.UserPreferences
             Callbacks.Add(setting, new List<Action<object>>() { action });
         }
 
+#nullable enable
+
         public static T? GetPreference<T>(string name)
         {
             return GetPreference(name, default(T));
@@ -107,5 +118,54 @@ namespace PixiEditor.Models.UserPreferences
                 ? (T)Preferences[name]
                 : fallbackValue;
         }
+
+        public static T? GetLocalPreference<T>(string name)
+        {
+            return GetPreference(name, default(T));
+        }
+
+        public static T? GetLocalPreference<T>(string name, T? fallbackValue)
+        {
+            if (IsLoaded == false)
+            {
+                Init();
+            }
+
+            return LocalPreferences.ContainsKey(name)
+                ? (T)LocalPreferences[name]
+                : fallbackValue;
+        }
+
+#nullable disable
+
+        private static string GetPathToSettings(Environment.SpecialFolder folder)
+        {
+            return Path.Join(
+            Environment.GetFolderPath(folder),
+            "PixiEditor",
+            "user_preferences.json");
+        }
+
+        private static Dictionary<string, object> InitPath(string path)
+        {
+            string dir = Path.GetDirectoryName(path);
+
+            if (!Directory.Exists(dir))
+            {
+                Directory.CreateDirectory(dir);
+            }
+
+            if (!File.Exists(path))
+            {
+                File.WriteAllText(path, "{\n}");
+            }
+            else
+            {
+                string json = File.ReadAllText(path);
+                return JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
+            }
+
+            return new Dictionary<string, object>();
+        }
     }
 }

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

@@ -42,7 +42,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             ExportFileCommand = new RelayCommand(ExportFile, CanSave);
             OpenRecentCommand = new RelayCommand(OpenRecent);
             Owner.OnStartupEvent += Owner_OnStartupEvent;
-            RecentlyOpened = new ObservableCollection<string>(PreferencesSettings.GetPreference<JArray>(nameof(RecentlyOpened), new JArray()).ToObject<string[]>());
+            RecentlyOpened = new ObservableCollection<string>(PreferencesSettings.GetLocalPreference<JArray>(nameof(RecentlyOpened), new JArray()).ToObject<string[]>());
         }
 
         public void OpenRecent(object parameter)