<commit_before>//
//  ProcedureKit
//
//  Copyright © 2015-2018 ProcedureKit. All rights reserved.
//

#if SWIFT_PACKAGE
import ProcedureKit
import Foundation
import UIKit
#endif

/// A struct of the views which require autolayout constraints
public struct AutolayoutViews {
    let child: UIView
    let parent: UIView
}

/// A block type which receives the child's view to perform any autolayout.
public typealias SetAutolayoutConstraintsBlockType = (AutolayoutViews) -> ()

@available(iOS 9.0, *)
public enum SetAutolayoutConstraints {

    // Uses NSLayoutConstraint pinning the child to the parent's anchors.
    case pinnedToParent

    // Provide a custom block
    case custom(SetAutolayoutConstraintsBlockType)

    public var block: SetAutolayoutConstraintsBlockType {
        switch self {
        case .pinnedToParent:
            return { views in
                NSLayoutConstraint.activate([
                    views.child.leadingAnchor.constraint(equalTo: views.parent.leadingAnchor),
                    views.child.trailingAnchor.constraint(equalTo: views.parent.trailingAnchor),
                    views.child.topAnchor.constraint(equalTo: views.parent.topAnchor),
                    views.child.bottomAnchor.constraint(equalTo: views.parent.bottomAnchor)
                ])
            }
        case let .custom(block):
            return block
        }
    }
}


internal extension UIViewController {

    func add(child: UIViewController, with frame: CGRect? = nil, in view: UIView, setAutolayoutConstraints block: @escaping SetAutolayoutConstraintsBlockType) {
        addChildViewController(child)
        child.view.frame = frame ?? CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
        view.addSubview(child.view)
        block(AutolayoutViews(child: child.view, parent: view))
        child.didMove(toParentViewController: self)
    }

    func removeFromParent() {
        self.willMove(toParentViewController: nil)
        self.view.removeFromSuperview()
        self.removeFromParentViewController()
    }
}


/**
 Procedure to safely add a child view controller to a parent using
 UIViewController containment.
 */
open class AddChildViewControllerProcedure: UIBlockProcedure {

    public init(_ child: UIViewController, to parent: UIViewController, with frame: CGRect? = nil, in subview: UIView? = nil, setAutolayoutConstraints block: @escaping SetAutolayoutConstraintsBlockType) {
        let view: UIView = subview ?? parent.view
        assert(view.isDescendant(of: parent.view))
        super.init {
            parent.add(child: child, with: frame, in: view, setAutolayoutConstraints: block)
        }
        name = "Add Child ViewController"
    }

    @available(iOS 9.0, *)
    public convenience init(_ child: UIViewController, to parent: UIViewController, with frame: CGRect? = nil, in view: UIView, setAutolayoutConstraints strategy: SetAutolayoutConstraints = .pinnedToParent) {
        self.init(child, to: parent, with: frame, in: view, setAutolayoutConstraints: strategy.block)
    }
}

/**
 Procedure to safely remove a child view controller from its parent using
 UIViewController containment.
 */
open class RemoveChildViewControllerProcedure: UIBlockProcedure {

    public init(_ child: UIViewController) {
        super.init {
            child.removeFromParent()
        }
        name = "Remove Child ViewController"
    }
}

open class SetChildViewControllerProcedure: UIBlockProcedure {

    public init(_ child: UIViewController, in parent: UIViewController, with frame: CGRect? = nil, in subview: UIView? = nil, setAutolayoutConstraints block: @escaping SetAutolayoutConstraintsBlockType) {
        let view: UIView = subview ?? parent.view
        assert(view.isDescendant(of: parent.view))
        super.init {
            parent.childViewControllers.forEach { $0.removeFromParent() }
            parent.add(child: child, with: frame, in: view, setAutolayoutConstraints: block)
        }
        name = "Set Child ViewController"
    }

    @available(iOS 9.0, *)
    public convenience init(_ child: UIViewController, to parent: UIViewController, with frame: CGRect? = nil, in view: UIView, setAutolayoutConstraints strategy: SetAutolayoutConstraints = .pinnedToParent) {
        self.init(child, in: parent, with: frame, in: view, setAutolayoutConstraints: strategy.block)
    }
}

