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/1046
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Mar 9, 2021
1 parent caa3ebc commit 4054552
Showing 1 changed file with 9 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,16 @@ async void OnSourcePropertyChanged(bool isBindingContextChanged)

try
{
var imageStreamLoadingTask = GetImageStreamLoadingTask(source, imageLoadingTokenSource.Token);
if (imageStreamLoadingTask != null)
if (source is UriImageSource uriImageSource)
{
using var stream = await imageStreamLoadingTask;
if (stream != null)
{
var newStream = new MemoryStream();
stream.CopyTo(newStream);
newStream.Position = 0;
Image.IsVisible = true;
source = ImageSource.FromStream(() => newStream);
}
else
{
Image.IsVisible = false;
source = null;
}
var getStream = new Func<CancellationToken, Task<Stream>>(async (token) => uriImageSource.Uri != null ? await uriImageSource.GetStreamAsync(token) : null);
var stream = await getStream.Invoke(imageLoadingTokenSource.Token);

source = stream != null
? ImageSource.FromStream(async (token) => stream?.CanRead ?? true ? stream : (stream = await getStream.Invoke(token)))
: null;

Image.IsVisible = source != null;
}
else
Image.IsVisible = await imageSourceValidator.IsImageSourceValidAsync(source);
Expand Down Expand Up @@ -402,20 +395,5 @@ async void OnSourcePropertyChanged(bool isBindingContextChanged)

return size * .4;
}

Task<Stream> GetImageStreamLoadingTask(ImageSource source, CancellationToken token)
=> source switch
{
IStreamImageSource streamImageSource => streamImageSource.GetStreamAsync(token),
UriImageSource uriImageSource => uriImageSource.Uri != null
? new UriImageSource
{
Uri = uriImageSource.Uri,
CachingEnabled = uriImageSource.CachingEnabled,
CacheValidity = uriImageSource.CacheValidity
}.GetStreamAsync(token)
: Task.FromResult<Stream>(null),
_ => null
};
}
}

0 comments on commit 4054552

Please sign in to comment.