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

Commit 12af95e

Browse files
https://github.com/xamarin/XamarinCommunityToolkit/issues/1046
1 parent caa3ebc commit 12af95e

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit/Views/AvatarView/AvatarView.shared.cs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -354,23 +354,17 @@ async void OnSourcePropertyChanged(bool isBindingContextChanged)
354354

355355
try
356356
{
357-
var imageStreamLoadingTask = GetImageStreamLoadingTask(source, imageLoadingTokenSource.Token);
358-
if (imageStreamLoadingTask != null)
357+
if (source is UriImageSource uriImageSource)
359358
{
360-
using var stream = await imageStreamLoadingTask;
361-
if (stream != null)
362-
{
363-
var newStream = new MemoryStream();
364-
stream.CopyTo(newStream);
365-
newStream.Position = 0;
366-
Image.IsVisible = true;
367-
source = ImageSource.FromStream(() => newStream);
368-
}
369-
else
370-
{
371-
Image.IsVisible = false;
372-
source = null;
373-
}
359+
static async Task<Stream> getStreamAsync(UriImageSource uriSource, CancellationToken token) => uriSource.Uri != null ? await uriSource.GetStreamAsync(token) : null;
360+
361+
var stream = await getStreamAsync(uriImageSource, imageLoadingTokenSource.Token);
362+
363+
source = stream != null
364+
? ImageSource.FromStream(async (token) => stream?.CanRead ?? true ? stream : (stream = await getStreamAsync(uriImageSource, token)))
365+
: null;
366+
367+
Image.IsVisible = source != null;
374368
}
375369
else
376370
Image.IsVisible = await imageSourceValidator.IsImageSourceValidAsync(source);
@@ -402,20 +396,5 @@ double CalculateFontSize()
402396

403397
return size * .4;
404398
}
405-
406-
Task<Stream> GetImageStreamLoadingTask(ImageSource source, CancellationToken token)
407-
=> source switch
408-
{
409-
IStreamImageSource streamImageSource => streamImageSource.GetStreamAsync(token),
410-
UriImageSource uriImageSource => uriImageSource.Uri != null
411-
? new UriImageSource
412-
{
413-
Uri = uriImageSource.Uri,
414-
CachingEnabled = uriImageSource.CachingEnabled,
415-
CacheValidity = uriImageSource.CacheValidity
416-
}.GetStreamAsync(token)
417-
: Task.FromResult<Stream>(null),
418-
_ => null
419-
};
420399
}
421400
}

0 commit comments

Comments
 (0)