From 6fe83d84c5343703bc94b2fa9f0a6301cf744b66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sat, 27 Mar 2021 17:26:53 +0800 Subject: [PATCH] make JsonStorge used with single instance --- Flow.Launcher.Plugin/IPublicAPI.cs | 20 ++++++++++++++-- Flow.Launcher/PublicAPIInstance.cs | 37 ++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) 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;