Skip to content

Commit 33accbd

Browse files
committed
Move most direct call of PluginJsonStorage to API call
1 parent 86598b5 commit 33accbd

File tree

11 files changed

+37
-75
lines changed

11 files changed

+37
-75
lines changed

Flow.Launcher.Plugin/IPublicAPI.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,5 @@ public interface IPublicAPI
186186
/// <typeparam name="T">Type for Serialization</typeparam>
187187
/// <returns></returns>
188188
void SaveJsonStorage<T>(T settings) where T : new();
189-
190-
/// <summary>
191-
/// Backup the JsonStorage you loaded from LoadJsonStorage
192-
/// </summary>
193-
/// <typeparam name="T"></typeparam>
194-
/// <param name="settings"></param>
195-
void BackupJsonStorage<T>() where T : new();
196189
}
197190
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ public void OpenSettingDialog()
133133

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

136-
private readonly Dictionary<Type, dynamic> PluginJsonStorages = new Dictionary<Type, dynamic>();
136+
private readonly Dictionary<Type, object> PluginJsonStorages = new Dictionary<Type, object>();
137137

138138
public T LoadJsonStorage<T>() where T : new()
139139
{
140140
var type = typeof(T);
141141
if (!PluginJsonStorages.ContainsKey(type))
142142
PluginJsonStorages[type] = new PluginJsonStorage<T>();
143143

144-
return PluginJsonStorages[type].Load();
144+
return ((PluginJsonStorage<T>) PluginJsonStorages[type]).Load();
145145
}
146146

147147
public void SaveJsonStorage<T>() where T : new()
@@ -150,24 +150,15 @@ public void OpenSettingDialog()
150150
if (!PluginJsonStorages.ContainsKey(type))
151151
PluginJsonStorages[type] = new PluginJsonStorage<T>();
152152

153-
PluginJsonStorages[type].Save();
153+
((PluginJsonStorage<T>) PluginJsonStorages[type]).Save();
154154
}
155155

156156
public void SaveJsonStorage<T>(T settings) where T : new()
157157
{
158158
var type = typeof(T);
159159
PluginJsonStorages[type] = new PluginJsonStorage<T>(settings);
160160

161-
PluginJsonStorages[type].Save();
162-
}
163-
164-
public void BackupJsonStorage<T>() where T : new()
165-
{
166-
var type = typeof(T);
167-
if (!PluginJsonStorages.ContainsKey(type))
168-
throw new InvalidOperationException("You haven't registered the JsonStorage for specific Type");
169-
170-
PluginJsonStorages[type].BackupOriginFile();
161+
((PluginJsonStorage<T>) PluginJsonStorages[type]).Save();
171162
}
172163

173164
public event FlowLauncherGlobalKeyboardEventHandler GlobalKeyboardEvent;

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,28 @@
1212

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

1919
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
2020

2121
private Settings _settings { get; set;}
22-
private PluginJsonStorage<Settings> _storage { get; set;}
2322

2423
public void Init(PluginInitContext context)
2524
{
2625
this.context = context;
2726

28-
_storage = new PluginJsonStorage<Settings>();
29-
_settings = _storage.Load();
27+
_settings = context.API.LoadJsonStorage<Settings>();
3028

3129
cachedBookmarks = Bookmarks.LoadAllBookmarks();
3230
}
3331

32+
public void Save()
33+
{
34+
context.API.SaveJsonStorage<Settings>();
35+
}
36+
3437
public List<Result> Query(Query query)
3538
{
3639
string param = query.Search.TrimStart();
@@ -113,11 +116,6 @@ public Control CreateSettingPanel()
113116
return new SettingsControl(_settings);
114117
}
115118

