|
@@ -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>();
|
|
|
+ }
|
|
|
}
|
|
|
}
|