MediaDataSourceAndDelegate.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if let mediaObject = services.mediaDataSource.object(at: indexPath.row, subcategory: mediaType.subcategory) as? NSManagedObject {
  37. playlistCell.mediaObject = mediaObject
  38. }
  39. return playlistCell
  40. }
  41. return UICollectionViewCell()
  42. }
  43. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  44. delegate?.collectionView!(collectionView, didSelectItemAt: indexPath)
  45. }
  46. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  47. let numberOfCells: CGFloat = collectionView.traitCollection.horizontalSizeClass == .regular ? 3.0 : 2.0
  48. let aspectRatio: CGFloat = 10.0 / 16.0
  49. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  50. // 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
  51. // since this might be an uneven number we ceil
  52. var cellWidth = collectionView.bounds.size.width / numberOfCells
  53. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  54. return CGSize(width: cellWidth, height: cellWidth * aspectRatio)
  55. }
  56. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  57. return UIEdgeInsets(top: cellPadding, left: cellPadding, bottom: cellPadding, right: cellPadding)
  58. }
  59. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  60. return cellPadding
  61. }
  62. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  63. return cellPadding
  64. }
  65. }