MediaViewController.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*****************************************************************************
  2. * MediaViewController.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. * Mike JS. Choi <mkchoi212 # icloud.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. import Foundation
  14. @objc public protocol VLCMediaViewControllerDelegate: class {
  15. func mediaViewControllerDidSelectMediaObject(_ mediaViewController: VLCMediaViewController, mediaObject: NSManagedObject)
  16. func mediaViewControllerDidSelectSort(_ mediaViewController: VLCMediaViewController)
  17. }
  18. public class VLCMediaViewController: UICollectionViewController, UISearchResultsUpdating, UISearchControllerDelegate {
  19. private var services: Services
  20. private var mediaDataSourceAndDelegate: MediaDataSourceAndDelegate?
  21. private var searchController: UISearchController?
  22. private let searchDataSource = VLCLibrarySearchDisplayDataSource()
  23. private var mediaType: VLCMediaType
  24. private var rendererButton: UIButton
  25. public weak var delegate: VLCMediaViewControllerDelegate?
  26. @available(iOS 11.0, *)
  27. lazy var dragAndDropManager: VLCDragAndDropManager = {
  28. let dragAndDropManager = VLCDragAndDropManager(type: mediaType)
  29. dragAndDropManager.delegate = services.mediaDataSource
  30. return dragAndDropManager
  31. }()
  32. lazy var emptyView: VLCEmptyLibraryView = {
  33. let name = String(describing: VLCEmptyLibraryView.self)
  34. let nib = Bundle.main.loadNibNamed(name, owner: self, options: nil)
  35. guard let emptyView = nib?.first as? VLCEmptyLibraryView else { fatalError("Can't find nib for \(name)") }
  36. return emptyView
  37. }()
  38. @available(*, unavailable)
  39. init() {
  40. fatalError()
  41. }
  42. init(services: Services, type: VLCMediaType) {
  43. self.services = services
  44. mediaType = type
  45. rendererButton = services.rendererDiscovererManager.setupRendererButton()
  46. super.init(collectionViewLayout: UICollectionViewFlowLayout())
  47. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  48. if mediaType.category == .video {
  49. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: .VLCAllVideosDidChangeNotification, object: nil)
  50. } else {
  51. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: .VLCTracksDidChangeNotification, object: nil)
  52. }
  53. }
  54. @objc func reloadData() {
  55. collectionView?.reloadData()
  56. updateUIForContent()
  57. }
  58. @available(*, unavailable)
  59. public required init?(coder aDecoder: NSCoder) {
  60. fatalError("init(coder: ) has not been implemented")
  61. }
  62. public override func viewDidLoad() {
  63. super.viewDidLoad()
  64. setupCollectionView()
  65. setupSearchController()
  66. setupNavigationBar()
  67. setupRendererButton()
  68. _ = (MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary).libraryDidAppear()
  69. }
  70. public override func viewWillAppear(_ animated: Bool) {
  71. super.viewWillAppear(animated)
  72. let manager = services.rendererDiscovererManager
  73. if manager.discoverers.isEmpty {
  74. // Either didn't start or stopped before
  75. manager.start()
  76. }
  77. manager.presentingViewController = self
  78. }
  79. @objc func themeDidChange() {
  80. collectionView?.backgroundColor = PresentationTheme.current.colors.background
  81. }
  82. func setupCollectionView() {
  83. mediaDataSourceAndDelegate = MediaDataSourceAndDelegate(services: services, type: mediaType)
  84. mediaDataSourceAndDelegate?.delegate = self
  85. let playlistnib = UINib(nibName: "VLCPlaylistCollectionViewCell", bundle: nil)
  86. collectionView?.register(playlistnib, forCellWithReuseIdentifier: VLCPlaylistCollectionViewCell.cellIdentifier())
  87. collectionView?.backgroundColor = PresentationTheme.current.colors.background
  88. collectionView?.alwaysBounceVertical = true
  89. collectionView?.dataSource = mediaDataSourceAndDelegate
  90. collectionView?.delegate = mediaDataSourceAndDelegate
  91. if #available(iOS 11.0, *) {
  92. collectionView?.dragDelegate = dragAndDropManager
  93. collectionView?.dropDelegate = dragAndDropManager
  94. }
  95. }
  96. public override func viewDidAppear(_ animated: Bool) {
  97. super.viewDidAppear(animated)
  98. reloadData()
  99. }
  100. func setupSearchController() {
  101. searchController = UISearchController(searchResultsController: nil)
  102. searchController?.searchResultsUpdater = self
  103. searchController?.dimsBackgroundDuringPresentation = false
  104. searchController?.delegate = self
  105. if let textfield = searchController?.searchBar.value(forKey: "searchField") as? UITextField {
  106. if let backgroundview = textfield.subviews.first {
  107. backgroundview.backgroundColor = UIColor.white
  108. backgroundview.layer.cornerRadius = 10
  109. backgroundview.clipsToBounds = true
  110. }
  111. }
  112. }
  113. func setupNavigationBar() {
  114. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("SORT", comment: ""), style: .plain, target: self, action: #selector(sort))
  115. }
  116. func updateUIForContent() {
  117. let isEmpty = collectionView?.numberOfItems(inSection: 0) == 0
  118. if isEmpty {
  119. collectionView?.setContentOffset(.zero, animated: false)
  120. }
  121. collectionView?.backgroundView = isEmpty ? emptyView : nil
  122. if #available(iOS 11.0, *) {
  123. navigationItem.searchController = isEmpty ? nil : searchController
  124. navigationController?.navigationBar.prefersLargeTitles = !isEmpty
  125. } else {
  126. navigationItem.titleView = isEmpty ? nil : searchController?.searchBar
  127. }
  128. }
  129. // MARK: Renderer
  130. private func setupRendererButton() {
  131. navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rendererButton)
  132. }
  133. @objc func sort() {
  134. delegate?.mediaViewControllerDidSelectSort(self)
  135. }
  136. public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  137. collectionView?.collectionViewLayout.invalidateLayout()
  138. }
  139. // MARK: - MediaDatasourceAndDelegate
  140. public override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  141. delegate?.mediaViewControllerDidSelectMediaObject(self, mediaObject: services.mediaDataSource.object(at: indexPath.row, subcategory: mediaType.subcategory))
  142. }
  143. // MARK: - Search
  144. public func updateSearchResults(for searchController: UISearchController) {
  145. searchDataSource.shouldReloadTable(forSearch: searchController.searchBar.text, searchableFiles: services.mediaDataSource.allObjects(for: mediaType.subcategory))
  146. collectionView?.reloadData()
  147. }
  148. public func didPresentSearchController(_ searchController: UISearchController) {
  149. collectionView?.dataSource = searchDataSource
  150. }
  151. public func didDismissSearchController(_ searchController: UISearchController) {
  152. collectionView?.dataSource = mediaDataSourceAndDelegate
  153. }
  154. }