VLCPlaybackController+VLCDialogProvider.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  51. let presentingController = rootViewController.presentedViewController ?? rootViewController
  52. presentingController.present(alertController, animated: true, completion: nil)
  53. }
  54. }
  55. public func showQuestion(withTitle title: String, message: String, type questionType: VLCDialogQuestionType, cancel cancelString: String?, action1String: String?, action2String: String?, withReference reference: NSValue) {
  56. let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  57. if let cancelTitle = cancelString {
  58. alertController.addAction(UIAlertAction(title: cancelTitle, style: .cancel, handler: { [weak self] action in
  59. self?.dialogProvider.postAction(3, forDialogReference: reference)
  60. }))
  61. }
  62. if let action1Ttile = action1String {
  63. let confirmAction = UIAlertAction(title: action1Ttile, style: .default, handler: { [weak self] action in
  64. self?.dialogProvider.postAction(1, forDialogReference: reference)
  65. })
  66. alertController.addAction(confirmAction)
  67. alertController.preferredAction = confirmAction
  68. }
  69. if let action2Title = action2String {
  70. alertController.addAction(UIAlertAction(title: action2Title, style: .default, handler: {[weak self] action in
  71. self?.dialogProvider.postAction(2, forDialogReference: reference)
  72. }))
  73. }
  74. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  75. let presentingController = rootViewController.presentedViewController ?? rootViewController
  76. presentingController.present(alertController, animated: true, completion: nil)
  77. }
  78. }
  79. public func showProgress(withTitle title: String, message: String, isIndeterminate: Bool, position: Float, cancel cancelString: String?, withReference reference: NSValue) {
  80. print("title: \(title), message: \(message), isIndeterminate: \(isIndeterminate), position: \(position), cancel: \(cancelString ?? ""), reference: \(reference)")
  81. }
  82. public func updateProgress(withReference reference: NSValue, message: String?, position: Float) {
  83. print("reference: \(reference) message: \(message ?? "") position: \(position)")
  84. }
  85. public func cancelDialog(withReference reference: NSValue) {
  86. if let rootViewController = UIApplication.shared.delegate?.window??.rootViewController {
  87. let presentingController = rootViewController.presentedViewController ?? rootViewController
  88. presentingController.dismiss(animated: true, completion: nil)
  89. }
  90. }
  91. }