MediaCategoryViewController.swift 8.6 KB

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