8
8
using System . Windows . Controls ;
9
9
using Flow . Launcher . Infrastructure ;
10
10
using Flow . Launcher . Infrastructure . Logger ;
11
- using Flow . Launcher . Infrastructure . Storage ;
12
11
using Flow . Launcher . Infrastructure . UserSettings ;
13
12
using Flow . Launcher . Plugin . Program . Programs ;
14
13
using Flow . Launcher . Plugin . Program . Views ;
19
18
20
19
namespace Flow . Launcher . Plugin . Program
21
20
{
22
- public class Main : ISettingProvider , IAsyncPlugin , IPluginI18n , IContextMenu , ISavable , IAsyncReloadable ,
23
- IDisposable
21
+ public class Main : ISettingProvider , IAsyncPlugin , IPluginI18n , IContextMenu , IAsyncReloadable , IDisposable
24
22
{
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" ;
28
25
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 ; }
29
29
30
30
internal static PluginInitContext Context { get ; private set ; }
31
31
32
- private static BinaryStorage < Win32 [ ] > _win32Storage ;
33
- private static BinaryStorage < UWPApp [ ] > _uwpStorage ;
34
-
35
32
private static readonly List < Result > emptyResults = new ( ) ;
36
33
37
34
private static readonly MemoryCacheOptions cacheOptions = new ( ) { SizeLimit = 1560 } ;
@@ -81,12 +78,6 @@ static Main()
81
78
{
82
79
}
83
80
84
- public void Save ( )
85
- {
86
- _win32Storage . SaveAsync ( _win32s ) ;
87
- _uwpStorage . SaveAsync ( _uwps ) ;
88
- }
89
-
90
81
public async Task < List < Result > > QueryAsync ( Query query , CancellationToken token )
91
82
{
92
83
var result = await cache . GetOrCreateAsync ( query . Search , async entry =>
@@ -191,7 +182,9 @@ public async Task InitAsync(PluginInitContext context)
191
182
192
183
await Stopwatch . NormalAsync ( "|Flow.Launcher.Plugin.Program.Main|Preload programs cost" , async ( ) =>
193
184
{
194
- Helper . ValidateDirectory ( Context . CurrentPluginMetadata . PluginCacheDirectoryPath ) ;
185
+ var pluginCachePath = Context . CurrentPluginMetadata . PluginCacheDirectoryPath ;
186
+
187
+ Helper . ValidateDirectory ( pluginCachePath ) ;
195
188
196
189
static void MoveFile ( string sourcePath , string destinationPath )
197
190
{
@@ -236,20 +229,18 @@ static void MoveFile(string sourcePath, string destinationPath)
236
229
}
237
230
238
231
// 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") ;
241
234
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") ;
244
237
MoveFile ( oldUWPCacheFile , newUWPCacheFile ) ;
245
238
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 > ( ) ) ;
250
241
} ) ;
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 } >") ;
253
244
254
245
bool cacheEmpty = ! _win32s . Any ( ) || ! _uwps . Any ( ) ;
255
246
@@ -273,36 +264,45 @@ static void WatchProgramUpdate()
273
264
}
274
265
}
275
266
276
- public static void IndexWin32Programs ( )
267
+ public static async Task IndexWin32ProgramsAsync ( )
277
268
{
278
269
var win32S = Win32 . All ( _settings ) ;
279
- _win32s = win32S ;
270
+ _win32s . Clear ( ) ;
271
+ foreach ( var win32 in win32S )
272
+ {
273
+ _win32s . Add ( win32 ) ;
274
+ }
280
275
ResetCache ( ) ;
281
- _win32Storage . SaveAsync ( _win32s ) ;
276
+ await Context . API . SaveCacheBinaryStorageAsync < List < Win32 > > ( Win32CacheName , Context . CurrentPluginMetadata . PluginCacheDirectoryPath ) ;
282
277
_settings . LastIndexTime = DateTime . Now ;
283
278
}
284
279
285
- public static void IndexUwpPrograms ( )
280
+ public static async Task IndexUwpProgramsAsync ( )
286
281
{
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
+ }
289
288
ResetCache ( ) ;
290
- _uwpStorage . SaveAsync ( _uwps ) ;
289
+ await Context . API . SaveCacheBinaryStorageAsync < List < UWPApp > > ( UwpCacheName , Context . CurrentPluginMetadata . PluginCacheDirectoryPath ) ;
291
290
_settings . LastIndexTime = DateTime . Now ;
292
291
}
293
292
294
293
public static async Task IndexProgramsAsync ( )
295
294
{
296
- var a = Task . Run ( ( ) =>
295
+ var win32Task = Task . Run ( async ( ) =>
297
296
{
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 ) ;
299
298
} ) ;
300
299
301
- var b = Task . Run ( ( ) =>
300
+ var uwpTask = Task . Run ( async ( ) =>
302
301
{
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 ) ;
304
303
} ) ;
305
- await Task . WhenAll ( a , b ) . ConfigureAwait ( false ) ;
304
+
305
+ await Task . WhenAll ( win32Task , uwpTask ) . ConfigureAwait ( false ) ;
306
306
}
307
307
308
308
internal static void ResetCache ( )
@@ -314,7 +314,7 @@ internal static void ResetCache()
314
314
315
315
public Control CreateSettingPanel ( )
316
316
{
317
- return new ProgramSetting ( Context , _settings , _win32s , _uwps ) ;
317
+ return new ProgramSetting ( Context , _settings ) ;
318
318
}
319
319
320
320
public string GetTranslatedPluginTitle ( )
@@ -370,7 +370,7 @@ private static void DisableProgram(IProgram programToDelete)
370
370
_settings . DisabledProgramSources . Add ( new ProgramSource ( program ) ) ;
371
371
_ = Task . Run ( ( ) =>
372
372
{
373
- IndexUwpPrograms ( ) ;
373
+ _ = IndexUwpProgramsAsync ( ) ;
374
374
} ) ;
375
375
}
376
376
else if ( _win32s . Any ( x => x . UniqueIdentifier == programToDelete . UniqueIdentifier ) )
@@ -380,7 +380,7 @@ private static void DisableProgram(IProgram programToDelete)
380
380
_settings . DisabledProgramSources . Add ( new ProgramSource ( program ) ) ;
381
381
_ = Task . Run ( ( ) =>
382
382
{
383
- IndexWin32Programs ( ) ;
383
+ _ = IndexWin32ProgramsAsync ( ) ;
384
384
} ) ;
385
385
}
386
386
}
0 commit comments