/***************************************************************************** * MediaCateogoryViewController.swift * VLC for iOS ***************************************************************************** * Copyright (c) 2018 VideoLAN. All rights reserved. * $Id$ * * Authors: Carola Nitz * Mike JS. Choi * * Refer to the COPYING file of the official project for license. *****************************************************************************/ import Foundation protocol VLCMediaCategoryViewControllerDelegate: class { func mediaViewControllerDidSelectMediaObject(_ mediaViewController: UIViewController, mediaObject: NSManagedObject) } class VLCMediaCategoryViewController: 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 weak var delegate: VLCMediaCategoryViewControllerDelegate? @available(iOS 11.0, *) lazy var dragAndDropManager: VLCDragAndDropManager = { () -> VLCDragAndDropManager in VLCDragAndDropManager(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) { 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 } }