MediaViewController+DataSourceDelegate.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*****************************************************************************
  2. * MediaViewController+DataSourceDelegate.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. extension Notification.Name {
  14. static let VLCTracksDidChangeNotification = Notification.Name("kTracksDidChangeNotification")
  15. static let VLCAllVideosDidChangeNotification = Notification.Name("kAllVideosDidChangeNotification")
  16. }
  17. // These are the delegate and datasource calls from the MediaViewController
  18. extension VLCMediaViewController: UICollectionViewDelegateFlowLayout {
  19. // MARK: - UICollectionViewDataSource
  20. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  21. return Int(services.mediaDataSource.numberOfFiles(subcategory: mediaType.subcategory))
  22. }
  23. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  24. if let playlistCell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCPlaylistCollectionViewCell.cellIdentifier(), for: indexPath) as? VLCPlaylistCollectionViewCell {
  25. if let mediaObject = services.mediaDataSource.object(at: indexPath.row, subcategory: mediaType.subcategory) as? NSManagedObject {
  26. playlistCell.mediaObject = mediaObject
  27. }
  28. return playlistCell
  29. }
  30. return UICollectionViewCell()
  31. }
  32. // MARK: - UICollectionViewDelegate
  33. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  34. if let mediaObject = services.mediaDataSource.object(at: indexPath.row, subcategory: mediaType.subcategory) as? NSManagedObject {
  35. delegate?.mediaViewControllerDidSelectMediaObject(self, mediaObject: mediaObject)
  36. }
  37. }
  38. // MARK: - UICollectionViewDelegateFlowLayout
  39. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  40. let numberOfCells: CGFloat = collectionView.traitCollection.horizontalSizeClass == .regular ? 3.0 : 2.0
  41. let aspectRatio: CGFloat = 10.0 / 16.0
  42. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  43. // 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
  44. // since this might be an uneven number we ceil
  45. var cellWidth = collectionView.bounds.size.width / numberOfCells
  46. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  47. return CGSize(width: cellWidth, height: cellWidth * aspectRatio)
  48. }
  49. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  50. return UIEdgeInsets(top: cellPadding, left: cellPadding, bottom: cellPadding, right: cellPadding)
  51. }
  52. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  53. return cellPadding
  54. }
  55. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  56. return cellPadding
  57. }
  58. }