<commit_before>// MvxImagePickerTask.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
// 
// Project Lead - Stuart Lodge, @slodge, <EMAIL>

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using Cirrious.CrossCore;
using Cirrious.CrossCore.Touch.Platform;
using Cirrious.CrossCore.Touch.Views;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Cirrious.MvvmCross.Plugins.PictureChooser.Touch
{
    public class MvxImagePickerTask
        : MvxTouchTask
          , IMvxPictureChooserTask          
    {
        private readonly UIImagePickerController _picker;
        private readonly IMvxTouchModalHost _modalHost;

        public MvxImagePickerTask()
        {
            _modalHost = Mvx.Resolve<IMvxTouchModalHost>();
            _picker = new UIImagePickerController();
        }

        #region IMvxPictureChooserTask Members

        public void ChoosePictureFromLibrary(int maxPixelDimension, int percentQuality, Action<Stream> pictureAvailable,
                                             Action assumeCancelled)
        {
            _picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            ChoosePictureCommon(maxPixelDimension, percentQuality, pictureAvailable, assumeCancelled);
        }

        public void TakePicture(int maxPixelDimension, int percentQuality, Action<Stream> pictureAvailable,
                                Action assumeCancelled)
        {
            _picker.SourceType = UIImagePickerControllerSourceType.Camera;
            ChoosePictureCommon(maxPixelDimension, percentQuality, pictureAvailable, assumeCancelled);
        }

        #endregion

        private void ChoosePictureCommon(int maxPixelDimension, int percentQuality,
                                         Action<Stream> pictureAvailable, Action assumeCancelled)
        {
            _picker.FinishedPickingMedia += (sender, e) =>
                {
                    var image = e.EditedImage ?? e.OriginalImage;
                    HandleImagePick(image, maxPixelDimension, percentQuality, pictureAvailable, assumeCancelled);
                };

            _picker.FinishedPickingImage += (sender, e) =>
                {
                    var image = e.Image;
                    HandleImagePick(image, maxPixelDimension, percentQuality, pictureAvailable, assumeCancelled);
                };

            _picker.Canceled += (sender, e) =>
                {
                    assumeCancelled();
                    _picker.DismissViewController(true, () => { });
                    _modalHost.NativeModalViewControllerDisappearedOnItsOwn();
                };
            _modalHost.PresentModalViewController(_picker, true);
        }

        private void HandleImagePick(UIImage image, int maxPixelDimension, int percentQuality,
                                     Action<Stream> pictureAvailable, Action assumeCancelled)
        {
            if (image != null)
            {
                // resize the image
                image = image.ImageToFitSize(new SizeF(maxPixelDimension, maxPixelDimension));

                using (NSData data = image.AsJPEG((float) (percentQuality/100.0)))
                {
                    var byteArray = new byte[data.Length];
                    Marshal.Copy(data.Bytes, byteArray, 0, Convert.ToInt32(data.Length));

                    var imageStream = new MemoryStream();
                    imageStream.Write(byteArray, 0, Convert.ToInt32(data.Length));
                    imageStream.Seek(0, SeekOrigin.Begin);

                    pictureAvailable(imageStream);
                }
            }
            else
            {
                assumeCancelled();
            }

            _picker.DismissViewController(true, () => { });
            _modalHost.NativeModalViewControllerDisappearedOnItsOwn();
        }

        #region Nested type: CameraDelegate

        private class CameraDelegate : UIImagePickerControllerDelegate
        {
            public Action<UIImage, NSDictionary> Callback { get; set; }

            public override void FinishedPickingImage(UIImagePickerController picker, UIImage image,
                                                      NSDictionary editingInfo)
            {
                if (Callback != null)
                    Callback(image, editingInfo);
            }

            public override void Canceled(UIImagePickerController picker)
            {
                if (Callback != null)
                    Callback(null, null);
            }
        }

        #endregion
    }
}<commit_msg>fixed the issue with multiple subscription to the handler in the same view for each call causing the HandleImagePick method to be called multiple times<commit_after>// MvxImagePickerTask.cs
// (c) Copyright Cirrious Ltd. http://www.cirrious.com
// MvvmCross is licensed using Microsoft Public License (Ms-PL)
// Contributions and inspirations noted in readme.md and license.txt
// 
// Project Lead - St<NAME>, @slodge, <EMAIL>

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using Cirrious.CrossCore;
using Cirrious.CrossCore.Touch.Platform;
using Cirrious.CrossCore.Touch.Views;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Cirrious.MvvmCross.Plugins.PictureChooser.Touch
{
    public class MvxImagePickerTask
        : MvxTouchTask
          , IMvxPictureChooserTask
    {
        private readonly UIImagePickerController _picker;
        private readonly IMvxTouchModalHost _modalHost;
        private int _maxPixelDimension;
        private int _percentQuality;
        private Action<Stream> _pictureAvailable;
        private Action _assumeCancelled;

        public MvxImagePickerTask()
        {
            _modalHost = Mvx.Resolve<IMvxTouchModalHost>();
            _picker = new UIImagePickerController();
            _picker.FinishedPickingMedia += _picker_FinishedPickingMedia;
            _picker.FinishedPickingImage += _picker_FinishedPickingImage;
            _picker.Canceled += _picker_Canceled;
        }

        #region IMvxPictureChooserTask Members

        public void ChoosePictureFromLibrary(int maxPixelDimension, int percentQuality, Action<Stream> pictureAvailable,
                                             Action assumeCancelled)
        {
            _picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            ChoosePictureCommon(maxPixelDimension, percentQuality, pictureAvailable, assumeCancelled);
        }

        public void TakePicture(int maxPixelDimension, int percentQuality, Action<Stream> pictureAvailable,
                                Action assumeCancelled)
        {
            _picker.SourceType = UIImagePickerControllerSourceType.Camera;
            ChoosePictureCommon(maxPixelDimension, percentQuality, pictureAvailable, assumeCancelled);
        }

        #endregion

        private void ChoosePictureCommon(int maxPixelDimension, int percentQuality,
                                         Action<Stream> pictureAvailable, Action assumeCancelled)
        {
            _maxPixelDimension = maxPixelDimension;
            _percentQuality = percentQuality;
            _pictureAvailable = pictureAvailable;
            _assumeCancelled = assumeCancelled;

            _modalHost.PresentModalViewController(_picker, true);
        }

        private void HandleImagePick(UIImage image)
        {
            if (image != null)
            {
                // resize the image
                image = image.ImageToFitSize(new SizeF(_maxPixelDimension, _maxPixelDimension));

                using (NSData data = image.AsJPEG((float)(_percentQuality / 100.0)))
                {
                    var byteArray = new byte[data.Length];
                    Marshal.Copy(data.Bytes, byteArray, 0, Convert.ToInt32(data.Length));

                    var imageStream = new MemoryStream();
                    imageStream.Write(byteArray, 0, Convert.ToInt32(data.Length));
                    imageStream.Seek(0, SeekOrigin.Begin);
                    if (_pictureAvailable != null)
                        _pictureAvailable(imageStream);
                }
            }
            else
            {
                if (_assumeCancelled != null)
                    _assumeCancelled();
            }

            _picker.DismissViewController(true, () => { });
            _modalHost.NativeModalViewControllerDisappearedOnItsOwn();
        }

        void _picker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
        {
            var image = e.EditedImage ?? e.OriginalImage;
            HandleImagePick(image);

        }

        void _picker_FinishedPickingImage(object sender, UIImagePickerImagePickedEventArgs e)
        {
            var image = e.Image;
            HandleImagePick(image);
        }

        void _picker_Canceled(object sender, EventArgs e)
        {
            if (_assumeCancelled != null)
                _assumeCancelled();
            _picker.DismissViewController(true, () => { });
            _modalHost.NativeModalViewControllerDisappearedOnItsOwn();
        }

        #region Nested type: CameraDelegate

        private class CameraDelegate : UIImagePickerControllerDelegate
        {
            public Action<UIImage, NSDictionary> Callback { get; set; }

            public override void FinishedPickingImage(UIImagePickerController picker, UIImage image,
                                                      NSDictionary editingInfo)
            {
                if (Callback != null)
                    Callback(image, editingInfo);
            }

            public override void Canceled(UIImagePickerController picker)
            {
                if (Callback != null)
                    Callback(null, null);
            }
        }

        #endregion
    }
}
{'fraction_non_alphanumeric': 0.07082294264339152, 'fraction_numerical': 0.0021945137157107233, 'mean_word_length': 1.9549071618037135, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7325794', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2343, 'n_words': 516}