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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Mar 11, 2021
1 parent 5a0bbfd commit 513633a
Showing 1 changed file with 10 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,23 +354,17 @@ 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;
}
static async Task<Stream?> getStreamAsync(UriImageSource uriSource, CancellationToken token) => uriSource.Uri != null ? await uriSource.GetStreamAsync(token) : null;

var stream = await getStreamAsync(uriImageSource, imageLoadingTokenSource.Token);

source = stream != null
? ImageSource.FromStream(async (token) => stream?.CanRead ?? true ? stream : (stream = await getStreamAsync(uriImageSource, token)))
: null;

Image.IsVisible = source != null;
}
else
Image.IsVisible = await imageSourceValidator.IsImageSourceValidAsync(source);
Expand Down Expand Up @@ -402,19 +396,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),
_ => Task.FromResult<Stream?>(null)
};
}
}

0 comments on commit 513633a

Please sign in to comment.