<commit_before>//
//  Copyright 2011-2013, Xamarin Inc.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
//

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using Media.Plugin.Abstractions;

#if __UNIFIED__
using CoreGraphics;
using AssetsLibrary;
using Foundation;
using UIKit;
using NSAction = global::System.Action;
#else
using MonoTouch.AssetsLibrary;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

using CGRect = global::System.Drawing.RectangleF;
using nfloat = global::System.Single;
#endif

namespace Media.Plugin
{
	internal class MediaPickerDelegate
		: UIImagePickerControllerDelegate
	{
		internal MediaPickerDelegate (UIViewController viewController, UIImagePickerControllerSourceType sourceType, StoreCameraMediaOptions options)
		{
			this.viewController = viewController;
			this.source = sourceType;
			this.options = options ?? new StoreCameraMediaOptions();

			if (viewController != null) {
				UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
				this.observer = NSNotificationCenter.DefaultCenter.AddObserver (UIDevice.OrientationDidChangeNotification, DidRotate);
			}
		}
		
		public UIPopoverController Popover
		{
			get;
			set;
		}
		
		public UIView View
		{
			get { return this.viewController.View; }
		}

		public Task<MediaFile> Task
		{
			get { return tcs.Task; }
		}

		public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
		{
			MediaFile mediaFile;
			switch ((NSString)info[UIImagePickerController.MediaType])
			{
        case MediaImplementation.TypeImage:
					mediaFile = GetPictureMediaFile (info);
					break;

        case MediaImplementation.TypeMovie:
					mediaFile = GetMovieMediaFile (info);
					break;

				default:
					throw new NotSupportedException();
			}

			Dismiss (picker, () => this.tcs.TrySetResult (mediaFile));
		}

		public override void Canceled (UIImagePickerController picker)
		{
			Dismiss (picker, () => this.tcs.SetResult(null));
		}

		public void DisplayPopover (bool hideFirst = false)
		{
			if (Popover == null)
				return;

			var swidth = UIScreen.MainScreen.Bounds.Width;
			var sheight= UIScreen.MainScreen.Bounds.Height;

			nfloat width = 400;
			nfloat height = 300;

			if (this.orientation == null)
			{
				if (IsValidInterfaceOrientation (UIDevice.CurrentDevice.Orientation))
					this.orientation = UIDevice.CurrentDevice.Orientation;
				else
					this.orientation = GetDeviceOrientation (this.viewController.InterfaceOrientation);
			}

			nfloat x, y;
			if (this.orientation == UIDeviceOrientation.LandscapeLeft || this.orientation == UIDeviceOrientation.LandscapeRight)
			{
				y = (swidth / 2) - (height / 2);
				x = (sheight / 2) - (width / 2);
			}
			else
			{
				x = (swidth / 2) - (width / 2);
				y = (sheight / 2) - (height / 2);
			}

			if (hideFirst && Popover.PopoverVisible)
				Popover.Dismiss (animated: false);

			Popover.PresentFromRect (new CGRect (x, y, width, height), View, 0, animated: true);
		}

		private UIDeviceOrientation? orientation;
		private NSObject observer;
		private readonly UIViewController viewController;
		private readonly UIImagePickerControllerSourceType source;
		private readonly TaskCompletionSource<MediaFile> tcs = new TaskCompletionSource<MediaFile>();
		private readonly StoreCameraMediaOptions options;

		private bool IsCaptured
		{
			get { return this.source == UIImagePickerControllerSourceType.Camera; }
		}
		
		private void Dismiss (UIImagePickerController picker, NSAction onDismiss)
		{
			if (this.viewController == null)
				onDismiss();
			else {
				NSNotificationCenter.DefaultCenter.RemoveObserver (this.observer);
				UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications();
<commit_msg>Making sure to trigger the disposal of the overlay to allow it to cleanup it's behaviour.<commit_after>//
//  Copyright 2011-2013, Xamarin Inc.
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
//

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using Media.Plugin.Abstractions;

#if __UNIFIED__
using CoreGraphics;
using AssetsLibrary;
using Foundation;
using UIKit;
using NSAction = global::System.Action;
#else
using MonoTouch.AssetsLibrary;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

using CGRect = global::System.Drawing.RectangleF;
using nfloat = global::System.Single;
#endif

namespace Media.Plugin
{
	internal class MediaPickerDelegate
		: UIImagePickerControllerDelegate
	{
		internal MediaPickerDelegate (UIViewController viewController, UIImagePickerControllerSourceType sourceType, StoreCameraMediaOptions options)
		{
			this.viewController = viewController;
			this.source = sourceType;
			this.options = options ?? new StoreCameraMediaOptions();

			if (viewController != null) {
				UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
				this.observer = NSNotificationCenter.DefaultCenter.AddObserver (UIDevice.OrientationDidChangeNotification, DidRotate);
			}
		}
		
		public UIPopoverController Popover
		{
			get;
			set;
		}
		
		public UIView View
		{
			get { return this.viewController.View; }
		}

		public Task<MediaFile> Task
		{
			get { return tcs.Task; }
		}

		public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
		{
			MediaFile mediaFile;
			switch ((NSString)info[UIImagePickerController.MediaType])
			{
        case MediaImplementation.TypeImage:
					mediaFile = GetPictureMediaFile (info);
					break;

        case MediaImplementation.TypeMovie:
					mediaFile = GetMovieMediaFile (info);
					break;

				default:
					throw new NotSupportedException();
			}

			Dismiss (picker, () => this.tcs.TrySetResult (mediaFile));
		}

		public override void Canceled (UIImagePickerController picker)
		{
			Dismiss (picker, () => this.tcs.SetResult(null));
		}

		public void DisplayPopover (bool hideFirst = false)
		{
			if (Popover == null)
				return;

			var swidth = UIScreen.MainScreen.Bounds.Width;
			var sheight= UIScreen.MainScreen.Bounds.Height;

			nfloat width = 400;
			nfloat height = 300;

			if (this.orientation == null)
			{
				if (IsValidInterfaceOrientation (UIDevice.CurrentDevice.Orientation))
					this.orientation = UIDevice.CurrentDevice.Orientation;
				else
					this.orientation = GetDeviceOrientation (this.viewController.InterfaceOrientation);
			}

			nfloat x, y;
			if (this.orientation == UIDeviceOrientation.LandscapeLeft || this.orientation == UIDeviceOrientation.LandscapeRight)
			{
				y = (swidth / 2) - (height / 2);
				x = (sheight / 2) - (width / 2);
			}
			else
			{
				x = (swidth / 2) - (width / 2);
				y = (sheight / 2) - (height / 2);
			}

			if (hideFirst && Popover.PopoverVisible)
				Popover.Dismiss (animated: false);

			Popover.PresentFromRect (new CGRect (x, y, width, height), View, 0, animated: true);
		}

		private UIDeviceOrientation? orientation;
		private NSObject observer;
		private readonly UIViewController viewController;
		private readonly UIImagePickerControllerSourceType source;
		private readonly TaskCompletionSource<MediaFile> tcs = new TaskCompletionSource<MediaFile>();
		private readonly StoreCameraMediaOptions options;

		private bool IsCaptured
		{
			get { return this.source == UIImagePickerControllerSourceType.Camera; }
		}
		
		private void Dismiss (UIImagePickerController picker, NSAction onDismiss)
		{
			if (this.viewController == null)
				onDismiss();
			else {
				NSNotificationCenter.DefaultCenter.RemoveObserver (this.observer);
				UIDevice.CurrentDevice.EndGeneratingDeviceOrientationNotifications();

				if (picker.CameraOverlayView != null) {
					picker.CameraOverlayView.Dispose ();
				}

{'fraction_non_alphanumeric': 0.0918100011390819, 'fraction_numerical': 0.00615104225993849, 'mean_word_length': 3.8083242059145674, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1455540', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2485, 'n_words': 687}