CustomDialogRendererHandler.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if #available(iOS 11.0, *) {
  62. textField.textContentType = .username
  63. }
  64. textField.placeholder = NSLocalizedString("USER_LABEL", comment:"")
  65. if username != "" {
  66. textField.text = username
  67. }
  68. }
  69. alertController.addTextField {
  70. textField in
  71. passwordField = textField
  72. if #available(iOS 11.0, *) {
  73. textField.textContentType = .password
  74. }
  75. textField.isSecureTextEntry = true
  76. textField.placeholder = NSLocalizedString("PASSWORD_LABEL", comment:"")
  77. }
  78. let loginAction = UIAlertAction(title: NSLocalizedString("LOGIN", comment:""),
  79. style: .default) {
  80. [weak self] action in
  81. let username = usernameField?.text ?? ""
  82. let password = passwordField?.text ?? ""
  83. self?.dialogProvider.postUsername(username,
  84. andPassword: password,
  85. forDialogReference: reference,
  86. store: false)
  87. }
  88. alertController.addAction(loginAction)
  89. alertController.preferredAction = loginAction
  90. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment:""),
  91. style: .cancel, handler: {
  92. [weak self] action in
  93. if self?.selectedSMBv1 ?? true {
  94. self?.completionHandler?(.stop)
  95. } else {
  96. self?.completionHandler?(.cancel)
  97. self?.dialogProvider.dismissDialog(withReference: reference)
  98. }
  99. }))
  100. if askingForStorage {
  101. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_SAVE", comment:""), style: .default,
  102. handler: {
  103. [weak self] action in
  104. let username = usernameField?.text ?? ""
  105. let password = passwordField?.text ?? ""
  106. self?.dialogProvider.postUsername(username,
  107. andPassword: password,
  108. forDialogReference: reference,
  109. store: true)
  110. }))
  111. }
  112. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  113. let presentingController = rootViewController.presentedViewController ?? rootViewController
  114. presentingController.present(alertController, animated: true, completion: nil)
  115. }
  116. }
  117. }
  118. // MARK: - VLCCustomDialogRendererProtocol
  119. extension CustomDialogRendererHandler: VLCCustomDialogRendererProtocol {
  120. func showError(withTitle error: String, message: String) {
  121. completionHandler?(.error)
  122. }
  123. func showProgress(withTitle title: String, message: String,
  124. isIndeterminate: Bool, position: Float, cancel cancelString: String?,
  125. withReference reference: NSValue) {
  126. // noop
  127. }
  128. func updateProgress(withReference reference: NSValue, message: String?, position: Float) {
  129. // noop
  130. }
  131. func showLogin(withTitle title: String, message: String,
  132. defaultUsername username: String?, askingForStorage: Bool,
  133. withReference reference: NSValue) {
  134. // Due to a keystore issue, we disable the overall SMBv1 dialog logic and direcly show the login
  135. handleLoginAlert(with: title, message: message,
  136. username: username,
  137. askingForStorage: false,
  138. withReference: reference)
  139. // if !title.contains("SMBv1") || selectedSMBv1 {
  140. // handleLoginAlert(with: title, message: message,
  141. // username: username,
  142. // askingForStorage: askingForStorage,
  143. // withReference: reference)
  144. // return
  145. // }
  146. // handleSMBv1() {
  147. // [weak self] isSMBv1 in
  148. // if isSMBv1 {
  149. // self?.selectedSMBv1 = true
  150. // self?.handleLoginAlert(with: title, message: message,
  151. // username: username,
  152. // askingForStorage: askingForStorage,
  153. // withReference: reference)
  154. // } else {
  155. // self?.dialogProvider.dismissDialog(withReference: reference)
  156. // }
  157. // }
  158. }
  159. func showQuestion(withTitle title: String, message: String,
  160. type questionType: VLCDialogQuestionType, cancel cancelString: String?,
  161. action1String: String?, action2String: String?, withReference reference: NSValue) {
  162. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  163. if let cancelTitle = cancelString {
  164. alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel,
  165. handler: {
  166. [weak self] action in
  167. self?.dialogProvider.postAction(3, forDialogReference: reference)
  168. }))
  169. }
  170. if let action1Ttile = action1String {
  171. let confirmAction = UIAlertAction(title: action1Ttile, style: .default, handler: {
  172. [weak self] action in
  173. self?.dialogProvider.postAction(1, forDialogReference: reference)
  174. })
  175. alertController.addAction(confirmAction)
  176. alertController.preferredAction = confirmAction
  177. }
  178. if let action2Title = action2String {
  179. alertController.addAction(UIAlertAction(title: action2Title, style: .default,
  180. handler: {
  181. [weak self] action in
  182. self?.dialogProvider.postAction(2, forDialogReference: reference)
  183. }))
  184. }
  185. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  186. let presentingController = rootViewController.presentedViewController ?? rootViewController
  187. presentingController.present(alertController, animated: true, completion: nil)
  188. }
  189. }
  190. func cancelDialog(withReference reference: NSValue) {
  191. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  192. let presentingController = rootViewController.presentedViewController ?? rootViewController
  193. presentingController.dismiss(animated: true, completion: nil)
  194. }
  195. }
  196. }