Refactor progress report in WhisperModelDownload

This commit optimizes the code in WhisperModelDownload.cs by pulling out the progress reporting into a separate variable. This new implementation enhances code readability and prevents code redundancy. Now, the 'progressReport' variable is used while calling the 'DownloadAsync' method.

Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-05-19 16:45:34 +01:00
parent 9d9f55de68
commit 8179cee693

View File

@ -103,6 +103,11 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
}
}
var progressReport = new Progress<float>((progress) =>
{
var pct = (int)Math.Round(progress * 100.0, MidpointRounding.AwayFromZero);
labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait + " " + pct + "%";
});
foreach (var url in LastDownloadedModel.Urls)
{
using (var httpClient = DownloaderFactory.MakeHttpClient())
@ -112,11 +117,8 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
labelFileName.Text = url.Split('/').Last();
using (var downloadStream = new FileStream(_downloadFileName, FileMode.Create, FileAccess.Write))
{
var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress<float>((progress) =>
{
var pct = (int)Math.Round(progress * 100.0, MidpointRounding.AwayFromZero);
labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait + " " + pct + "%";
}), _cancellationTokenSource.Token);
var downloadTask = httpClient.DownloadAsync(url, downloadStream, progressReport,
_cancellationTokenSource.Token);
while (!downloadTask.IsCompleted && !downloadTask.IsCanceled)
{