CustomDialogRendererHandler.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. @objc var completionHandler: CustomDialogRendererHandlerClosure?
  24. @objc init(dialogProvider: VLCDialogProvider) {
  25. self.dialogProvider = dialogProvider
  26. super.init()
  27. }
  28. }
  29. // MARK: - Private helpers
  30. private extension CustomDialogRendererHandler {
  31. private func handleSMBv1(completionHandler: @escaping (Bool) -> Void) {
  32. let alertController = UIAlertController(title: NSLocalizedString("SMBV1_WARN_TITLE", comment: ""),
  33. message: NSLocalizedString("SMBV1_WARN_DESCRIPTION", comment: ""),
  34. preferredStyle: .alert)
  35. alertController.addAction(UIAlertAction(title: NSLocalizedString("SMBV1_CONTINUE", comment:""),
  36. style: .destructive, handler: {
  37. action in
  38. completionHandler(true)
  39. }))
  40. alertController.addAction(UIAlertAction(title: NSLocalizedString("SMBV1_NEXT_PROTOCOL", comment:""),
  41. style: .default, handler: {
  42. action in
  43. completionHandler(false)
  44. }))
  45. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  46. let presentingController = rootViewController.presentedViewController ?? rootViewController
  47. presentingController.present(alertController, animated: true, completion: nil)
  48. }
  49. }
  50. private func handleLoginAlert(with title: String, message: String,
  51. username: String?, askingForStorage: Bool,
  52. withReference reference: NSValue,
  53. shouldIgnoreNextDialog: Bool) {
  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 shouldIgnoreNextDialog {
  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") {
  129. handleSMBv1() {
  130. [weak self] isSMBv1 in
  131. if isSMBv1 {
  132. self?.handleLoginAlert(with: title, message: message,
  133. username: username,
  134. askingForStorage: askingForStorage,
  135. withReference: reference, shouldIgnoreNextDialog: true)
  136. } else {
  137. self?.dialogProvider.dismissDialog(withReference: reference)
  138. }
  139. }
  140. } else {
  141. handleLoginAlert(with: title, message: message,
  142. username: username,
  143. askingForStorage: askingForStorage,
  144. withReference: reference, shouldIgnoreNextDialog: false)
  145. }
  146. }
  147. func showQuestion(withTitle title: String, message: String,
  148. type questionType: VLCDialogQuestionType, cancel cancelString: String?,
  149. action1String: String?, action2String: String?, withReference reference: NSValue) {
  150. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  151. if let cancelTitle = cancelString {
  152. alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel,
  153. handler: {
  154. [weak self] action in
  155. self?.dialogProvider.postAction(3, forDialogReference: reference)
  156. }))
  157. }
  158. if let action1Ttile = action1String {
  159. let confirmAction = UIAlertAction(title: action1Ttile, style: .default, handler: {
  160. [weak self] action in
  161. self?.dialogProvider.postAction(1, forDialogReference: reference)
  162. })
  163. alertController.addAction(confirmAction)
  164. alertController.preferredAction = confirmAction
  165. }
  166. if let action2Title = action2String {
  167. alertController.addAction(UIAlertAction(title: action2Title, style: .default,
  168. handler: {
  169. [weak self] action in
  170. self?.dialogProvider.postAction(2, forDialogReference: reference)
  171. }))
  172. }
  173. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  174. let presentingController = rootViewController.presentedViewController ?? rootViewController
  175. presentingController.present(alertController, animated: true, completion: nil)
  176. }
  177. }
  178. func cancelDialog(withReference reference: NSValue) {
  179. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  180. let presentingController = rootViewController.presentedViewController ?? rootViewController
  181. presentingController.dismiss(animated: true, completion: nil)
  182. }
  183. }
  184. }