<commit_before>import UIKit

/**
 The tab bar delegate that will tell you when a button in the tab bar was pressed.
 */
public protocol TabbyBarDelegate {

  func tabbyButtonDidPress(index: Int)
}

/**
 The actual tab bar.
 */
public class TabbyBar: UIView {

  /**
   A translucent view that will simulate a blur effect for your tab bar.
   */
  public lazy var translucentView: UIVisualEffectView = {
    let view = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
    view.translatesAutoresizingMaskIntoConstraints = false

    return view
  }()

  /**
   The indicator that lives in the tab bar telling the user in which tab bar she is currently in.
   */
  public lazy var indicator: UIView = {
    let view = UIView()
    view.backgroundColor = Constant.Color.indicator
    view.translatesAutoresizingMaskIntoConstraints = false

    return view
  }()

  /**
   The separator between the controller and the tab bar.
   */
  public lazy var separator: UIView = {
    let view = UIView()
    view.backgroundColor = Constant.Color.separator
    view.translatesAutoresizingMaskIntoConstraints = false

    return view
  }()

  /**
   The selected controller that needs to be displayed.
   */
  public var selectedController = 0 {
    didSet {
      configureController(selectedController)
      selectedIndex = selectedController
    }
  }

  /**
   The general icons.
   */
  public var icons: [UIImage] = []

  /**
   The selected icons, if you don't want tint but want to change completely the icon.
   */
  public var selectedIcons: [UIImage] = []

  /**
   The list of buttons of the tab bar.
   */
  public var buttons: [UIButton] = []

  /**
   The labels that contain the titles.
   */
  public var titles: [UILabel] = []

  /**
   The animations that the icons will have when the button is tapped.
   */
  public var animations : [TabbyAnimation.Kind] = []

  /**
   The delegate that will inform when an item was tapped.
   */
  public var delegate: TabbyBarDelegate?

  var selectedIndex = 0 {
    didSet {
      animateIndicator(selectedIndex)
    }
  }

  // MARK: - Initializers

  /**
   Initializer
   */
  public override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = Constant.Color.background

