CustomDialogRendererHandler.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*****************************************************************************
  2. * CustomDialogRendererHandler.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <caro # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. @objc(VLCCustomDialogRendererHandlerCompletionType)
  13. enum CustomDialogRendererHandlerCompletionType: Int {
  14. case cancel
  15. case error
  16. case complete
  17. }
  18. typealias CustomDialogRendererHandlerClosure = (_ status: CustomDialogRendererHandlerCompletionType) -> Void
  19. @objc(VLCCustomDialogRendererHandler)
  20. class CustomDialogRendererHandler: NSObject {
  21. private var dialogProvider: VLCDialogProvider
  22. @objc var completionHandler: CustomDialogRendererHandlerClosure?
  23. @objc init(dialogProvider: VLCDialogProvider) {
  24. self.dialogProvider = dialogProvider
  25. super.init()
  26. }
  27. }
  28. // MARK: - VLCCustomDialogRendererProtocol
  29. extension CustomDialogRendererHandler: VLCCustomDialogRendererProtocol {
  30. func showError(withTitle error: String, message: String) {
  31. completionHandler?(.error)
  32. }
  33. func showProgress(withTitle title: String, message: String,
  34. isIndeterminate: Bool, position: Float, cancel cancelString: String?,
  35. withReference reference: NSValue) {
  36. // noop
  37. }
  38. func updateProgress(withReference reference: NSValue, message: String?, position: Float) {
  39. // noop
  40. }
  41. func showLogin(withTitle title: String, message: String,
  42. defaultUsername username: String?, askingForStorage: Bool, withReference reference: NSValue) {
  43. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  44. var usernameField: UITextField?
  45. var passwordField: UITextField?
  46. alertController.addTextField {
  47. textField in
  48. usernameField = textField
  49. textField.placeholder = NSLocalizedString("USER_LABEL", comment:"")
  50. if username != "" {
  51. textField.text = username
  52. }
  53. }
  54. alertController.addTextField {
  55. textField in
  56. passwordField = textField
  57. textField.isSecureTextEntry = true
  58. textField.placeholder = NSLocalizedString("PASSWORD_LABEL", comment:"")
  59. }
  60. let loginAction = UIAlertAction(title: NSLocalizedString("LOGIN", comment:""),
  61. style: .default) {
  62. [weak self] action in
  63. let username = usernameField?.text ?? ""
  64. let password = passwordField?.text ?? ""
  65. self?.dialogProvider.postUsername(username,
  66. andPassword: password,
  67. forDialogReference: reference,
  68. store: false)
  69. }
  70. alertController.addAction(loginAction)
  71. alertController.preferredAction = loginAction
  72. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment:""),
  73. style: .cancel, handler: {
  74. [weak self] action in
  75. self?.dialogProvider.dismissDialog(withReference: reference)
  76. self?.completionHandler?(.cancel)
  77. }))
  78. if askingForStorage {
  79. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_SAVE", comment:""), style: .default,
  80. handler: {
  81. [weak self] action in
  82. let username = usernameField?.text ?? ""
  83. let password = passwordField?.text ?? ""
  84. self?.dialogProvider.postUsername(username,
  85. andPassword: password,
  86. forDialogReference: reference,
  87. store: true)
  88. }))
  89. }
  90. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  91. let presentingController = rootViewController.presentedViewController ?? rootViewController
  92. presentingController.present(alertController, animated: true, completion: nil)
  93. }
  94. }
  95. func showQuestion(withTitle title: String, message: String,
  96. type questionType: VLCDialogQuestionType, cancel cancelString: String?,
  97. action1String: String?, action2String: String?, withReference reference: NSValue) {
  98. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  99. if let cancelTitle = cancelString {
  100. alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel,
  101. handler: {
  102. [weak self] action in
  103. self?.dialogProvider.postAction(3, forDialogReference: reference)
  104. }))
  105. }
  106. if let action1Ttile = action1String {
  107. let confirmAction = UIAlertAction(title: action1Ttile, style: .default, handler: {
  108. [weak self] action in
  109. self?.dialogProvider.postAction(1, forDialogReference: reference)
  110. })
  111. alertController.addAction(confirmAction)
  112. alertController.preferredAction = confirmAction
  113. }
  114. if let action2Title = action2String {
  115. alertController.addAction(UIAlertAction(title: action2Title, style: .default,
  116. handler: {
  117. [weak self] action in
  118. self?.dialogProvider.postAction(2, forDialogReference: reference)
  119. }))
  120. }
  121. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  122. let presentingController = rootViewController.presentedViewController ?? rootViewController
  123. presentingController.present(alertController, animated: true, completion: nil)
  124. }
  125. }
  126. func cancelDialog(withReference reference: NSValue) {
  127. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  128. let presentingController = rootViewController.presentedViewController ?? rootViewController
  129. presentingController.dismiss(animated: true, completion: nil)
  130. }
  131. }
  132. }