Skip to content

Commit 646bad6

Browse files
committed
Use parameter for function for readability
1 parent c32435f commit 646bad6

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
148148

149149
if (!plugin.IsFromLocalInstallPath)
150150
{
151-
await DeleteFileAndDownloadMsgBoxAsync(
151+
await DownloadFileAsync(
152152
$"{Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin")} {plugin.Name}",
153153
plugin.UrlDownload, filePath, cts);
154154
}
@@ -199,32 +199,39 @@ await DeleteFileAndDownloadMsgBoxAsync(
199199
}
200200
}
201201

202-
private async Task DeleteFileAndDownloadMsgBoxAsync(string prgBoxTitle, string downloadUrl, string filePath, CancellationTokenSource cts)
202+
private async Task DownloadFileAsync(string prgBoxTitle, string downloadUrl, string filePath, CancellationTokenSource cts, bool deleteFile = true, bool showProgress = true)
203203
{
204-
if (File.Exists(filePath))
204+
if (deleteFile && File.Exists(filePath))
205205
File.Delete(filePath);
206206

207-
var exceptionHappened = false;
208-
await Context.API.ShowProgressBoxAsync(prgBoxTitle,
209-
async (reportProgress) =>
210-
{
211-
if (reportProgress == null)
212-
{
213-
// when reportProgress is null, it means there is expcetion with the progress box
214-
// so we record it with exceptionHappened and return so that progress box will close instantly
215-
exceptionHappened = true;
216-
return;
217-
}
218-
else
207+
if (showProgress)
208+
{
209+
var exceptionHappened = false;
210+
await Context.API.ShowProgressBoxAsync(prgBoxTitle,
211+
async (reportProgress) =>
219212
{
220-
await Context.API.HttpDownloadAsync(downloadUrl, filePath, reportProgress, cts.Token).ConfigureAwait(false);
221-
}
222-
}, cts.Cancel);
213+
if (reportProgress == null)
214+
{
215+
// when reportProgress is null, it means there is expcetion with the progress box
216+
// so we record it with exceptionHappened and return so that progress box will close instantly
217+
exceptionHappened = true;
218+
return;
219+
}
220+
else
221+
{
222+
await Context.API.HttpDownloadAsync(downloadUrl, filePath, reportProgress, cts.Token).ConfigureAwait(false);
223+
}
224+
}, cts.Cancel);
223225

224-
// if exception happened while downloading and user does not cancel downloading,
225-
// we need to redownload the plugin
226-
if (exceptionHappened && (!cts.IsCancellationRequested))
227-
await Context.API.HttpDownloadAsync(downloadUrl, filePath).ConfigureAwait(false);
226+
// if exception happened while downloading and user does not cancel downloading,
227+
// we need to redownload the plugin
228+
if (exceptionHappened && (!cts.IsCancellationRequested))
229+
await Context.API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false);
230+
}
231+
else
232+
{
233+
await Context.API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false);
234+
}
228235
}
229236

230237
internal async ValueTask<List<Result>> RequestUpdateAsync(string search, CancellationToken token,
@@ -318,7 +325,7 @@ where string.Compare(existingPlugin.Metadata.Version, pluginUpdateSource.Version
318325

319326
if (!x.PluginNewUserPlugin.IsFromLocalInstallPath)
320327
{
321-
await DeleteFileAndDownloadMsgBoxAsync(
328+
await DownloadFileAsync(
322329
$"{Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin")} {x.PluginNewUserPlugin.Name}",
323330
x.PluginNewUserPlugin.UrlDownload, downloadToFilePath, cts);
324331
}
@@ -418,7 +425,7 @@ await Task.WhenAll(resultsForUpdate.Select(async plugin =>
418425
{
419426
using var cts = new CancellationTokenSource();
420427

421-
await DeleteFileAndDownloadMsgBoxAsync(
428+
await DownloadFileAsync(
422429
$"{Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin")} {plugin.PluginNewUserPlugin.Name}",
423430
plugin.PluginNewUserPlugin.UrlDownload, downloadToFilePath, cts);
424431

0 commit comments

Comments
 (0)