diff --git a/Flow.Launcher.Plugin/IPublicAPI.cs b/Flow.Launcher.Plugin/IPublicAPI.cs index 07e2b270053..0c1cd7bfd53 100644 --- a/Flow.Launcher.Plugin/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/IPublicAPI.cs @@ -165,10 +165,26 @@ public interface IPublicAPI T LoadJsonStorage() where T : new(); /// - /// Save JsonStorage for current plugin. This is the method used to save settings to json in Flow + /// Save JsonStorage for current plugin. This is the method used to save settings to json in Flow.Launcher + /// This method will save the original instance loaded with LoadJsonStorage. /// /// Type for Serialization /// - void SaveJsonStorage(T setting) where T : new(); + void SaveJsonStorage() where T : new(); + + /// + /// Save JsonStorage for current plugin. This is the method used to save settings to json in Flow.Launcher + /// This method will override the original class instance loaded from LoadJsonStorage + /// + /// Type for Serialization + /// + void SaveJsonStorage(T settings) where T : new(); + + /// + /// Backup the JsonStorage you loaded from LoadJsonStorage + /// + /// + /// + void BackupJsonStorage() where T : new(); } } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 9b41c2c05ba..77902ecd9bc 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -131,9 +131,42 @@ public void OpenSettingDialog() public void LogException(string className, string message, Exception e, [CallerMemberName] string methodName = "") => Log.Exception(className, message, e, methodName); - public T LoadJsonStorage() where T : new() => new PluginJsonStorage().Load(); + private readonly Dictionary PluginJsonStorages = new Dictionary(); - public void SaveJsonStorage(T setting) where T : new() => new PluginJsonStorage(setting).Save(); + public T LoadJsonStorage() where T : new() + { + var type = typeof(T); + if (!PluginJsonStorages.ContainsKey(type)) + PluginJsonStorages[type] = new PluginJsonStorage(); + + return PluginJsonStorages[type].Load(); + } + + public void SaveJsonStorage() where T : new() + { + var type = typeof(T); + if (!PluginJsonStorages.ContainsKey(type)) + PluginJsonStorages[type] = new PluginJsonStorage(); + + PluginJsonStorages[type].Save(); + } + + public void SaveJsonStorage(T settings) where T : new() + { + var type = typeof(T); + PluginJsonStorages[type] = new PluginJsonStorage(settings); + + PluginJsonStorages[type].Save(); + } + + public void BackupJsonStorage() where T : new() + { + var type = typeof(T); + if (!PluginJsonStorages.ContainsKey(type)) + throw new InvalidOperationException("You haven't registered the JsonStorage for specific Type"); + + PluginJsonStorages[type].BackupOriginFile(); + } public event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;