<commit_before>//
//  ActionsCollectionView.swift
//  SDCAlertController
//
//  Created by <NAME> on 7/13/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

private let kActionCellIdentifier = "actionCell"

class ActionsCollectionView: UICollectionView {

    var actions: [AlertAction] = []

    var visualStyle: VisualStyle! {
        didSet {
            guard let layout = self.collectionViewLayout as? ActionsCollectionViewFlowLayout else { return }
            layout.visualStyle = self.visualStyle
        }
    }

    var displayHeight: CGFloat {
        guard let layout = self.collectionViewLayout as? ActionsCollectionViewFlowLayout,
            let visualStyle = self.visualStyle else {
                return -1
            }

        if layout.scrollDirection == .Horizontal {
            return visualStyle.actionViewSize.height
        } else {
            return visualStyle.actionViewSize.height * CGFloat(self.numberOfItemsInSection(0))
        }
    }

    var actionTapped: ((AlertAction) -> Void)?

    private var highlightedCell: UICollectionViewCell?

    init() {
        super.init(frame: .zero, collectionViewLayout: ActionsCollectionViewFlowLayout())
        self.dataSource = self
        self.delegate = self
        self.backgroundColor = UIColor.clearColor()
        self.delaysContentTouches = false

        self.collectionViewLayout.registerClass(ActionSeparatorView.self,
            forDecorationViewOfKind: kHorizontalActionSeparator)
        self.collectionViewLayout.registerClass(ActionSeparatorView.self,
            forDecorationViewOfKind: kVerticalActionSeparator)

        self.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: "highlightCurrentAction:"))
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "highlightCurrentAction:"))

        let nibName = NSStringFromClass(ActionCell.self).componentsSeparatedByString(".").last!
        let nib = UINib(nibName: nibName, bundle: NSBundle(forClass: self.dynamicType))
        self.registerNib(nib, forCellWithReuseIdentifier: kActionCellIdentifier)
    }

    convenience required init?(coder aDecoder: NSCoder) {
        self.init()
    }

    @objc
    private func highlightCurrentAction(sender: UIGestureRecognizer) {
        if sender.state == .Cancelled || sender.state == .Failed || sender.state == .Ended {
            self.highlightedCell?.highlighted = false
            self.highlightedCell = nil
        }

        let touchPoint = sender.locationInView(self)
        guard let indexPath = indexPathForItemAtPoint(touchPoint), cell = cellForItemAtIndexPath(indexPath)
            where cell != self.highlightedCell else {
                return
            }

        if sender.state == .Began || sender.state == .Changed {
            self.highlightedCell?.highlighted = false
            cell.highlighted = true
            self.highlightedCell = cell
        }

        if sender.state == .Ended {
            self.actionTapped?(self.actions[indexPath.item])
        }
    }
}

extension ActionsCollectionView: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.actions.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
        -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kActionCellIdentifier,
            forIndexPath: indexPath) as? ActionCell
        cell?.action = self.actions[indexPath.item]
        cell?.visualStyle = self.visualStyle
        return cell!
    }
}

extension ActionsCollectionView: UICollectionViewDelegateFlowLayout {

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        let actionWidth = self.visualStyle.actionViewSize.width
        let actionHeight = self.visualStyle.actionViewSize.height

        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if layout.scrollDirection == .Horizontal {
            let width = max(self.bounds.width / CGFloat(self.numberOfItemsInSection(0)), actionWidth)
            return CGSize(width: width, height: actionHeight)
        } else {
            return CGSize(width: self.bounds.width, height: actionHeight)
        }
    }
}
<commit_msg>Allow for horizontal scrolling in buttons<commit_after>//
//  ActionsCollectionView.swift
//  SDCAlertController
//
//  Created by <NAME> on 7/13/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

private let kActionCellIdentifier = "actionCell"

class ActionsCollectionView: UICollectionView {

    var actions: [AlertAction] = []

    var visualStyle: VisualStyle! {
        didSet {
            guard let layout = self.collectionViewLayout as? ActionsCollectionViewFlowLayout else { return }
            layout.visualStyle = self.visualStyle
        }
    }

