Esempio n. 1
0
		/// <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;
		}
Esempio n. 2
0
		/// <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;
		}