    [indicator, separator].forEach { addSubview($0) }
  }

  /**
   Initializer.
   */
  public required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  // MARK: - Action methods

  func buttonDidTouchDown(button: UIButton) {
    UIView.animateWithDuration(0.1, animations: {
      button.transform = CGAffineTransformMakeScale(0.8, 0.8)
    })
  }

  func buttonDidCancel(button: UIButton) {
    UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [.CurveEaseInOut], animations: {
      button.transform = CGAffineTransformIdentity
      }, completion: nil)
  }

  func buttonDidPress(button: UIButton) {
    buttonDidCancel(button)

    if selectedIcons.isEmpty {
      for button in buttons { button.tintColor = Constant.Color.disabled }

      button.tintColor = Constant.Color.selected
    } else {
      for (index, button) in buttons.enumerate() {
        button.setImage(icons[index], forState: .Normal)
      }

      button.setImage(selectedIcons[button.tag], forState: .Normal)
    }

    for label in titles { label.textColor = Constant.Color.disabled }

    if let index = buttons.indexOf(button) where index < titles.count {
      titles[index].textColor = Constant.Color.selected
    }

    delegate?.tabbyButtonDidPress(button.tag)
    selectedIndex = button.tag
  }

  // MARK: - Animations

  func animateIndicator(index: Int) {
    UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [.CurveEaseIn], animations: {
      self.indicator.center.x = self.buttons[index].center.x
      }, completion: nil)
  }

  // MARK: - Helper methods

  func configureController(index: Int) {
    guard index < buttons.count else { return }
    buttonDidPress(buttons[index])
  }

  func prepareTranslucency(translucent: Bool) {
    translucentView.removeFromSuperview()

    if translucent {
      insertSubview(translucentView, atIndex: 0)
      constraint(translucentView, attributes: [.Width, .Height, .Top, .Left])
      backgroundColor = Constant.Color.background.colorWithAlphaComponent(0.85)
    } else {
      backgroundColor = Constant.Color.background
    }
  }

  func prepare(tuples: [(controller: UIViewController, image: UIImage?)]) {
    buttons = []
    titles = []
    icons = []
    animations = []

    for (index, tuple) in tuples.enumerate() {
      let button = UIButton()
      button.tag = index
      button.adjustsImageWhenHighlighted = false
      button.translatesAutoresizingMaskIntoConstraints = false
      button.addTarget(self, action: #selector(buttonDidTouchDown(_:)), forControlEvents: .TouchDown)
      button.addTarget(self, action: #selector(buttonDidTouchDown(_:)), forControlEvents: .TouchDragEnter)
      button.addTarget(self, action: #selector(buttonDidCancel(_:)), forControlEvents: .TouchCancel)
      button.addTarget(self, action: #selector(buttonDidCancel(_:)), forControlEvents: .TouchDragExit)
      button.addTarget(self, action: #selector(buttonDidPress(_:)), forControlEvents: .TouchUpInside)
<commit_msg>Use array when setting targets to the button<commit_after>import UIKit

/**
 The tab bar delegate that will tell you when a button in the tab bar was pressed.
 */
public protocol TabbyBarDelegate {

  func tabbyButtonDidPress(index: Int)
}

/**
 The actual tab bar.
 */
public class TabbyBar: UIView {

  /**
   A translucent view that will simulate a blur effect for your tab bar.
   */
  public lazy var translucentView: UIVisualEffectView = {
    let view = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
    view.translatesAutoresizingMaskIntoConstraints = false

    return view
  }()

  /**
   The indicator that lives in the tab bar telling the user in which tab bar she is currently in.
   */
  public lazy var indicator: UIView = {
    let view = UIView()
    view.backgroundColor = Constant.Color.indicator
    view.translatesAutoresizingMaskIntoConstraints = false

    return view
  }()

  /**
   The separator between the controller and the tab bar.
   */
  public lazy var separator: UIView = {
    let view = UIView()
    view.backgroundColor = Constant.Color.separator
    view.translatesAutoresizingMaskIntoConstraints = false

    return view
  }()

  /**
   The selected controller that needs to be displayed.
   */
  public var selectedController = 0 {
    didSet {
      configureController(selectedController)
      selectedIndex = selectedController
    }
  }

  /**
   The general icons.
   */
  public var icons: [UIImage] = []

  /**
   The selected icons, if you don't want tint but want to change completely the icon.
   */
  public var selectedIcons: [UIImage] = []

  /**
   The list of buttons of the tab bar.
   */
  public var buttons: [UIButton] = []

  /**
   The labels that contain the titles.
   */
  public var titles: [UILabel] = []

  /**
   The animations that the icons will have when the button is tapped.
   */
  public var animations : [TabbyAnimation.Kind] = []

  /**
   The delegate that will inform when an item was tapped.
   */
  public var delegate: TabbyBarDelegate?

  var selectedIndex = 0 {
    didSet {
      animateIndicator(selectedIndex)
    }
  }

  // MARK: - Initializers

  /**
   Initializer
   */
  public override init(frame: CGRect) {
    super.init(frame: frame)

    backgroundColor = Constant.Color.background

    [indicator, separator].forEach { addSubview($0) }
  }

  /**
   Initializer.
   */
  public required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  // MARK: - Action methods

  func buttonDidTouchDown(button: UIButton) {
    UIView.animateWithDuration(0.1, animations: {
      button.transform = CGAffineTransformMakeScale(0.8, 0.8)
    })
  }

  func buttonDidCancel(button: UIButton) {
    UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [.CurveEaseInOut], animations: {
      button.transform = CGAffineTransformIdentity
      }, completion: nil)
  }

  func buttonDidPress(button: UIButton) {
    buttonDidCancel(button)

    if selectedIcons.isEmpty {
      for button in buttons { button.tintColor = Constant.Color.disabled }

      button.tintColor = Constant.Color.selected
    } else {
      for (index, button) in buttons.enumerate() {
        button.setImage(icons[index], forState: .Normal)
      }

      button.setImage(selectedIcons[button.tag], forState: .Normal)
    }

    for label in titles { label.textColor = Constant.Color.disabled }

    if let index = buttons.indexOf(button) where index < titles.count {
      titles[index].textColor = Constant.Color.selected
    }

    delegate?.tabbyButtonDidPress(button.tag)
    selectedIndex = button.tag
  }

  // MARK: - Animations

  func animateIndicator(index: Int) {
    UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [.CurveEaseIn], animations: {
      self.indicator.center.x = self.buttons[index].center.x
      }, completion: nil)
  }

  // MARK: - Helper methods

  func configureController(index: Int) {
    guard index < buttons.count else { return }
    buttonDidPress(buttons[index])
  }

  func prepareTranslucency(translucent: Bool) {
    translucentView.removeFromSuperview()

    if translucent {
      insertSubview(translucentView, atIndex: 0)
      constraint(translucentView, attributes: [.Width, .Height, .Top, .Left])
      backgroundColor = Constant.Color.background.colorWithAlphaComponent(0.85)
    } else {
      backgroundColor = Constant.Color.background
    }
  }

  func prepare(tuples: [(controller: UIViewController, image: UIImage?)]) {
    buttons = []
    titles = []
    icons = []
    animations = []

    for (index, tuple) in tuples.enumerate() {
      let button = UIButton()
      button.tag = index
      button.adjustsImageWhenHighlighted = false
      button.translatesAutoresizingMaskIntoConstraints = false
      button.addTarget(self, action: #selector(buttonDidTouchDown(_:)), forControlEvents: [.TouchDown, .TouchDragEnter])
      button.addTarget(self, action: #selector(buttonDidCancel(_:)), forControlEvents: [.TouchCancel, .TouchDragExit])
      button.addTarget(self, action: #selector(buttonDidPress(_:)), forControlEvents: .TouchUpInside)

{'fraction_non_alphanumeric': 0.10351966873706005, 'fraction_numerical': 0.004705439488048184, 'mean_word_length': 3.254203362690152, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '7353443', 'n_tokens_mistral': 3239, 'n_tokens_neox': 3094, 'n_words': 823}