Skip to content

Commit 25f6e66

Browse files
committed
add auto save for all setting that has been registered, and remove Separate Save
1 parent 33accbd commit 25f6e66

File tree

10 files changed

+27
-63
lines changed

10 files changed

+27
-63
lines changed

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public void RestartApp()
7676

7777
public void SaveAppAllSettings()
7878
{
79+
SaveAllJsonStorage();
7980
_mainVM.Save();
8081
_settingsVM.Save();
8182
PluginManager.Save();
@@ -133,32 +134,41 @@ public void OpenSettingDialog()
133134

134135
public void LogException(string className, string message, Exception e, [CallerMemberName] string methodName = "") => Log.Exception(className, message, e, methodName);
135136

136-
private readonly Dictionary<Type, object> PluginJsonStorages = new Dictionary<Type, object>();
137+
private readonly Dictionary<Type, object> _pluginJsonStorages = new();
137138

139+
private void SaveAllJsonStorage()
140+
{
141+
foreach (var value in _pluginJsonStorages.Values)
142+
{
143+
var method = value.GetType().GetMethod("Save");
144+
method?.Invoke(value, null);
145+
}
146+
}
147+
138148
public T LoadJsonStorage<T>() where T : new()
139149
{
140150
var type = typeof(T);
141-
if (!PluginJsonStorages.ContainsKey(type))
142-
PluginJsonStorages[type] = new PluginJsonStorage<T>();
151+
if (!_pluginJsonStorages.ContainsKey(type))
152+
_pluginJsonStorages[type] = new PluginJsonStorage<T>();
143153

144-
return ((PluginJsonStorage<T>) PluginJsonStorages[type]).Load();
154+
return ((PluginJsonStorage<T>) _pluginJsonStorages[type]).Load();
145155
}
146156

147157
public void SaveJsonStorage<T>() where T : new()
148158
{
149159
var type = typeof(T);
150-
if (!PluginJsonStorages.ContainsKey(type))
151-
PluginJsonStorages[type] = new PluginJsonStorage<T>();
160+
if (!_pluginJsonStorages.ContainsKey(type))
161+
_pluginJsonStorages[type] = new PluginJsonStorage<T>();
152162

153-
((PluginJsonStorage<T>) PluginJsonStorages[type]).Save();
163+
((PluginJsonStorage<T>) _pluginJsonStorages[type]).Save();
154164
}
155165

156166
public void SaveJsonStorage<T>(T settings) where T : new()
157167
{
158168
var type = typeof(T);
159-
PluginJsonStorages[type] = new PluginJsonStorage<T>(settings);
169+
_pluginJsonStorages[type] = new PluginJsonStorage<T>(settings);
160170

161-
((PluginJsonStorage<T>) PluginJsonStorages[type]).Save();
171+
((PluginJsonStorage<T>) _pluginJsonStorages[type]).Save();
162172
}
163173

164174
public event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Flow.Launcher.Plugin.BrowserBookmark
1414
{
15-
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu, ISavable
15+
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, IContextMenu
1616
{
1717
private PluginInitContext context;
1818

@@ -29,11 +29,6 @@ public void Init(PluginInitContext context)
2929
cachedBookmarks = Bookmarks.LoadAllBookmarks();
3030
}
3131

32-
public void Save()
33-
{
34-
context.API.SaveJsonStorage<Settings>();
35-
}
36-
3732
public List<Result> Query(Query query)
3833
{
3934
string param = query.Search.TrimStart();

Plugins/Flow.Launcher.Plugin.Calculator/Main.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Flow.Launcher.Plugin.Caculator
1414
{
15-
public class Main : IPlugin, IPluginI18n, ISavable, ISettingProvider
15+
public class Main : IPlugin, IPluginI18n, ISettingProvider
1616
{
1717
private static readonly Regex RegValidExpressChar = new Regex(
1818
@"^(" +
@@ -30,11 +30,6 @@ public class Main : IPlugin, IPluginI18n, ISavable, ISettingProvider
3030
private static Settings _settings;
3131
private static SettingsViewModel _viewModel;
3232

33-
static Main()
34-
{
35-
36-
}
37-
3833
public void Init(PluginInitContext context)
3934
{
4035
Context = context;
@@ -187,10 +182,5 @@ public Control CreateSettingPanel()
187182
{
188183
return new CalculatorSettings(_viewModel);
189184
}
190-
191-
public void Save()
192-
{
193-
_viewModel.Save();
194-
}
195185
}
196186
}

Plugins/Flow.Launcher.Plugin.Calculator/ViewModels/SettingsViewModel.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Flow.Launcher.Plugin.Caculator.ViewModels
1010
{
11-
public class SettingsViewModel : BaseModel, ISavable
11+
public class SettingsViewModel : BaseModel
1212
{
1313
private readonly PluginJsonStorage<Settings> _storage;
1414

@@ -21,10 +21,5 @@ public SettingsViewModel()
2121
public Settings Settings { get; set; }
2222

2323
public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
24-
25-
public void Save()
26-
{
27-
_storage.Save();
28-
}
2924
}
3025
}

Plugins/Flow.Launcher.Plugin.PluginsManager/Main.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Flow.Launcher.Plugin.PluginsManager
1414
{
15-
public class Main : ISettingProvider, IAsyncPlugin, ISavable, IContextMenu, IPluginI18n, IAsyncReloadable
15+
public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n, IAsyncReloadable
1616
{
1717
internal PluginInitContext Context { get; set; }
1818

@@ -79,11 +79,6 @@ var s when s.StartsWith(Settings.HotkeyUpdate) => await pluginManager.RequestUpd
7979
};
8080
}
8181

82-
public void Save()
83-
{
84-
viewModel.Save();
85-
}
86-
8782
public string GetTranslatedPluginTitle()
8883
{
8984
return Context.API.GetTranslation("plugin_pluginsmanager_plugin_name");

Plugins/Flow.Launcher.Plugin.PluginsManager/ViewModels/SettingsViewModel.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,5 @@ public SettingsViewModel(PluginInitContext context, Settings settings)
1717
storage = new PluginJsonStorage<Settings>();
1818
Settings = settings;
1919
}
20-
21-
public void Save()
22-
{
23-
storage.Save();
24-
}
2520
}
2621
}

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public Main()
3333

3434
public void Save()
3535
{
36-
_context.API.SaveJsonStorage<Settings>();
3736
_win32Storage.Save(_win32s);
3837
_uwpStorage.Save(_uwps);
3938
}

Plugins/Flow.Launcher.Plugin.Shell/Main.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
namespace Flow.Launcher.Plugin.Shell
2020
{
21-
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavable
21+
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu
2222
{
2323
private const string Image = "Images/shell.png";
2424
private PluginInitContext context;
@@ -27,12 +27,6 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavab
2727

2828
private Settings _settings;
2929

30-
public void Save()
31-
{
32-
context.API.SaveJsonStorage<Settings>();
33-
}
34-
35-
3630
public List<Result> Query(Query query)
3731
{
3832
List<Result> results = new List<Result>();

Plugins/Flow.Launcher.Plugin.Url/Main.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Flow.Launcher.Plugin.Url
99
{
10-
public class Main : ISettingProvider,IPlugin, IPluginI18n, ISavable
10+
public class Main : ISettingProvider,IPlugin, IPluginI18n
1111
{
1212
//based on https://gist.github.com/dperini/729294
1313
private const string urlPattern = "^" +
@@ -46,12 +46,7 @@ public class Main : ISettingProvider,IPlugin, IPluginI18n, ISavable
4646
private PluginInitContext context;
4747
private Settings _settings;
4848

49-
50-
public void Save()
51-
{
52-
context.API.SaveJsonStorage<Settings>();
53-
}
54-
49+
5550
public List<Result> Query(Query query)
5651
{
5752
var raw = query.Search;

Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Flow.Launcher.Plugin.WebSearch
1616
{
17-
public class Main : IAsyncPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpdated
17+
public class Main : IAsyncPlugin, ISettingProvider, IPluginI18n, IResultUpdated
1818
{
1919
private PluginInitContext _context;
2020

@@ -31,10 +31,6 @@ public class Main : IAsyncPlugin, ISettingProvider, IPluginI18n, ISavable, IResu
3131

3232
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
3333

34-
public void Save()
35-
{
36-
_viewModel.Save();
37-
}
3834

3935
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
4036
{

0 commit comments

Comments
 (0)