Improve UX while extracting - extract off main thread, add user feedback while it happens

This commit is contained in:
dginovker 2023-08-26 20:57:49 +09:00
parent 3449d88fb1
commit b2cf0bfb01
2 changed files with 14 additions and 9 deletions

View file

@ -29,16 +29,16 @@ namespace Saradomin.Infrastructure.Services
using (HttpClient httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead);
var contentLength = response.Content.Headers.ContentLength ?? 1;
var contentLength = response.Content.Headers.ContentLength ?? 40 * 1024 * 1024L;
var totalRead = 0L;
var buffer = new byte[8192];
// Create a FileStream to write the downloaded bytes to
using (var fileStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None))
await using (var fileStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (var stream = await response.Content.ReadAsStreamAsync())
await using (var stream = await response.Content.ReadAsStreamAsync())
{
var bytesRead = 0;
int bytesRead;
do
{
bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
@ -53,16 +53,16 @@ namespace Saradomin.Infrastructure.Services
}
}
}
JavaDownloadProgressChanged?.Invoke(this, 1f);
if (Path.GetExtension(downloadUrl) == ".zip")
{
ZipFile.ExtractToDirectory(downloadPath, extractedPath);
await Task.Run(() => ZipFile.ExtractToDirectory(downloadPath, extractedPath));
}
else if (Path.GetExtension(downloadUrl) == ".gz" || Path.GetExtension(downloadUrl) == ".tar.gz")
{
if (!Directory.Exists(extractedPath)) Directory.CreateDirectory(extractedPath);
string extractOutput = CrossPlatform.RunCommandAndGetOutput($"tar xf {downloadPath} -C {extractedPath} --strip-components 1");
Console.WriteLine($"Extract output: {extractOutput}");
await Task.Run(() => CrossPlatform.RunCommandAndGetOutput($"tar xf {downloadPath} -C {extractedPath} --strip-components 1"));
}
File.Delete(downloadPath);

View file

@ -1,6 +1,5 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Threading;
@ -308,6 +307,12 @@ namespace Saradomin.ViewModel.Windows
}
private void OnJavaDownloadProgressUpdated(object sender, float e)
{
if (e >= 0.999f)
{
Console.WriteLine($"Writing about extracting Java");
LaunchText = "Updating... (Extracting Java)";
return;
}
LaunchText = $"Updating... (Downloading Java: {e * 100:F2}%)";
}
}