<commit_before>//
//  ScrollableForm.swift
//  Pods
//
//  Created by <NAME> on 2/26/16.
//
//

import Foundation
import UIKit

public protocol ScrollableForm {
    var form:Form               {get set}
    
    func addKeyboardObserver()
    func removeKeyboardObserver()
    func keyboardWillShow(notification:NSNotification)
    func keyboardWillHide(notification:NSNotification)
    func scrollTo(frame:CGRect, duration:NSTimeInterval)
    func scrollView() -> UIScrollView
}

public extension ScrollableForm where Self:UIViewController {
    func addKeyboardObserver() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillHideNotification, object: nil)
    }
    
    func removeKeyboardObserver() {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
    }

    func keyboardWillShow(notification:NSNotification) {
        if let userInfo     = notification.userInfo, currentField = form.currentField {
            let frame       = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
            let size        = frame.size
            let height      = size.height
            let duration    = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber) as NSTimeInterval
            
            scrollTo(currentField.frame, duration: duration)
        }
    }
    
    func keyboardWillHide(notification:NSNotification) {
        if let userInfo = notification.userInfo, currentField = form.currentField {
            if let userInfo     = notification.userInfo {
                let duration    = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber) as NSTimeInterval
                
                scrollTo(.zero, duration: duration)
            }
        }
    }
    
    func scrollTo(frame:CGRect, duration:NSTimeInterval = 0.25) {
        UIView.animateWithDuration(duration, delay: 0.25, options: .CurveEaseInOut, animations: {
            self.scrollView().scrollRectToVisible(frame, animated: false)
        }, completion: nil)
    }
}

public protocol ScrollableTableViewForm : ScrollableForm {
    var tableView:UITableView {get}<commit_msg>making adjustments to scrollable forms<commit_after>//
//  ScrollableForm.swift
//  Pods
//
//  Created by <NAME> on 2/26/16.
//
//

import Foundation
import UIKit

public protocol ScrollableForm {
    var form:Form               {get set}
    
    func addKeyboardObserver()
    func removeKeyboardObserver()
    func keyboardWillShow(notification:NSNotification)
    func keyboardWillHide(notification:NSNotification)
    func scrollTo(frame:CGRect, duration:NSTimeInterval)
    func setScrollInsets(insets: UIEdgeInsets, duration:NSTimeInterval)
    func scrollView() -> UIScrollView
}

public extension ScrollableForm where Self:UIViewController {
    func addKeyboardObserver() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
    }
    
    func removeKeyboardObserver() {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func keyboardWillShow(notification:NSNotification) {
        if let userInfo     = notification.userInfo, currentField = form.currentField {
            let frame       = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
            let size        = frame.size
            let height      = size.height
            let duration    = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber) as NSTimeInterval
            
            let insets = UIEdgeInsetsMake(0, 0, height, 0)
            setScrollInsets(insets, duration: duration)
            scrollTo(currentField.frame, duration: duration)
        }
    }
    
    func keyboardWillHide(notification:NSNotification) {
        if let userInfo = notification.userInfo, currentField = form.currentField {
            let duration    = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber) as NSTimeInterval
            setScrollInsets(UIEdgeInsetsZero, duration: duration)
            scrollTo(.zero, duration: duration)
        }
    }
    
    func scrollTo(frame:CGRect, duration:NSTimeInterval = 0.25) {
        UIView.animateWithDuration(duration, delay: 0.25, options: .CurveEaseInOut, animations: {
            self.scrollView().scrollRectToVisible(frame, animated: false)
        }, completion: nil)
    }
    
    func setScrollInsets(insets: UIEdgeInsets, duration:NSTimeInterval = 0.25) {
        UIView.animateWithDuration(duration) {
            self.scrollView().contentInset = insets
            self.scrollView().scrollIndicatorInsets = insets
        }
    }
}

public protocol ScrollableViewForm : ScrollableForm {
    var scrollView: UIScrollView {get}
}

internal extension ScrollableViewForm {
    internal func scrollView() -> UIScrollView {
        return scrollView
    }
}

public protocol ScrollableTableViewForm : ScrollableForm {
    var tableView:UITableView {get}
{'fraction_non_alphanumeric': 0.0831353919239905, 'fraction_numerical': 0.0051160241183994155, 'mean_word_length': 3.146969696969697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '4123232', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1450, 'n_words': 311}