<commit_before>//
//  SwAlert.swift
//  SwAlert
//
//  Created by xxxAIRINxxx on 2015/03/18.
//  Copyright (c) 2015 xxxAIRINxxx inc. All rights reserved.
//

import UIKit

public typealias CompletionHandler = (SwAlertResult -> Void)

public enum AlertButtonType {
    case Cancel(title: String)
    case Other(title: String)
    case TextField(text: String, placeholder: String?)
}

extension AlertButtonType: Equatable { }

public func ==(lhs: AlertButtonType, rhs: AlertButtonType) -> Bool {
    switch (lhs, rhs) {
    case (.Cancel(let buttonTitleL), .Cancel(let buttonTitleR)):
        return buttonTitleL == buttonTitleR
    case (.Other(let buttonTitleL), .Other(let buttonTitleR)):
        return buttonTitleL == buttonTitleR
    case (.TextField(_, _), .TextField(_, _)):
        return true
    default:
        return false
    }
}

public enum SwAlertResult {
    case Cancel
    case Other(inputText: [String])
}

private final class AlertManager {
    
    static let sharedInstance = AlertManager()
    
    let window : UIWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
    
    var alertQueue : [SwAlert] = []
    var showingAlertView : SwAlert?
    
    lazy var parentController : UIViewController = {
        var parentController = UIViewController()
        parentController.view.backgroundColor = UIColor.clearColor()
        
        self.window.windowLevel = UIWindowLevelAlert
        self.window.rootViewController = parentController
        
        return parentController
    }()
}

private struct AlertInfo {
    
    let type : AlertButtonType
    let completion : CompletionHandler?
    
    init(type: AlertButtonType, completion: CompletionHandler?) {
        self.type = type
        self.completion = completion
    }
}

public final class SwAlert: NSObject, UIAlertViewDelegate {
    
    private let title : String
    private let message : String
    
    private var alertInfo : [AlertInfo] = []
    private var cancelInfo : AlertInfo?
    
    // MARK: - Static Functions
    
    public static func showNoActionAlert(title: String, message: String, buttonTitle: String) {
        let alert = SwAlert(title: title, message: message)
        alert.cancelInfo = AlertInfo(type: .Cancel(title: buttonTitle), completion: nil)
        alert.show()
    }
    
    public static func showOneActionAlert(title: String, message: String, buttonTitle: String, completion: CompletionHandler?) {
        let alert = SwAlert(title: title, message: message)
        alert.cancelInfo = AlertInfo(type: .Other(title: buttonTitle), completion: completion)
        alert.show()
    }
    
    // MARK: - Initializer
    
    public init(title: String?, message: String?) {
        self.title = title ?? ""
        self.message = message ?? ""
    }
    
    // MARK: - Instance Functions
    
    public func setCancelAction(buttonTitle: String, completion: CompletionHandler?) {
        self.cancelInfo = AlertInfo(type: .Cancel(title: buttonTitle), completion: completion)
    }
    
    public func addAction(buttonTitle: String, completion: CompletionHandler?) {
        let alertInfo = AlertInfo(type: .Other(title: buttonTitle), completion: completion)
        self.alertInfo.append(alertInfo)
    }
    
    public func addTextField(text: String, placeholder: String?) {
        let alertInfo = AlertInfo(type: .TextField(text: text, placeholder: placeholder), completion: nil)
        self.alertInfo.append(alertInfo)
    }
    
    public func show() {
        if #available(iOS 8.0, *) {
            self.showAlertController()
        } else {
            self.showAlertView()
        }
    }<commit_msg>Apply method chain<commit_after>//
//  SwAlert.swift
//  SwAlert
//
//  Created by xxxAIRINxxx on 2015/03/18.
//  Copyright (c) 2015 xxxAIRINxxx inc. All rights reserved.
//

import UIKit

public typealias CompletionHandler = (SwAlertResult -> Void)

public enum AlertButtonType {
    case Cancel(title: String)
    case Other(title: String)
    case TextField(text: String, placeholder: String?)
}

extension AlertButtonType: Equatable { }

public func ==(lhs: AlertButtonType, rhs: AlertButtonType) -> Bool {
    switch (lhs, rhs) {
    case (.Cancel(let buttonTitleL), .Cancel(let buttonTitleR)):
        return buttonTitleL == buttonTitleR
    case (.Other(let buttonTitleL), .Other(let buttonTitleR)):
        return buttonTitleL == buttonTitleR
    case (.TextField(_, _), .TextField(_, _)):
        return true
    default:
        return false
    }
}

public enum SwAlertResult {
    case Cancel
    case Other(inputText: [String])
}

private final class AlertManager {
    
    static let sharedInstance = AlertManager()
    
    let window : UIWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
    
    var alertQueue : [SwAlert] = []
    var showingAlertView : SwAlert?
    
    lazy var parentController : UIViewController = {
        var parentController = UIViewController()
        parentController.view.backgroundColor = UIColor.clearColor()
        
        self.window.windowLevel = UIWindowLevelAlert
        self.window.rootViewController = parentController
        
        return parentController
    }()
}

private struct AlertInfo {
    
    let type : AlertButtonType
    let completion : CompletionHandler?
    
    init(type: AlertButtonType, completion: CompletionHandler?) {
        self.type = type
        self.completion = completion
    }
}

public final class SwAlert: NSObject, UIAlertViewDelegate {
    
    private let title : String
    private let message : String
    
    private var alertInfo : [AlertInfo] = []
    private var cancelInfo : AlertInfo?
    
    // MARK: - Static Functions
    
    public static func showNoActionAlert(title: String, message: String, buttonTitle: String) {
        let alert = SwAlert(title: title, message: message)
        alert.cancelInfo = AlertInfo(type: .Cancel(title: buttonTitle), completion: nil)
        alert.show()
    }
    
    public static func showOneActionAlert(title: String, message: String, buttonTitle: String, completion: CompletionHandler?) {
        let alert = SwAlert(title: title, message: message)
        alert.cancelInfo = AlertInfo(type: .Other(title: buttonTitle), completion: completion)
        alert.show()
    }
    
    // MARK: - Initializer
    
    public init(title: String?, message: String?) {
        self.title = title ?? ""
        self.message = message ?? ""
    }
    
    // MARK: - Instance Functions
    
    public func setCancelAction(buttonTitle: String, _ completion: CompletionHandler? = nil) -> SwAlert {
        self.cancelInfo = AlertInfo(type: .Cancel(title: buttonTitle), completion: completion)
        return self
    }
    
    public func addAction(buttonTitle: String, _ completion: CompletionHandler? = nil) -> SwAlert {
        let alertInfo = AlertInfo(type: .Other(title: buttonTitle), completion: completion)
        self.alertInfo.append(alertInfo)
        return self
    }
    
    public func addTextField(text: String, placeholder: String? = nil) -> SwAlert {
        let alertInfo = AlertInfo(type: .TextField(text: text, placeholder: placeholder), completion: nil)
        self.alertInfo.append(alertInfo)
        return self
    }
    
    public func show() {
        if #available(iOS 8.0, *) {
            self.showAlertController()
        } else {
            self.showAlertView()
        }
    }
{'fraction_non_alphanumeric': 0.09816365109370781, 'fraction_numerical': 0.003780718336483932, 'mean_word_length': 2.771384928716904, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '1031449', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2017, 'n_words': 552}