Exemple #1
0
        public MarkdownAccessoryViewModel(
            IImgurService imgurService      = null,
            IMedia mediaPicker              = null,
            IAlertDialogService alertDialog = null)
        {
            imgurService = imgurService ?? Locator.Current.GetService <IImgurService>();
            mediaPicker  = mediaPicker ?? Plugin.Media.CrossMedia.Current;
            alertDialog  = alertDialog ?? Locator.Current.GetService <IAlertDialogService>();

            PostToImgurCommand = ReactiveCommand.CreateFromTask(async _ => {
                if (!Settings.HasSeenImgurUploadWarn)
                {
                    Settings.HasSeenImgurUploadWarn = true;
                    await alertDialog.Alert("Please Read!", IMGUR_UPLOAD_WARN_MESSAGE);
                }

                var photo = await mediaPicker.PickPhotoAsync(new PickMediaOptions
                {
                    CompressionQuality = 80
                });

                var memoryStream = new MemoryStream();
                await photo.GetStream().CopyToAsync(memoryStream);

                using (alertDialog.Activate("Uploading..."))
                {
                    var model = await imgurService.SendImage(memoryStream.ToArray());
                    if (model == null || model.Data == null || model.Data.Link == null)
                    {
                        throw new InvalidOperationException("Unable to upload to Imgur. Please try again later.");
                    }
                    return(model.Data.Link);
                }
            });

            PostToImgurCommand.ThrownExceptions
            .Where(x => !(x is TaskCanceledException))
            .Subscribe(x => alertDialog.Alert("Upload Error", x.Message));
        }