MediaDataSource.swift 3.5 KB

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