Example #1
0
        /// <summary>
        /// Loads and gets BitmapDrawable using defined parameters.
        /// IMPORTANT: you should call SetNoLongerDisplayed method if drawable is no longer displayed
        /// </summary>
        /// <returns>The bitmap drawable async.</returns>
        /// <param name="parameters">Parameters.</param>
        public static Task <BitmapDrawable> AsBitmapDrawableAsync(this TaskParameter parameters)
        {
            var target            = new BitmapTarget();
            var userErrorCallback = parameters.OnError;
            var finishCallback    = parameters.OnFinish;
            var tcs = new TaskCompletionSource <BitmapDrawable>();

            parameters
            .Error(ex =>
            {
                userErrorCallback(ex);
                tcs.SetException(ex);
            })
            .Finish(scheduledWork =>
            {
                finishCallback(scheduledWork);
                tcs.TrySetResult(target.BitmapDrawable);
            });

            if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path))
            {
                target.SetAsEmpty();
                parameters.Dispose();
                return(null);
            }

            var task = CreateTask(parameters, target);

            ImageService.Instance.LoadImage(task);

            return(tcs.Task);
        }
        private static IScheduledWork Into <TImageView>(this TaskParameter parameters, ITarget <WriteableBitmap, TImageView> target) where TImageView : class
        {
            if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path))
            {
                target.SetAsEmpty(null);
                parameters?.Dispose();
                return(null);
            }

            var task = CreateTask(parameters, target);

            ImageService.Instance.LoadImage(task);
            return(task);
        }
        private static IScheduledWork Into(this TaskParameter parameters, float imageScale, ITarget <UIImage, ImageLoaderTask> target)
        {
            if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path))
            {
                target.SetAsEmpty();
                parameters.Dispose();
                return(null);
            }

            var task = CreateTask(parameters, imageScale, target);

            ImageService.Instance.LoadImage(task);
            return(task);
        }
        /// <summary>
        /// Loads the image into given imageView using defined parameters.
        /// </summary>
        /// <param name="parameters">Parameters for loading the image.</param>
        /// <param name="imageView">Image view that should receive the image.</param>
        public static IScheduledWork Into(this TaskParameter parameters, ImageViewAsync imageView)
        {
            var target = new ImageViewTarget(imageView);

            if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path))
            {
                target.SetAsEmpty(null);
                parameters.Dispose();
                return(null);
            }

            var task = CreateTask(parameters, target);

            ImageService.Instance.LoadImage(task);
            return(task);
        }
Example #5
0
        /// <summary>
        /// Loads and gets UIImage using defined parameters.
        /// IMPORTANT: It throws image loading exceptions - you should handle them
        /// </summary>
        /// <returns>The UIImage async.</returns>
        /// <param name="parameters">Parameters.</param>
        /// <param name="imageScale">Image scale.</param>
        public static Task <UIImage> AsUIImageAsync(this TaskParameter parameters, float imageScale = -1f)
        {
            var target            = new UIImageTarget();
            var userErrorCallback = parameters.OnError;
            var finishCallback    = parameters.OnFinish;
            var tcs = new TaskCompletionSource <UIImage>();
            List <Exception> exceptions = null;

            parameters
            .Error(ex =>
            {
                if (exceptions == null)
                {
                    exceptions = new List <Exception>();
                }

                exceptions.Add(ex);
                userErrorCallback?.Invoke(ex);
            })
            .Finish(scheduledWork =>
            {
                finishCallback?.Invoke(scheduledWork);

                if (exceptions != null)
                {
                    tcs.TrySetException(exceptions);
                }
                else
                {
                    tcs.TrySetResult(target.UIImage);
                }
            });

            if (parameters.Source != ImageSource.Stream && string.IsNullOrWhiteSpace(parameters.Path))
            {
                target.SetAsEmpty(null);
                parameters?.Dispose();
                return(null);
            }

            var task = CreateTask(parameters, imageScale, target);

            ImageService.Instance.LoadImage(task);

            return(tcs.Task);
        }
        public override void BindViewModel(ChatMessageViewModel viewModel)
        {
            _viewModelRef = WeakReferenceEx.Create(viewModel);

            Bindings.Add(this.SetBinding(() => _viewModelRef.Target.Body).WhenSourceChanges(() =>
            {
                // TODO: check
                Execute.OnUIThread(() =>
                {
                    var hideMessageBody            = string.IsNullOrEmpty(_viewModelRef.Target.Body) && _viewModelRef.Target.HasAttachment;
                    MessageBodyTextView.Visibility = BoolToViewStateConverter.ConvertGone(hideMessageBody == false);
                    MessageBodyTextView.Text       = _viewModelRef.Target.Body;
                });
            }));
            Bindings.Add(this.SetBinding(() => _viewModelRef.Target.TextDateTime).WhenSourceChanges(() =>
            {
                // TODO: check
                Execute.OnUIThread(() =>
                {
                    MessageDateTimeTextView.Text = _viewModelRef.Target.TextDateTime;
                });
            }));

            if (_viewModelRef.Target.HasAttachment)
            {
                var model = _viewModelRef.Target.Model;
                var expr  = default(TaskParameter);

                AttachmentImageView.SetImageResource(StyleHelper.Style.AttachmentImagePlaceholder);
                UpdateAttachmentImageViewSizeAndVisibility();

                if (!string.IsNullOrEmpty(model.ImageCacheKey))
                {
                    expr = ImageService.Instance.LoadFile(model.ImageCacheKey);
                }
                else if (!string.IsNullOrEmpty(model.ImageRemoteUrl))
                {
                    expr = ImageService.Instance.LoadUrl(model.ImageRemoteUrl);
                }

                if (expr == null)
                {
                    return;
                }

                _downloadAttachTask = expr.DownSampleInDip(90, 90)
                                      .Finish(x =>
                {
                    Execute.BeginOnUIThread(UpdateAttachmentImageViewSizeAndVisibility);
                });
                _downloadAttachTask.IntoAsync(AttachmentImageView);
            }
            else
            {
                _downloadAttachTask?.Dispose();
                AttachmentImageView.SetImageDrawable(null);
                AttachmentImageView.Visibility = ViewStates.Gone;
            }

            if (_isIncomingMessageViewType && SenderPhotoImageView != null)
            {
                SenderPhotoImageView.LoadImageWithTextPlaceholder(
                    _viewModelRef.Target.SenderPhotoUrl,
                    _viewModelRef.Target.SenderName,
                    new WhiteLabel.Droid.Controls.AvatarPlaceholderDrawable.AvatarStyles
                {
                    BackgroundHexColors = StyleHelper.Style.ChatAvatarStyles.BackgroundHexColors,
                    Size = new System.Drawing.Size(35, 35)
                },
                    x => x.Transform(new CircleTransformation()));
            }

            if (!_isIncomingMessageViewType && MessageStatusView != null)
            {
                Bindings.Add(this.SetBinding(() => _viewModelRef.Target.Status).WhenSourceChanges(() =>
                {
                    // TODO: check
                    Execute.OnUIThread(() =>
                    {
                        if (_viewModelRef.Target == null)
                        {
                            return;
                        }

                        ChangeMessageViewStatus(_viewModelRef.Target.Status);
                    });
                }));
            }
        }