add auto save for all setting that has been registered, and remove Separate Save

This commit is contained in:
张弘韬 2021-05-12 00:37:28 +08:00
parent 33accbd5d4
commit 25f6e664f2
10 changed files with 27 additions and 63 deletions

View file

@ -76,6 +76,7 @@ namespace Flow.Launcher
public void SaveAppAllSettings()
{
SaveAllJsonStorage();
_mainVM.Save();
_settingsVM.Save();
PluginManager.Save();
@ -133,32 +134,41 @@ namespace Flow.Launcher
public void LogException(string className, string message, Exception e, [CallerMemberName] string methodName = "") => Log.Exception(className, message, e, methodName);
private readonly Dictionary<Type, object> PluginJsonStorages = new Dictionary<Type, object>();
private readonly Dictionary<Type, object> _pluginJsonStorages = new();
private void SaveAllJsonStorage()
{
foreach (var value in _pluginJsonStorages.Values)
{
var method = value.GetType().GetMethod("Save");
method?.Invoke(value, null);
}
}
public T LoadJsonStorage<T>() where T : new()
{
var type = typeof(T);
if (!PluginJsonStorages.ContainsKey(type))
PluginJsonStorages[type] = new PluginJsonStorage<T>();
if (!_pluginJsonStorages.ContainsKey(type))
_pluginJsonStorages[type] = new PluginJsonStorage<T>();
return ((PluginJsonStorage<T>) PluginJsonStorages[type]).Load();
return ((PluginJsonStorage<T>) _pluginJsonStorages[type]).Load();
}
public void SaveJsonStorage<T>() where T : new()
{
var type = typeof(T);
if (!PluginJsonStorages.ContainsKey(type))
PluginJsonStorages[type] = new PluginJsonStorage<T>();
if (!_pluginJsonStorages.ContainsKey(type))
_pluginJsonStorages[type] = new PluginJsonStorage<T>();
((PluginJsonStorage<T>) PluginJsonStorages[type]).Save();
((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] = new PluginJsonStorage<T>(settings);
((PluginJsonStorage<T>) PluginJsonStorages[type]).Save();
((PluginJsonStorage<T>) _pluginJsonStorages[type]).Save();
}
public event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;

View file

@ -12,7 +12,7 @@ using Flow.Launcher.Plugin.SharedCommands;
namespace Flow.Launcher.Plugin.BrowserBookmark
{
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu, ISavable
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu
{
private PluginInitContext context;
@ -29,11 +29,6 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
cachedBookmarks = Bookmarks.LoadAllBookmarks();
}
public void Save()
{
context.API.SaveJsonStorage<Settings>();
}
public List<Result> Query(Query query)
{
string param = query.Search.TrimStart();

View file

@ -12,7 +12,7 @@ using Flow.Launcher.Plugin.Caculator.Views;
namespace Flow.Launcher.Plugin.Caculator
{
public class Main : IPlugin, IPluginI18n, ISavable, ISettingProvider
public class Main : IPlugin, IPluginI18n, ISettingProvider
{
private static readonly Regex RegValidExpressChar = new Regex(
@"^(" +
@ -30,11 +30,6 @@ namespace Flow.Launcher.Plugin.Caculator
private static Settings _settings;
private static SettingsViewModel _viewModel;
static Main()
{
}
public void Init(PluginInitContext context)
{
Context = context;
@ -187,10 +182,5 @@ namespace Flow.Launcher.Plugin.Caculator
{
return new CalculatorSettings(_viewModel);
}
public void Save()
{
_viewModel.Save();
}
}
}

View file

@ -8,7 +8,7 @@ using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher.Plugin.Caculator.ViewModels
{
public class SettingsViewModel : BaseModel, ISavable
public class SettingsViewModel : BaseModel
{
private readonly PluginJsonStorage<Settings> _storage;
@ -21,10 +21,5 @@ namespace Flow.Launcher.Plugin.Caculator.ViewModels
public Settings Settings { get; set; }
public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
public void Save()
{
_storage.Save();
}
}
}

View file

@ -12,7 +12,7 @@ using System.Windows;
namespace Flow.Launcher.Plugin.PluginsManager
{
public class Main : ISettingProvider, IAsyncPlugin, ISavable, IContextMenu, IPluginI18n, IAsyncReloadable
public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n, IAsyncReloadable
{
internal PluginInitContext Context { get; set; }
@ -79,11 +79,6 @@ namespace Flow.Launcher.Plugin.PluginsManager
};
}
public void Save()
{
viewModel.Save();
}
public string GetTranslatedPluginTitle()
{
return Context.API.GetTranslation("plugin_pluginsmanager_plugin_name");

View file

@ -17,10 +17,5 @@ namespace Flow.Launcher.Plugin.PluginsManager.ViewModels
storage = new PluginJsonStorage<Settings>();
Settings = settings;
}
public void Save()
{
storage.Save();
}
}
}

View file

@ -33,7 +33,6 @@ namespace Flow.Launcher.Plugin.Program
public void Save()
{
_context.API.SaveJsonStorage<Settings>();
_win32Storage.Save(_win32s);
_uwpStorage.Save(_uwps);
}

View file

@ -18,7 +18,7 @@ using Keys = System.Windows.Forms.Keys;
namespace Flow.Launcher.Plugin.Shell
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu
{
private const string Image = "Images/shell.png";
private PluginInitContext context;
@ -27,12 +27,6 @@ namespace Flow.Launcher.Plugin.Shell
private Settings _settings;
public void Save()
{
context.API.SaveJsonStorage<Settings>();
}
public List<Result> Query(Query query)
{
List<Result> results = new List<Result>();

View file

@ -7,7 +7,7 @@ using Flow.Launcher.Plugin.SharedCommands;
namespace Flow.Launcher.Plugin.Url
{
public class Main : ISettingProvider,IPlugin, IPluginI18n, ISavable
public class Main : ISettingProvider,IPlugin, IPluginI18n
{
//based on https://gist.github.com/dperini/729294
private const string urlPattern = "^" +
@ -46,12 +46,7 @@ namespace Flow.Launcher.Plugin.Url
private PluginInitContext context;
private Settings _settings;
public void Save()
{
context.API.SaveJsonStorage<Settings>();
}
public List<Result> Query(Query query)
{
var raw = query.Search;

View file

@ -14,7 +14,7 @@ using Flow.Launcher.Plugin.SharedCommands;
namespace Flow.Launcher.Plugin.WebSearch
{
public class Main : IAsyncPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpdated
public class Main : IAsyncPlugin, ISettingProvider, IPluginI18n, IResultUpdated
{
private PluginInitContext _context;
@ -31,10 +31,6 @@ namespace Flow.Launcher.Plugin.WebSearch
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
public void Save()
{
_viewModel.Save();
}
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{