MediaDataSource.swift 3.3 KB

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