MovieCollectionViewCell.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*****************************************************************************
  2. * MovieCollectionViewCell.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. class MovieCollectionViewCell: BaseCollectionViewCell {
  14. @IBOutlet weak var thumbnailView: UIImageView!
  15. @IBOutlet weak var titleLabel: UILabel!
  16. @IBOutlet weak var newLabel: UILabel!
  17. @IBOutlet weak var descriptionLabel: UILabel!
  18. @IBOutlet weak var progressView: UIProgressView!
  19. @IBOutlet weak var collectionOverlay: UIView!
  20. @IBOutlet weak var numberOfTracks: UILabel!
  21. override class var edgePadding: CGFloat {
  22. return 12.5
  23. }
  24. override class var interItemPadding: CGFloat {
  25. return 7.5
  26. }
  27. override var media: VLCMLObject? {
  28. didSet {
  29. if let movie = media as? VLCMLMedia {
  30. update(movie:movie)
  31. } else if let playlist = media as? VLCMLPlaylist {
  32. update(playlist:playlist)
  33. } else if let videoGroup = media as? VLCMLVideoGroup {
  34. update(videoGroup: videoGroup)
  35. } else {
  36. assertionFailure("MovieCollectionViewCell: media: Needs to be of a supported Type.")
  37. }
  38. }
  39. }
  40. override func awakeFromNib() {
  41. super.awakeFromNib()
  42. if #available(iOS 11.0, *) {
  43. thumbnailView.accessibilityIgnoresInvertColors = true
  44. }
  45. clipsToBounds = true
  46. layer.cornerRadius = 2
  47. newLabel.text = NSLocalizedString("NEW", comment: "")
  48. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  49. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  50. themeDidChange()
  51. }
  52. @objc fileprivate func themeDidChange() {
  53. backgroundColor = PresentationTheme.current.colors.background
  54. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  55. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  56. }
  57. func update(movie: VLCMLMedia) {
  58. var title = movie.title
  59. if UserDefaults.standard.bool(forKey: kVLCOptimizeItemNamesForDisplay) == true {
  60. title = (title as NSString).deletingPathExtension
  61. }
  62. titleLabel.text = title
  63. accessibilityLabel = movie.accessibilityText(editing: false)
  64. descriptionLabel.text = movie.mediaDuration()
  65. thumbnailView.image = movie.thumbnailImage()
  66. let progress = movie.progress
  67. progressView.isHidden = progress == 0
  68. progressView.progress = progress
  69. newLabel.isHidden = !movie.isNew
  70. }
  71. func update(playlist: VLCMLPlaylist) {
  72. collectionOverlay.isHidden = false
  73. numberOfTracks.text = String(playlist.media?.count ?? 0)
  74. titleLabel.text = playlist.name
  75. accessibilityLabel = playlist.accessibilityText()
  76. descriptionLabel.text = playlist.numberOfTracksString()
  77. thumbnailView.image = playlist.thumbnail()
  78. }
  79. func update(videoGroup: VLCMLVideoGroup) {
  80. collectionOverlay.isHidden = false
  81. numberOfTracks.text = String(videoGroup.count())
  82. titleLabel.text = videoGroup.name()
  83. accessibilityLabel = videoGroup.accessibilityText()
  84. descriptionLabel.text = videoGroup.numberOfTracksString()
  85. thumbnailView.image = videoGroup.thumbnail()
  86. }
  87. override class func numberOfColumns(for width: CGFloat) -> CGFloat {
  88. if width <= DeviceWidth.iPhonePortrait.rawValue {
  89. return 2
  90. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  91. return 3
  92. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  93. return 4
  94. } else {
  95. return 5
  96. }
  97. }
  98. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  99. let numberOfCells: CGFloat = numberOfColumns(for: width)
  100. let aspectRatio: CGFloat = 10.0 / 16.0
  101. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  102. //
  103. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  104. //
  105. let overallWidth = width - (2 * edgePadding)
  106. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  107. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  108. // 17 * 2 for title, 14 for new + duration, 3 * 4 paddings for lines
  109. return CGSize(width: cellWidth, height: cellWidth * aspectRatio + (16 * 2) + 14 + (3 * 3))
  110. }
  111. override func prepareForReuse() {
  112. super.prepareForReuse()
  113. titleLabel.text = ""
  114. descriptionLabel.text = ""
  115. thumbnailView.image = nil
  116. progressView.isHidden = true
  117. newLabel.isHidden = true
  118. collectionOverlay.isHidden = true
  119. numberOfTracks.text = ""
  120. }
  121. }