MediaCategoryViewController.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*****************************************************************************
  2. * MediaCateogoryViewController.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. class VLCMediaCategoryViewController<T, ModelType: MediaLibraryBaseModel>: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchResultsUpdating, UISearchControllerDelegate, IndicatorInfoProvider {
  15. let cellPadding: CGFloat = 5.0
  16. private var services: Services
  17. private var searchController: UISearchController?
  18. private let searchDataSource = VLCLibrarySearchDisplayDataSource()
  19. var category: ModelType
  20. // @available(iOS 11.0, *)
  21. // lazy var dragAndDropManager: VLCDragAndDropManager = { () -> VLCDragAndDropManager<T> in
  22. // VLCDragAndDropManager<T>(subcategory: VLCMediaSubcategories<>)
  23. // }()
  24. lazy var emptyView: VLCEmptyLibraryView = {
  25. let name = String(describing: VLCEmptyLibraryView.self)
  26. let nib = Bundle.main.loadNibNamed(name, owner: self, options: nil)
  27. guard let emptyView = nib?.first as? VLCEmptyLibraryView else { fatalError("Can't find nib for \(name)") }
  28. return emptyView
  29. }()
  30. @available(*, unavailable)
  31. init() {
  32. fatalError()
  33. }
  34. init(services: Services, category: ModelType) {
  35. self.services = services
  36. self.category = category
  37. super.init(collectionViewLayout: UICollectionViewFlowLayout())
  38. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  39. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: category.notificaitonName, object: nil)
  40. }
  41. override var preferredStatusBarStyle: UIStatusBarStyle {
  42. return PresentationTheme.current.colors.statusBarStyle
  43. }
  44. @objc func reloadData() {
  45. DispatchQueue.main.async {
  46. [weak self] in
  47. self?.collectionView?.reloadData()
  48. self?.updateUIForContent()
  49. }
  50. }
  51. @available(*, unavailable)
  52. required init?(coder aDecoder: NSCoder) {
  53. fatalError("init(coder: ) has not been implemented")
  54. }
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. setupCollectionView()
  58. setupSearchController()
  59. _ = (MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary).libraryDidAppear()
  60. }
  61. override func viewWillAppear(_ animated: Bool) {
  62. super.viewWillAppear(animated)
  63. let manager = services.rendererDiscovererManager
  64. if manager.discoverers.isEmpty {
  65. // Either didn't start or stopped before
  66. manager.start()
  67. }
  68. manager.presentingViewController = self
  69. }
  70. @objc func themeDidChange() {
  71. collectionView?.backgroundColor = PresentationTheme.current.colors.background
  72. setNeedsStatusBarAppearanceUpdate()
  73. }
  74. func setupCollectionView() {
  75. let playlistnib = UINib(nibName: "VLCPlaylistCollectionViewCell", bundle: nil)
  76. collectionView?.register(playlistnib, forCellWithReuseIdentifier: VLCPlaylistCollectionViewCell.cellIdentifier())
  77. collectionView?.backgroundColor = PresentationTheme.current.colors.background
  78. collectionView?.alwaysBounceVertical = true
  79. if #available(iOS 11.0, *) {
  80. // collectionView?.dragDelegate = dragAndDropManager
  81. // collectionView?.dropDelegate = dragAndDropManager
  82. }
  83. }
  84. override func viewDidAppear(_ animated: Bool) {
  85. super.viewDidAppear(animated)
  86. reloadData()
  87. }
  88. func setupSearchController() {
  89. searchController = UISearchController(searchResultsController: nil)
  90. searchController?.searchResultsUpdater = self
  91. searchController?.dimsBackgroundDuringPresentation = false
  92. searchController?.delegate = self
  93. if let textfield = searchController?.searchBar.value(forKey: "searchField") as? UITextField {
  94. if let backgroundview = textfield.subviews.first {
  95. backgroundview.backgroundColor = UIColor.white
  96. backgroundview.layer.cornerRadius = 10
  97. backgroundview.clipsToBounds = true
  98. }
  99. }
  100. }
  101. func updateUIForContent() {
  102. let isEmpty = collectionView?.numberOfItems(inSection: 0) == 0
  103. if isEmpty {
  104. collectionView?.setContentOffset(.zero, animated: false)
  105. }
  106. collectionView?.backgroundView = isEmpty ? emptyView : nil
  107. if #available(iOS 11.0, *) {
  108. navigationItem.searchController = isEmpty ? nil : searchController
  109. } else {
  110. navigationItem.titleView = isEmpty ? nil : searchController?.searchBar
  111. }
  112. }
  113. // MARK: Renderer
  114. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  115. collectionView?.collectionViewLayout.invalidateLayout()
  116. }
  117. // MARK: - Search
  118. func updateSearchResults(for searchController: UISearchController) {
  119. searchDataSource.shouldReloadTable(forSearch: searchController.searchBar.text, searchableFiles: category.files)
  120. collectionView?.reloadData()
  121. }
  122. func didPresentSearchController(_ searchController: UISearchController) {
  123. collectionView?.dataSource = searchDataSource
  124. }
  125. func didDismissSearchController(_ searchController: UISearchController) {
  126. collectionView?.dataSource = self
  127. }
  128. func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
  129. return IndicatorInfo(title:category.indicatorName)
  130. }
  131. // MARK: - UICollectionViewDataSource
  132. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  133. return category.files.count
  134. }
  135. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  136. if let playlistCell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCPlaylistCollectionViewCell.cellIdentifier(), for: indexPath) as? VLCPlaylistCollectionViewCell {
  137. if let mediaObject = category.files[indexPath.row] as? NSManagedObject {
  138. playlistCell.mediaObject = mediaObject
  139. }
  140. if let media = category.files[indexPath.row] as? VLCMLMedia {
  141. playlistCell.media = media
  142. if media.mainFile() == nil {
  143. playlistCell.media = nil
  144. }
  145. }
  146. return playlistCell
  147. }
  148. return UICollectionViewCell()
  149. }
  150. // MARK: - UICollectionViewDelegate
  151. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  152. if let mediaObject = category.files[indexPath.row] as? NSManagedObject {
  153. play(mediaObject: mediaObject)
  154. }
  155. }
  156. // MARK: - UICollectionViewDelegateFlowLayout
  157. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  158. let numberOfCells: CGFloat = collectionView.traitCollection.horizontalSizeClass == .regular ? 3.0 : 2.0
  159. let aspectRatio: CGFloat = 10.0 / 16.0
  160. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  161. // we then have the entire padding, we divide the entire padding by the number of Cells to know how much needs to be substracted from ech cell
  162. // since this might be an uneven number we ceil
  163. var cellWidth = collectionView.bounds.size.width / numberOfCells
  164. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  165. return CGSize(width: cellWidth, height: cellWidth * aspectRatio)
  166. }
  167. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  168. return UIEdgeInsets(top: cellPadding, left: cellPadding, bottom: cellPadding, right: cellPadding)
  169. }
  170. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  171. return cellPadding
  172. }
  173. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  174. return cellPadding
  175. }
  176. }
  177. // MARK: - Player
  178. extension VLCMediaCategoryViewController {
  179. func play(mediaObject: NSManagedObject) {
  180. VLCPlaybackController.sharedInstance().playMediaLibraryObject(mediaObject)
  181. }
  182. }