MovieCollectionViewCell.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 cellPadding: CGFloat {
  22. return 5.0
  23. }
  24. override var media: VLCMLObject? {
  25. didSet {
  26. if let movie = media as? VLCMLMedia {
  27. update(movie:movie)
  28. } else if let playlist = media as? VLCMLPlaylist {
  29. update(playlist:playlist)
  30. } else {
  31. fatalError("wrong object")
  32. }
  33. }
  34. }
  35. override func awakeFromNib() {
  36. super.awakeFromNib()
  37. newLabel.text = NSLocalizedString("NEW", comment: "")
  38. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  39. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  40. themeDidChange()
  41. }
  42. @objc fileprivate func themeDidChange() {
  43. backgroundColor = PresentationTheme.current.colors.background
  44. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  45. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  46. }
  47. func update(movie: VLCMLMedia) {
  48. titleLabel.text = movie.title
  49. descriptionLabel.text = movie.mediaDuration()
  50. if movie.isThumbnailGenerated() {
  51. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail.absoluteString)
  52. }
  53. let progress = movie.mediaProgress()
  54. progressView.isHidden = progress == 0
  55. progressView.progress = progress
  56. newLabel.isHidden = !movie.isNew()
  57. }
  58. func update(playlist: VLCMLPlaylist) {
  59. collectionOverlay.isHidden = false
  60. numberOfTracks.text = String(playlist.media.count)
  61. titleLabel.text = playlist.name
  62. descriptionLabel.text = playlist.description()
  63. }
  64. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  65. let numberOfCells: CGFloat = round(width / 250)
  66. let aspectRatio: CGFloat = 10.0 / 16.0
  67. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  68. // we then have the entire padding, we divide the entire padding by the number of Cells to know how much needs to be substracted from ech cell
  69. // since this might be an uneven number we ceil
  70. var cellWidth = width / numberOfCells
  71. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  72. // 3*20 for the labels + 24 for 3*8 which is the padding
  73. return CGSize(width: cellWidth, height: cellWidth * aspectRatio + 3*20+24)
  74. }
  75. override func prepareForReuse() {
  76. super.prepareForReuse()
  77. titleLabel.text = ""
  78. descriptionLabel.text = ""
  79. thumbnailView.image = nil
  80. progressView.isHidden = true
  81. newLabel.isHidden = true
  82. collectionOverlay.isHidden = true
  83. numberOfTracks.text = ""
  84. }
  85. }