<commit_before>import FBSDKCoreKit
import FBSDKLoginKit
import GoogleSignIn

@objc(MWMAuthorizationViewController)
final class AuthorizationViewController: MWMViewController {
  typealias ViewModel = MWMAuthorizationViewModel

  private let transitioningManager: AuthorizationTransitioningManager

  lazy var chromeView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.blackStatusBarBackground()
    return view
  }()

  weak var containerView: UIView! {
    didSet {
      containerView.insertSubview(chromeView, at: 0)
    }
  }

  @IBOutlet private weak var titleLabel: UILabel! {
    didSet {
      titleLabel.font = UIFont.bold22()
      titleLabel.textColor = UIColor.blackPrimaryText()
      titleLabel.text = L("profile_authorization_title")
    }
  }

  @IBOutlet weak var separator: UIView! {
    didSet {
      separator.backgroundColor = UIColor.blackDividers()
    }
  }

  @IBOutlet private weak var textLabel: UILabel! {
    didSet {
      textLabel.font = UIFont.regular14()
      textLabel.textColor = UIColor.blackSecondaryText()
      textLabel.text = L("profile_authorization_message")
    }
  }

  @IBOutlet private weak var googleButton: UIButton! {
    didSet {
      let layer = googleButton.layer
      layer.cornerRadius = 8
      layer.borderWidth = 1
      layer.borderColor = UIColor.blackDividers().cgColor
      googleButton.clipsToBounds = true
      googleButton.setTitle("Google", for: .normal)
      googleButton.setTitleColor(UIColor.blackPrimaryText(), for: .normal)
      googleButton.titleLabel?.font = UIFont.bold14()
      let gid = GIDSignIn.sharedInstance()!
      gid.delegate = self
      gid.uiDelegate = self
    }
  }

  @IBAction func googleSignIn() {
    GIDSignIn.sharedInstance().signIn()
  }

  private lazy var facebookButton: FBSDKLoginButton = {
    let button = FBSDKLoginButton()
    button.delegate = self
    button.loginBehavior = .systemAccount
    button.setAttributedTitle(NSAttributedString(string: "Facebook"), for: .normal)
    button.readPermissions = ["public_profile", "email"]
    return button
  }()

  @IBOutlet private weak var facebookButtonHolder: UIView! {
    didSet {
      facebookButton.translatesAutoresizingMaskIntoConstraints = false
      facebookButtonHolder.addSubview(facebookButton)
      facebookButton.removeConstraints(facebookButton.constraints)
      addConstraints(v1: facebookButton, v2: facebookButtonHolder)
      facebookButtonHolder.layer.cornerRadius = 8
      facebookButtonHolder.clipsToBounds = true
    }
  }

  private let completion: (() -> Void)?

  private func addConstraints(v1: UIView, v2: UIView) {
    [NSLayoutAttribute.top, .bottom, .left, .right].forEach {
      NSLayoutConstraint(item: v1, attribute: $0, relatedBy: .equal, toItem: v2, attribute: $0, multiplier: 1, constant: 0).isActive = true
    }
  }

  @objc init(barButtonItem: UIBarButtonItem?, completion: (() -> Void)? = nil) {
    self.completion = completion
    transitioningManager = AuthorizationTransitioningManager(barButtonItem: barButtonItem)
    super.init(nibName: toString(type(of: self)), bundle: nil)
    transitioningDelegate = transitioningManager
    modalPresentationStyle = .custom
  }

  @objc init(popoverSourceView: UIView? = nil, permittedArrowDirections: UIPopoverArrowDirection = .unknown, completion: (() -> Void)? = nil) {
    self.completion = completion
    transitioningManager = AuthorizationTransitioningManager(popoverSourceView: popoverSourceView, permittedArrowDirections: permittedArrowDirections)
    super.init(nibName: toString(type(of: self)), bundle: nil)
    transitioningDelegate = transitioningManager
    modalPresentationStyle = .custom
  }

  required init?(coder _: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let fbImage = facebookButton.subviews.first(where: { $0 is UIImageView && $0.frame != facebookButton.frame })
    fbImage?.frame = CGRect(x: 16, y: 8, width: 24, height: 24)
  }

  @IBAction func onCancel() {
    dismiss(animated: true, completion: nil)
  }

  private func process(error: Error) {
    textLabel.text = L("profile_authorization_error")
    Crashlytics.sharedInstance().recordError(error)
  }

  private func process(token: String, type: MWMSocialTokenType) {
    ViewModel.authenticate(withToken: token, type: type)
    dismiss(animated: true, completion: completion)
  }
}

extension AuthorizationViewController: FBSDKLoginButtonDelegate {
  func loginButton(_: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if let error = error {
      process(error: error)
    } else if let result = result {
      process(token: result.token.tokenString, type: .facebook)
    }
  }

  func loginButtonDidLogOut(_: FBSDKLoginButton!) {}<commit_msg>[ios] Correct auth cancel.<commit_after>import FBSDKCoreKit
import FBSDKLoginKit
import GoogleSignIn

@objc(MWMAuthorizationViewController)
final class AuthorizationViewController: MWMViewController {
  typealias ViewModel = MWMAuthorizationViewModel

