Skip to content

Commit

Permalink
feat: Adds download speed and real-time network speed on download page.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiguoli committed Jul 13, 2024
1 parent 4176794 commit f61d774
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 5 deletions.
179 changes: 179 additions & 0 deletions .github/workflows/dotnet-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
name: GitHub Release Publisher

on:
workflow_dispatch:

permissions:
contents: write

env:
# Configuring Project
PROJECT_PATH: YOUR_FOLDER/YOUR_APP_NAME.csproj
APP_NAME: YOUR_APP_NAME

# Check Tag
CHECK_TAG_EXISTENCE_BEFORE_CREATING_TAG: false

# Custom Nuget Source
IS_COMMUNITY_TOOLKIT_NUGET_SOURCE_ENABLED: false
IS_CUSTOM_NUGET_SOURCE_ENABLED: false
CUSTOM_NUGET_SOURCES: '' # Example ('https://api.nuget.org/v3/index.json, https://api.nuget.org/v2/index.json,...')

# Configuring Dotnet Build Commands
PUBLISH_OUTPUT_FOLDER: Publish
PUBLISH_SELF_CONTAINED: false
PUBLISH_SINGLE_FILE: false
PUBLISH_READY_TO_RUN: false
PUBLISH_AOT: false # currently only Console App Supported
PUBLISH_TRIMMED: false
PUBLISH_TRIM_MODE: partial # or full

# Configuring GitHub Release
IS_PRE_RELEASE: false
SKIP_IF_RELEASE_EXIST: true
MAKE_LATEST: true
ALLOW_UPDATES: false
ARTIFACT_ERRORS_FAIL_BUILD: false

jobs:
build:
runs-on: windows-latest
outputs: # For accessing them from 'release' job
app-version: ${{ steps.get-version.outputs.version }}
IS_PRE_RELEASE: ${{ env.IS_PRE_RELEASE }}
strategy:
matrix:
platform: [x64, x86, arm64] # Change platform if you want to build only a specific platform

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

# Add CommunityToolkit Labs and Main nuget sources
- run: |
nuget sources add -name CommunityToolkit-Labs -source https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-Labs/nuget/v3/index.json
nuget sources add -name CommunityToolkit-Main -source https://pkgs.dev.azure.com/dotnet/CommunityToolkit/_packaging/CommunityToolkit-MainLatest/nuget/v3/index.json
if: contains(env.IS_COMMUNITY_TOOLKIT_NUGET_SOURCE_ENABLED, 'true')
# Add Custom nuget sources
- run: |
$sources = $env:CUSTOM_NUGET_SOURCES -split ','
$trimmedSources = $sources | ForEach-Object { $_.Trim() }
$prefix = "CUSTOM_SOURCES_"
for ($i = 0; $i -lt $trimmedSources.Length; $i++) {
$sourceName = "${prefix}$($i + 1)"
nuget sources add -name $sourceName -source $trimmedSources[$i]
}
if: contains(env.IS_CUSTOM_NUGET_SOURCE_ENABLED, 'true')
# Reading version tag from the csproj file.
- uses: kzrnm/get-net-sdk-project-versions-action@v2
id: get-version
with:
proj-path: ${{ env.PROJECT_PATH }}

# Building with configured commands
- run: |
$runtimeIdentifier = "${{ matrix.platform }}"
dotnet publish ${{ env.PROJECT_PATH }} -c Release -r win-$($runtimeIdentifier.ToLower()) /p:GITHUB_ACTIONS=true -p:Platform=${{ matrix.platform }} --self-contained ${{ env.PUBLISH_SELF_CONTAINED }} -p:PublishSingleFile=${{ env.PUBLISH_SINGLE_FILE }} -p:PublishReadyToRun=${{ env.PUBLISH_READY_TO_RUN }} -p:PublishTrimmed=${{ env.PUBLISH_TRIMMED }} -p:TrimMode=${{ env.PUBLISH_TRIM_MODE }} -p:PublishAot=${{ env.PUBLISH_AOT }} --output ${{ env.PUBLISH_OUTPUT_FOLDER }}/${{ matrix.platform }}
# Zipping folder and all files
- uses: vimtor/[email protected]
with:
files: ${{ env.PUBLISH_OUTPUT_FOLDER }}/${{ matrix.platform }}
recursive: true
dest: ${{ env.APP_NAME }}-v${{ steps.get-version.outputs.version }}-${{ matrix.platform }}.zip

