/// <summary> /// Loads given placeHolder into the imageView. /// </summary> /// <returns>An awaitable task.</returns> /// <param name="placeholderPath">Full path to the placeholder.</param> /// <param name="source">Source for the path: local, web, assets</param> protected async Task <bool> LoadPlaceHolderAsync(string placeholderPath, ImageSource source, bool isLoadingPlaceholder) { if (string.IsNullOrWhiteSpace(placeholderPath)) { return(false); } if (!_target.IsValid) { return(false); } bool isLocalOrFromCache = true; var cacheEntry = ImageCache.Instance.Get(GetKey(placeholderPath)); BitmapDrawable drawable = cacheEntry == null ? null: cacheEntry.Item1; if (drawable != null && drawable.Handle != IntPtr.Zero && drawable.Bitmap != null && drawable.Bitmap.Handle != IntPtr.Zero && !drawable.Bitmap.IsRecycled) { // We should wrap drawable in an AsyncDrawable, nothing is deferred drawable = new SelfDisposingAsyncDrawable(Context.Resources, drawable.Bitmap, this); } else { // Here we asynchronously load our placeholder: it is deferred so we need a temporary AsyncDrawable drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => _target.Set(this, drawable, true, isLoadingPlaceholder)).ConfigureAwait(false); // temporary assign this AsyncDrawable try { var drawableWithResult = await RetrieveDrawableAsync(placeholderPath, source, true, true).ConfigureAwait(false); drawable = drawableWithResult.Item; isLocalOrFromCache = drawableWithResult.Result.IsLocalOrCachedResult(); } catch (Exception ex) { Logger.Error("An error occured while retrieving drawable.", ex); return(false); } } if (drawable == null) { return(false); } _loadingPlaceholderWeakReference = new WeakReference <BitmapDrawable>(drawable); if (IsCancelled) { return(false); } await MainThreadDispatcher.PostAsync(() => _target.Set(this, drawable, isLocalOrFromCache, isLoadingPlaceholder)).ConfigureAwait(false); return(true); }
/// <summary> /// Loads given placeHolder into the imageView. /// </summary> /// <returns>An awaitable task.</returns> /// <param name="placeholderPath">Full path to the placeholder.</param> /// <param name="source">Source for the path: local, web, assets</param> protected async Task <bool> LoadPlaceHolderAsync(string placeholderPath, ImageSource source, ImageView imageView, bool isLoadingPlaceholder) { if (string.IsNullOrWhiteSpace(placeholderPath)) { return(false); } if (imageView == null) { return(false); } BitmapDrawable drawable = ImageCache.Instance.Get(GetKey(placeholderPath)); if (drawable != null && drawable.Bitmap != null && drawable.Bitmap.Handle != IntPtr.Zero && !drawable.Bitmap.IsRecycled) { // We should wrap drawable in an AsyncDrawable, nothing is deferred drawable = new SelfDisposingAsyncDrawable(Context.Resources, drawable.Bitmap, this); } else { // Here we asynchronously load our placeholder: it is deferred so we need a temporary AsyncDrawable drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => { if (imageView == null || imageView.Handle == IntPtr.Zero) { return; } imageView.SetImageDrawable(drawable); // temporary assign this AsyncDrawable }).ConfigureAwait(false); try { var drawableWithResult = await RetrieveDrawableAsync(placeholderPath, source, isLoadingPlaceholder, true).ConfigureAwait(false); drawable = drawableWithResult == null ? null : drawableWithResult.Item; } catch (Exception ex) { Logger.Error("An error occured while retrieving drawable.", ex); return(false); } } if (drawable == null) { return(false); } _loadingPlaceholderWeakReference = new WeakReference <Drawable>(drawable); if (CancellationToken.IsCancellationRequested) { return(false); } await MainThreadDispatcher.PostAsync(() => { if (CancellationToken.IsCancellationRequested) { return; } if (imageView == null || imageView.Handle == IntPtr.Zero) { return; } SetImageDrawable(imageView, drawable, false); }).ConfigureAwait(false); return(true); }