<commit_before>//
//  SimpleKeyboard.swift
//  AnyTracker
//
//  Created by <NAME> on 27/10/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

extension UIView {
    
    func isTextField() -> Bool {
        return self is UITextField
    }
}

// MARK: - SimpleKeyboard implementation

class SimpleKeyboard: NSObject {

    private var targetControls: [UIView]
    fileprivate var viewController: UIViewController
    
    fileprivate var activeView: UIView?
    
    fileprivate var viewOffset: CGFloat = 0
    fileprivate var keyboardAnimationDuration: Double = Constants.Animations.keyboardDuration
    fileprivate var keyboardAnimationCurve: UIViewAnimationCurve = Constants.Animations.keyboardCurve
    
    // MARK: - UITextField callbacks
    
    var textFieldDidBeginEditing: ((_ textField: UITextField)->())?
    var textFieldDidEndEditing: ((_ textField: UITextField)->())?
    var textFieldShouldReturn: ((_ textField: UITextField)->(Bool))?
    
    // MARK: - UITextView callbacks
    
    var textViewShouldBeginEditing: ((_ textView: UITextView)->(Bool))?
    var textViewShouldEndEditing: ((_ textView: UITextView)->(Bool))?
    
    // MARK: - Creation
    
    init(fromViewController vc: UIViewController) {
        targetControls = []
        viewController = vc
    }
    
    private init(withControls controls: [UIView], fromViewController vc: UIViewController) {
        targetControls = controls
        viewController = vc
        super.init()
        
        for control in targetControls {
            setDelegate(forControl: control)
        }
    }
    
    static func createKeyboard(forControls controls: [UIView?], fromViewController vc: UIViewController, andEnable enable: Bool = false) -> SimpleKeyboard? {
        var newKeyboard: SimpleKeyboard?
        var validControls: [UIView] = []
        for control in controls {
            if control != nil {
                if control is UITextField || control is UITextView {
                    validControls.append(control!)
                }
            }
        }
        
        if validControls.count > 0 {
            newKeyboard = SimpleKeyboard(withControls: validControls, fromViewController: vc)
            
            if enable {
                newKeyboard!.enable()
            }
        }
        
        return newKeyboard
    }
    
    // MARK: - Exposed actions
    
    func add(control: UIView, withDoneButtonKeyboard doneButton: Bool = false, doneTarget target: Any? = nil, doneSelector selector: Selector? = nil) {
        if control is UITextField || control is UITextView {
            targetControls.append(control)
            setDelegate(forControl: targetControls[targetControls.count - 1])
            
            if doneButton {
                SimpleKeyboard.addKeyboardDoneButton(forInput: control, fromViewController: viewController, withTarget: target, doneSelector: selector)
            }
        }
    }
    
    func enable() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    
    func disable() {
        NotificationCenter.default.removeObserver(self)
    }
    
    // Manually override the active input control<commit_msg>Update keyboard handling code<commit_after>//
//  SimpleKeyboard.swift
//  AnyTracker
//
//  Created by <NAME> on 27/10/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

extension UIView {
    
    func isTextField() -> Bool {
        return self is UITextField
    }
}

// MARK: - SimpleKeyboard implementation

class SimpleKeyboard: NSObject {

    private var targetControls: [UIView]
    fileprivate var viewController: UIViewController
    
    fileprivate var activeView: UIView?
    
    fileprivate var viewOffset: CGFloat = 0
    fileprivate var keyboardAnimationDuration: Double = Constants.Animations.keyboardDuration
    fileprivate var keyboardAnimationCurve: UIViewAnimationCurve = Constants.Animations.keyboardCurve
    
    // MARK: - UITextField callbacks
    
    var textFieldDidBeginEditing: ((_ textField: UITextField)->())?
    var textFieldDidEndEditing: ((_ textField: UITextField)->())?
    var textFieldShouldReturn: ((_ textField: UITextField)->(Bool))?
    
    // MARK: - UITextView callbacks
    
    var textViewShouldBeginEditing: ((_ textView: UITextView)->(Bool))?
    var textViewShouldEndEditing: ((_ textView: UITextView)->(Bool))?
    
    // MARK: - Creation
    
    init(fromViewController vc: UIViewController) {
        targetControls = []
        viewController = vc
    }
    
    private init(withControls controls: [UIView], fromViewController vc: UIViewController) {
        targetControls = controls
        viewController = vc
        super.init()
        
        for control in targetControls {
            setDelegate(forControl: control)
        }
    }
    
    static func createKeyboard(forControls controls: [UIView?], fromViewController vc: UIViewController, andEnable enable: Bool = false) -> SimpleKeyboard {
        var validControls: [UIView] = []
        for control in controls {
            if control != nil {
                if control is UITextField || control is UITextView {
                    validControls.append(control!)
                }
            }
        }
        
        var newKeyboard: SimpleKeyboard
        if validControls.count > 0 {
            newKeyboard = SimpleKeyboard(withControls: validControls, fromViewController: vc)
        } else {
            newKeyboard = SimpleKeyboard(fromViewController: vc)
        }
        
        if enable {
            newKeyboard.enable()
        }
        
        return newKeyboard
    }
    
    // MARK: - Exposed actions
    
    func add(control: UIView, withDoneButtonKeyboard doneButton: Bool = false, doneTarget target: Any? = nil, doneSelector selector: Selector? = nil) {
        if control is UITextField || control is UITextView {
            targetControls.append(control)
            setDelegate(forControl: targetControls[targetControls.count - 1])
            
            if doneButton {
                SimpleKeyboard.addKeyboardDoneButton(forInput: control, fromViewController: viewController, withTarget: target, doneSelector: selector)
            }
        }
    }
    
    func enable() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    
    func disable() {
        NotificationCenter.default.removeObserver(self)
        
        for control in targetControls {
            if control.isFirstResponder {
                control.resignFirstResponder()
                
                adaptView(moveUp: false)
                viewOffset = 0
                
                break
            }
        }
    }
    
    // Manually override the active input control
{'fraction_non_alphanumeric': 0.08545479565157561, 'fraction_numerical': 0.003715425897894592, 'mean_word_length': 2.3632577510411847, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '1185898', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1883, 'n_words': 462}