# Uploading all zip files to access them in the 'release' job
- uses: actions/upload-artifact@v4
with:
name: artifacts-${{ matrix.platform }}
path: ${{ env.APP_NAME }}-v${{ steps.get-version.outputs.version }}-${{ matrix.platform }}.zip

# Checking version suffix for words like [alpha, beta, preview, and experiment]. Marking the release as a pre-release if any exists.
- run: |
# Define the list of strings
$list = @("beta", "alpha", "preview", "experiment")
# Define the suffix variable
$suffix = "${{ steps.get-version.outputs.version-suffix }}"
foreach ($item in $list) {
# Convert both strings to lower case for case-insensitive comparison
if ($suffix.ToLower().StartsWith($item.ToLower())) {
echo "IS_PRE_RELEASE=true" >> $env:GITHUB_ENV
break
}
}
release:
needs: build
runs-on: ubuntu-latest
env:
# Read some variables from the 'build' job
APP_VERSION: ${{ needs.build.outputs.app-version }}
IS_PRE_RELEASE: ${{ needs.build.outputs.IS_PRE_RELEASE }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

# Downloading all zip files into the GitHub root directory (uploaded in 'build' job)
- uses: actions/download-artifact@v4
with:
merge-multiple: true

# Configuring git to create a tag
- run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Actions"
TAG_NAME="v${{ env.APP_VERSION }}"
if [[ "${{ env.CHECK_TAG_EXISTENCE_BEFORE_CREATING_TAG }}" == "true" ]]; then
git fetch --tags
if [[ $(git tag -l "$TAG_NAME") ]]; then
echo "Tag found (already exist). Skipping to the next step"
else
echo "Tag not found, creating new tag"
git tag "$TAG_NAME"
git push origin "$TAG_NAME"
fi
else
echo "Creating new tag"
git tag "$TAG_NAME"
git push origin "$TAG_NAME"
fi
# Installing a package for generating a changelog file
- run: npm install -g auto-changelog
- run: auto-changelog --tag-pattern .+ --commit-limit false --starting-version v${{ env.APP_VERSION }} --ending-version v${{ env.APP_VERSION }} --hide-credit

# Creating a Release in GitHub
- uses: ncipollo/release-action@v1
with:
artifacts: "${{ env.APP_NAME }}-v${{ env.APP_VERSION }}-*.zip"
bodyFile: "CHANGELOG.md"
name: v${{ env.APP_VERSION }}
tag: v${{ env.APP_VERSION }}
prerelease: ${{ env.IS_PRE_RELEASE }}
skipIfReleaseExists: ${{ env.SKIP_IF_RELEASE_EXIST }}
makeLatest: ${{ env.MAKE_LATEST }}
allowUpdates: ${{ env.ALLOW_UPDATES }}
artifactErrorsFailBuild: ${{ env.ARTIFACT_ERRORS_FAIL_BUILD }}



# - uses: softprops/action-gh-release@v1
# with:
# name: v${{ env.APP_VERSION }}
# tag_name: v${{ env.APP_VERSION }}
# body_path: CHANGELOG.md
# prerelease: ${{ env.PUBLISH_PRE_RELEASE }}
# files: |
# ${{ env.APP_NAME }}-v${{ env.APP_VERSION }}-*.zip
12 changes: 11 additions & 1 deletion SimpleList/Pages/TaskManagerPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
<Grid.Resources>
<converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
<converters:BoolToObjectConverter x:Key="BoolToObjectConverter" TrueValue="Collapsed" FalseValue="Visible" />
<converters:FileSizeToFriendlyStringConverter x:Key="FileSizeToFriendlyStringConverter" />
<DataTemplate x:Key="DownloadTaskTemplate" x:DataType="vm:DownloadTaskViewModel">
<Grid Margin="0 10 0 10">
<Grid.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem x:Uid="TaskManagerPage_Resume" Command="{Binding ResumeDownloadCommand}" IsEnabled="{Binding IsPaused}" />
<MenuFlyoutItem x:Uid="TaskManagerPage_Remove" Command="{Binding CancelTaskCommand}" />
<MenuFlyoutItem x:Uid="TaskManagerPage_OpenFolder" Command="{Binding OpenFolderCommand}" />
</MenuFlyout>
</Grid.ContextFlyout>
<Grid.ColumnDefinitions>
Expand All @@ -29,6 +31,14 @@
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" FontSize="20" Margin="0 5 0 5" />
<ProgressBar Value="{Binding Progress}" Visibility="{Binding Completed, Converter={StaticResource BoolToObjectConverter}}" />
<TextBlock>
<Run Text="{Binding DownloadedBytes, Converter={StaticResource FileSizeToFriendlyStringConverter}}" />
<Run Text=" / " />
<Run Text="{Binding TotalBytes, Converter={StaticResource FileSizeToFriendlyStringConverter}}" />
<Run Text=" " />
<Run Text="{Binding DownloadSpeed, Converter={StaticResource FileSizeToFriendlyStringConverter}}" />
<Run Text="/s" />
</TextBlock>
<TextBlock Text="Download completed" Visibility="{Binding Completed, Converter={StaticResource BoolToVisibilityConverter}}" />
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Horizontal" Margin="10 0 10 0">
Expand Down Expand Up @@ -73,7 +83,7 @@
</Grid>
</DataTemplate>
</Grid.Resources>

<Pivot>
<PivotItem x:Uid="TaskManagerPage_Pivot_Download">
<ListView ItemsSource="{Binding DownloadTasks}" ItemTemplate="{StaticResource DownloadTaskTemplate}" />
Expand Down
2 changes: 1 addition & 1 deletion SimpleList/SimpleList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<AssemblyName>SimpleList</AssemblyName>
<Authors>Thawne</Authors>
<PackageProjectUrl>https://github.com/aiguoli/SimpleList</PackageProjectUrl>
<AssemblyVersion>1.13.0</AssemblyVersion>
<AssemblyVersion>1.13.1</AssemblyVersion>
<ApplicationIcon>Assets\favicon.ico</ApplicationIcon>
<PackageIcon>128.png</PackageIcon>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
3 changes: 3 additions & 0 deletions SimpleList/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@
<data name="ShareCommunityPage_Flyout_Refresh.Text" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="TaskManagerPage_OpenFolder.Text" xml:space="preserve">
<value>Open Folder</value>
</data>
<data name="TaskManagerPage_Pivot_Download.Header" xml:space="preserve">
<value>Download</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions SimpleList/Strings/zh-CN/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@
<data name="String1" xml:space="preserve">
<value />
</data>
<data name="TaskManagerPage_OpenFolder.Text" xml:space="preserve">
<value>打开所在文件夹</value>
</data>
<data name="TaskManagerPage_Pivot_Download.Header" xml:space="preserve">
<value>下载</value>
</data>
Expand Down
29 changes: 26 additions & 3 deletions SimpleList/ViewModels/DownloadTaskViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ private void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

private void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
// If double data type is used here, it will cause the progress control to handle additional data, resulting in the page being stuck.
_dispatcher.TryEnqueue(() => Progress = (int)e.ProgressPercentage);
if (DateTime.Now - _lastUpdate >= _updateInterval)
{
_lastUpdate = DateTime.Now;
// If double data type is used here, it will cause the progress control to handle additional data, resulting in the page being stuck.
_dispatcher.TryEnqueue(() =>
{
Progress = (int)e.ProgressPercentage;
DownloadedBytes = e.ReceivedBytesSize;
TotalBytes = e.TotalBytesToReceive;
DownloadSpeed = (long)e.BytesPerSecondSpeed;
});
}
}