    var displayHeight: CGFloat {
        guard let layout = self.collectionViewLayout as? ActionsCollectionViewFlowLayout,
            let visualStyle = self.visualStyle else {
                return -1
            }

        if layout.scrollDirection == .Horizontal {
            return visualStyle.actionViewSize.height
        } else {
            return visualStyle.actionViewSize.height * CGFloat(self.numberOfItemsInSection(0))
        }
    }

    var actionTapped: ((AlertAction) -> Void)?

    private var highlightedCell: UICollectionViewCell?

    init() {
        super.init(frame: .zero, collectionViewLayout: ActionsCollectionViewFlowLayout())
        self.dataSource = self
        self.delegate = self
        self.backgroundColor = UIColor.clearColor()
        self.delaysContentTouches = false

        self.collectionViewLayout.registerClass(ActionSeparatorView.self,
            forDecorationViewOfKind: kHorizontalActionSeparator)
        self.collectionViewLayout.registerClass(ActionSeparatorView.self,
            forDecorationViewOfKind: kVerticalActionSeparator)

        let panGesture = UIPanGestureRecognizer(target: self, action: "highlightCurrentAction:")
        panGesture.delegate = self
        self.addGestureRecognizer(panGesture)

        let nibName = NSStringFromClass(ActionCell.self).componentsSeparatedByString(".").last!
        let nib = UINib(nibName: nibName, bundle: NSBundle(forClass: self.dynamicType))
        self.registerNib(nib, forCellWithReuseIdentifier: kActionCellIdentifier)
    }

    convenience required init?(coder aDecoder: NSCoder) {
        self.init()
    }

    @objc
    private func highlightCurrentAction(sender: UIGestureRecognizer) {
        let touchPoint = sender.locationInView(self)
        let touchIsInCollectionView = CGRectContainsPoint(self.bounds, touchPoint)

        let state = sender.state

        if state == .Cancelled || state == .Failed || state == .Ended || !touchIsInCollectionView {
            self.highlightedCell?.highlighted = false
            self.highlightedCell = nil
        }

        guard let indexPath = indexPathForItemAtPoint(touchPoint), cell = cellForItemAtIndexPath(indexPath)
            where cell != self.highlightedCell else {
                return
            }

        if sender.state == .Began || sender.state == .Changed {
            self.highlightedCell?.highlighted = false
            cell.highlighted = true
            self.highlightedCell = cell
        }

        if sender.state == .Ended {
            self.actionTapped?(self.actions[indexPath.item])
        }
    }
}

extension ActionsCollectionView: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.actions.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
        -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kActionCellIdentifier,
            forIndexPath: indexPath) as? ActionCell
        cell?.action = self.actions[indexPath.item]
        cell?.visualStyle = self.visualStyle
        return cell!
    }
}

extension ActionsCollectionView: UICollectionViewDelegateFlowLayout {

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        let actionWidth = self.visualStyle.actionViewSize.width
        let actionHeight = self.visualStyle.actionViewSize.height

        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if layout.scrollDirection == .Horizontal {
            let width = max(self.bounds.width / CGFloat(self.numberOfItemsInSection(0)), actionWidth)
            return CGSize(width: width, height: actionHeight)
        } else {
            return CGSize(width: self.bounds.width, height: actionHeight)
        }
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        self.actionTapped?(self.actions[indexPath.item])
    }
}

extension ActionsCollectionView: UIGestureRecognizerDelegate {

    override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer != self.panGestureRecognizer {
            let contentSize = self.contentSize
            return self.bounds.width >= contentSize.width && self.bounds.height >= contentSize.height
        }

        return super.gestureRecognizerShouldBegin(gestureRecognizer)
    }


}

{'fraction_non_alphanumeric': 0.0807165654277772, 'fraction_numerical': 0.0024709152681972615, 'mean_word_length': 2.9232633279483036, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, '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': '828865', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2537, 'n_words': 520}