MovieCollectionViewCell.swift 4.5 KB

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