VLCAlertViewController.swift 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*****************************************************************************
  2. * VLCAlertViewController.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Quentin Richard <Quentinr75@gmail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import UIKit
  13. typealias AlertAction = (UIAlertAction) -> Void
  14. @objcMembers class VLCAlertButton: NSObject {
  15. let title: String
  16. let style: UIAlertAction.Style
  17. let action: AlertAction?
  18. init(title: String, style: UIAlertAction.Style = .default, action: AlertAction? = nil) {
  19. self.title = title
  20. self.style = style
  21. self.action = action
  22. }
  23. // For Objc
  24. convenience init(title: String, action: AlertAction? = nil) {
  25. self.init(title: title, style: .default, action: action)
  26. }
  27. }
  28. @objcMembers class VLCAlertViewController: UIAlertController {
  29. static func alertViewManager(title: String,
  30. errorMessage: String? = nil,
  31. viewController: UIViewController) {
  32. VLCAlertViewController.alertViewManager(title: title,
  33. errorMessage: errorMessage,
  34. viewController: viewController,
  35. buttonsAction: nil)
  36. }
  37. static func alertViewManager(title: String,
  38. errorMessage: String? = nil,
  39. viewController: UIViewController,
  40. buttonsAction: [VLCAlertButton]?) {
  41. let alert = UIAlertController(title: title, message: errorMessage, preferredStyle: .alert)
  42. if let buttonsAction = buttonsAction {
  43. for buttonAction in buttonsAction {
  44. let action = UIAlertAction(title: buttonAction.title,
  45. style: buttonAction.style,
  46. handler: buttonAction.action)
  47. alert.addAction(action)
  48. }
  49. } else {
  50. let action = UIAlertAction(title: NSLocalizedString("BUTTON_OK", comment:""),
  51. style: .default,
  52. handler: nil)
  53. alert.addAction(action)
  54. }
  55. alert.show(viewController, sender: Any?.self)
  56. viewController.present(alert, animated: true, completion: nil)
  57. }
  58. static func alertManagerWithTextField(title: String, description: String? = nil,
  59. viewController: UIViewController,
  60. buttonsAction: [VLCAlertButton],
  61. textFieldText: String? = nil,
  62. textFieldPlaceholder: String? = nil) {
  63. let alert = UIAlertController(title: title, message: description, preferredStyle: .alert)
  64. alert.addTextField(configurationHandler: { textField in
  65. textField.placeholder = textFieldPlaceholder
  66. textField.text = textFieldText
  67. })
  68. for buttonAction in buttonsAction {
  69. let action = UIAlertAction(title: buttonAction.title,
  70. style: buttonAction.style,
  71. handler: buttonAction.action)
  72. alert.addAction(action)
  73. }
  74. alert.show(viewController, sender: Any?.self)
  75. viewController.present(alert, animated: true, completion: nil)
  76. }
  77. }