[RelayCommand]
Expand Down Expand Up @@ -77,7 +87,8 @@ public async Task ResumeDownload()
if (_pack != null)
{
await _downloader.DownloadFileTaskAsync(_pack);
} else
}
else
{
_downloader.Resume();
}
Expand All @@ -94,7 +105,16 @@ public async Task CancelTaskAsync()
_manager.RemoveSelectedDownloadTasks(this);
}

[RelayCommand]
public void OpenFolder()
{
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{_file.Path}\"");
}

public static readonly int chunkSize = 1024 * 1024; // 1MB chunks
// The download speed is updated every second
private DateTime _lastUpdate;
private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(1000);
private readonly string _itemId;
private readonly StorageFile _file;
private DriveViewModel Drive { get; }
Expand All @@ -106,6 +126,9 @@ public async Task CancelTaskAsync()
[ObservableProperty] private bool _completed = false;
[ObservableProperty] private bool _isDownloading = true;
[ObservableProperty] private bool _isPaused = false;
[ObservableProperty] private long _downloadedBytes = 0;
[ObservableProperty] private long _totalBytes = 0;
[ObservableProperty] private long _downloadSpeed = 0;

public DateTime StartTime { get; private set; }
public DateTime FinishTime { get; private set; }
Expand Down

0 comments on commit f61d774

Please sign in to comment.