Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0 progress and status reporting #27

Merged
merged 6 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public class Constants

public const string SupportURl = "https://bento.me/nor0x";
public const string GithubUrl = "https://github.com/nor0x/Dots";

public const string DownloadingText = "Downloading...";
public const string InstallingText = "Installing...";
public const string UninstallingText = "Uninstalling...";
public const string OpeningText = "Opening...";
}
62 changes: 15 additions & 47 deletions src/Dots.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<TargetFramework>net8.0</TargetFramework>
<OutputType>WinExe</OutputType>
<ApplicationIcon>Assets/appicon.ico</ApplicationIcon>
<Version>2.0.0</Version>
</PropertyGroup>

<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows')) and '$(Configuration)' == 'Release'">
Expand Down Expand Up @@ -67,58 +66,27 @@


<ItemGroup>
<PackageReference
Include="Avalonia"
Version="11.0.5-rc1" />
<PackageReference
Include="Avalonia.Desktop"
Version="11.0.5-rc1" />
<PackageReference
Include="Avalonia.Themes.Fluent"
Version="11.0.5-rc1" />
<PackageReference
Include="Avalonia.Fonts.Inter"
Version="11.0.5-rc1" />
<PackageReference
Include="Avalonia.Xaml.Behaviors"
Version="11.0.2" />
<PackageReference Include="Avalonia" Version="11.0.7" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.7" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.7" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.0.7" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.0.6" />
<!--Condition
below is needed to remove Avalonia.Diagnostics package from build output in Release
configuration.-->
<PackageReference
Condition="'$(Configuration)' == 'Debug'"
Include="Avalonia.Diagnostics"
Version="11.0.5-rc1" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.7" />
</ItemGroup>

<ItemGroup>
<PackageReference
Include="System.CommandLine"
Version="2.0.0-beta4.22272.1" />
<PackageReference
Include="System.Reactive"
Version="6.0.1-preview.1" />
<PackageReference
Include="M.BindableProperty.Generator"
Version="0.11.1" />
<PackageReference
Include="CliWrap"
Version="3.6.4" />
<PackageReference
Include="CommunityToolkit.Mvvm"
Version="8.2.1" />
<PackageReference
Include="akavache"
Version="9.1.20" />
<PackageReference
Include="HyperText.Avalonia"
Version="11.0.0-rc1" />
<PackageReference
Include="AnimatedImage.Avalonia"
Version="1.0.3" />
<PackageReference
Include="Dotnet.Bundle"
Version="*" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Reactive" Version="6.0.1-preview.1" />
<PackageReference Include="M.BindableProperty.Generator" Version="0.11.1" />
<PackageReference Include="CliWrap" Version="3.6.6" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="akavache" Version="9.1.20" />
<PackageReference Include="HyperText.Avalonia" Version="11.0.0-rc1" />
<PackageReference Include="AnimatedImage.Avalonia" Version="1.0.3" />
<PackageReference Include="Dotnet.Bundle" Version="*" />
</ItemGroup>

</Project>
51 changes: 51 additions & 0 deletions src/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Dots.Helpers;
Expand Down Expand Up @@ -145,4 +148,52 @@ public static void OpenFilePath(this string path)
Process.Start("open", path);
return;
}


//credits https://gist.github.com/dalexsoto/9fd3c5bdbe9f61a717d47c5843384d11
public static async Task DownloadDataAsync(this HttpClient client, string requestUrl, Stream destination, IProgress<float> progress = null, CancellationToken cancellationToken = default(CancellationToken))
{
using (var response = await client.GetAsync(requestUrl, HttpCompletionOption.ResponseHeadersRead))
{
var contentLength = response.Content.Headers.ContentLength;
using (var download = await response.Content.ReadAsStreamAsync())
{
// no progress... no contentLength... very sad
if (progress is null || !contentLength.HasValue)
{
await download.CopyToAsync(destination);
return;
}
// Such progress and contentLength much reporting Wow!
var progressWrapper = new Progress<long>(totalBytes => progress.Report(GetProgressPercentage(totalBytes, contentLength.Value)));
await download.CopyToAsync(destination, 81920, progressWrapper, cancellationToken);
}
}

float GetProgressPercentage(float totalBytes, float currentBytes) => (totalBytes / currentBytes) * 100f;
}

static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<long> progress = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (bufferSize < 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize));
if (source is null)
throw new ArgumentNullException(nameof(source));
if (!source.CanRead)
throw new InvalidOperationException($"'{nameof(source)}' is not readable.");
if (destination == null)
throw new ArgumentNullException(nameof(destination));
if (!destination.CanWrite)
throw new InvalidOperationException($"'{nameof(destination)}' is not writable.");

var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead);
}
}
}
Loading