Skip to content

Commit

Permalink
XF: Placeholders don't always show or show with incorrect aspect ratio
Browse files Browse the repository at this point in the history
…Fixes #353 #1239
  • Loading branch information
daniel-luberda committed Apr 26, 2019
1 parent 44aa9a0 commit 9361251
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
12 changes: 7 additions & 5 deletions source/FFImageLoading.Forms.Droid/CachedImageFastRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class CachedImageFastRenderer : CachedImageView, IVisualElementRenderer
private static readonly MethodInfo _viewExtensionsMethod = typeof(ImageRenderer).Assembly.GetType("Xamarin.Forms.Platform.Android.ViewExtensions")?.GetRuntimeMethod("EnsureId", new[] { typeof(Android.Views.View) });
private static readonly MethodInfo _elementRendererTypeOnTouchEvent = ElementRendererType?.GetRuntimeMethod("OnTouchEvent", new[] { typeof(MotionEvent) });
private bool _isDisposed;
private int? _defaultLabelFor;
private int? _defaultLabelFor;
private VisualElementTracker _visualElementTracker;
private IDisposable _visualElementRenderer;
private IScheduledWork _currentTask;
Expand Down Expand Up @@ -110,7 +110,7 @@ private void OnElementChanged(ElementChangedEventArgs<CachedImage> e)

if (e.NewElement != null)
{
e.NewElement.InternalReloadImage = new Action(ReloadImage);
e.NewElement.InternalReloadImage = new Action(ReloadImage);
e.NewElement.InternalCancel = new Action(CancelIfNeeded);
e.NewElement.InternalGetImageAsJPG = new Func<GetImageAsJpgArgs, Task<byte[]>>(GetImageAsJpgAsync);
e.NewElement.InternalGetImageAsPNG = new Func<GetImageAsPngArgs, Task<byte[]>>(GetImageAsPngAsync);
Expand Down Expand Up @@ -316,16 +316,18 @@ private void UpdateBitmap(CachedImageView imageView, CachedImage image, CachedIm

private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading)
{
if (element == null || _isDisposed || isLoading)
if (element == null || _isDisposed)
return;

await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
{
if (element == null || _isDisposed)
return;
((IVisualElementController)element).NativeSizeChanged();
element.SetIsLoading(isLoading);
if (!isLoading)
element.SetIsLoading(isLoading);
});
}

Expand Down
18 changes: 10 additions & 8 deletions source/FFImageLoading.Forms.Droid/CachedImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static void RegisterRenderer(Type type, Type renderer)
}

private bool _isDisposed;
private IScheduledWork _currentTask;
private IScheduledWork _currentTask;
private ImageSourceBinding _lastImageSource;
private readonly MotionEventHelper _motionEventHelper = CachedImage.FixedAndroidMotionEventHandler ? new MotionEventHelper() : null;
private readonly static Type _platformDefaultRendererType = typeof(ImageRenderer).Assembly.GetType("Xamarin.Forms.Platform.Android.Platform+DefaultRenderer");
Expand Down Expand Up @@ -131,7 +131,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<CachedImage> e)

if (e.NewElement != null)
{
e.NewElement.InternalReloadImage = new Action(ReloadImage);
e.NewElement.InternalReloadImage = new Action(ReloadImage);
e.NewElement.InternalCancel = new Action(CancelIfNeeded);
e.NewElement.InternalGetImageAsJPG = new Func<GetImageAsJpgArgs, Task<byte[]>>(GetImageAsJpgAsync);
e.NewElement.InternalGetImageAsPNG = new Func<GetImageAsPngArgs, Task<byte[]>>(GetImageAsPngAsync);
Expand Down Expand Up @@ -234,9 +234,9 @@ private void UpdateBitmap(CachedImageView imageView, CachedImage image, CachedIm
}
}

private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading)
{
if (element == null || _isDisposed || isLoading)
private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading)
{
if (element == null || _isDisposed)
return;

await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
Expand All @@ -245,11 +245,13 @@ await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
return;
((IVisualElementController)element).NativeSizeChanged();
element.SetIsLoading(isLoading);
if (!isLoading)
element.SetIsLoading(isLoading);
});
}
}

private void ReloadImage()
private void ReloadImage()
{
UpdateBitmap(Control, Element, null);
}
Expand Down
10 changes: 6 additions & 4 deletions source/FFImageLoading.Forms.Touch/CachedImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal class _CachedImageRenderer
}

private bool _isDisposed;
private IScheduledWork _currentTask;
private IScheduledWork _currentTask;
private ImageSourceBinding _lastImageSource;
private readonly object _updateBitmapLock = new object();

Expand Down Expand Up @@ -99,7 +99,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<CachedImage> e)

if (e.NewElement != null)
{
e.NewElement.InternalReloadImage = new Action(ReloadImage);
e.NewElement.InternalReloadImage = new Action(ReloadImage);
e.NewElement.InternalCancel = new Action(CancelIfNeeded);
e.NewElement.InternalGetImageAsJPG = new Func<GetImageAsJpgArgs, Task<byte[]>>(GetImageAsJpgAsync);
e.NewElement.InternalGetImageAsPNG = new Func<GetImageAsPngArgs, Task<byte[]>>(GetImageAsPngAsync);
Expand Down Expand Up @@ -221,7 +221,7 @@ private void UpdateImage(PImageView imageView, CachedImage image, CachedImage pr

private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading)
{
if (element == null || _isDisposed || isLoading)
if (element == null || _isDisposed)
return;

await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
Expand All @@ -230,7 +230,9 @@ await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
return;
((IVisualElementController)element).NativeSizeChanged();
element.SetIsLoading(isLoading);
if (!isLoading)
element.SetIsLoading(isLoading);
});
}

Expand Down
14 changes: 8 additions & 6 deletions source/FFImageLoading.Forms.WinUWP/CachedImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,22 @@ static Stretch GetStretch(Aspect aspect)
}
}

private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading)
{
if (element == null || _isDisposed || isLoading)
private async void ImageLoadingSizeChanged(CachedImage element, bool isLoading)
{
if (element == null || _isDisposed)
return;

await ImageService.Instance.Config.MainThreadDispatcher.PostAsync(() =>
{
if (element == null || _isDisposed)
return;
((IVisualElementController)element).InvalidateMeasure(Xamarin.Forms.Internals.InvalidationTrigger.RendererReady);
element.SetIsLoading(isLoading);
if (!isLoading)
element.SetIsLoading(isLoading);
});
}
}

void ReloadImage()
{
Expand Down

0 comments on commit 9361251

Please sign in to comment.