From 8179cee6938a6ff98ba7feb4147951b39258e442 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sun, 19 May 2024 16:45:34 +0100 Subject: [PATCH] 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 --- src/ui/Forms/AudioToText/WhisperModelDownload.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ui/Forms/AudioToText/WhisperModelDownload.cs b/src/ui/Forms/AudioToText/WhisperModelDownload.cs index 0f559e2b4..d04ee2197 100644 --- a/src/ui/Forms/AudioToText/WhisperModelDownload.cs +++ b/src/ui/Forms/AudioToText/WhisperModelDownload.cs @@ -103,6 +103,11 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText } } + var progressReport = new Progress((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((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) {