Skip to content

Commit

Permalink
Refactor ImageBrushLoader without Reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
SKProCH committed Jul 17, 2023
1 parent 3dbf1af commit ec5c927
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions AsyncImageLoader.Avalonia/ImageBrushLoader.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using System;
using System.Reactive.Linq;
using AsyncImageLoader.Loaders;
using AsyncImageLoader.Loaders;
using Avalonia;
using Avalonia.Media;

namespace AsyncImageLoader {
public static class ImageBrushLoader {
public static IAsyncImageLoader AsyncImageLoader { get; set; } = new RamCachedWebImageLoader();
static ImageBrushLoader() {
SourceProperty.Changed
.Where(args => args.IsEffectiveValueChange)
.Subscribe(args => OnSourceChanged((ImageBrush)args.Sender, args.NewValue.Value));
SourceProperty.Changed.AddClassHandler<ImageBrush>(OnSourceChanged);
}

private static async void OnSourceChanged(ImageBrush sender, string? url) {
SetIsLoading(sender, true);
private static async void OnSourceChanged(ImageBrush imageBrush, AvaloniaPropertyChangedEventArgs args) {
var (oldValue, newValue) = args.GetOldAndNewValue<string?>();
if (oldValue == newValue)
return;

SetIsLoading(imageBrush, true);

var bitmap = url == null
var bitmap = newValue == null
? null
: await AsyncImageLoader.ProvideImageAsync(url);
if (GetSource(sender) != url) return;
sender.Source = bitmap;
: await AsyncImageLoader.ProvideImageAsync(newValue);
if (GetSource(imageBrush) != newValue) return;
imageBrush.Source = bitmap;

SetIsLoading(sender, false);
SetIsLoading(imageBrush, false);
}

public static readonly AttachedProperty<string?> SourceProperty = AvaloniaProperty.RegisterAttached<ImageBrush, string?>("Source", typeof(ImageLoader));
Expand Down

0 comments on commit ec5c927

Please sign in to comment.