MediaCategoryViewController.swift 9.5 KB

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