<commit_before>//
//  SWImagePickerManager.swift
//  Pods
//
//  Created by <NAME> on 1/1/16.
//
//

import UIKit
import MobileCoreServices

public typealias ImageHandler = (result: SWImagePickerManagerResult) -> Void

public class SWImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    private var handler: ImageHandler?
    
    public override init() {
        
    }
    
    public func showImageSourcesSelector(fromViewController viewController: UIViewController, handler: ImageHandler) {
        self.handler = handler
        
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        
        let takePhoto = UIAlertAction(title: "Take Photo", style: .Default) { (action) -> Void in
            
        }
        
        if UIImagePickerController.isSourceTypeAvailable(.Camera) {
            actionSheet.addAction(takePhoto)
        }
        
        let photoLibrary = UIAlertAction(title: "Choose from Library", style: .Default) { (action) -> Void in
            self.showImagePickerWithSourceType(.PhotoLibrary, fromViewController: viewController)
        }
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            actionSheet.addAction(photoLibrary)
        }
        
        let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) -> Void in
            
        }
        
        actionSheet.addAction(cancel)
        
        viewController.presentViewController(actionSheet, animated: true, completion: nil)
    }
    
    private func showImagePickerWithSourceType(sourceType: UIImagePickerControllerSourceType, fromViewController viewController: UIViewController) {
        let picker = UIImagePickerController()
        picker.allowsEditing = false
        picker.mediaTypes = [kUTTypeImage as String]
        picker.sourceType = sourceType
        picker.delegate = self
        
        
        viewController.presentViewController(picker, animated: true, completion: {})
    }
    
    // MARK: - UIImagePickerControllerDelegate
    public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
 
        guard let handler = self.handler else {
            picker.dismissViewControllerAnimated(true, completion: nil)
            return
        }
        
        let image = (info[UIImagePickerControllerEditedImage] ?? info[UIImagePickerControllerOriginalImage]) as! UIImage
        
        let result = SWImagePickerManagerResult.Image(image)
        handler(result: result)<commit_msg>Add missing handler in cancel action<commit_after>//
//  SWImagePickerManager.swift
//  Pods
//
//  Created by <NAME> on 1/1/16.
//
//

import UIKit
import MobileCoreServices

public typealias ImageHandler = (result: SWImagePickerManagerResult) -> Void

public class SWImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    private var handler: ImageHandler?
    
    public override init() {
        
    }
    
    public func showImageSourcesSelector(fromViewController viewController: UIViewController, handler: ImageHandler) {
        self.handler = handler
        
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        
        let takePhoto = UIAlertAction(title: "Take Photo", style: .Default) { (action) -> Void in
            
        }
        
        if UIImagePickerController.isSourceTypeAvailable(.Camera) {
            actionSheet.addAction(takePhoto)
        }
        
        let photoLibrary = UIAlertAction(title: "Choose from Library", style: .Default) { (action) -> Void in
            self.showImagePickerWithSourceType(.PhotoLibrary, fromViewController: viewController)
        }
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            actionSheet.addAction(photoLibrary)
        }
        
        let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { [unowned self] (action) -> Void in
            let result = SWImagePickerManagerResult.Cancelled
            self.handler?(result: result)
        }
        
        actionSheet.addAction(cancel)
        
        viewController.presentViewController(actionSheet, animated: true, completion: nil)
    }
    
    private func showImagePickerWithSourceType(sourceType: UIImagePickerControllerSourceType, fromViewController viewController: UIViewController) {
        let picker = UIImagePickerController()
        picker.allowsEditing = false
        picker.mediaTypes = [kUTTypeImage as String]
        picker.sourceType = sourceType
        picker.delegate = self
        
        
        viewController.presentViewController(picker, animated: true, completion: {})
    }
    
    // MARK: - UIImagePickerControllerDelegate
    public func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
 
        guard let handler = self.handler else {
            picker.dismissViewControllerAnimated(true, completion: nil)
            return
        }
        
        let image = (info[UIImagePickerControllerEditedImage] ?? info[UIImagePickerControllerOriginalImage]) as! UIImage
        
        let result = SWImagePickerManagerResult.Image(image)
        handler(result: result)
{'fraction_non_alphanumeric': 0.07619221633473415, 'fraction_numerical': 0.0014617211766855473, 'mean_word_length': 2.757035003431709, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3731858', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1338, 'n_words': 305}