116-
public void Save()
117-
{
118-
_storage.Save();
119-
}
120-
121119
public List<Result> LoadContextMenus(Result selectedResult)
122120
{
123121
return new List<Result>() {

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Flow.Launcher.Plugin.Explorer
1313
{
14-
public class Main : ISettingProvider, IAsyncPlugin, ISavable, IContextMenu, IPluginI18n
14+
public class Main : ISettingProvider, IAsyncPlugin, IContextMenu, IPluginI18n
1515
{
1616
internal PluginInitContext Context { get; set; }
1717

@@ -31,9 +31,11 @@ public Control CreateSettingPanel()
3131
public async Task InitAsync(PluginInitContext context)
3232
{
3333
Context = context;
34-
viewModel = new SettingsViewModel(context);
35-
await viewModel.LoadStorage();
36-
Settings = viewModel.Settings;
34+
35+
Settings = context.API.LoadJsonStorage<Settings>();
36+
37+
viewModel = new SettingsViewModel(context, Settings);
38+
3739

3840
// as at v1.7.0 this is to maintain backwards compatibility, need to be removed afterwards.
3941
if (Settings.QuickFolderAccessLinks.Any())
@@ -57,11 +59,6 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
5759
return await searchManager.SearchAsync(query, token);
5860
}
5961

60-
public void Save()
61-
{
62-
viewModel.Save();
63-
}
64-
6562
public string GetTranslatedPluginTitle()
6663
{
6764
return Context.API.GetTranslation("plugin_explorer_plugin_name");

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,20 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
99
{
1010
public class SettingsViewModel
1111
{
12-
private readonly PluginJsonStorage<Settings> storage;
13-
1412
internal Settings Settings { get; set; }
1513

1614
internal PluginInitContext Context { get; set; }
1715

18-
public SettingsViewModel(PluginInitContext context)
16+
public SettingsViewModel(PluginInitContext context, Settings settings)
1917
{
2018
Context = context;
21-
storage = new PluginJsonStorage<Settings>();
22-
Settings = storage.Load();
19+
Settings = settings;
2320
}
2421

25-
public Task LoadStorage()
26-
{
27-
return Task.Run(() => Settings = storage.Load());
28-
}
2922

3023
public void Save()
3124
{
32-
storage.Save();
25+
Context.API.SaveJsonStorage<Settings>();
3326
}
3427

3528
internal void RemoveLinkFromQuickAccess(AccessLink selectedRow) => Settings.QuickAccessLinks.Remove(selectedRow);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System;
99
using System.Threading.Tasks;
1010
using System.Threading;
11+
using System.Windows;
1112

1213
namespace Flow.Launcher.Plugin.PluginsManager
1314
{
@@ -33,8 +34,8 @@ public Control CreateSettingPanel()
3334
public Task InitAsync(PluginInitContext context)
3435
{
3536
Context = context;
36-
viewModel = new SettingsViewModel(context);
37-
Settings = viewModel.Settings;
37+
Settings = context.API.LoadJsonStorage<Settings>();
38+
viewModel = new SettingsViewModel(context, Settings);
3839
contextMenu = new ContextMenu(Context);
3940
pluginManager = new PluginsManager(Context, Settings);
4041
_ = pluginManager.UpdateManifest().ContinueWith(_ =>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
namespace Flow.Launcher.Plugin.PluginsManager.ViewModels
55
{
6-
public class SettingsViewModel
6+
internal class SettingsViewModel
77
{
88
private readonly PluginJsonStorage<Settings> storage;
99

1010
internal Settings Settings { get; set; }
1111

1212
internal PluginInitContext Context { get; set; }
1313

14-
public SettingsViewModel(PluginInitContext context)
14+
public SettingsViewModel(PluginInitContext context, Settings settings)
1515
{
1616
Context = context;
1717
storage = new PluginJsonStorage<Settings>();
18-
Settings = storage.Load();
18+
Settings = settings;
1919
}
2020

2121
public void Save()

Plugins/Flow.Launcher.Plugin.PluginsManager/Views/PluginsManagerSettings.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public partial class PluginsManagerSettings
1010
{
1111
private readonly SettingsViewModel viewModel;
1212

13-
public PluginsManagerSettings(SettingsViewModel viewModel)
13+
internal PluginsManagerSettings(SettingsViewModel viewModel)
1414
{
1515
InitializeComponent();
1616

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, I
2525

2626
private static BinaryStorage<Win32[]> _win32Storage;
2727
private static BinaryStorage<UWP.Application[]> _uwpStorage;
28-
private readonly PluginJsonStorage<Settings> _settingsStorage;
2928

3029
public Main()
3130
{
32-
_settingsStorage = new PluginJsonStorage<Settings>();
31+
3332
}
3433

3534
public void Save()
3635
{
37-
_settingsStorage.Save();
36+
_context.API.SaveJsonStorage<Settings>();
3837
_win32Storage.Save(_win32s);
3938
_uwpStorage.Save(_uwps);
4039
}
@@ -71,7 +70,7 @@ public async Task InitAsync(PluginInitContext context)
7170
{
7271
_context = context;
7372

74-
_settings = _settingsStorage.Load();
73+
_settings = context.API.LoadJsonStorage<Settings>();
7574

7675
await Task.Yield();
7776

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu, ISavab
2525
private bool _winRStroked;
2626
private readonly KeyboardSimulator _keyboardSimulator = new KeyboardSimulator(new InputSimulator());
2727

28-
private readonly Settings _settings;
29-
private readonly PluginJsonStorage<Settings> _storage;
30-
31-
public Main()
32-
{
33-
_storage = new PluginJsonStorage<Settings>();
34-
_settings = _storage.Load();
35-
}
28+
private Settings _settings;
3629

3730
public void Save()
3831
{
39-
_storage.Save();
32+
context.API.SaveJsonStorage<Settings>();
4033
}
4134

4235

@@ -285,6 +278,7 @@ public void Init(PluginInitContext context)
285278
{
286279
this.context = context;
287280
context.API.GlobalKeyboardEvent += API_GlobalKeyboardEvent;
281+
_settings = context.API.LoadJsonStorage<Settings>();
288282
}
289283

290284
bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)

0 commit comments

Comments
 (0)