MediaCategoryViewController.swift 11 KB

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