7
7
using System . Collections . Generic ;
8
8
using System . IO ;
9
9
using System . Linq ;
10
+ using System . Threading ;
10
11
using System . Threading . Tasks ;
11
12
using System . Windows ;
12
13
@@ -36,7 +37,7 @@ private bool ShouldHideWindow
36
37
}
37
38
}
38
39
39
- private readonly string icoPath = "Images\\ pluginsmanager.png" ;
40
+ internal readonly string icoPath = "Images\\ pluginsmanager.png" ;
40
41
41
42
internal PluginsManager ( PluginInitContext context , Settings settings )
42
43
{
@@ -64,27 +65,27 @@ internal List<Result> GetDefaultHotKeys()
64
65
return false ;
65
66
}
66
67
} ,
67
- new Result ( )
68
+ new Result ( )
69
+ {
70
+ Title = Settings . HotkeyUninstall ,
71
+ IcoPath = icoPath ,
72
+ Action = _ =>
68
73
{
69
- Title = Settings . HotkeyUninstall ,
70
- IcoPath = icoPath ,
71
- Action = _ =>
72
- {
73
- Context . API . ChangeQuery ( "pm uninstall " ) ;
74
- return false ;
75
- }
76
- } ,
77
- new Result ( )
74
+ Context . API . ChangeQuery ( "pm uninstall " ) ;
75
+ return false ;
76
+ }
77
+ } ,
78
+ new Result ( )
79
+ {
80
+ Title = Settings . HotkeyUpdate ,
81
+ IcoPath = icoPath ,
82
+ Action = _ =>
78
83
{
79
- Title = Settings . HotkeyUpdate ,
80
- IcoPath = icoPath ,
81
- Action = _ =>
82
- {
83
- Context . API . ChangeQuery ( "pm update " ) ;
84
- return false ;
85
- }
84
+ Context . API . ChangeQuery ( "pm update " ) ;
85
+ return false ;
86
86
}
87
- } ;
87
+ }
88
+ } ;
88
89
}
89
90
90
91
internal async Task InstallOrUpdate ( UserPlugin plugin )
@@ -137,7 +138,8 @@ internal async Task InstallOrUpdate(UserPlugin plugin)
137
138
catch ( Exception e )
138
139
{
139
140
Context . API . ShowMsg ( Context . API . GetTranslation ( "plugin_pluginsmanager_install_error_title" ) ,
140
- string . Format ( Context . API . GetTranslation ( "plugin_pluginsmanager_install_error_subtitle" ) , plugin . Name ) ) ;
141
+ string . Format ( Context . API . GetTranslation ( "plugin_pluginsmanager_install_error_subtitle" ) ,
142
+ plugin . Name ) ) ;
141
143
142
144
Log . Exception ( "PluginsManager" , "An error occured while downloading plugin" , e , "InstallOrUpdate" ) ;
143
145
@@ -164,7 +166,8 @@ internal List<Result> RequestUpdate(string search)
164
166
from existingPlugin in Context . API . GetAllPlugins ( )
165
167
join pluginFromManifest in pluginsManifest . UserPlugins
166
168
on existingPlugin . Metadata . ID equals pluginFromManifest . ID
167
- where existingPlugin . Metadata . Version . CompareTo ( pluginFromManifest . Version ) < 0 // if current version precedes manifest version
169
+ where existingPlugin . Metadata . Version . CompareTo ( pluginFromManifest . Version ) <
170
+ 0 // if current version precedes manifest version
168
171
select
169
172
new
170
173
{
@@ -214,22 +217,29 @@ on existingPlugin.Metadata.ID equals pluginFromManifest.ID
214
217
215
218
Task . Run ( async delegate
216
219
{
217
- Context . API . ShowMsg ( Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) ,
218
- Context . API . GetTranslation ( "plugin_pluginsmanager_please_wait" ) ) ;
220
+ Context . API . ShowMsg (
221
+ Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) ,
222
+ Context . API . GetTranslation ( "plugin_pluginsmanager_please_wait" ) ) ;
219
223
220
- await Http . DownloadAsync ( x . PluginNewUserPlugin . UrlDownload , downloadToFilePath ) . ConfigureAwait ( false ) ;
224
+ await Http . DownloadAsync ( x . PluginNewUserPlugin . UrlDownload , downloadToFilePath )
225
+ . ConfigureAwait ( false ) ;
221
226
222
- Context . API . ShowMsg ( Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) ,
223
- Context . API . GetTranslation ( "plugin_pluginsmanager_download_success" ) ) ;
227
+ Context . API . ShowMsg (
228
+ Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) ,
229
+ Context . API . GetTranslation ( "plugin_pluginsmanager_download_success" ) ) ;
224
230
225
231
Install ( x . PluginNewUserPlugin , downloadToFilePath ) ;
226
232
227
233
Context . API . RestartApp ( ) ;
228
234
} ) . ContinueWith ( t =>
229
235
{
230
- Log . Exception ( "PluginsManager" , $ "Update failed for { x . Name } ", t . Exception . InnerException , "RequestUpdate" ) ;
231
- Context . API . ShowMsg ( Context . API . GetTranslation ( "plugin_pluginsmanager_install_error_title" ) ,
232
- string . Format ( Context . API . GetTranslation ( "plugin_pluginsmanager_install_error_subtitle" ) , x . Name ) ) ;
236
+ Log . Exception ( "PluginsManager" , $ "Update failed for { x . Name } ",
237
+ t . Exception . InnerException , "RequestUpdate" ) ;
238
+ Context . API . ShowMsg (
239
+ Context . API . GetTranslation ( "plugin_pluginsmanager_install_error_title" ) ,
240
+ string . Format (
241
+ Context . API . GetTranslation ( "plugin_pluginsmanager_install_error_subtitle" ) ,
242
+ x . Name ) ) ;
233
243
} , TaskContinuationOptions . OnlyOnFaulted ) ;
234
244
235
245
return true ;
@@ -264,8 +274,21 @@ internal List<Result> Search(IEnumerable<Result> results, string searchName)
264
274
. ToList ( ) ;
265
275
}
266
276
267
- internal List < Result > RequestInstallOrUpdate ( string searchName )
277
+ private Task _downloadManifestTask = Task . CompletedTask ;
278
+
279
+ internal async ValueTask < List < Result > > RequestInstallOrUpdate ( string searchName , CancellationToken token )
268
280
{
281
+ if ( ! pluginsManifest . UserPlugins . Any ( ) &&
282
+ _downloadManifestTask . Status != TaskStatus . Running )
283
+ {
284
+ _downloadManifestTask = pluginsManifest . DownloadManifest ( ) ;
285
+ }
286
+
287
+ await _downloadManifestTask ;
288
+
289
+ if ( token . IsCancellationRequested )
290
+ return null ;
291
+
269
292
var searchNameWithoutKeyword = searchName . Replace ( Settings . HotKeyInstall , string . Empty ) . Trim ( ) ;
270
293
271
294
var results =
@@ -406,4 +429,4 @@ private List<Result> AutoCompleteReturnAllResults(string search, string hotkey,
406
429
return new List < Result > ( ) ;
407
430
}
408
431
}
409
- }
432
+ }
0 commit comments