/// <summary>
 /// Initializes a new instance of the <see cref="MediaPickerController"/> class.
 /// </summary>
 /// <param name="mpDelegate">The mp delegate.</param>
 internal MediaPickerController(MediaPickerDelegate mpDelegate)
 {
     base.Delegate = mpDelegate;
 }
        /// <summary>
        /// Gets the media asynchronous.
        /// </summary>
        /// <param name="sourceType">Type of the source.</param>
        /// <param name="mediaType">Type of the media.</param>
        /// <param name="options">The options.</param>
        /// <returns>Task&lt;MediaFile&gt;.</returns>
        /// <exception cref="InvalidOperationException">
        /// There's no current active window
        /// or
        /// Could not find current view controller
        /// or
        /// Only one operation can be active at at time
        /// </exception>
        private Task <MediaFile> GetMediaAsync(
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            MediaStorageOptions options = null)
        {
            var window = UIApplication.SharedApplication.KeyWindow;

            if (window == null)
            {
                throw new InvalidOperationException("There's no current active window");
            }

            var viewController = window.RootViewController;

#if __IOS_10__
            if (viewController == null || (viewController.PresentedViewController != null && viewController.PresentedViewController.GetType() == typeof(UIAlertController)))
            {
                window =
                    UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel)
                    .FirstOrDefault(w => w.RootViewController != null);

                if (window == null)
                {
                    throw new InvalidOperationException("Could not find current view controller");
                }

                viewController = window.RootViewController;
            }
#endif
            while (viewController.PresentedViewController != null)
            {
                viewController = viewController.PresentedViewController;
            }

            var ndelegate = new MediaPickerDelegate(viewController, sourceType, options);
            var od        = Interlocked.CompareExchange(ref _pickerDelegate, ndelegate, null);
            if (od != null)
            {
                throw new InvalidOperationException("Only one operation can be active at at time");
            }

            var picker = SetupController(ndelegate, sourceType, mediaType, options);

            if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad &&
                sourceType == UIImagePickerControllerSourceType.PhotoLibrary)
            {
                ndelegate.Popover = new UIPopoverController(picker)
                {
                    Delegate = new MediaPickerPopoverDelegate(ndelegate, picker)
                };
                ndelegate.DisplayPopover();
            }
            else
            {
                viewController.PresentViewController(picker, true, null);
            }

            return(ndelegate.Task.ContinueWith(
                       t =>
            {
                if (_popover != null)
                {
                    _popover.Dispose();
                    _popover = null;
                }

                Interlocked.Exchange(ref _pickerDelegate, null);
                return t;
            }).Unwrap());
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MediaPickerPopoverDelegate"/> class.
 /// </summary>
 /// <param name="pickerDelegate">The picker delegate.</param>
 /// <param name="picker">The picker.</param>
 internal MediaPickerPopoverDelegate(MediaPickerDelegate pickerDelegate, UIImagePickerController picker)
 {
     _pickerDelegate = pickerDelegate;
     _picker         = picker;
 }