<commit_before>import Library
import Prelude
import UIKit

internal final class DebugPushNotificationsViewController: UIViewController {

  @IBOutlet private weak var rootStackView: UIStackView!
  @IBOutlet private weak var scrollView: UIScrollView!
  @IBOutlet private var separatorViews: [UIView]!

  // swiftlint:disable function_body_length
  internal override func bindStyles() {
    super.bindStyles()

    self
      |> baseControllerStyle()

    let rowsStackViews = self.rootStackView.arrangedSubviews.flatMap { $0 as? UIStackView }
    let rowStackViews = rowsStackViews.flatMap { rows in
      rows.arrangedSubviews.flatMap { $0 as? UIStackView }
    }
    let buttonStackViews = rowStackViews.flatMap { $0.arrangedSubviews.last as? UIStackView }
    let titleLabels = self.rootStackView.arrangedSubviews.flatMap { $0 as? UILabel }
    let buttons = buttonStackViews
      .flatMap { stackView in stackView.arrangedSubviews.flatMap { $0 as? UIButton } }
    let inAppButtons = buttons.enumerate().filter { idx, _ in idx % 2 == 0 }.map(second)
    let delayedButtons = buttons.enumerate().filter { idx, _ in idx % 2 == 1 }.map(second)

    self.rootStackView
      |> UIStackView.lens.spacing .~ Styles.grid(3)

    self.scrollView
      |> UIScrollView.lens.delaysContentTouches .~ false

    rowsStackViews
      ||> UIStackView.lens.spacing .~ Styles.grid(2)

    rowStackViews
      ||> UIStackView.lens.distribution .~ .EqualSpacing
      ||> UIStackView.lens.alignment .~ .Center

    buttonStackViews
      ||> UIStackView.lens.spacing .~ Styles.grid(1)

    titleLabels
      ||> UILabel.lens.textColor .~ .ksr_text_navy_600
      ||> UILabel.lens.font .~ .ksr_title1(size: 22)

    rowStackViews.flatMap { $0.arrangedSubviews.first as? UILabel }
      ||> UILabel.lens.textColor .~ .ksr_text_navy_900
      ||> UILabel.lens.font .~ .ksr_body()

    buttons
      ||> greenButtonStyle
      ||> UIButton.lens.contentEdgeInsets %~ {
        .init(top: $0.top/2, left: $0.left/2, bottom: $0.bottom/2, right: $0.right/2)
    }

    inAppButtons.enumerate().forEach { idx, button in
      button
        |> UIButton.lens.tag .~ idx
        |> UIButton.lens.title(forState: .Normal) .~ "In-app"
        |> UIButton.lens.targets .~ [(self, #selector(inAppButtonTapped(_:)), .TouchUpInside)]
    }

    delayedButtons.enumerate().forEach { idx, button in
      button
        |> UIButton.lens.tag .~ idx
        |> UIButton.lens.title(forState: .Normal) .~ "Delayed"
        |> UIButton.lens.targets .~ [(self, #selector(delayedButtonTapped(_:)), .TouchUpInside)]
    }

    self.separatorViews
      ||> separatorStyle
  }
  // swiftlint:enable function_body_length

  @objc private func inAppButtonTapped(button: UIButton) {
    self.scheduleNotification(forIndex: button.tag, delay: false)
  }

  @objc private func delayedButtonTapped(button: UIButton) {
    self.scheduleNotification(forIndex: button.tag, delay: true)
  }

  private func scheduleNotification(forIndex index: Int, delay: Bool) {
    guard index >= 0 && index < allPushData.count else { return }

    let pushData = allPushData[index]

    let notification = UILocalNotification()
    notification.fireDate = NSDate(timeIntervalSinceNow: delay ? 5 : 0)
    notification.alertBody = (pushData["aps"] as? [String:AnyObject])?["alert"] as? String
    notification.userInfo = pushData
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
  }
}

private let allPushData: [[String:AnyObject]] = [
  [
    "aps": [
      "alert": "Blob McBlobby backed Double Fine Adventure."
    ],
    "activity": [
      "category": "backing",
      "id": 1,
      "project_id": 1929840910
    ]
  ],

  [
    "aps": [
      "alert": "Update #6 posted by Double Fine Adventure."
    ],
    "activity": [
      "category": "post",
      "id": 1,
      "project_id": 1929840910,
      "update_id": 190349
    ]
  ],

  [
    "aps": [
      "alert": "Double Fine Adventure has been successfully funded!"<commit_msg>Category of updates is not "post"<commit_after>import Library
import Prelude
import UIKit

internal final class DebugPushNotificationsViewController: UIViewController {

  @IBOutlet private weak var rootStackView: UIStackView!
  @IBOutlet private weak var scrollView: UIScrollView!
  @IBOutlet private var separatorViews: [UIView]!

  // swiftlint:disable function_body_length
  internal override func bindStyles() {
    super.bindStyles()

    self
      |> baseControllerStyle()

    let rowsStackViews = self.rootStackView.arrangedSubviews.flatMap { $0 as? UIStackView }
    let rowStackViews = rowsStackViews.flatMap { rows in
      rows.arrangedSubviews.flatMap { $0 as? UIStackView }
    }
    let buttonStackViews = rowStackViews.flatMap { $0.arrangedSubviews.last as? UIStackView }
    let titleLabels = self.rootStackView.arrangedSubviews.flatMap { $0 as? UILabel }
    let buttons = buttonStackViews
      .flatMap { stackView in stackView.arrangedSubviews.flatMap { $0 as? UIButton } }
    let inAppButtons = buttons.enumerate().filter { idx, _ in idx % 2 == 0 }.map(second)
    let delayedButtons = buttons.enumerate().filter { idx, _ in idx % 2 == 1 }.map(second)

    self.rootStackView
      |> UIStackView.lens.spacing .~ Styles.grid(3)

    self.scrollView
      |> UIScrollView.lens.delaysContentTouches .~ false

    rowsStackViews
      ||> UIStackView.lens.spacing .~ Styles.grid(2)

    rowStackViews
      ||> UIStackView.lens.distribution .~ .EqualSpacing
      ||> UIStackView.lens.alignment .~ .Center

    buttonStackViews
      ||> UIStackView.lens.spacing .~ Styles.grid(1)

    titleLabels
      ||> UILabel.lens.textColor .~ .ksr_text_navy_600
      ||> UILabel.lens.font .~ .ksr_title1(size: 22)

    rowStackViews.flatMap { $0.arrangedSubviews.first as? UILabel }
      ||> UILabel.lens.textColor .~ .ksr_text_navy_900
      ||> UILabel.lens.font .~ .ksr_body()

    buttons
      ||> greenButtonStyle
      ||> UIButton.lens.contentEdgeInsets %~ {
        .init(top: $0.top/2, left: $0.left/2, bottom: $0.bottom/2, right: $0.right/2)
    }

    inAppButtons.enumerate().forEach { idx, button in
      button
        |> UIButton.lens.tag .~ idx
        |> UIButton.lens.title(forState: .Normal) .~ "In-app"
        |> UIButton.lens.targets .~ [(self, #selector(inAppButtonTapped(_:)), .TouchUpInside)]
    }

    delayedButtons.enumerate().forEach { idx, button in
      button
        |> UIButton.lens.tag .~ idx
        |> UIButton.lens.title(forState: .Normal) .~ "Delayed"
        |> UIButton.lens.targets .~ [(self, #selector(delayedButtonTapped(_:)), .TouchUpInside)]
    }

    self.separatorViews
      ||> separatorStyle
  }
  // swiftlint:enable function_body_length

  @objc private func inAppButtonTapped(button: UIButton) {
    self.scheduleNotification(forIndex: button.tag, delay: false)
  }

  @objc private func delayedButtonTapped(button: UIButton) {
    self.scheduleNotification(forIndex: button.tag, delay: true)
  }

  private func scheduleNotification(forIndex index: Int, delay: Bool) {
    guard index >= 0 && index < allPushData.count else { return }

    let pushData = allPushData[index]

    let notification = UILocalNotification()
    notification.fireDate = NSDate(timeIntervalSinceNow: delay ? 5 : 0)
    notification.alertBody = (pushData["aps"] as? [String:AnyObject])?["alert"] as? String
    notification.userInfo = pushData
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
  }
}

private let allPushData: [[String:AnyObject]] = [
  [
    "aps": [
      "alert": "Blob McBlobby backed Double Fine Adventure."
    ],
    "activity": [
      "category": "backing",
      "id": 1,
      "project_id": 1929840910
    ]
  ],

  [
    "aps": [
      "alert": "Update #6 posted by Double Fine Adventure."
    ],
    "activity": [
      "category": "update",
      "id": 1,
      "project_id": 1929840910,
      "update_id": 190349
    ]
  ],

  [
    "aps": [
      "alert": "Double Fine Adventure has been successfully funded!"
{'fraction_non_alphanumeric': 0.12496882015465204, 'fraction_numerical': 0.015465203292591668, 'mean_word_length': 3.2072402938090243, 'pattern_counts': {'":': 30, '<': 5, '<?xml version=': 0, '>': 45, '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': '6022003', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2543, 'n_words': 532}