<commit_before>//
//  Alert.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 9/10/15.
//  Copyright © 2015 Drewag LLC. All rights reserved.
//

#if os(iOS)

import UIKit
import ObjectiveC

public class ErrorOccured: EventType { public typealias CallbackParam = UserReportableError }

class Alert: NSObject {
    private let onButtonClicked: (_ buttonTitle: String?, _ textFieldText: String?) -> ()

    private init(onButtonClicked: @escaping (_ buttonTitle: String?, _ textFieldText: String?) -> ()) {
        self.onButtonClicked = onButtonClicked

        super.init()
    }
}

public final class AlertAction {
    let name: String
    let handler: (() -> ())?

    init(name: String, handler: (() -> ())?) {
        self.name = name
        self.handler = handler
    }

    public static func action(_ name: String, handler: (() -> ())? = nil) -> AlertAction {
        return AlertAction(name: name, handler: handler)
    }
}

public final class TextAction {
    let name: String
    let handler: ((_ text: String) -> ())?

    init(name: String, handler: ((_ text: String) -> ())?) {
        self.name = name
        self.handler = handler
    }

    public static func action(_ name: String, handler: ((_ text: String) -> ())? = nil) -> TextAction {
        return TextAction(name: name, handler: handler)
    }
}

extension UIViewController {
    public func showAlert(withError error: UserReportableError) {
        EventCenter.defaultCenter().triggerEvent(ErrorOccured.self, params: error)
        self.showAlert(
            withTitle: error.alertTitle,
            message: error.alertMessage
        )
    }

    public func showAlert(
        withTitle title: String,
        message: String,
        cancel: AlertAction? = nil,
        other: [AlertAction] = []
        )
    {
        func onTapped(buttonTitle: String?, textFieldText: String?) {
            if let action = cancel, action.name == buttonTitle {
                action.handler?()
                return
            }
            for action in other {
                if action.name == buttonTitle {
                    action.handler?()
                    return
                }
            }
        }

        var other = other
        if cancel == nil && other.isEmpty {
            other.append(.action("OK"))
        }
        Alert.showAlertController(
            title: title,
            message: message,
            cancelButtonTitle: cancel?.name,
            otherButtonTitles: other.map({$0.name}),
            textFieldPlaceholder: nil,
            textFieldDefault: nil,
            onButtonClicked: onTapped,
            fromViewController: self
        )
    }

    public func showTextInput(
        withTitle title: String,
        message: String,
        textFieldPlaceholder: String? = nil,
        textFieldDefault: String? = nil,
        keyboardType: UIKeyboardType = .default,
        cancel: TextAction? = nil,
        other: [TextAction] = []
        )
    {
        func onTapped(buttonTitle: String?, textFieldText: String?) {
            if let action = cancel, action.name == buttonTitle {
                action.handler?(textFieldText ?? "")
                return
            }
            for action in other {
                if action.name == buttonTitle {
                    action.handler?(textFieldText ?? "")
                    return
                }
            }
        }

        var other = other
        if cancel == nil && other.isEmpty {
            other.append(.action("OK"))
        }
        Alert.showAlertController(
            title: title,
            message: message,
            cancelButtonTitle: cancel?.name,
            otherButtonTitles: other.map({$0.name}),
            textFieldPlaceholder: textFieldPlaceholder ?? "",
            textFieldDefault: textFieldDefault,
            keyboardType: keyboardType,
            onButtonClicked: onTapped,
            fromViewController: self
        )
    }
}

private extension Alert {
    struct Keys {
        static var Delegate = "Delegate"
    }

    class func showAlertController(
        title: String,
        message: String,
        cancelButtonTitle: String?,
        otherButtonTitles: [String]?,
        textFieldPlaceholder: String?,
        textFieldDefault: String? = nil,
        keyboardType: UIKeyboardType = .default,
        onButtonClicked: ((_ buttonTitle: String?, _ textFieldText: String?) -> ())?,
        fromViewController: UIViewController
        )
    {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

        var promptTextField: UITextField?
        if let placeholder = textFieldPlaceholder {
            alert.addTextField { textField in
                textField.placeholder = placeholder
                textField.text = textFieldDefault
                textField.keyboardType = keyboardType
                promptTextField = textField
            }
        }

        func handler(action: UIAlertAction!) {
            if let block = onButtonClicked {
                block(action.title, promptTextField?.text)
            }
        }


        if let cancelButtonTitle = cancelButtonTitle {
            alert.addAction(UIAlertAction(
                title: cancelButtonTitle,
                style: .cancel,
                handler: handler
                ))
        }
        for otherTitle in otherButtonTitles ?? [] {
            alert.addAction(UIAlertAction(
                title: otherTitle,
                style: .default,
                handler: handler
                ))
        }

        fromViewController.present(alert, animated: true, completion: nil)
    }
}

#endif<commit_msg>[NF] Allow specifying descructive and prefered actions in alerts<commit_after>//
//  Alert.swift
//  SwiftPlusPlus
//
//  Created by <NAME> on 9/10/15.
//  Copyright © 2015 Drewag LLC. All rights reserved.
//

#if os(iOS)

import UIKit
import ObjectiveC

public class ErrorOccured: EventType { public typealias CallbackParam = UserReportableError }

