MovieCollectionViewCell.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. thumbnailView.image = movie.thumbnailImage()
  54. let progress = movie.progress
  55. progressView.isHidden = progress == 0
  56. progressView.progress = progress
  57. newLabel.isHidden = !movie.isNew
  58. }
  59. func update(playlist: VLCMLPlaylist) {
  60. collectionOverlay.isHidden = false
  61. numberOfTracks.text = String(playlist.media?.count ?? 0)
  62. titleLabel.text = playlist.name
  63. descriptionLabel.text = playlist.numberOfTracksString()
  64. thumbnailView.image = playlist.thumbnail()
  65. }
  66. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  67. let numberOfCells: CGFloat
  68. if width <= DeviceWidth.iPhonePortrait.rawValue {
  69. numberOfCells = 2
  70. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  71. numberOfCells = 3
  72. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  73. numberOfCells = 4
  74. } else {
  75. numberOfCells = 5
  76. }
  77. let aspectRatio: CGFloat = 10.0 / 16.0
  78. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  79. //
  80. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  81. //
  82. let overallWidth = width - (2 * edgePadding)
  83. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  84. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  85. // 3*20 for the labels + 24 for 3*8 which is the padding
  86. return CGSize(width: cellWidth, height: cellWidth * aspectRatio + 3*20+24)
  87. }
  88. override func prepareForReuse() {
  89. super.prepareForReuse()
  90. titleLabel.text = ""
  91. descriptionLabel.text = ""
  92. thumbnailView.image = nil
  93. progressView.isHidden = true
  94. newLabel.isHidden = true
  95. collectionOverlay.isHidden = true
  96. numberOfTracks.text = ""
  97. }
  98. }