public static async Task <IImageSourceServiceResult <Image>?> UpdateSourceAsync(this IImageSourcePart image, Image destinationContext, IImageSourceServiceProvider services, Action <Image?> setImage, CancellationToken cancellationToken = default)
        {
            image.UpdateIsLoading(false);

            var imageSource = image.Source;

            if (imageSource == null)
            {
                return(null);
            }

            var events = image as IImageSourcePartEvents;

            events?.LoadingStarted();
            image.UpdateIsLoading(true);

            try
            {
                var service = services.GetRequiredImageSourceService(imageSource);
                var result  = await service.GetImageAsync(imageSource, destinationContext, cancellationToken);

                var tImage = result?.Value;

                var applied = !cancellationToken.IsCancellationRequested && tImage != null && imageSource == image.Source;

                // only set the image if we are still on the same one
                if (applied)
                {
                    setImage.Invoke(tImage);
                    destinationContext.UpdateIsAnimationPlaying(image);
                }

                events?.LoadingCompleted(applied);

                return(result);
            }
            catch (OperationCanceledException)
            {
                // no-op
                events?.LoadingCompleted(false);
            }
            catch (Exception ex)
            {
                events?.LoadingFailed(ex);
            }
            finally
            {
                // only mark as finished if we are still working on the same image
                if (imageSource == image.Source)
                {
                    image.UpdateIsLoading(false);
                }
            }

            return(null);
        }
        public static async Task UpdateThumbImageSourceAsync(this UISlider uiSlider, ISlider slider, IImageSourceServiceProvider provider)
        {
            var thumbImageSource = slider.ThumbImageSource;

            if (thumbImageSource != null)
            {
                var service = provider.GetRequiredImageSourceService(thumbImageSource);
                var result  = await service.GetImageAsync(thumbImageSource);

                var thumbImage = result?.Value;

                uiSlider.SetThumbImage(thumbImage, UIControlState.Normal);
            }
        }
Exemple #3
0
        public static async Task UpdateThumbImageSourceAsync(this SeekBar seekBar, ISlider slider, IImageSourceServiceProvider provider, Drawable?defaultThumb)
        {
            var context = seekBar.Context;

            if (context == null)
            {
                return;
            }

            var thumbImageSource = slider.ThumbImageSource;

            if (thumbImageSource != null)
            {
                var service = provider.GetRequiredImageSourceService(thumbImageSource);
                var result  = await service.GetDrawableAsync(thumbImageSource, context);

                Drawable?thumbDrawable = result?.Value;

                seekBar.SetThumb(thumbDrawable ?? defaultThumb);
            }
        }
 public static IImageSourceService GetRequiredImageSourceService <T>(this IImageSourceServiceProvider provider)
     where T : IImageSource =>
 provider.GetRequiredImageSourceService(typeof(T));
 public static IImageSourceService GetRequiredImageSourceService(this IImageSourceServiceProvider provider, IImageSource imageSource) =>
 provider.GetRequiredImageSourceService(imageSource.GetType());
Exemple #6
0
        public static async Task <IImageSourceServiceResult <WImageSource>?> UpdateSourceAsync(
            this IImageSourcePart image,
            FrameworkElement destinationContext,
            IImageSourceServiceProvider services,
            Action <WImageSource?> setImage,
            CancellationToken cancellationToken = default)
        {
            image.UpdateIsLoading(false);

            var imageSource = image.Source;

            if (imageSource == null)
            {
                return(null);
            }

            var events = image as IImageSourcePartEvents;

            events?.LoadingStarted();
            image.UpdateIsLoading(true);

            try
            {
                var service = services.GetRequiredImageSourceService(imageSource);

                var scale  = destinationContext.XamlRoot?.RasterizationScale ?? 1;
                var result = await service.GetImageSourceAsync(imageSource, (float)scale, cancellationToken);

                var uiImage = result?.Value;

                var applied = !cancellationToken.IsCancellationRequested && imageSource == image.Source;

                // only set the image if we are still on the same one
                if (applied)
                {
                    setImage(uiImage);
                    if (destinationContext is WImage imageView)
                    {
                        imageView.UpdateIsAnimationPlaying(image);
                    }
                }

                events?.LoadingCompleted(applied);

                return(result);
            }
            catch (OperationCanceledException)
            {
                // no-op
                events?.LoadingCompleted(false);
            }
            catch (Exception ex)
            {
                events?.LoadingFailed(ex);
            }
            finally
            {
                // only mark as finished if we are still working on the same image
                if (imageSource == image.Source)
                {
                    image.UpdateIsLoading(false);
                }
            }

            return(null);
        }
        public static async Task <IImageSourceServiceResult?> UpdateSourceAsync(
            this IImageSourcePart image,
            View destinationContext,
            IImageSourceServiceProvider services,
            Action <Drawable?> setImage,
            CancellationToken cancellationToken = default)
        {
            image.UpdateIsLoading(false);

            var context = destinationContext.Context;

            if (context == null)
            {
                return(null);
            }

            var destinationImageView = destinationContext as Android.Widget.ImageView;

            if (destinationImageView is null && setImage is null)
            {
                return(null);
            }

            var imageSource = image.Source;

            if (imageSource == null)
            {
                return(null);
            }

            var events = image as IImageSourcePartEvents;

            events?.LoadingStarted();
            image.UpdateIsLoading(true);

            try
            {
                var service = services.GetRequiredImageSourceService(imageSource);

                var applied = !cancellationToken.IsCancellationRequested && destinationContext.IsAlive() && imageSource == image.Source;

                IImageSourceServiceResult?result = null;

                if (applied)
                {
                    if (destinationImageView is not null)
                    {
                        result = await service.LoadDrawableAsync(imageSource, destinationImageView, cancellationToken);
                    }
                    else
                    {
                        result = await service.GetDrawableAsync(imageSource, context, cancellationToken);

                        if (setImage is not null && result is IImageSourceServiceResult <Drawable> drawableResult)
                        {
                            setImage.Invoke(drawableResult.Value);
                        }
                    }

                    if (result is null)
                    {
                        throw new InvalidOperationException("Glide failed to load image");
                    }
                }

                events?.LoadingCompleted(applied);
                return(result);
            }
            catch (OperationCanceledException)
            {
                // no-op
                events?.LoadingCompleted(false);
            }
            catch (Exception ex)
            {
                events?.LoadingFailed(ex);
            }
            finally
            {
                // only mark as finished if we are still working on the same image
                if (imageSource == image.Source)
                {
                    image.UpdateIsLoading(false);
                }
            }

            return(null);
        }