<commit_before>//
//  Keyboard+LayoutGuide.swift
//  KeyboardLayoutGuide
//
//  Created by <NAME> on 14/11/2017.
//  Copyright © 2017 freshos. All rights reserved.
//

#if canImport(UIKit)

import UIKit

private class Keyboard {
    static let shared = Keyboard()
    var currentHeight: CGFloat = 0
}

public extension UIView {
    
    private struct AssociatedKeys {
        static var keyboardLayoutGuide = "keyboardLayoutGuide"
    }
    
    /// A layout guide representing the inset for the keyboard.
    /// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard.
    var bswKeyboardLayoutGuide: UILayoutGuide {
        get {
            #if targetEnvironment(macCatalyst)
            return self.safeAreaLayoutGuide
            #else
            #if swift(>=5.5) /// Let's assume Swift 5.5 ships with iOS 15 SDK
            if #available(iOS 15, *) {
                return self.keyboardLayoutGuide
            } else {
                return self.getCustomKeybardLayoutGuide()
            }
            #else
            return self.getCustomKeybardLayoutGuide()
            #endif
            #endif
        }
    }
    
    private func getCustomKeybardLayoutGuide() -> UILayoutGuide {
        if let obj = objc_getAssociatedObject(self, &AssociatedKeys.keyboardLayoutGuide) as? KeyboardLayoutGuide {
            return obj
        }
        let new = KeyboardLayoutGuide()
        addLayoutGuide(new)
        new.setUp()
        objc_setAssociatedObject(self, &AssociatedKeys.keyboardLayoutGuide, new as Any, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        return new
    }
}

@available(macCatalyst, unavailable)
open class KeyboardLayoutGuide: UILayoutGuide {
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override init() {
        super.init()
        
        // Observe keyboardWillChangeFrame notifications
        let nc = NotificationCenter.default
        nc.addObserver(self,
                       selector: #selector(keyboardWillChangeFrame(_:)),
                       name: UIResponder.keyboardWillChangeFrameNotification,
                       object: nil)
    }
    
    internal func setUp() {
        guard let view = owningView else {
            return
        }
        NSLayoutConstraint.activate([
            heightAnchor.constraint(equalToConstant: Keyboard.shared.currentHeight),
            leftAnchor.constraint(equalTo: view.leftAnchor),
            rightAnchor.constraint(equalTo: view.rightAnchor),
            ])
        let viewBottomAnchor = view.safeAreaLayoutGuide.bottomAnchor
        bottomAnchor.constraint(equalTo: viewBottomAnchor).isActive = true
    }
    
    @objc
    private func keyboardWillChangeFrame(_ note: Notification) {
        if var height = note.keyboardHeight {
            if height > 0 {
                height -= (owningView?.safeAreaInsets.bottom)!
            }
            heightConstraint?.constant = height
            animate(note)
            Keyboard.shared.currentHeight = height
        }
    }<commit_msg>Pinned Keyboard to bottomAnchor instead safeAreaLayoutGuide<commit_after>//
//  Keyboard+LayoutGuide.swift
//  KeyboardLayoutGuide
//
//  Created by <NAME> on 14/11/2017.
//  Copyright © 2017 freshos. All rights reserved.
//

#if canImport(UIKit)

import UIKit

private class Keyboard {
    static let shared = Keyboard()
    var currentHeight: CGFloat = 0
}

public extension UIView {
    
    private struct AssociatedKeys {
        static var keyboardLayoutGuide = "keyboardLayoutGuide"
    }
    
    /// A layout guide representing the inset for the keyboard.
    /// Use this layout guide’s top anchor to create constraints pinning to the top of the keyboard.
    var bswKeyboardLayoutGuide: UILayoutGuide {
        get {
            #if targetEnvironment(macCatalyst)
            return self.safeAreaLayoutGuide
            #else
            #if swift(>=5.5) /// Let's assume Swift 5.5 ships with iOS 15 SDK
            if #available(iOS 15, *) {
                return self.keyboardLayoutGuide
            } else {
                return self.getCustomKeybardLayoutGuide()
            }
            #else
            return self.getCustomKeybardLayoutGuide()
            #endif
            #endif
        }
    }
    
    private func getCustomKeybardLayoutGuide() -> UILayoutGuide {
        if let obj = objc_getAssociatedObject(self, &AssociatedKeys.keyboardLayoutGuide) as? KeyboardLayoutGuide {
            return obj
        }
        let new = KeyboardLayoutGuide()
        addLayoutGuide(new)
        new.setUp()
        objc_setAssociatedObject(self, &AssociatedKeys.keyboardLayoutGuide, new as Any, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        return new
    }
}

@available(macCatalyst, unavailable)
open class KeyboardLayoutGuide: UILayoutGuide {
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override init() {
        super.init()
        
        // Observe keyboardWillChangeFrame notifications
        let nc = NotificationCenter.default
        nc.addObserver(self,
                       selector: #selector(keyboardWillChangeFrame(_:)),
                       name: UIResponder.keyboardWillChangeFrameNotification,
                       object: nil)
    }
    
    internal func setUp() {
        guard let view = owningView else {
            return
        }
        NSLayoutConstraint.activate([
            heightAnchor.constraint(equalToConstant: Keyboard.shared.currentHeight),
            leftAnchor.constraint(equalTo: view.leftAnchor),
            rightAnchor.constraint(equalTo: view.rightAnchor),
            bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
    
    @objc
    private func keyboardWillChangeFrame(_ note: Notification) {
        if let height = note.keyboardHeight {
            heightConstraint?.constant = height
            animate(note)
            Keyboard.shared.currentHeight = height
        }
    }
{'fraction_non_alphanumeric': 0.07598883965205974, 'fraction_numerical': 0.007057278844575743, 'mean_word_length': 2.1091836734693876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, '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': '979492', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1573, 'n_words': 394}