diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs index ae5d7de2..e648b333 100644 --- a/Netch/Controllers/UpdateChecker.cs +++ b/Netch/Controllers/UpdateChecker.cs @@ -19,7 +19,7 @@ namespace Netch.Controllers public const string Name = @"Netch"; public const string Copyright = @"Copyright © 2019 - 2020"; - public const string AssemblyVersion = @"1.7.0"; + public const string AssemblyVersion = @"1.7.1"; private const string Suffix = @""; public static readonly string Version = $"{AssemblyVersion}{(string.IsNullOrEmpty(Suffix) ? "" : $"-{Suffix}")}"; @@ -68,10 +68,12 @@ namespace Netch.Controllers } } - public static async Task UpdateNetch() + public static async Task UpdateNetch(DownloadProgressChangedEventHandler onDownloadProgressChanged) { + using WebClient client = new(); + var latestVersionDownloadUrl = LatestRelease.assets[0].browser_download_url; - var tagPage = await WebUtil.DownloadStringAsync(WebUtil.CreateRequest(LatestVersionUrl)); + var tagPage = await client.DownloadStringTaskAsync(LatestVersionUrl); var match = Regex.Match(tagPage, @"(?.*)", RegexOptions.Singleline); // TODO Replace with regex get basename and sha256 @@ -92,11 +94,11 @@ namespace Netch.Controllers File.Delete(fileFullPath); } - try { - // TODO Replace "New Version Found" to Progress bar - await WebUtil.DownloadFileAsync(WebUtil.CreateRequest(latestVersionDownloadUrl), fileFullPath); + client.DownloadProgressChanged += onDownloadProgressChanged; + await client.DownloadFileTaskAsync(new Uri(latestVersionDownloadUrl), fileFullPath); + client.DownloadProgressChanged -= onDownloadProgressChanged; } catch (Exception e) { diff --git a/Netch/Forms/MainForm.Misc.cs b/Netch/Forms/MainForm.Misc.cs index b89f4092..05adee06 100644 --- a/Netch/Forms/MainForm.Misc.cs +++ b/Netch/Forms/MainForm.Misc.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Net; using System.Windows.Forms; using Netch.Controllers; using Netch.Utils; @@ -30,12 +31,20 @@ namespace Netch.Forms return; NotifyTip(i18N.Translate("Start downloading new version")); + NewVersionLabel.Enabled = false; + NewVersionLabel.Text = "..."; try { - await UpdateChecker.UpdateNetch(); + DownloadProgressChangedEventHandler OnDownloadProgressChanged() + { + return (_, args) => { BeginInvoke(new Action(() => { NewVersionLabel.Text = $"{args.ProgressPercentage}%"; })); }; + } + + await UpdateChecker.UpdateNetch(OnDownloadProgressChanged()); } catch (Exception exception) { + Logging.Error(exception.Message); NotifyTip(exception.Message); } }