123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- /*****************************************************************************
- * MediaCateogoryViewController.swift
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2018 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Carola Nitz <nitz.carola # gmail.com>
- * Mike JS. Choi <mkchoi212 # icloud.com>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- import Foundation
- protocol VLCMediaCategoryViewControllerDelegate: class {
- func mediaViewControllerDidSelectMediaObject(_ mediaViewController: UIViewController, mediaObject: NSManagedObject)
- }
- class VLCMediaCategoryViewController<T>: UICollectionViewController, UICollectionViewDelegateFlowLayout, UISearchResultsUpdating, UISearchControllerDelegate, IndicatorInfoProvider {
- let cellPadding: CGFloat = 5.0
- private var services: Services
- private var searchController: UISearchController?
- private let searchDataSource = VLCLibrarySearchDisplayDataSource()
- var subcategory: VLCMediaSubcategoryModel<T>
- weak var delegate: VLCMediaCategoryViewControllerDelegate?
- @available(iOS 11.0, *)
- lazy var dragAndDropManager: VLCDragAndDropManager = { () -> VLCDragAndDropManager<T> in
- VLCDragAndDropManager<T>(subcategory: subcategory)
- }()
- lazy var emptyView: VLCEmptyLibraryView = {
- let name = String(describing: VLCEmptyLibraryView.self)
- let nib = Bundle.main.loadNibNamed(name, owner: self, options: nil)
- guard let emptyView = nib?.first as? VLCEmptyLibraryView else { fatalError("Can't find nib for \(name)") }
- return emptyView
- }()
- @available(*, unavailable)
- init() {
- fatalError()
- }
- init(services: Services, subcategory: VLCMediaSubcategoryModel<T>) {
- self.services = services
- self.subcategory = subcategory
- super.init(collectionViewLayout: UICollectionViewFlowLayout())
- NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: subcategory.changeNotificationName, object: nil)
- }
- override var preferredStatusBarStyle: UIStatusBarStyle {
- return PresentationTheme.current.colors.statusBarStyle
- }
- @objc func reloadData() {
- collectionView?.reloadData()
- updateUIForContent()
- }
- @available(*, unavailable)
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder: ) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- setupCollectionView()
- setupSearchController()
- _ = (MLMediaLibrary.sharedMediaLibrary() as! MLMediaLibrary).libraryDidAppear()
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- let manager = services.rendererDiscovererManager
- if manager.discoverers.isEmpty {
- // Either didn't start or stopped before
- manager.start()
- }
- manager.presentingViewController = self
- }
- @objc func themeDidChange() {
- collectionView?.backgroundColor = PresentationTheme.current.colors.background
- setNeedsStatusBarAppearanceUpdate()
- }
- func setupCollectionView() {
- let playlistnib = UINib(nibName: "VLCPlaylistCollectionViewCell", bundle: nil)
- collectionView?.register(playlistnib, forCellWithReuseIdentifier: VLCPlaylistCollectionViewCell.cellIdentifier())
- collectionView?.backgroundColor = PresentationTheme.current.colors.background
- collectionView?.alwaysBounceVertical = true
- if #available(iOS 11.0, *) {
- collectionView?.dragDelegate = dragAndDropManager
- collectionView?.dropDelegate = dragAndDropManager
- }
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- reloadData()
- }
- func setupSearchController() {
- searchController = UISearchController(searchResultsController: nil)
- searchController?.searchResultsUpdater = self
- searchController?.dimsBackgroundDuringPresentation = false
- searchController?.delegate = self
- if let textfield = searchController?.searchBar.value(forKey: "searchField") as? UITextField {
- if let backgroundview = textfield.subviews.first {
- backgroundview.backgroundColor = UIColor.white
- backgroundview.layer.cornerRadius = 10
- backgroundview.clipsToBounds = true
- }
- }
- }
-
- func updateUIForContent() {
- let isEmpty = collectionView?.numberOfItems(inSection: 0) == 0
- if isEmpty {
- collectionView?.setContentOffset(.zero, animated: false)
- }
- collectionView?.backgroundView = isEmpty ? emptyView : nil
-
- if #available(iOS 11.0, *) {
- navigationItem.searchController = isEmpty ? nil : searchController
- } else {
- navigationItem.titleView = isEmpty ? nil : searchController?.searchBar
- }
- }
- // MARK: Renderer
- override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
- collectionView?.collectionViewLayout.invalidateLayout()
- }
- // MARK: - Search
- func updateSearchResults(for searchController: UISearchController) {
- searchDataSource.shouldReloadTable(forSearch: searchController.searchBar.text, searchableFiles: subcategory.files)
- collectionView?.reloadData()
- }
- func didPresentSearchController(_ searchController: UISearchController) {
- collectionView?.dataSource = searchDataSource
- }
- func didDismissSearchController(_ searchController: UISearchController) {
- collectionView?.dataSource = self
- }
- func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
- return IndicatorInfo(title:subcategory.indicatorInfoName)
- }
- // MARK: - UICollectionViewDataSource
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return subcategory.files.count
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- if let playlistCell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCPlaylistCollectionViewCell.cellIdentifier(), for: indexPath) as? VLCPlaylistCollectionViewCell {
- if let mediaObject = subcategory.files[indexPath.row] as? NSManagedObject {
- playlistCell.mediaObject = mediaObject
- }
- return playlistCell
- }
- return UICollectionViewCell()
- }
- // MARK: - UICollectionViewDelegate
- override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if let mediaObject = subcategory.files[indexPath.row] as? NSManagedObject {
- delegate?.mediaViewControllerDidSelectMediaObject(self, mediaObject: mediaObject)
- }
- }
- // MARK: - UICollectionViewDelegateFlowLayout
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- let numberOfCells: CGFloat = collectionView.traitCollection.horizontalSizeClass == .regular ? 3.0 : 2.0
- let aspectRatio: CGFloat = 10.0 / 16.0
- // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
- // 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
- // since this might be an uneven number we ceil
- var cellWidth = collectionView.bounds.size.width / numberOfCells
- cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
- return CGSize(width: cellWidth, height: cellWidth * aspectRatio)
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
- return UIEdgeInsets(top: cellPadding, left: cellPadding, bottom: cellPadding, right: cellPadding)
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
- return cellPadding
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
- return cellPadding
- }
- }
|