/// <summary> /// Prepares the instance before it runs. /// </summary> public override async Task <bool> PrepareAndTryLoadingFromCacheAsync() { ImageView imageView; _imageWeakReference.TryGetTarget(out imageView); if (imageView == null || imageView.Handle == IntPtr.Zero) { return(false); } if (CanUseMemoryCache()) { var cacheResult = await TryLoadingFromCacheAsync(imageView).ConfigureAwait(false); if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // If image is loaded from cache there is nothing to do here anymore, if something weird happened with the cache... error callback has already been called, let's just leave { return(true); // stop processing if loaded from cache OR if loading from cached raised an exception } if (IsCancelled) { return(true); // stop processing if cancelled } } bool hasDrawable = await LoadPlaceHolderAsync(Parameters.LoadingPlaceholderPath, Parameters.LoadingPlaceholderSource, imageView, true).ConfigureAwait(false); if (!hasDrawable) { // Assign the Drawable to the image var drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => SetImageDrawable(imageView, drawable)).ConfigureAwait(false); } return(false); }
/// <summary> /// Prepares the instance before it runs. /// </summary> public override async Task<bool> PrepareAndTryLoadingFromCacheAsync() { if (!_target.IsValid) return false; if (CanUseMemoryCache()) { var cacheResult = await TryLoadingFromCacheAsync().ConfigureAwait(false); if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // If image is loaded from cache there is nothing to do here anymore, if something weird happened with the cache... error callback has already been called, let's just leave return true; // stop processing if loaded from cache OR if loading from cached raised an exception if (IsCancelled) return true; // stop processing if cancelled } bool hasDrawable = await LoadPlaceHolderAsync(Parameters.LoadingPlaceholderPath, Parameters.LoadingPlaceholderSource, true).ConfigureAwait(false); if (!hasDrawable) { // Assign the Drawable to the image var drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => _target.Set(this, drawable, true, true)).ConfigureAwait(false); } return false; }
/// <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> /// Prepares the instance before it runs. /// </summary> public override async Task <bool> PrepareAndTryLoadingFromCacheAsync() { ImageView imageView; _imageWeakReference.TryGetTarget(out imageView); if (imageView == null) { return(false); } // Cancel current task attached to the same image view, if needed only var currentAssignedTask = imageView.GetImageLoaderTask(); if (currentAssignedTask != null) { Logger.Debug("Cancel current task attached to the same image view"); currentAssignedTask.CancelIfNeeded(); } var cacheResult = await TryLoadingFromCacheAsync(imageView).ConfigureAwait(false); if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // If image is loaded from cache there is nothing to do here anymore, if something weird happened with the cache... error callback has already been called, let's just leave { return(true); // stop processing if loaded from cache OR if loading from cached raised an exception } bool hasDrawable = await LoadPlaceHolderAsync(Parameters.LoadingPlaceholderPath, Parameters.LoadingPlaceholderSource, imageView, true).ConfigureAwait(false); if (!hasDrawable) { // Assign the Drawable to the image var drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => { imageView.SetImageDrawable(drawable); }).ConfigureAwait(false); } return(false); }
/// <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) { // We should wrap drawable in an AsyncDrawable, nothing is deferred drawable = new AsyncDrawable(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(() => { imageView.SetImageDrawable(drawable); // temporary assign this AsyncDrawable }).ConfigureAwait(false); try { drawable = await RetrieveDrawableAsync(placeholderPath, source, isLoadingPlaceholder).ConfigureAwait(false); } 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; } SetImageDrawable(imageView, drawable, false); }).ConfigureAwait(false); return(true); }
/// <summary> /// Prepares the instance before it runs. /// </summary> public override async Task<bool> PrepareAndTryLoadingFromCacheAsync() { ImageView imageView; _imageWeakReference.TryGetTarget(out imageView); if (imageView == null) return false; var cacheResult = await TryLoadingFromCacheAsync(imageView).ConfigureAwait(false); if (cacheResult == CacheResult.Found || cacheResult == CacheResult.ErrorOccured) // If image is loaded from cache there is nothing to do here anymore, if something weird happened with the cache... error callback has already been called, let's just leave return true; // stop processing if loaded from cache OR if loading from cached raised an exception bool hasDrawable = await LoadPlaceHolderAsync(Parameters.LoadingPlaceholderPath, Parameters.LoadingPlaceholderSource, imageView, true).ConfigureAwait(false); if (!hasDrawable) { // Assign the Drawable to the image var drawable = new AsyncDrawable(Context.Resources, null, this); await MainThreadDispatcher.PostAsync(() => { if (imageView == null || imageView.Handle == IntPtr.Zero) return; imageView.SetImageDrawable(drawable); }).ConfigureAwait(false); } return false; }
/// <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) { // We should wrap drawable in an AsyncDrawable, nothing is deferred drawable = new AsyncDrawable(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).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; }