MovieCollectionViewCell.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 {
  34. fatalError("wrong object")
  35. }
  36. }
  37. }
  38. override func awakeFromNib() {
  39. super.awakeFromNib()
  40. if #available(iOS 11.0, *) {
  41. thumbnailView.accessibilityIgnoresInvertColors = true
  42. }
  43. clipsToBounds = true
  44. layer.cornerRadius = 2
  45. newLabel.text = NSLocalizedString("NEW", comment: "")
  46. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  47. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  48. themeDidChange()
  49. }
  50. @objc fileprivate func themeDidChange() {
  51. backgroundColor = PresentationTheme.current.colors.background
  52. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  53. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  54. }
  55. func update(movie: VLCMLMedia) {
  56. var title = movie.title
  57. if UserDefaults.standard.bool(forKey: kVLCOptimizeItemNamesForDisplay) == true {
  58. title = (title as NSString).deletingPathExtension
  59. }
  60. titleLabel.text = title
  61. accessibilityLabel = movie.accessibilityText(editing: false)
  62. descriptionLabel.text = movie.mediaDuration()
  63. thumbnailView.image = movie.thumbnailImage()
  64. let progress = movie.progress
  65. progressView.isHidden = progress == 0
  66. progressView.progress = progress
  67. newLabel.isHidden = !movie.isNew
  68. }
  69. func update(playlist: VLCMLPlaylist) {
  70. collectionOverlay.isHidden = false
  71. numberOfTracks.text = String(playlist.media?.count ?? 0)
  72. titleLabel.text = playlist.name
  73. accessibilityLabel = playlist.accessibilityText()
  74. descriptionLabel.text = playlist.numberOfTracksString()
  75. thumbnailView.image = playlist.thumbnail()
  76. }
  77. override class func numberOfColumns(for width: CGFloat) -> CGFloat {
  78. if width <= DeviceWidth.iPhonePortrait.rawValue {
  79. return 2
  80. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  81. return 3
  82. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  83. return 4
  84. } else {
  85. return 5
  86. }
  87. }
  88. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  89. let numberOfCells: CGFloat = numberOfColumns(for: width)
  90. let aspectRatio: CGFloat = 10.0 / 16.0
  91. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  92. //
  93. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  94. //
  95. let overallWidth = width - (2 * edgePadding)
  96. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  97. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  98. // 17 * 2 for title, 14 for new + duration, 3 * 4 paddings for lines
  99. return CGSize(width: cellWidth, height: cellWidth * aspectRatio + (17 * 2) + 14 + (3 * 4))
  100. }
  101. override func prepareForReuse() {
  102. super.prepareForReuse()
  103. titleLabel.text = ""
  104. descriptionLabel.text = ""
  105. thumbnailView.image = nil
  106. progressView.isHidden = true
  107. newLabel.isHidden = true
  108. collectionOverlay.isHidden = true
  109. numberOfTracks.text = ""
  110. }
  111. }