<commit_before>//
//  Copyright 2016 Lionheart Software LLC
//
//  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.
//

import UIKit

public struct KeyboardAdjustmentHelper {
    public var constraint: NSLayoutConstraint?
    public var animated = false

    fileprivate var willHideBlockObserver: NSObjectProtocol?
    fileprivate var willShowBlockObserver: NSObjectProtocol?

    public init() { }
}

public protocol KeyboardAdjuster: class {
    var keyboardAdjustmentHelper: KeyboardAdjustmentHelper { get set }
}

private extension UIViewController {
    enum KeyboardState {
        case hidden
        case visible
    }

    private func keyboardWillChangeAppearance(_ sender: Notification, toState: KeyboardState) {
        guard let conformingSelf = self as? KeyboardAdjuster,
            let constraint = conformingSelf.keyboardAdjustmentHelper.constraint,
            let userInfo = (sender as NSNotification).userInfo as? [String: AnyObject],
            let _curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int,
            let curve = UIViewAnimationCurve(rawValue: _curve),
            let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double else {
                return
        }

        let curveAnimationOption: UIViewAnimationOptions
        switch curve {
        case .easeIn:
            curveAnimationOption = .curveEaseIn

        case .easeInOut:
            curveAnimationOption = []

        case .easeOut:
            curveAnimationOption = .curveEaseOut

        case .linear:
            curveAnimationOption = .curveLinear
        }

        switch toState {
        case .hidden:
            constraint.constant = 0

        case .visible:
            guard let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
                debugPrint("UIKeyboardFrameEndUserInfoKey not available.")
                break
            }

            let frame = value.cgRectValue
            let keyboardFrameInViewCoordinates = view.convert(frame, from: nil)
            constraint.constant = view.bounds.height - keyboardFrameInViewCoordinates.origin.y
        }

        if conformingSelf.keyboardAdjustmentHelper.animated {
            let animationOptions: UIViewAnimationOptions = [UIViewAnimationOptions.beginFromCurrentState, curveAnimationOption]
            UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: {
                self.view.layoutIfNeeded()
            })
        } else {
            view.layoutIfNeeded()
        }
    }

    /**
     A callback that manages the bottom constraint when the keyboard is about to be hidden.
<commit_msg>allow helper to save original constant value<commit_after>//
//  Copyright 2016 Lionheart Software LLC
//
//  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.
//

import UIKit

public struct KeyboardAdjustmentHelper {
    public var constraint: NSLayoutConstraint? {
        didSet {
            guard let constraint = constraint else {
                return
            }

            originalConstant = constraint.constant
        }
    }
    public var animated = false

    fileprivate var originalConstant: CGFloat = 0
    fileprivate var willHideBlockObserver: NSObjectProtocol?
    fileprivate var willShowBlockObserver: NSObjectProtocol?

    public init() { }
}

public protocol KeyboardAdjuster: class {
    var keyboardAdjustmentHelper: KeyboardAdjustmentHelper { get set }
}

private extension UIViewController {
    enum KeyboardState {
        case hidden
        case visible
    }

    private func keyboardWillChangeAppearance(_ sender: Notification, toState: KeyboardState) {
        guard let conformingSelf = self as? KeyboardAdjuster,
            let constraint = conformingSelf.keyboardAdjustmentHelper.constraint,
            let userInfo = (sender as NSNotification).userInfo as? [String: AnyObject],
            let _curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? Int,
            let curve = UIViewAnimationCurve(rawValue: _curve),
            let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double else {
                return
        }

        let curveAnimationOption: UIViewAnimationOptions
        switch curve {
        case .easeIn:
            curveAnimationOption = .curveEaseIn

        case .easeInOut:
            curveAnimationOption = []

        case .easeOut:
            curveAnimationOption = .curveEaseOut

        case .linear:
            curveAnimationOption = .curveLinear
        }

        switch toState {
        case .hidden:
            constraint.constant = conformingSelf.keyboardAdjustmentHelper.originalConstant

        case .visible:
            guard let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
                debugPrint("UIKeyboardFrameEndUserInfoKey not available.")
                break
            }

            let frame = value.cgRectValue
            let keyboardFrameInViewCoordinates = view.convert(frame, from: nil)
            constraint.constant = view.bounds.height - keyboardFrameInViewCoordinates.origin.y + conformingSelf.keyboardAdjustmentHelper.originalConstant
        }

        if conformingSelf.keyboardAdjustmentHelper.animated {
            let animationOptions: UIViewAnimationOptions = [UIViewAnimationOptions.beginFromCurrentState, curveAnimationOption]
            UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: {
                self.view.layoutIfNeeded()
            })
        } else {
            view.layoutIfNeeded()
        }
    }

    /**
     A callback that manages the bottom constraint when the keyboard is about to be hidden.

{'fraction_non_alphanumeric': 0.06406436233611443, 'fraction_numerical': 0.0029797377830750892, 'mean_word_length': 2.5368809272918864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2344018', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1612, 'n_words': 515}