Skip to content

Commit 0496d6c

Browse files
committed
Use api functions for Program plugin
1 parent ca221d7 commit 0496d6c

File tree

4 files changed

+46
-46
lines changed

4 files changed

+46
-46
lines changed

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

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Windows.Controls;
99
using Flow.Launcher.Infrastructure;
1010
using Flow.Launcher.Infrastructure.Logger;
11-
using Flow.Launcher.Infrastructure.Storage;
1211
using Flow.Launcher.Infrastructure.UserSettings;
1312
using Flow.Launcher.Plugin.Program.Programs;
1413
using Flow.Launcher.Plugin.Program.Views;
@@ -19,19 +18,17 @@
1918

2019
namespace Flow.Launcher.Plugin.Program
2120
{
22-
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, ISavable, IAsyncReloadable,
23-
IDisposable
21+
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, IAsyncReloadable, IDisposable
2422
{
25-
internal static Win32[] _win32s { get; set; }
26-
internal static UWPApp[] _uwps { get; set; }
27-
internal static Settings _settings { get; set; }
23+
private const string Win32CacheName = "Win32";
24+
private const string UwpCacheName = "UWP";
2825

26+
internal static List<Win32> _win32s { get; private set; }
27+
internal static List<UWPApp> _uwps { get; private set; }
28+
internal static Settings _settings { get; private set; }
2929

3030
internal static PluginInitContext Context { get; private set; }
3131

32-
private static BinaryStorage<Win32[]> _win32Storage;
33-
private static BinaryStorage<UWPApp[]> _uwpStorage;
34-
3532
private static readonly List<Result> emptyResults = new();
3633

3734
private static readonly MemoryCacheOptions cacheOptions = new() { SizeLimit = 1560 };
@@ -81,12 +78,6 @@ static Main()
8178
{
8279
}
8380

84-
public void Save()
85-
{
86-
_win32Storage.SaveAsync(_win32s);
87-
_uwpStorage.SaveAsync(_uwps);
88-
}
89-
9081
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
9182
{
9283
var result = await cache.GetOrCreateAsync(query.Search, async entry =>
@@ -191,7 +182,9 @@ public async Task InitAsync(PluginInitContext context)
191182

192183
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Preload programs cost", async () =>
193184
{
194-
Helper.ValidateDirectory(Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
185+
var pluginCachePath = Context.CurrentPluginMetadata.PluginCacheDirectoryPath;
186+
187+
Helper.ValidateDirectory(pluginCachePath);
195188

196189
static void MoveFile(string sourcePath, string destinationPath)
197190
{
@@ -236,20 +229,18 @@ static void MoveFile(string sourcePath, string destinationPath)
236229
}
237230

238231
// Move old cache files to the new cache directory
239-
var oldWin32CacheFile = Path.Combine(DataLocation.CacheDirectory, $"Win32.cache");
240-
var newWin32CacheFile = Path.Combine(Context.CurrentPluginMetadata.PluginCacheDirectoryPath, $"Win32.cache");
232+
var oldWin32CacheFile = Path.Combine(DataLocation.CacheDirectory, $"{Win32CacheName}.cache");
233+
var newWin32CacheFile = Path.Combine(pluginCachePath, $"{Win32CacheName}.cache");
241234
MoveFile(oldWin32CacheFile, newWin32CacheFile);
242-
var oldUWPCacheFile = Path.Combine(DataLocation.CacheDirectory, $"UWP.cache");
243-
var newUWPCacheFile = Path.Combine(Context.CurrentPluginMetadata.PluginCacheDirectoryPath, $"UWP.cache");
235+
var oldUWPCacheFile = Path.Combine(DataLocation.CacheDirectory, $"{UwpCacheName}.cache");
236+
var newUWPCacheFile = Path.Combine(pluginCachePath, $"{UwpCacheName}.cache");
244237
MoveFile(oldUWPCacheFile, newUWPCacheFile);
245238

246-
_win32Storage = new BinaryStorage<Win32[]>("Win32", Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
247-
_win32s = await _win32Storage.TryLoadAsync(Array.Empty<Win32>());
248-
_uwpStorage = new BinaryStorage<UWPApp[]>("UWP", Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
249-
_uwps = await _uwpStorage.TryLoadAsync(Array.Empty<UWPApp>());
239+
_win32s = await context.API.LoadCacheBinaryStorageAsync(Win32CacheName, pluginCachePath, new List<Win32>());
240+
_uwps = await context.API.LoadCacheBinaryStorageAsync(UwpCacheName, pluginCachePath, new List<UWPApp>());
250241
});
251-
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload win32 programs <{_win32s.Length}>");
252-
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload uwps <{_uwps.Length}>");
242+
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload win32 programs <{_win32s.Count}>");
243+
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload uwps <{_uwps.Count}>");
253244

254245
bool cacheEmpty = !_win32s.Any() || !_uwps.Any();
255246

@@ -273,36 +264,45 @@ static void WatchProgramUpdate()
273264
}
274265
}
275266

276-
public static void IndexWin32Programs()
267+
public static async Task IndexWin32ProgramsAsync()
277268
{
278269
var win32S = Win32.All(_settings);
279-
_win32s = win32S;
270+
_win32s.Clear();
271+
foreach (var win32 in win32S)
272+
{
273+
_win32s.Add(win32);
274+
}
280275
ResetCache();
281-
_win32Storage.SaveAsync(_win32s);
276+
await Context.API.SaveCacheBinaryStorageAsync<List<Win32>>(Win32CacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
282277
_settings.LastIndexTime = DateTime.Now;
283278
}
284279

285-
public static void IndexUwpPrograms()
280+
public static async Task IndexUwpProgramsAsync()
286281
{
287-
var applications = UWPPackage.All(_settings);
288-
_uwps = applications;
282+
var uwps = UWPPackage.All(_settings);
283+
_uwps.Clear();
284+
foreach (var uwp in uwps)
285+
{
286+
_uwps.Add(uwp);
287+
}
289288
ResetCache();
290-
_uwpStorage.SaveAsync(_uwps);
289+
await Context.API.SaveCacheBinaryStorageAsync<List<UWPApp>>(UwpCacheName, Context.CurrentPluginMetadata.PluginCacheDirectoryPath);
291290
_settings.LastIndexTime = DateTime.Now;
292291
}
293292

294293
public static async Task IndexProgramsAsync()
295294
{
296-
var a = Task.Run(() =>
295+
var win32Task = Task.Run(async () =>
297296
{
298-
Stopwatch.Normal("|Flow.Launcher.Plugin.Program.Main|Win32Program index cost", IndexWin32Programs);
297+
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|Win32Program index cost", IndexWin32ProgramsAsync);
299298
});
300299

301-
var b = Task.Run(() =>
300+
var uwpTask = Task.Run(async () =>
302301
{
303-
Stopwatch.Normal("|Flow.Launcher.Plugin.Program.Main|UWPProgram index cost", IndexUwpPrograms);
302+
await Stopwatch.NormalAsync("|Flow.Launcher.Plugin.Program.Main|UWPProgram index cost", IndexUwpProgramsAsync);
304303
});
305-
await Task.WhenAll(a, b).ConfigureAwait(false);
304+
305+
await Task.WhenAll(win32Task, uwpTask).ConfigureAwait(false);
306306
}
307307

308308
internal static void ResetCache()
@@ -314,7 +314,7 @@ internal static void ResetCache()
314314

315315
public Control CreateSettingPanel()
316316
{
317-
return new ProgramSetting(Context, _settings, _win32s, _uwps);
317+
return new ProgramSetting(Context, _settings);
318318
}
319319

320320
public string GetTranslatedPluginTitle()
@@ -370,7 +370,7 @@ private static void DisableProgram(IProgram programToDelete)
370370
_settings.DisabledProgramSources.Add(new ProgramSource(program));
371371
_ = Task.Run(() =>
372372
{
373-
IndexUwpPrograms();
373+
_ = IndexUwpProgramsAsync();
374374
});
375375
}
376376
else if (_win32s.Any(x => x.UniqueIdentifier == programToDelete.UniqueIdentifier))
@@ -380,7 +380,7 @@ private static void DisableProgram(IProgram programToDelete)
380380
_settings.DisabledProgramSources.Add(new ProgramSource(program));
381381
_ = Task.Run(() =>
382382
{
383-
IndexWin32Programs();
383+
_ = IndexWin32ProgramsAsync();
384384
});
385385
}
386386
}

Plugins/Flow.Launcher.Plugin.Program/Programs/UWPPackage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public static async Task WatchPackageChange()
317317
{
318318
await Task.Delay(3000).ConfigureAwait(false);
319319
PackageChangeChannel.Reader.TryRead(out _);
320-
await Task.Run(Main.IndexUwpPrograms);
320+
await Task.Run(Main.IndexUwpProgramsAsync);
321321
}
322322
}
323323
}

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ public static async Task MonitorDirectoryChangeAsync()
797797
{
798798
}
799799

800-
await Task.Run(Main.IndexWin32Programs);
800+
await Task.Run(Main.IndexWin32ProgramsAsync);
801801
}
802802
}
803803

Plugins/Flow.Launcher.Plugin.Program/Views/ProgramSetting.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ namespace Flow.Launcher.Plugin.Program.Views
1818
/// </summary>
1919
public partial class ProgramSetting : UserControl
2020
{
21-
private PluginInitContext context;
22-
private Settings _settings;
21+
private readonly PluginInitContext context;
22+
private readonly Settings _settings;
2323
private GridViewColumnHeader _lastHeaderClicked;
2424
private ListSortDirection _lastDirection;
2525

@@ -109,7 +109,7 @@ public bool EnableUWP
109109

110110
public bool ShowUWPCheckbox => UWPPackage.SupportUWP();
111111

112-
public ProgramSetting(PluginInitContext context, Settings settings, Win32[] win32s, UWPApp[] uwps)
112+
public ProgramSetting(PluginInitContext context, Settings settings)
113113
{
114114
this.context = context;
115115
_settings = settings;

0 commit comments

Comments
 (0)