<commit_msg>Fixes API mistakes with ViewControllerContainment stuff (#873)<commit_after>//
//  ProcedureKit
//
//  Copyright © 2015-2018 ProcedureKit. All rights reserved.
//

#if SWIFT_PACKAGE
import ProcedureKit
import Foundation
import UIKit
#endif

/// A struct of the views which require autolayout constraints
public struct AutolayoutViews {
    let child: UIView
    let parent: UIView
}

/// A block type which receives the child's view to perform any autolayout.
public typealias SetAutolayoutConstraintsBlockType = (AutolayoutViews) -> ()

@available(iOS 9.0, *)
public enum SetAutolayoutConstraints {

    // Uses NSLayoutConstraint pinning the child to the parent's anchors.
    case pinnedToParent

    // Provide a custom block
    case custom(SetAutolayoutConstraintsBlockType)

    public var block: SetAutolayoutConstraintsBlockType {
        switch self {
        case .pinnedToParent:
            return { views in
                NSLayoutConstraint.activate([
                    views.child.leadingAnchor.constraint(equalTo: views.parent.leadingAnchor),
                    views.child.trailingAnchor.constraint(equalTo: views.parent.trailingAnchor),
                    views.child.topAnchor.constraint(equalTo: views.parent.topAnchor),
                    views.child.bottomAnchor.constraint(equalTo: views.parent.bottomAnchor)
                ])
            }
        case let .custom(block):
            return block
        }
    }
}


internal extension UIViewController {

    func add(child: UIViewController, with frame: CGRect? = nil, in subview: UIView, setAutolayoutConstraints block: @escaping SetAutolayoutConstraintsBlockType) {
        addChildViewController(child)
        child.view.frame = frame ?? CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
        view.addSubview(child.view)
        block(AutolayoutViews(child: child.view, parent: view))
        child.didMove(toParentViewController: self)
    }

    func removeFromParent() {
        self.willMove(toParentViewController: nil)
        self.view.removeFromSuperview()
        self.removeFromParentViewController()
    }
}


/**
 Procedure to safely add a child view controller to a parent using
 UIViewController containment.
 */
open class AddChildViewControllerProcedure: UIBlockProcedure {

    public init(_ child: UIViewController, to parent: UIViewController, with frame: CGRect? = nil, in subview: UIView? = nil, setAutolayoutConstraints block: @escaping SetAutolayoutConstraintsBlockType) {
        let view: UIView = subview ?? parent.view
        assert(view.isDescendant(of: parent.view))
        super.init {
            parent.add(child: child, with: frame, in: view, setAutolayoutConstraints: block)
        }
        name = "Add Child ViewController"
    }

    @available(iOS 9.0, *)
    public convenience init(_ child: UIViewController, to parent: UIViewController, with frame: CGRect? = nil, in subview: UIView? = nil, setAutolayoutConstraints strategy: SetAutolayoutConstraints = .pinnedToParent) {
        self.init(child, to: parent, with: frame, in: subview, setAutolayoutConstraints: strategy.block)
    }
}

/**
 Procedure to safely remove a child view controller from its parent using
 UIViewController containment.
 */
open class RemoveChildViewControllerProcedure: UIBlockProcedure {

    public init(_ child: UIViewController) {
        super.init {
            child.removeFromParent()
        }
        name = "Remove Child ViewController"
    }
}

open class SetChildViewControllerProcedure: UIBlockProcedure {

    public init(_ child: UIViewController, in parent: UIViewController, with frame: CGRect? = nil, in subview: UIView? = nil, setAutolayoutConstraints block: @escaping SetAutolayoutConstraintsBlockType) {
        let view: UIView = subview ?? parent.view
        assert(view.isDescendant(of: parent.view))
        super.init {
            parent.childViewControllers.forEach { $0.removeFromParent() }
            parent.add(child: child, with: frame, in: view, setAutolayoutConstraints: block)
        }
        name = "Set Child ViewController"
    }

    @available(iOS 9.0, *)
    public convenience init(_ child: UIViewController, in parent: UIViewController, with frame: CGRect? = nil, in subview: UIView? = nil, setAutolayoutConstraints strategy: SetAutolayoutConstraints = .pinnedToParent) {
        self.init(child, in: parent, with: frame, in: subview, setAutolayoutConstraints: strategy.block)
    }
}


{'fraction_non_alphanumeric': 0.0863431151241535, 'fraction_numerical': 0.004176072234762979, 'mean_word_length': 3.2498800959232614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '134718', 'n_tokens_mistral': 2482, 'n_tokens_neox': 2342, 'n_words': 668}