Merge pull request #393 from Flow-Launcher/PluginJsonStorageSingleInstance

Make PluginJsonStorage in IPublicAPI become single instance per type
This commit is contained in:
Jeremy Wu 2021-04-01 07:21:47 +11:00 committed by GitHub
commit b359d709a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 4 deletions

View file

@ -165,10 +165,26 @@ namespace Flow.Launcher.Plugin
T LoadJsonStorage<T>() where T : new();
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">Type for Serialization</typeparam>
/// <returns></returns>
void SaveJsonStorage<T>(T setting) where T : new();
void SaveJsonStorage<T>() where T : new();
/// <summary>
/// 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
/// </summary>
/// <typeparam name="T">Type for Serialization</typeparam>
/// <returns></returns>
void SaveJsonStorage<T>(T settings) where T : new();
/// <summary>
/// Backup the JsonStorage you loaded from LoadJsonStorage
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="settings"></param>
void BackupJsonStorage<T>() where T : new();
}
}

View file

@ -131,9 +131,42 @@ namespace Flow.Launcher
public void LogException(string className, string message, Exception e, [CallerMemberName] string methodName = "") => Log.Exception(className, message, e, methodName);
public T LoadJsonStorage<T>() where T : new() => new PluginJsonStorage<T>().Load();
private readonly Dictionary<Type, dynamic> PluginJsonStorages = new Dictionary<Type, dynamic>();
public void SaveJsonStorage<T>(T setting) where T : new() => new PluginJsonStorage<T>(setting).Save();
public T LoadJsonStorage<T>() where T : new()
{
var type = typeof(T);
if (!PluginJsonStorages.ContainsKey(type))
PluginJsonStorages[type] = new PluginJsonStorage<T>();
return PluginJsonStorages[type].Load();
}
public void SaveJsonStorage<T>() where T : new()
{
var type = typeof(T);
if (!PluginJsonStorages.ContainsKey(type))
PluginJsonStorages[type] = new PluginJsonStorage<T>();
PluginJsonStorages[type].Save();
}
public void SaveJsonStorage<T>(T settings) where T : new()
{
var type = typeof(T);
PluginJsonStorages[type] = new PluginJsonStorage<T>(settings);
PluginJsonStorages[type].Save();
}
public void BackupJsonStorage<T>() 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;