  private let transitioningManager: AuthorizationTransitioningManager

  lazy var chromeView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.blackStatusBarBackground()
    return view
  }()

  weak var containerView: UIView! {
    didSet {
      containerView.insertSubview(chromeView, at: 0)
    }
  }

  @IBOutlet private weak var titleLabel: UILabel! {
    didSet {
      titleLabel.font = UIFont.bold22()
      titleLabel.textColor = UIColor.blackPrimaryText()
      titleLabel.text = L("profile_authorization_title")
    }
  }

  @IBOutlet weak var separator: UIView! {
    didSet {
      separator.backgroundColor = UIColor.blackDividers()
    }
  }

  @IBOutlet private weak var textLabel: UILabel! {
    didSet {
      textLabel.font = UIFont.regular14()
      textLabel.textColor = UIColor.blackSecondaryText()
      textLabel.text = L("profile_authorization_message")
    }
  }

  @IBOutlet private weak var googleButton: UIButton! {
    didSet {
      let layer = googleButton.layer
      layer.cornerRadius = 8
      layer.borderWidth = 1
      layer.borderColor = UIColor.blackDividers().cgColor
      googleButton.clipsToBounds = true
      googleButton.setTitle("Google", for: .normal)
      googleButton.setTitleColor(UIColor.blackPrimaryText(), for: .normal)
      googleButton.titleLabel?.font = UIFont.bold14()
      let gid = GIDSignIn.sharedInstance()!
      gid.delegate = self
      gid.uiDelegate = self
    }
  }

  @IBAction func googleSignIn() {
    GIDSignIn.sharedInstance().signIn()
  }

  private lazy var facebookButton: FBSDKLoginButton = {
    let button = FBSDKLoginButton()
    button.delegate = self
    button.loginBehavior = .systemAccount
    button.setAttributedTitle(NSAttributedString(string: "Facebook"), for: .normal)
    button.readPermissions = ["public_profile", "email"]
    return button
  }()

  @IBOutlet private weak var facebookButtonHolder: UIView! {
    didSet {
      facebookButton.translatesAutoresizingMaskIntoConstraints = false
      facebookButtonHolder.addSubview(facebookButton)
      facebookButton.removeConstraints(facebookButton.constraints)
      addConstraints(v1: facebookButton, v2: facebookButtonHolder)
      facebookButtonHolder.layer.cornerRadius = 8
      facebookButtonHolder.clipsToBounds = true
    }
  }

  private let completion: (() -> Void)?

  private func addConstraints(v1: UIView, v2: UIView) {
    [NSLayoutAttribute.top, .bottom, .left, .right].forEach {
      NSLayoutConstraint(item: v1, attribute: $0, relatedBy: .equal, toItem: v2, attribute: $0, multiplier: 1, constant: 0).isActive = true
    }
  }

  @objc init(barButtonItem: UIBarButtonItem?, completion: (() -> Void)? = nil) {
    self.completion = completion
    transitioningManager = AuthorizationTransitioningManager(barButtonItem: barButtonItem)
    super.init(nibName: toString(type(of: self)), bundle: nil)
    transitioningDelegate = transitioningManager
    modalPresentationStyle = .custom
  }

  @objc init(popoverSourceView: UIView? = nil, permittedArrowDirections: UIPopoverArrowDirection = .unknown, completion: (() -> Void)? = nil) {
    self.completion = completion
    transitioningManager = AuthorizationTransitioningManager(popoverSourceView: popoverSourceView, permittedArrowDirections: permittedArrowDirections)
    super.init(nibName: toString(type(of: self)), bundle: nil)
    transitioningDelegate = transitioningManager
    modalPresentationStyle = .custom
  }

  required init?(coder _: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let fbImage = facebookButton.subviews.first(where: { $0 is UIImageView && $0.frame != facebookButton.frame })
    fbImage?.frame = CGRect(x: 16, y: 8, width: 24, height: 24)
  }

  @IBAction func onCancel() {
    dismiss(animated: true, completion: {
      (UIApplication.shared.keyWindow?.rootViewController as?
              UINavigationController)?.popToRootViewController(animated: true)
    })
  }

  private func process(error: Error) {
    textLabel.text = L("profile_authorization_error")
    Crashlytics.sharedInstance().recordError(error)
  }

  private func process(token: String, type: MWMSocialTokenType) {
    ViewModel.authenticate(withToken: token, type: type)
    dismiss(animated: true, completion: completion)
  }
}

extension AuthorizationViewController: FBSDKLoginButtonDelegate {
  func loginButton(_: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if let error = error {
      process(error: error)
    } else if let result = result {
      process(token: result.token.tokenString, type: .facebook)
    }
  }

  func loginButtonDidLogOut(_: FBSDKLoginButton!) {}
{'fraction_non_alphanumeric': 0.09820347194186516, 'fraction_numerical': 0.005853855470327008, 'mean_word_length': 4.071136131013306, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6646264', 'n_tokens_mistral': 3065, 'n_tokens_neox': 2888, 'n_words': 628}