MediaDataSourceAndDelegate.swift 3.7 KB

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