Move most direct call of PluginJsonStorage to API call

This commit is contained in:
张弘韬 2021-05-11 20:18:57 +08:00
parent 86598b5eef
commit 33accbd5d4
11 changed files with 37 additions and 75 deletions

View file

@ -186,12 +186,5 @@ namespace Flow.Launcher.Plugin
/// <typeparam name="T">Type for Serialization</typeparam> /// <typeparam name="T">Type for Serialization</typeparam>
/// <returns></returns> /// <returns></returns>
void SaveJsonStorage<T>(T settings) where T : new(); 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

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

View file

@ -12,25 +12,28 @@ using Flow.Launcher.Plugin.SharedCommands;
namespace Flow.Launcher.Plugin.BrowserBookmark namespace Flow.Launcher.Plugin.BrowserBookmark
{ {
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, ISavable, IContextMenu public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu, ISavable
{ {
private PluginInitContext context; private PluginInitContext context;
private List<Bookmark> cachedBookmarks = new List<Bookmark>(); private List<Bookmark> cachedBookmarks = new List<Bookmark>();
private Settings _settings { get; set;} private Settings _settings { get; set;}
private PluginJsonStorage<Settings> _storage { get; set;}
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
this.context = context; this.context = context;
_storage = new PluginJsonStorage<Settings>(); _settings = context.API.LoadJsonStorage<Settings>();
_settings = _storage.Load();
cachedBookmarks = Bookmarks.LoadAllBookmarks(); cachedBookmarks = Bookmarks.LoadAllBookmarks();
} }
public void Save()
{
context.API.SaveJsonStorage<Settings>();
}
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
string param = query.Search.TrimStart(); string param = query.Search.TrimStart();
@ -113,11 +116,6 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
return new SettingsControl(_settings); return new SettingsControl(_settings);
} }
public void Save()
{
_storage.Save();
}
public List<Result> LoadContextMenus(Result selectedResult) public List<Result> LoadContextMenus(Result selectedResult)
{ {
return new List<Result>() { return new List<Result>() {

View file

@ -11,7 +11,7 @@ using System.Windows.Controls;
namespace Flow.Launcher.Plugin.Explorer namespace Flow.Launcher.Plugin.Explorer
{ {
public class Main : ISettingProvider, IAsyncPlugin, ISavable, IContextMenu, IPluginI18n public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n
{ {
internal PluginInitContext Context { get; set; } internal PluginInitContext Context { get; set; }
@ -31,9 +31,11 @@ namespace Flow.Launcher.Plugin.Explorer
public async Task InitAsync(PluginInitContext context) public async Task InitAsync(PluginInitContext context)
{ {
Context = context; Context = context;
viewModel = new SettingsViewModel(context);
await viewModel.LoadStorage(); Settings = context.API.LoadJsonStorage<Settings>();
Settings = viewModel.Settings;
viewModel = new SettingsViewModel(context, Settings);
// as at v1.7.0 this is to maintain backwards compatibility, need to be removed afterwards. // as at v1.7.0 this is to maintain backwards compatibility, need to be removed afterwards.
if (Settings.QuickFolderAccessLinks.Any()) if (Settings.QuickFolderAccessLinks.Any())
@ -57,11 +59,6 @@ namespace Flow.Launcher.Plugin.Explorer
return await searchManager.SearchAsync(query, token); return await searchManager.SearchAsync(query, token);
} }
public void Save()
{
viewModel.Save();
}
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()
{ {
return Context.API.GetTranslation("plugin_explorer_plugin_name"); return Context.API.GetTranslation("plugin_explorer_plugin_name");

View file

@ -9,27 +9,20 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
{ {
public class SettingsViewModel public class SettingsViewModel
{ {
private readonly PluginJsonStorage<Settings> storage;
internal Settings Settings { get; set; } internal Settings Settings { get; set; }
internal PluginInitContext Context { get; set; } internal PluginInitContext Context { get; set; }
public SettingsViewModel(PluginInitContext context) public SettingsViewModel(PluginInitContext context, Settings settings)
{ {
Context = context; Context = context;
storage = new PluginJsonStorage<Settings>(); Settings = settings;
Settings = storage.Load();
} }
public Task LoadStorage()
{
return Task.Run(() => Settings = storage.Load());
}
public void Save() public void Save()
{ {
storage.Save(); Context.API.SaveJsonStorage<Settings>();
} }
internal void RemoveLinkFromQuickAccess(AccessLink selectedRow) => Settings.QuickAccessLinks.Remove(selectedRow); internal void RemoveLinkFromQuickAccess(AccessLink selectedRow) => Settings.QuickAccessLinks.Remove(selectedRow);

View file

@ -8,6 +8,7 @@ using Flow.Launcher.Infrastructure;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Threading; using System.Threading;
using System.Windows;
namespace Flow.Launcher.Plugin.PluginsManager namespace Flow.Launcher.Plugin.PluginsManager
{ {
@ -33,8 +34,8 @@ namespace Flow.Launcher.Plugin.PluginsManager
public Task InitAsync(PluginInitContext context) public Task InitAsync(PluginInitContext context)
{ {
Context = context; Context = context;
viewModel = new SettingsViewModel(context); Settings = context.API.LoadJsonStorage<Settings>();
Settings = viewModel.Settings; viewModel = new SettingsViewModel(context, Settings);
contextMenu = new ContextMenu(Context); contextMenu = new ContextMenu(Context);
pluginManager = new PluginsManager(Context, Settings); pluginManager = new PluginsManager(Context, Settings);
_ = pluginManager.UpdateManifest().ContinueWith(_ => _ = pluginManager.UpdateManifest().ContinueWith(_ =>

View file

@ -3,7 +3,7 @@ using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher.Plugin.PluginsManager.ViewModels namespace Flow.Launcher.Plugin.PluginsManager.ViewModels
{ {
public class SettingsViewModel internal class SettingsViewModel
{ {
private readonly PluginJsonStorage<Settings> storage; private readonly PluginJsonStorage<Settings> storage;
@ -11,11 +11,11 @@ namespace Flow.Launcher.Plugin.PluginsManager.ViewModels
internal PluginInitContext Context { get; set; } internal PluginInitContext Context { get; set; }
public SettingsViewModel(PluginInitContext context) public SettingsViewModel(PluginInitContext context, Settings settings)
{ {
Context = context; Context = context;
storage = new PluginJsonStorage<Settings>(); storage = new PluginJsonStorage<Settings>();
Settings = storage.Load(); Settings = settings;
} }
public void Save() public void Save()

View file

@ -10,7 +10,7 @@ namespace Flow.Launcher.Plugin.PluginsManager.Views
{ {
private readonly SettingsViewModel viewModel; private readonly SettingsViewModel viewModel;
public PluginsManagerSettings(SettingsViewModel viewModel) internal PluginsManagerSettings(SettingsViewModel viewModel)
{ {
InitializeComponent(); InitializeComponent();

View file

@ -25,16 +25,15 @@ namespace Flow.Launcher.Plugin.Program
private static BinaryStorage<Win32[]> _win32Storage; private static BinaryStorage<Win32[]> _win32Storage;
private static BinaryStorage<UWP.Application[]> _uwpStorage; private static BinaryStorage<UWP.Application[]> _uwpStorage;
private readonly PluginJsonStorage<Settings> _settingsStorage;
public Main() public Main()
{ {
_settingsStorage = new PluginJsonStorage<Settings>();
} }
public void Save() public void Save()
{ {
_settingsStorage.Save(); _context.API.SaveJsonStorage<Settings>();
_win32Storage.Save(_win32s); _win32Storage.Save(_win32s);
_uwpStorage.Save(_uwps); _uwpStorage.Save(_uwps);
} }
@ -71,7 +70,7 @@ namespace Flow.Launcher.Plugin.Program
{ {
_context = context; _context = context;
_settings = _settingsStorage.Load(); _settings = context.API.LoadJsonStorage<Settings>();
await Task.Yield(); await Task.Yield();

View file

@ -25,18 +25,11 @@ namespace Flow.Launcher.Plugin.Shell
private bool _winRStroked; private bool _winRStroked;
private readonly KeyboardSimulator _keyboardSimulator = new KeyboardSimulator(new InputSimulator()); private readonly KeyboardSimulator _keyboardSimulator = new KeyboardSimulator(new InputSimulator());
private readonly Settings _settings; private Settings _settings;
private readonly PluginJsonStorage<Settings> _storage;
public Main()
{
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
}
public void Save() public void Save()
{ {
_storage.Save(); context.API.SaveJsonStorage<Settings>();
} }
@ -285,6 +278,7 @@ namespace Flow.Launcher.Plugin.Shell
{ {
this.context = context; this.context = context;
context.API.GlobalKeyboardEvent += API_GlobalKeyboardEvent; context.API.GlobalKeyboardEvent += API_GlobalKeyboardEvent;
_settings = context.API.LoadJsonStorage<Settings>();
} }
bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state) bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)

View file

@ -44,18 +44,12 @@ namespace Flow.Launcher.Plugin.Url
"$"; "$";
Regex reg = new Regex(urlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); Regex reg = new Regex(urlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
private PluginInitContext context; private PluginInitContext context;
private readonly Settings _settings; private Settings _settings;
private readonly PluginJsonStorage<Settings> _storage;
public Main()
{
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
}
public void Save() public void Save()
{ {
_storage.Save(); context.API.SaveJsonStorage<Settings>();
} }
public List<Result> Query(Query query) public List<Result> Query(Query query)
@ -128,6 +122,8 @@ namespace Flow.Launcher.Plugin.Url
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
this.context = context; this.context = context;
_settings = context.API.LoadJsonStorage<Settings>();
} }
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()