CustomDialogRendererHandler.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 stop
  16. case error
  17. case complete
  18. }
  19. typealias CustomDialogRendererHandlerClosure = (_ status: CustomDialogRendererHandlerCompletionType) -> Void
  20. @objc(VLCCustomDialogRendererHandler)
  21. class CustomDialogRendererHandler: NSObject {
  22. private var dialogProvider: VLCDialogProvider
  23. private var selectedSMBv1 = false
  24. @objc var completionHandler: CustomDialogRendererHandlerClosure?
  25. @objc init(dialogProvider: VLCDialogProvider) {
  26. self.dialogProvider = dialogProvider
  27. super.init()
  28. }
  29. }
  30. // MARK: - Private helpers
  31. private extension CustomDialogRendererHandler {
  32. private func handleSMBv1(completionHandler: @escaping (Bool) -> Void) {
  33. let alertController = UIAlertController(title: NSLocalizedString("SMBV1_WARN_TITLE", comment: ""),
  34. message: NSLocalizedString("SMBV1_WARN_DESCRIPTION", comment: ""),
  35. preferredStyle: .alert)
  36. alertController.addAction(UIAlertAction(title: NSLocalizedString("SMBV1_CONTINUE", comment:""),
  37. style: .destructive, handler: {
  38. action in
  39. completionHandler(true)
  40. }))
  41. alertController.addAction(UIAlertAction(title: NSLocalizedString("SMBV1_NEXT_PROTOCOL", comment:""),
  42. style: .default, handler: {
  43. action in
  44. completionHandler(false)
  45. }))
  46. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  47. let presentingController = rootViewController.presentedViewController ?? rootViewController
  48. presentingController.present(alertController, animated: true, completion: nil)
  49. }
  50. }
  51. private func handleLoginAlert(with title: String, message: String,
  52. username: String?, askingForStorage: Bool,
  53. withReference reference: NSValue) {
  54. let alertController = UIAlertController(title: title, message: message,
  55. preferredStyle: .alert)
  56. var usernameField: UITextField?
  57. var passwordField: UITextField?
  58. alertController.addTextField {
  59. textField in
  60. usernameField = textField
  61. textField.placeholder = NSLocalizedString("USER_LABEL", comment:"")
  62. if username != "" {
  63. textField.text = username
  64. }
  65. }
  66. alertController.addTextField {
  67. textField in
  68. passwordField = textField
  69. textField.isSecureTextEntry = true
  70. textField.placeholder = NSLocalizedString("PASSWORD_LABEL", comment:"")
  71. }
  72. let loginAction = UIAlertAction(title: NSLocalizedString("LOGIN", comment:""),
  73. style: .default) {
  74. [weak self] action in
  75. let username = usernameField?.text ?? ""
  76. let password = passwordField?.text ?? ""
  77. self?.dialogProvider.postUsername(username,
  78. andPassword: password,
  79. forDialogReference: reference,
  80. store: false)
  81. }
  82. alertController.addAction(loginAction)
  83. alertController.preferredAction = loginAction
  84. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment:""),
  85. style: .cancel, handler: {
  86. [weak self] action in
  87. if self?.selectedSMBv1 ?? true {
  88. self?.completionHandler?(.stop)
  89. } else {
  90. self?.completionHandler?(.cancel)
  91. self?.dialogProvider.dismissDialog(withReference: reference)
  92. }
  93. }))
  94. if askingForStorage {
  95. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_SAVE", comment:""), style: .default,
  96. handler: {
  97. [weak self] action in
  98. let username = usernameField?.text ?? ""
  99. let password = passwordField?.text ?? ""
  100. self?.dialogProvider.postUsername(username,
  101. andPassword: password,
  102. forDialogReference: reference,
  103. store: true)
  104. }))
  105. }
  106. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  107. let presentingController = rootViewController.presentedViewController ?? rootViewController
  108. presentingController.present(alertController, animated: true, completion: nil)
  109. }
  110. }
  111. }
  112. // MARK: - VLCCustomDialogRendererProtocol
  113. extension CustomDialogRendererHandler: VLCCustomDialogRendererProtocol {
  114. func showError(withTitle error: String, message: String) {
  115. completionHandler?(.error)
  116. }
  117. func showProgress(withTitle title: String, message: String,
  118. isIndeterminate: Bool, position: Float, cancel cancelString: String?,
  119. withReference reference: NSValue) {
  120. // noop
  121. }
  122. func updateProgress(withReference reference: NSValue, message: String?, position: Float) {
  123. // noop
  124. }
  125. func showLogin(withTitle title: String, message: String,
  126. defaultUsername username: String?, askingForStorage: Bool,
  127. withReference reference: NSValue) {
  128. if !title.contains("SMBv1") || selectedSMBv1 {
  129. handleLoginAlert(with: title, message: message,
  130. username: username,
  131. askingForStorage: askingForStorage,
  132. withReference: reference)
  133. return
  134. }
  135. handleSMBv1() {
  136. [weak self] isSMBv1 in
  137. if isSMBv1 {
  138. self?.selectedSMBv1 = true
  139. self?.handleLoginAlert(with: title, message: message,
  140. username: username,
  141. askingForStorage: askingForStorage,
  142. withReference: reference)
  143. } else {
  144. self?.dialogProvider.dismissDialog(withReference: reference)
  145. }
  146. }
  147. }
  148. func showQuestion(withTitle title: String, message: String,
  149. type questionType: VLCDialogQuestionType, cancel cancelString: String?,
  150. action1String: String?, action2String: String?, withReference reference: NSValue) {
  151. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  152. if let cancelTitle = cancelString {
  153. alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel,
  154. handler: {
  155. [weak self] action in
  156. self?.dialogProvider.postAction(3, forDialogReference: reference)
  157. }))
  158. }
  159. if let action1Ttile = action1String {
  160. let confirmAction = UIAlertAction(title: action1Ttile, style: .default, handler: {
  161. [weak self] action in
  162. self?.dialogProvider.postAction(1, forDialogReference: reference)
  163. })
  164. alertController.addAction(confirmAction)
  165. alertController.preferredAction = confirmAction
  166. }
  167. if let action2Title = action2String {
  168. alertController.addAction(UIAlertAction(title: action2Title, style: .default,
  169. handler: {
  170. [weak self] action in
  171. self?.dialogProvider.postAction(2, forDialogReference: reference)
  172. }))
  173. }
  174. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  175. let presentingController = rootViewController.presentedViewController ?? rootViewController
  176. presentingController.present(alertController, animated: true, completion: nil)
  177. }
  178. }
  179. func cancelDialog(withReference reference: NSValue) {
  180. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  181. let presentingController = rootViewController.presentedViewController ?? rootViewController
  182. presentingController.dismiss(animated: true, completion: nil)
  183. }
  184. }
  185. }