MediaCategoryViewController.swift 11 KB

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