Skip to content

Commit 297d191

Browse files
committed
Use function to delegate the progress task
1 parent ed7265d commit 297d191

File tree

5 files changed

+74
-79
lines changed

5 files changed

+74
-79
lines changed

Flow.Launcher.Plugin/IProgressBoxEx.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,13 @@ public interface IPublicAPI
322322
/// If there is issue when showing the message box, it will return null.
323323
/// </summary>
324324
/// <param name="caption">The caption of the message box.</param>
325+
/// <param name="reportProgressAsync">
326+
/// Time-consuming task function, whose input is the action to report progress.
327+
/// The input of the action is the progress value which is a double value between 0 and 100.
328+
/// If there are any exceptions, this action will be null.
329+
/// </param>
325330
/// <param name="forceClosed">When user closes the progress box manually by button or esc key, this action will be called.</param>
326331
/// <returns>A progress box interface.</returns>
327-
public IProgressBoxEx ShowProgressBox(string caption, Action forceClosed = null);
332+
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action forceClosed = null);
328333
}
329334
}

Flow.Launcher/ProgressBoxEx.xaml.cs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
using System.Windows;
44
using System.Windows.Input;
55
using Flow.Launcher.Infrastructure.Logger;
6-
using Flow.Launcher.Plugin;
76

87
namespace Flow.Launcher
98
{
10-
public partial class ProgressBoxEx : Window, IProgressBoxEx
9+
public partial class ProgressBoxEx : Window
1110
{
1211
private readonly Action _forceClosed;
1312

@@ -17,31 +16,48 @@ private ProgressBoxEx(Action forceClosed)
1716
InitializeComponent();
1817
}
1918

20-
public static IProgressBoxEx Show(string caption, Action forceClosed = null)
19+
public static async Task ShowAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action forceClosed = null)
2120
{
22-
if (!Application.Current.Dispatcher.CheckAccess())
23-
{
24-
return Application.Current.Dispatcher.Invoke(() => Show(caption, forceClosed));
25-
}
26-
21+
ProgressBoxEx prgBox = null;
2722
try
2823
{
29-
var prgBox = new ProgressBoxEx(forceClosed)
24+
if (!Application.Current.Dispatcher.CheckAccess())
3025
{
31-
Title = caption
32-
};
33-
prgBox.TitleTextBlock.Text = caption;
34-
prgBox.Show();
35-
return prgBox;
26+
await Application.Current.Dispatcher.InvokeAsync(() =>
27+
{
28+
prgBox = new ProgressBoxEx(forceClosed)
29+
{
30+
Title = caption
31+
};
32+
prgBox.TitleTextBlock.Text = caption;
33+
prgBox.Show();
34+
});
35+
}
36+
37+
await reportProgressAsync(prgBox.ReportProgress).ConfigureAwait(false);
3638
}
3739
catch (Exception e)
3840
{
3941
Log.Error($"|ProgressBoxEx.Show|An error occurred: {e.Message}");
40-
return null;
42+
43+
await reportProgressAsync(null).ConfigureAwait(false);
44+
}
45+
finally
46+
{
47+
if (!Application.Current.Dispatcher.CheckAccess())
48+
{
49+
await Application.Current.Dispatcher.InvokeAsync(async () =>
50+
{
51+
if (prgBox != null)
52+
{
53+
await prgBox.CloseAsync();
54+
}
55+
});
56+
}
4157
}
4258
}
4359

44-
public void ReportProgress(double progress)
60+
private void ReportProgress(double progress)
4561
{
4662
if (!Application.Current.Dispatcher.CheckAccess())
4763
{
@@ -64,7 +80,7 @@ public void ReportProgress(double progress)
6480
}
6581
}
6682

