From f02aaab960ee9226bd01afd62faffd999b2898f0 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Sat, 17 Aug 2024 17:23:04 +0300 Subject: [PATCH] Migrate to common pipelines, bump dependencies in demo --- .github/workflows/build-publish.yml | 21 ++++ .github/workflows/build.yml | 21 ---- .github/workflows/publish.yml | 24 ---- .../AsyncImageLoader.Avalonia.Demo.csproj | 12 +- .../AdvancedImage.axaml.cs | 117 ++++++++++-------- .../AsyncImageLoader.Avalonia.csproj | 6 +- 6 files changed, 90 insertions(+), 111 deletions(-) create mode 100644 .github/workflows/build-publish.yml delete mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/build-publish.yml b/.github/workflows/build-publish.yml new file mode 100644 index 0000000..d34a7e9 --- /dev/null +++ b/.github/workflows/build-publish.yml @@ -0,0 +1,21 @@ +name: Build and publish + +on: + push: + branches: + - master + - main + - release/** + paths-ignore: + - Material.Avalonia.Demo*/** + tags: + - v** + +jobs: + build-and-test: + uses: SKProCH/CommonWorkflows/.github/workflows/build-publish.yml@main + secrets: + NUGET_KEY: ${{ secrets.NUGET_KEY }} + with: + publish-nightly: false + dotnet-version: 8 \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 9ee5a07..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: .NET Build - -on: - push: - pull_request: - branches: - - '**:**' - -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - name: Setup .NET Core - uses: actions/setup-dotnet@v1 - with: - dotnet-version: 6.0.x - - name: Restore dependencies - run: dotnet restore - - name: Build with dotnet - run: dotnet build --configuration Release \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index c4f93e6..0000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: .NET Publish - -on: - push: - tags: - - '*' - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Setup .NET - uses: actions/setup-dotnet@v1 - with: - dotnet-version: 6.0.x - - name: Build and Pack - run: dotnet build AsyncImageLoader.Avalonia --configuration Release - - name: Publish to Nuget - run: dotnet nuget push "AsyncImageLoader.Avalonia/bin/Release/*.nupkg" --api-key ${{secrets.NUGET_KEY}} --source https://api.nuget.org/v3/index.json --skip-duplicate - - name: Publish to GitHub Packages - run: dotnet nuget push "AsyncImageLoader.Avalonia/bin/Release/*.nupkg" --api-key ${{secrets.GITHUB_TOKEN}} --source https://nuget.pkg.github.com/AvaloniaUtils/index.json --skip-duplicate diff --git a/AsyncImageLoader.Avalonia.Demo/AsyncImageLoader.Avalonia.Demo.csproj b/AsyncImageLoader.Avalonia.Demo/AsyncImageLoader.Avalonia.Demo.csproj index 4f56d20..e5eed58 100644 --- a/AsyncImageLoader.Avalonia.Demo/AsyncImageLoader.Avalonia.Demo.csproj +++ b/AsyncImageLoader.Avalonia.Demo/AsyncImageLoader.Avalonia.Demo.csproj @@ -1,7 +1,7 @@  WinExe - net6.0 + net8.0 enable @@ -9,11 +9,11 @@ - - - - - + + + + + diff --git a/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs b/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs index 4dc9997..34301d8 100644 --- a/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs +++ b/AsyncImageLoader.Avalonia/AdvancedImage.axaml.cs @@ -8,7 +8,7 @@ using Avalonia.Media.Imaging; using Avalonia.Platform; -namespace AsyncImageLoader; +namespace AsyncImageLoader; public class AdvancedImage : ContentControl { @@ -177,8 +177,10 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang base.OnPropertyChanged(change); } - private void ClearSourceIfUserProvideImage() { - if (CurrentImage is not null and not ImageWrapper) { + private void ClearSourceIfUserProvideImage() + { + if (CurrentImage is not null and not ImageWrapper) + { // User provided image himself Source = null; } @@ -186,7 +188,7 @@ private void ClearSourceIfUserProvideImage() { private async void UpdateImage(string? source, IAsyncImageLoader? loader) { - var cancellationTokenSource = new CancellationTokenSource(); + var cancellationTokenSource = new CancellationTokenSource(); var oldCancellationToken = Interlocked.Exchange(ref _updateCancellationToken, cancellationTokenSource); @@ -197,59 +199,59 @@ private async void UpdateImage(string? source, IAsyncImageLoader? loader) catch (ObjectDisposedException) { } - - if (source is null && CurrentImage is not ImageWrapper) { + + if (source is null && CurrentImage is not ImageWrapper) + { // User provided image himself return; } - - IsLoading = true; - CurrentImage = null; - - - var bitmap = await Task.Run(async () => - { - try - { - if (source == null) - return null; - - // A small delay allows to cancel early if the image goes out of screen too fast (eg. scrolling) - // The Bitmap constructor is expensive and cannot be cancelled - await Task.Delay(10, cancellationTokenSource.Token); - - // Hack to support relative URI - // TODO: Refactor IAsyncImageLoader to support BaseUri - try - { - var uri = new Uri(source, UriKind.RelativeOrAbsolute); - if (AssetLoader.Exists(uri, _baseUri)) + + IsLoading = true; + CurrentImage = null; + + + var bitmap = await Task.Run(async () => + { + try + { + if (source == null) + return null; + + // A small delay allows to cancel early if the image goes out of screen too fast (eg. scrolling) + // The Bitmap constructor is expensive and cannot be cancelled + await Task.Delay(10, cancellationTokenSource.Token); + + // Hack to support relative URI + // TODO: Refactor IAsyncImageLoader to support BaseUri + try + { + var uri = new Uri(source, UriKind.RelativeOrAbsolute); + if (AssetLoader.Exists(uri, _baseUri)) return new Bitmap(AssetLoader.Open(uri, _baseUri)); - } - catch (Exception) - { - // ignored - } - - loader ??= ImageLoader.AsyncImageLoader; - return await loader.ProvideImageAsync(source); - } - catch (TaskCanceledException) - { - return null; - } - finally - { - cancellationTokenSource.Dispose(); - } - - }, CancellationToken.None); - - if (cancellationTokenSource.IsCancellationRequested) + } + catch (Exception) + { + // ignored + } + + loader ??= ImageLoader.AsyncImageLoader; + return await loader.ProvideImageAsync(source); + } + catch (TaskCanceledException) + { + return null; + } + finally + { + cancellationTokenSource.Dispose(); + } + }, CancellationToken.None); + + if (cancellationTokenSource.IsCancellationRequested) return; CurrentImage = bitmap is null ? null : new ImageWrapper(bitmap); IsLoading = false; - } + } private void UpdateCornerRadius(CornerRadius radius) { @@ -308,18 +310,23 @@ protected override Size ArrangeOverride(Size finalSize) ? Stretch.CalculateSize(finalSize, CurrentImage.Size) : base.ArrangeOverride(finalSize); } - - public sealed class ImageWrapper : IImage { + + public sealed class ImageWrapper : IImage + { public IImage ImageImplementation { get; } - internal ImageWrapper(IImage imageImplementation) { + + internal ImageWrapper(IImage imageImplementation) + { ImageImplementation = imageImplementation; } + /// - public void Draw(DrawingContext context, Rect sourceRect, Rect destRect) { + public void Draw(DrawingContext context, Rect sourceRect, Rect destRect) + { ImageImplementation.Draw(context, sourceRect, destRect); } /// public Size Size => ImageImplementation.Size; } -} +} \ No newline at end of file diff --git a/AsyncImageLoader.Avalonia/AsyncImageLoader.Avalonia.csproj b/AsyncImageLoader.Avalonia/AsyncImageLoader.Avalonia.csproj index d64717b..cdda43a 100644 --- a/AsyncImageLoader.Avalonia/AsyncImageLoader.Avalonia.csproj +++ b/AsyncImageLoader.Avalonia/AsyncImageLoader.Avalonia.csproj @@ -5,7 +5,7 @@ enable netstandard2.0;netstandard2.1 AsyncImageLoader - true + AsyncImageLoader.Avalonia SKProCH Provides way to asynchronous bitmap loading from web for Avalonia Image control and more @@ -15,10 +15,6 @@ git image cross-platform avalonia avaloniaui c-sharp-library README.md - 3.2.1 - -- Fix ObjectDisposedException in Cancellation token access (#19) -