MovieCollectionViewCell.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. newLabel.text = NSLocalizedString("NEW", comment: "")
  41. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  42. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  43. themeDidChange()
  44. }
  45. @objc fileprivate func themeDidChange() {
  46. backgroundColor = PresentationTheme.current.colors.background
  47. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  48. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  49. }
  50. func update(movie: VLCMLMedia) {
  51. titleLabel.text = movie.title
  52. descriptionLabel.text = movie.mediaDuration()
  53. if movie.isThumbnailGenerated() {
  54. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail.absoluteString)
  55. }
  56. let progress = movie.mediaProgress()
  57. progressView.isHidden = progress == 0
  58. progressView.progress = progress
  59. newLabel.isHidden = !movie.isNew()
  60. }
  61. func update(playlist: VLCMLPlaylist) {
  62. collectionOverlay.isHidden = false
  63. numberOfTracks.text = String(playlist.media.count)
  64. titleLabel.text = playlist.name
  65. descriptionLabel.text = playlist.description()
  66. }
  67. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  68. let numberOfCells: CGFloat
  69. if width <= DeviceWidth.iPhonePortrait.rawValue {
  70. numberOfCells = 2
  71. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  72. numberOfCells = 3
  73. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  74. numberOfCells = 4
  75. } else {
  76. numberOfCells = 5
  77. }
  78. let aspectRatio: CGFloat = 10.0 / 16.0
  79. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  80. //
  81. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  82. //
  83. let overallWidth = width - (2 * edgePadding)
  84. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  85. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  86. // 3*20 for the labels + 24 for 3*8 which is the padding
  87. return CGSize(width: cellWidth, height: cellWidth * aspectRatio + 3*20+24)
  88. }
  89. override func prepareForReuse() {
  90. super.prepareForReuse()
  91. titleLabel.text = ""
  92. descriptionLabel.text = ""
  93. thumbnailView.image = nil
  94. progressView.isHidden = true
  95. newLabel.isHidden = true
  96. collectionOverlay.isHidden = true
  97. numberOfTracks.text = ""
  98. }
  99. }