Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
https://github.com/xamarin/XamarinCommunityToolkit/issues/805
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Feb 7, 2021
1 parent 832e5d4 commit 694530f
Showing 1 changed file with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.CommunityToolkit.UI.Views.Internals;
using Xamarin.Forms;
using static System.Math;
Expand All @@ -11,8 +13,11 @@ namespace Xamarin.CommunityToolkit.UI.Views
public class AvatarView : BaseTemplatedView<Frame>
{
const string emptyText = "X";

static readonly IImageSourceValidator imageSourceValidator = new ImageSourceValidator();

readonly SemaphoreSlim imageSourceSemaphore = new SemaphoreSlim(1);

/// <summary>
/// Backing BindableProperty for the <see cref="Aspect"/> property.
/// </summary>
Expand Down Expand Up @@ -259,23 +264,37 @@ async void OnValuePropertyChanged(bool shouldUpdateSource)
Image.BatchBegin();
if (shouldUpdateSource)
{
if (Image.Source == Source)
Image.Source = null;

await imageSourceSemaphore.WaitAsync();
try
{
Image.IsVisible = await imageSourceValidator.IsImageSourceValidAsync(Source);
}
catch (OperationCanceledException)
{
Forms.Internals.Log.Warning("CancellationException", "IsImageSourceValidAsync was cancelled.");
// Loading was canceled due to a new loading is started.
}
catch (Exception ex)

if (Image.Source == Source)
{
Forms.Internals.Log.Warning("Error", ex.Message);
throw;
Image.Source = null;

// We put a short delay to ensure that Image will refresh its Source
// in case Source's BindingContex updated.
await Task.Delay(5);
}
Image.Source = Source;

// Image has probles with loading images with disabled caching
// that's why we force to enable caching
Image.Source = Source is UriImageSource uriImageSource && !uriImageSource.CachingEnabled
? new UriImageSource
{
Uri = uriImageSource.Uri,
CachingEnabled = true,
CacheValidity = TimeSpan.FromSeconds(1)
}
: Source;

imageSourceSemaphore.Release();
}

Image.Aspect = Aspect;
Expand Down

0 comments on commit 694530f

Please sign in to comment.