MovieCollectionViewCell.swift 5.8 KB

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