MediaDataSource.swift 3.3 KB

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