MovieCollectionViewCell.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. override class var cellPadding: CGFloat {
  20. return 5.0
  21. }
  22. override var media: VLCMLObject? {
  23. didSet {
  24. guard let movie = media as? VLCMLMedia else {
  25. //Todo: fatalerror here once all celltypes are there and this is not abused for others anymore
  26. return
  27. }
  28. update(movie:movie)
  29. }
  30. }
  31. override func awakeFromNib() {
  32. super.awakeFromNib()
  33. newLabel.text = NSLocalizedString("NEW", comment: "")
  34. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  35. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  36. themeDidChange()
  37. }
  38. @objc fileprivate func themeDidChange() {
  39. backgroundColor = PresentationTheme.current.colors.background
  40. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  41. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  42. }
  43. func update(movie: VLCMLMedia) {
  44. titleLabel.text = movie.title
  45. descriptionLabel.text = movie.mediaDuration()
  46. if movie.isThumbnailGenerated() {
  47. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail.absoluteString)
  48. }
  49. let progress = movie.mediaProgress()
  50. progressView.isHidden = progress == 0
  51. progressView.progress = progress
  52. newLabel.isHidden = !movie.isNew()
  53. }
  54. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  55. let numberOfCells: CGFloat = round(width / 250)
  56. let aspectRatio: CGFloat = 10.0 / 16.0
  57. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  58. // 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
  59. // since this might be an uneven number we ceil
  60. var cellWidth = width / numberOfCells
  61. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  62. // 3*20 for the labels + 24 for 3*8 which is the padding
  63. return CGSize(width: cellWidth, height: cellWidth * aspectRatio + 3*20+24)
  64. }
  65. override func prepareForReuse() {
  66. super.prepareForReuse()
  67. titleLabel.text = ""
  68. descriptionLabel.text = ""
  69. thumbnailView.image = nil
  70. progressView.isHidden = true
  71. newLabel.isHidden = true
  72. }
  73. }