<commit_before>//
//  AlertViewController.swift
//  BFWControls
//
//  Created by <NAME> on 2/12/2015.
//  Copyright © 2016 BareFeetWare.
//  Free to use at your own risk, with acknowledgement to BareFeetWare.
//

import UIKit

protocol AlertViewDelegate {
    
    func alertView(alertView: AlertView, clickedButtonAtIndex index: Int)
    
}

class AlertViewController: UIViewController {
    
    // MARK: - Variables
    
    @IBInspectable var action0Segue: String?
    @IBInspectable var action1Segue: String?
    @IBInspectable var action2Segue: String?
    @IBInspectable var action3Segue: String?
    @IBInspectable var action4Segue: String?
    @IBInspectable var action6Segue: String?
    @IBInspectable var action7Segue: String?
    @IBInspectable var autoDismisses: Bool = true
    
    var delegate: AlertViewDelegate?
    
    lazy var alertView: AlertView = {
        let foundAlertView: AlertView
        let alertViewOverlay = self.view.subviews.filter { subview in
            subview is AlertViewOverlay
        }.first as? AlertViewOverlay
        if let alertViewOverlay = alertViewOverlay {
            foundAlertView = alertViewOverlay.alertView
        } else {
            foundAlertView = self.view.subviews.filter { subview in
                subview is AlertView
                }.first as! AlertView
        }
        return foundAlertView
    }()
    
    // MARK: - Private variables
    
    private var isInNavigationController: Bool {
        return presentingViewController?.presentedViewController is UINavigationController
    }
    
    private var segueIdentifiers: [String?] {
        return [action0Segue, action1Segue, action2Segue, action3Segue, action4Segue, action6Segue, action7Segue]
    }
    
    private let translationTransitioningController = TranslationTransitioningController()
    
    private var overlayView: UIView? {
        let overlayView = view.subviews.filter { subview in
            var white: CGFloat = 0.0
            var alpha: CGFloat = 0.0
            subview.backgroundColor?.getWhite(&white, alpha: &alpha)
            return alpha < 1.0
            }.first
        return overlayView
    }

    // MARK: - Actions
    
    @IBAction func actionButton(button: UIButton) {
        let index = alertView.indexOfButton(button)!
        if alertView.hasCancel && index == 0 {
            dismissAlert(button)
        } else {
            if let delegate = delegate {
                delegate.alertView(alertView, clickedButtonAtIndex: index)
            } else {
                let identifer = segueIdentifiers[index]
                    ?? alertView.buttonTitleAtIndex(index)!
                performSegueWithIdentifier(identifer, sender: alertView)
            }
            if !isInNavigationController && autoDismisses {
                dismissAlert(button)
            }
        }
    }
    
    @IBAction func dismissAlert(sender: AnyObject?) {
        if presentingViewController != nil {
            dismissViewControllerAnimated(true, completion: nil)
        } else {
            navigationController?.popViewControllerAnimated(true)<commit_msg>Calling the delegate after the AlertViewController is dismissed.<commit_after>//
//  AlertViewController.swift
//  BFWControls
//
//  Created by <NAME> on 2/12/2015.
//  Copyright © 2016 BareFeetWare.
//  Free to use at your own risk, with acknowledgement to BareFeetWare.
//

import UIKit

protocol AlertViewDelegate {
    
    func alertView(alertView: AlertView, clickedButtonAtIndex index: Int)
    
}

class AlertViewController: UIViewController {
    
    // MARK: - Variables
    
    @IBInspectable var action0Segue: String?
    @IBInspectable var action1Segue: String?
    @IBInspectable var action2Segue: String?
    @IBInspectable var action3Segue: String?
    @IBInspectable var action4Segue: String?
    @IBInspectable var action6Segue: String?
    @IBInspectable var action7Segue: String?
    @IBInspectable var autoDismisses: Bool = true
    
    var delegate: AlertViewDelegate?
    
    lazy var alertView: AlertView = {
        let foundAlertView: AlertView
        let alertViewOverlay = self.view.subviews.filter { subview in
            subview is AlertViewOverlay
        }.first as? AlertViewOverlay
        if let alertViewOverlay = alertViewOverlay {
            foundAlertView = alertViewOverlay.alertView
        } else {
            foundAlertView = self.view.subviews.filter { subview in
                subview is AlertView
                }.first as! AlertView
        }
        return foundAlertView
    }()
    
    // MARK: - Private variables
    
    private var isInNavigationController: Bool {
        return presentingViewController?.presentedViewController is UINavigationController
    }
    
    private var segueIdentifiers: [String?] {
        return [action0Segue, action1Segue, action2Segue, action3Segue, action4Segue, action6Segue, action7Segue]
    }
    
    private let translationTransitioningController = TranslationTransitioningController()
    
    private var overlayView: UIView? {
        let overlayView = view.subviews.filter { subview in
            var white: CGFloat = 0.0
            var alpha: CGFloat = 0.0
            subview.backgroundColor?.getWhite(&white, alpha: &alpha)
            return alpha < 1.0
            }.first
        return overlayView
    }
    
    private var onCompletion: (() -> Void)?
    
    // MARK: - Actions
    
    @IBAction func actionButton(button: UIButton) {
        let index = alertView.indexOfButton(button)!
        if alertView.hasCancel && index == 0 {
            dismissAlert(button)
        } else {
            onCompletion = { [weak self] _ in
                guard let strongSelf = self else { return }
                if let delegate = strongSelf.delegate {
                    delegate.alertView(strongSelf.alertView, clickedButtonAtIndex: index)
                } else {
                    let identifer = strongSelf.segueIdentifiers[index]
                        ?? strongSelf.alertView.buttonTitleAtIndex(index)!
                    strongSelf.performSegueWithIdentifier(identifer, sender: strongSelf.alertView)
                }
            }
            if !isInNavigationController && autoDismisses {
                dismissAlert(button)
            } else {
                onCompletion?()
            }
        }
    }
    
    @IBAction func dismissAlert(sender: AnyObject?) {
        if presentingViewController != nil {
            dismissViewControllerAnimated(true, completion: onCompletion)
        } else {
            onCompletion?()
            navigationController?.popViewControllerAnimated(true)
{'fraction_non_alphanumeric': 0.07256610576923077, 'fraction_numerical': 0.009615384615384616, 'mean_word_length': 2.233122875182127, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, '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': '7486281', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1749, 'n_words': 443}