MediaDataSource.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*****************************************************************************
  2. * MediaDataSource.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. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. public class MediaDataSourceAndDelegate: NSObject, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  13. private let cellPadding: CGFloat = 5.0
  14. private var services: Services
  15. public weak var delegate: UICollectionViewDelegate?
  16. public convenience init(services: Services) {
  17. self.init()
  18. self.services = services
  19. }
  20. public override init() {
  21. self.services = Services()
  22. super.init()
  23. }
  24. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  25. return Int(services.mediaDataSource.numberOfFiles())
  26. }
  27. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  28. if let playlistCell = collectionView.dequeueReusableCell(withReuseIdentifier: VLCPlaylistCollectionViewCell.cellIdentifier(), for: indexPath) as? VLCPlaylistCollectionViewCell {
  29. playlistCell.mediaObject = services.mediaDataSource.object(at: UInt(indexPath.row))
  30. return playlistCell
  31. }
  32. return UICollectionViewCell()
  33. }
  34. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  35. delegate?.collectionView!(collectionView, didSelectItemAt: indexPath)
  36. }
  37. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  38. let numberOfCells: CGFloat = collectionView.traitCollection.horizontalSizeClass == .regular ? 3.0 : 2.0
  39. let aspectRatio: CGFloat = 10.0 / 16.0
  40. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  41. // 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
  42. // since this might be an uneven number we ceil
  43. var cellWidth = collectionView.bounds.size.width / numberOfCells
  44. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  45. return CGSize(width:cellWidth, height:cellWidth * aspectRatio)
  46. }
  47. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  48. return UIEdgeInsets(top: cellPadding, left: cellPadding, bottom: cellPadding, right: cellPadding)
  49. }
  50. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  51. return cellPadding
  52. }
  53. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  54. return cellPadding
  55. }
  56. }