67-
public async Task CloseAsync()
83+
private async Task CloseAsync()
6884
{
6985
if (!Application.Current.Dispatcher.CheckAccess())
7086
{

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public bool IsGameModeOn()
324324
public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.OK) =>
325325
MessageBoxEx.Show(messageBoxText, caption, button, icon, defaultResult);
326326

327-
public IProgressBoxEx ShowProgressBox(string caption, Action forceClosed = null) => ProgressBoxEx.Show(caption, forceClosed);
327+
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action forceClosed = null) => ProgressBoxEx.ShowAsync(caption, reportProgressAsync, forceClosed);
328328

329329
#endregion
330330

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

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
144144

145145
var filePath = Path.Combine(Path.GetTempPath(), downloadFilename);
146146

147-
IProgressBoxEx prgBox = null;
148147
var downloadCancelled = false;
149148
try
150149
{
@@ -162,42 +161,45 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
162161
var canReportProgress = totalBytes != -1;
163162

164163
var prgBoxTitle = $"{Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin")} {plugin.Name}";
165-
if (canReportProgress &&
166-
(prgBox = Context.API.ShowProgressBox(prgBoxTitle, () =>
167-
{
168-
if (prgBox != null)
169-
{
170-
cts.Cancel();
171-
downloadCancelled = true;
172-
}
173-
})) != null)
164+
if (canReportProgress)
174165
{
175-
await using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
176-
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
177-
178-
var buffer = new byte[8192];
179-
long totalRead = 0;
180-
int read;
166+
await Context.API.ShowProgressBoxAsync(prgBoxTitle,
167+
async (reportProgress) =>
168+
{
169+
if (reportProgress == null)
170+
{
171+
// cannot use progress box
172+
await Http.DownloadAsync(plugin.UrlDownload, filePath).ConfigureAwait(false);
173+
}
174+
else
175+
{
176+
await using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
177+
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
181178

182-
while ((read = await contentStream.ReadAsync(buffer).ConfigureAwait(false)) > 0)
183-
{
184-
await fileStream.WriteAsync(buffer.AsMemory(0, read)).ConfigureAwait(false);
185-
totalRead += read;
179+
var buffer = new byte[8192];
180+
long totalRead = 0;
181+
int read;
186182

187-
var progressValue = totalRead * 100 / totalBytes;
183+
while ((read = await contentStream.ReadAsync(buffer).ConfigureAwait(false)) > 0)
184+
{
185+
await fileStream.WriteAsync(buffer.AsMemory(0, read)).ConfigureAwait(false);
186+
totalRead += read;
188187

189-
// check if user cancelled download before reporting progress
190-
if (downloadCancelled)
191-
return;
192-
else
193-
prgBox.ReportProgress(progressValue);
194-
}
188+
var progressValue = totalRead * 100 / totalBytes;
195189

196-
// check if user cancelled download before closing progress box
197-
if (downloadCancelled)
198-
return;
199-
else
200-
await prgBox?.CloseAsync();
190+
// check if user cancelled download before reporting progress
191+
if (downloadCancelled)
192+
return;
193+
else
194+
reportProgress(progressValue);
195+
}
196+
}
197+
},
198+
() =>
199+
{
200+
cts.Cancel();
201+
downloadCancelled = true;
202+
});
201203
}
202204
else
203205
{
@@ -217,9 +219,6 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
217219
}
218220
catch (HttpRequestException e)
219221
{
220-
// force close progress box
221-
await prgBox?.CloseAsync();
222-
223222
// show error message
224223
Context.API.ShowMsgError(
225224
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_downloading_plugin"), plugin.Name),
@@ -230,9 +229,6 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
230229
}
231230
catch (Exception e)
232231
{
233-
// force close progress box
234-
await prgBox?.CloseAsync();
235-
236232
// show error message
237233
Context.API.ShowMsgError(Context.API.GetTranslation("plugin_pluginsmanager_install_error_title"),
238234
string.Format(Context.API.GetTranslation("plugin_pluginsmanager_install_error_subtitle"),

0 commit comments

Comments
 (0)