Skip to content

Commit c06ba59

Browse files
committed
Add support for cancelling download
1 parent f69dd0f commit c06ba59

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
143143
var filePath = Path.Combine(Path.GetTempPath(), downloadFilename);
144144

145145
IProgressBoxEx prgBox = null;
146+
var downloadCancelled = false;
146147
try
147148
{
148149
if (!plugin.IsFromLocalInstallPath)
@@ -158,7 +159,13 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
158159
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
159160
var canReportProgress = totalBytes != -1;
160161

161-
if (canReportProgress && (prgBox = Context.API.ShowProgressBox($"Download {plugin.Name}...")) != null)
162+
if (canReportProgress &&
163+
(prgBox = Context.API.ShowProgressBox($"Download {plugin.Name}...", () =>
164+
{
165+
httpClient.CancelPendingRequests();
166+
downloadCancelled = true;
167+
prgBox = null;
168+
})) != null)
162169
{
163170
await using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
164171
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
@@ -173,27 +180,38 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
173180
totalRead += read;
174181

175182
var progressValue = totalRead * 100 / totalBytes;
183+
184+
// check if user cancelled download before reporting progress
185+
if (downloadCancelled)
186+
return;
187+
176188
prgBox.ReportProgress(progressValue);
177189
}
178190

191+
// check if user cancelled download before closing progress box
192+
if (downloadCancelled)
193+
return;
194+
179195
Application.Current.Dispatcher.Invoke(() =>
180196
{
181197
prgBox.Close();
182198
prgBox = null;
183199
});
200+
201+
Install(plugin, filePath);
184202
}
185203
else
186204
{
187205
await Http.DownloadAsync(plugin.UrlDownload, filePath).ConfigureAwait(false);
206+
Install(plugin, filePath);
188207
}
189208
}
190209
else
191210
{
192211
filePath = plugin.LocalInstallPath;
193-
}
194-
195212
Install(plugin, filePath);
196213
}
214+
}
197215
catch (HttpRequestException e)
198216
{
199217
Context.API.ShowMsgError(

0 commit comments

Comments
 (0)