class Alert: NSObject {
    private let onButtonClicked: (_ buttonTitle: String?, _ textFieldText: String?) -> ()

    private init(onButtonClicked: @escaping (_ buttonTitle: String?, _ textFieldText: String?) -> ()) {
        self.onButtonClicked = onButtonClicked

        super.init()
    }
}

protocol AnyAlertAction {
    var name: String {get}
    var isDestructive: Bool {get}
}

public final class AlertAction: AnyAlertAction {
    let name: String
    let isDestructive: Bool
    let handler: (() -> ())?

    init(name: String, isDestructive: Bool = false, handler: (() -> ())?) {
        self.name = name
        self.isDestructive = isDestructive
        self.handler = handler
    }

    public static func action(_ name: String, isDestructive: Bool = false, handler: (() -> ())? = nil) -> AlertAction {
        return AlertAction(name: name, isDestructive: isDestructive, handler: handler)
    }
}

public final class TextAction: AnyAlertAction {
    let name: String
    let isDestructive: Bool
    let handler: ((_ text: String) -> ())?

    init(name: String, isDestructive: Bool = false, handler: ((_ text: String) -> ())?) {
        self.name = name
        self.isDestructive = isDestructive
        self.handler = handler
    }

    public static func action(_ name: String, isDestructive: Bool = false, handler: ((_ text: String) -> ())? = nil) -> TextAction {
        return TextAction(name: name, isDestructive: isDestructive, handler: handler)
    }
}

extension UIViewController {
    public func showAlert(withError error: UserReportableError) {
        EventCenter.defaultCenter().triggerEvent(ErrorOccured.self, params: error)
        self.showAlert(
            withTitle: error.alertTitle,
            message: error.alertMessage
        )
    }

    public func showAlert(
        withTitle title: String,
        message: String,
        cancel: AlertAction? = nil,
        preferred: AlertAction? = nil,
        other: [AlertAction] = []
        )
    {
        var other = other
        if cancel == nil && other.isEmpty {
            other.append(.action("OK"))
        }

        let alert = Alert.buildAlert(
            withTitle: title,
            message: message,
            cancel: cancel,
            preferred: preferred,
            other: other,
            onTapped: { tappedAction in
                if let action = cancel, action.name == tappedAction.title {
                    action.handler?()
                    return
                }
                if let preferred = preferred, preferred.name == tappedAction.title {
                    preferred.handler?()
                    return
                }
                for action in other {
                    if action.name == tappedAction.title {
                        action.handler?()
                        return
                    }
                }
            }
        )
        self.present(alert, animated: true, completion: nil)
    }

    public func showTextInput(
        withTitle title: String,
        message: String,
        textFieldPlaceholder: String = "",
        textFieldDefault: String? = nil,
        keyboardType: UIKeyboardType = .default,
        cancel: TextAction? = nil,
        preferred: TextAction? = nil,
        other: [TextAction] = []
        )
    {
        var other = other
        if cancel == nil && other.isEmpty {
            other.append(.action("OK"))
        }

        var promptTextField: UITextField?
        let alert = Alert.buildAlert(
            withTitle: title,
            message: message,
            cancel: cancel,
            preferred: preferred,
            other: other,
            onTapped: { tappedAction in
                if let action = cancel, action.name == tappedAction.title {
                    action.handler?(promptTextField?.text ?? "")
                    return
                }
                if let preferred = preferred, preferred.name == tappedAction.title {
                    preferred.handler?(promptTextField?.text ?? "")
                    return
                }
                for action in other {
                    if action.name == tappedAction.title {
                        action.handler?(promptTextField?.text ?? "")
                        return
                    }
                }
            }
        )

        alert.addTextField { textField in
            textField.placeholder = textFieldPlaceholder
            textField.text = textFieldDefault
            textField.keyboardType = keyboardType
            promptTextField = textField
        }
        self.present(alert, animated: true, completion: nil)
    }
}

private extension Alert {
    struct Keys {
        static var Delegate = "Delegate"
    }

    class func buildAlert(
        withTitle title: String,
        message: String,
        cancel: AnyAlertAction? = nil,
        preferred: AnyAlertAction? = nil,
        other: [AnyAlertAction] = [],
        onTapped: @escaping (UIAlertAction) -> ()
        ) -> UIAlertController
    {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

        if let cancel = cancel {
            alert.addAction(UIAlertAction(
                title: cancel.name,
                style: .cancel,
                handler: onTapped
            ))
        }

        if let preferred = preferred {
            let action = UIAlertAction(
                title: preferred.name,
                style: preferred.isDestructive ? .destructive : .default,
                handler: onTapped
            )
            alert.addAction(action)
            alert.preferredAction = action
        }

        for action in other {
            alert.addAction(UIAlertAction(
                title: action.name,
                style: action.isDestructive ? .destructive : .default,
                handler: onTapped
            ))
        }

        return alert
    }
}

#endif
{'fraction_non_alphanumeric': 0.09501167834501167, 'fraction_numerical': 0.001668335001668335, 'mean_word_length': 1.7167459777928846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '1733374', 'n_tokens_mistral': 3179, 'n_tokens_neox': 3009, 'n_words': 780}