PreferencesSettings.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Newtonsoft.Json;
  5. using PixiEditor.ViewModels;
  6. namespace PixiEditor.Models.UserPreferences
  7. {
  8. public class PreferencesSettings : IPreferences
  9. {
  10. public static IPreferences Current => ViewModelMain.Current.Preferences;
  11. public bool IsLoaded { get; private set; } = false;
  12. public string PathToRoamingUserPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.ApplicationData, "user_preferences.json");
  13. public string PathToLocalPreferences { get; private set; } = GetPathToSettings(Environment.SpecialFolder.LocalApplicationData, "editor_data.json");
  14. public Dictionary<string, object> Preferences { get; set; } = new Dictionary<string, object>();
  15. public Dictionary<string, object> LocalPreferences { get; set; } = new Dictionary<string, object>();
  16. public void Init()
  17. {
  18. Init(PathToRoamingUserPreferences, PathToLocalPreferences);
  19. }
  20. public void Init(string path, string localPath)
  21. {
  22. PathToRoamingUserPreferences = path;
  23. PathToLocalPreferences = localPath;
  24. if (IsLoaded == false)
  25. {
  26. Preferences = InitPath(path);
  27. LocalPreferences = InitPath(localPath);
  28. IsLoaded = true;
  29. }
  30. }
  31. public void UpdatePreference<T>(string name, T value)
  32. {
  33. if (IsLoaded == false)
  34. {
  35. Init();
  36. }
  37. Preferences[name] = value;
  38. if (Callbacks.ContainsKey(name))
  39. {
  40. foreach (var action in Callbacks[name])
  41. {
  42. action.Invoke(value);
  43. }
  44. }
  45. Save();
  46. }
  47. public void UpdateLocalPreference<T>(string name, T value)
  48. {
  49. if (IsLoaded == false)
  50. {
  51. Init();
  52. }
  53. LocalPreferences[name] = value;
  54. if (Callbacks.ContainsKey(name))
  55. {
  56. foreach (var action in Callbacks[name])
  57. {
  58. action.Invoke(value);
  59. }
  60. }
  61. Save();
  62. }
  63. public void Save()
  64. {
  65. if (IsLoaded == false)
  66. {
  67. Init();
  68. }
  69. File.WriteAllText(PathToRoamingUserPreferences, JsonConvert.SerializeObject(Preferences));
  70. File.WriteAllText(PathToLocalPreferences, JsonConvert.SerializeObject(LocalPreferences));
  71. }
  72. public Dictionary<string, List<Action<object>>> Callbacks { get; set; } = new Dictionary<string, List<Action<object>>>();
  73. public void AddCallback(string setting, Action<object> action)
  74. {
  75. if (Callbacks.ContainsKey(setting))
  76. {
  77. Callbacks[setting].Add(action);
  78. return;
  79. }
  80. Callbacks.Add(setting, new List<Action<object>>() { action });
  81. }
  82. #nullable enable
  83. public T? GetPreference<T>(string name)
  84. {
  85. return GetPreference(name, default(T));
  86. }
  87. public T? GetPreference<T>(string name, T? fallbackValue)
  88. {
  89. if (IsLoaded == false)
  90. {
  91. Init();
  92. }
  93. return Preferences.ContainsKey(name)
  94. ? (T)Preferences[name]
  95. : fallbackValue;
  96. }
  97. public T? GetLocalPreference<T>(string name)
  98. {
  99. return GetPreference(name, default(T));
  100. }
  101. public T? GetLocalPreference<T>(string name, T? fallbackValue)
  102. {
  103. if (IsLoaded == false)
  104. {
  105. Init();
  106. }
  107. return LocalPreferences.ContainsKey(name)
  108. ? (T)LocalPreferences[name]
  109. : fallbackValue;
  110. }
  111. #nullable disable
  112. private static string GetPathToSettings(Environment.SpecialFolder folder, string fileName)
  113. {
  114. return Path.Join(
  115. Environment.GetFolderPath(folder),
  116. "PixiEditor",
  117. fileName);
  118. }
  119. private static Dictionary<string, object> InitPath(string path)
  120. {
  121. string dir = Path.GetDirectoryName(path);
  122. if (!Directory.Exists(dir))
  123. {
  124. Directory.CreateDirectory(dir);
  125. }
  126. if (!File.Exists(path))
  127. {
  128. File.WriteAllText(path, "{\n}");
  129. }
  130. else
  131. {
  132. string json = File.ReadAllText(path);
  133. return JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  134. }
  135. return new Dictionary<string, object>();
  136. }
  137. }
  138. }