VLCPlaybackController+VLCDialogProvider.swift 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*****************************************************************************
  2. * VLCPlaybackController+VLCDialogProvider.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. import Foundation
  13. extension VLCPlaybackController: VLCCustomDialogRendererProtocol {
  14. public func showError(withTitle error: String, message: String) {
  15. //noop
  16. }
  17. public func showLogin(withTitle title: String, message: String, defaultUsername username: String?, askingForStorage: Bool, withReference reference: NSValue) {
  18. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  19. var usernameField: UITextField?
  20. var passwordField: UITextField?
  21. alertController.addTextField { textField in
  22. usernameField = textField
  23. textField.placeholder = NSLocalizedString("USER_LABEL", comment:"")
  24. if username != "" {
  25. textField.text = username
  26. }
  27. }
  28. alertController.addTextField { textField in
  29. passwordField = textField
  30. textField.isSecureTextEntry = true
  31. textField.placeholder = NSLocalizedString("PASSWORD_LABEL", comment:"")
  32. }
  33. let loginAction = UIAlertAction(title: NSLocalizedString("LOGIN", comment:""), style: .default) {[weak self] action in
  34. let username = usernameField?.text ?? ""
  35. let password = passwordField?.text ?? ""
  36. self?.dialogProvider.postUsername(username, andPassword: password, forDialogReference: reference, store: false)
  37. }
  38. alertController.addAction(loginAction)
  39. alertController.preferredAction = loginAction
  40. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_CANCEL", comment:""), style: .cancel, handler: { [weak self] action in
  41. self?.dialogProvider.dismissDialog(withReference: reference)
  42. }))
  43. if askingForStorage {
  44. alertController.addAction(UIAlertAction(title: NSLocalizedString("BUTTON_SAVE", comment:""), style: .default, handler: {[weak self] action in
  45. let username = usernameField?.text ?? ""
  46. let password = passwordField?.text ?? ""
  47. self?.dialogProvider.postUsername(username, andPassword: password, forDialogReference: reference, store: true)
  48. }))
  49. }
  50. UIApplication.shared.delegate?.window??.rootViewController?.presentedViewController?.present(alertController, animated: true, completion: nil)
  51. }
  52. public func showQuestion(withTitle title: String, message: String, type questionType: VLCDialogQuestionType, cancel cancelString: String?, action1String: String?, action2String: String?, withReference reference: NSValue) {
  53. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  54. if let cancelTitle = cancelString {
  55. alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel, handler: { [weak self] action in
  56. self?.dialogProvider.postAction(3, forDialogReference: reference)
  57. }))
  58. }
  59. if let action1Ttile = action1String {
  60. let confirmAction = UIAlertAction(title: action1Ttile, style: .default, handler: { [weak self] action in
  61. self?.dialogProvider.postAction(1, forDialogReference: reference)
  62. })
  63. alertController.addAction(confirmAction)
  64. alertController.preferredAction = confirmAction
  65. }
  66. if let action2Title = action2String {
  67. alertController.addAction(UIAlertAction(title: action2Title, style: .default, handler: {[weak self] action in
  68. self?.dialogProvider.postAction(2, forDialogReference: reference)
  69. }))
  70. }
  71. UIApplication.shared.delegate?.window??.rootViewController?.presentedViewController?.present(alertController, animated: true, completion: nil)
  72. }
  73. public func showProgress(withTitle title: String, message: String, isIndeterminate: Bool, position: Float, cancel cancelString: String?, withReference reference: NSValue) {
  74. print("title: \(title), message: \(message), isIndeterminate: \(isIndeterminate), position: \(position), cancel: \(cancelString ?? ""), reference: \(reference)")
  75. }
  76. public func updateProgress(withReference reference: NSValue, message: String?, postion position: Float) {
  77. print("reference: \(reference) message: \(message ?? "") position: \(position)")
  78. }
  79. public func cancelDialog(withReference reference: NSValue) {
  80. UIApplication.shared.delegate?.window??.rootViewController?.presentedViewController?.dismiss(animated: true, completion: nil)
  81. }
  82. }