AppCoordinator.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*****************************************************************************
  2. * AppCoordinator.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # gmail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. @objc(VLCServices)
  13. class Services: NSObject {
  14. @objc let medialibraryService = MediaLibraryService()
  15. @objc let rendererDiscovererManager = VLCRendererDiscovererManager(presentingViewController: nil)
  16. }
  17. @objc class AppCoordinator: NSObject {
  18. private var services = Services()
  19. private var childCoordinators: [NSObject] = []
  20. private var playerDisplayController: VLCPlayerDisplayController
  21. private var tabBarController: UITabBarController
  22. private lazy var tabBarCoordinator: VLCTabBarCoordinator = {
  23. return VLCTabBarCoordinator(tabBarController: tabBarController, services: services)
  24. }()
  25. private var migrationViewController = VLCMigrationViewController(nibName: String(describing: VLCMigrationViewController.self),
  26. bundle: nil)
  27. @objc init(tabBarController: UITabBarController) {
  28. self.playerDisplayController = VLCPlayerDisplayController(services: services)
  29. self.tabBarController = tabBarController
  30. super.init()
  31. setupChildViewControllers()
  32. // Init the HTTP Server and clean its cache
  33. // FIXME: VLCHTTPUploaderController should perhaps be a service?
  34. VLCHTTPUploaderController.sharedInstance().cleanCache()
  35. VLCHTTPUploaderController.sharedInstance().medialibrary = services.medialibraryService
  36. services.medialibraryService.migrationDelegate = self
  37. }
  38. private func setupChildViewControllers() {
  39. tabBarController.addChild(playerDisplayController)
  40. tabBarController.view.addSubview(playerDisplayController.view)
  41. playerDisplayController.view.layoutMargins = UIEdgeInsets(top: 0,
  42. left: 0,
  43. bottom: tabBarController.tabBar.frame.size.height,
  44. right: 0)
  45. playerDisplayController.realBottomAnchor = tabBarController.tabBar.topAnchor
  46. playerDisplayController.didMove(toParent: tabBarController)
  47. // Set app cornerRadius back to 0
  48. // For some unknown reason, reading tabBarController anchors sets round corners for the entire app.
  49. // We set cornerRadius to 0 for the topmost layer so there are no round corners anymore.
  50. if #available(iOS 13, *) {
  51. tabBarController.view.layer.superlayer?.cornerRadius = 0
  52. }
  53. }
  54. @objc func start() {
  55. childCoordinators.append(tabBarCoordinator)
  56. }
  57. @objc func handleShortcutItem(_ item: UIApplicationShortcutItem) {
  58. tabBarCoordinator.handleShortcutItem(item)
  59. }
  60. @objc func mediaForUserActivity(_ activity: NSUserActivity) -> VLCMLMedia? {
  61. let userActivityType = activity.activityType
  62. guard let dict = activity.userInfo else { return nil }
  63. var identifier: Int64? = nil
  64. if userActivityType == CSSearchableItemActionType, let searchIdentifier = dict[CSSearchableItemActivityIdentifier] as? NSString {
  65. identifier = Int64(searchIdentifier.integerValue)
  66. } else if let mediaIdentifier = dict["playingmedia"] as? Int64 {
  67. identifier = mediaIdentifier
  68. }
  69. guard let mediaIdentifier = identifier else { return nil }
  70. return services.medialibraryService.media(for: mediaIdentifier)
  71. }
  72. }
  73. extension AppCoordinator: MediaLibraryMigrationDelegate {
  74. func medialibraryDidStartMigration(_ medialibrary: MediaLibraryService) {
  75. DispatchQueue.main.async {
  76. [tabBarController, migrationViewController] in
  77. tabBarController.present(migrationViewController, animated: true, completion: nil)
  78. }
  79. }
  80. func medialibraryDidFinishMigration(_ medialibrary: MediaLibraryService) {
  81. DispatchQueue.main.async {
  82. [migrationViewController] in
  83. migrationViewController.dismiss(animated: true, completion: nil)
  84. }
  85. }
  86. func medialibraryDidStopMigration(_ medialibrary: MediaLibraryService) {
  87. if tabBarController.presentedViewController === migrationViewController {
  88. DispatchQueue.main.async {
  89. [tabBarController] in
  90. tabBarController.dismiss(animated: true, completion: nil)
  91. }
  92. }
  93. }
  94. }