Feat: Update Download Progress

This commit is contained in:
ChsBuffer
2021-01-14 16:28:14 +08:00
parent 2b53a7ca9e
commit 8cf765de92
2 changed files with 18 additions and 7 deletions

View File

@@ -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, @"<td .*>(?<sha256>.*)</td>", 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)
{

View File

@@ -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);
}
}