VLCAlertViewController.swift 3.64 KB
Newer Older
1
/*****************************************************************************
2
 * VLCAlertViewController.swift
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2018 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Quentin Richard <Quentinr75@gmail.com>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

import UIKit

typealias AlertAction = (UIAlertAction) -> Void

17 18
@objcMembers class VLCAlertButton: NSObject {
    let title: String
19 20 21
    let style: UIAlertActionStyle
    let action: AlertAction?
    init(title: String, style: UIAlertActionStyle = .default, action: AlertAction? = nil) {
22
        self.title = title
23
        self.style = style
24
        self.action = action
25
    }
26 27 28

    // For Objc
    convenience init(title: String, action: AlertAction? = nil) {
29
        self.init(title: title, style: .default, action: action)
30
    }
31 32
}

33
@objcMembers class VLCAlertViewController: UIAlertController {
34

35 36 37
    static func alertViewManager(title: String,
                                 errorMessage: String? = nil,
                                 viewController: UIViewController) {
38 39 40 41
        VLCAlertViewController.alertViewManager(title: title,
                                                errorMessage: errorMessage,
                                                viewController: viewController,
                                                buttonsAction: nil)
42 43
    }

44 45 46 47
    static func alertViewManager(title: String,
                                 errorMessage: String? = nil,
                                 viewController: UIViewController,
                                 buttonsAction: [VLCAlertButton]?) {
48
        let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
49 50
        if let buttonsAction = buttonsAction {
            for buttonAction in buttonsAction {
51 52 53
                let action = UIAlertAction(title: buttonAction.title,
                                           style: buttonAction.style,
                                           handler: buttonAction.action)
54 55 56
                alert.addAction(action)
            }
        } else {
57 58 59
            let action = UIAlertAction(title: NSLocalizedString("BUTTON_OK", comment:""),
                                       style: .default,
                                       handler: nil)
60 61
            alert.addAction(action)
        }
62
        alert.show(viewController, sender: Any?.self)
63 64 65
        viewController.present(alert, animated: true, completion: nil)
    }

66 67 68 69 70
    static func alertManagerWithTextField(title: String, description: String? = nil,
                                          viewController: UIViewController,
                                          buttonsAction: [VLCAlertButton],
                                          textFieldText: String? = nil,
                                          textFieldPlaceholder: String? = nil) {
71
        let alert = UIAlertController(title: title, message: description, preferredStyle: .alert)
72 73 74 75 76
        alert.addTextField(configurationHandler: { textField in
            textField.placeholder = textFieldPlaceholder
            textField.text = textFieldText
        })
        for buttonAction in buttonsAction {
77 78 79
            let action = UIAlertAction(title: buttonAction.title,
                                       style: buttonAction.style,
                                       handler: buttonAction.action)
80 81
            alert.addAction(action)
        }
82
        alert.show(viewController, sender: Any?.self)
83 84 85
        viewController.present(alert, animated: true, completion: nil)
    }
}