MediaCollectionViewCell.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*****************************************************************************
  2. * MediaCollectionViewCell.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 MediaCollectionViewCell: BaseCollectionViewCell {
  14. @IBOutlet weak var thumbnailView: UIImageView!
  15. @IBOutlet weak var titleLabel: UILabel!
  16. @IBOutlet weak var descriptionLabel: UILabel!
  17. @IBOutlet weak var newLabel: UILabel!
  18. override var media: VLCMLObject? {
  19. didSet {
  20. if let albumTrack = media as? VLCMLMedia, albumTrack.subtype() == .albumTrack {
  21. update(audiotrack:albumTrack)
  22. } else if let album = media as? VLCMLAlbum {
  23. update(album:album)
  24. } else if let artist = media as? VLCMLArtist {
  25. update(artist:artist)
  26. } else if let movie = media as? VLCMLMedia, movie.subtype() == .unknown {
  27. update(movie:movie)
  28. } else if let playlist = media as? VLCMLPlaylist {
  29. update(playlist: playlist)
  30. } else {
  31. fatalError("needs to be of Type VLCMLMedia or VLCMLAlbum")
  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(audiotrack: VLCMLMedia) {
  48. thumbnailView.layer.masksToBounds = true
  49. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  50. titleLabel.text = audiotrack.title
  51. descriptionLabel.text = audiotrack.albumTrack.artist.name
  52. if audiotrack.isThumbnailGenerated() {
  53. thumbnailView.image = UIImage(contentsOfFile: audiotrack.thumbnail.path)
  54. }
  55. newLabel.isHidden = !audiotrack.isNew()
  56. }
  57. func update(album: VLCMLAlbum) {
  58. titleLabel.text = album.title
  59. descriptionLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
  60. }
  61. func update(artist: VLCMLArtist) {
  62. thumbnailView.layer.masksToBounds = true
  63. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  64. titleLabel.text = artist.name
  65. descriptionLabel.text = artist.numberOfTracksString()
  66. }
  67. func update(movie: VLCMLMedia) {
  68. titleLabel.text = movie.title
  69. descriptionLabel.text = movie.mediaDuration()
  70. if movie.isThumbnailGenerated() {
  71. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail.path)
  72. }
  73. newLabel.isHidden = !movie.isNew()
  74. }
  75. func update(playlist: VLCMLPlaylist) {
  76. newLabel.isHidden = true
  77. titleLabel.text = playlist.name
  78. descriptionLabel.text = playlist.numberOfTracksString()
  79. thumbnailView.image = UIImage(contentsOfFile: playlist.artworkMrl())
  80. }
  81. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  82. let numberOfCells: CGFloat
  83. if width <= DeviceWidth.iPhonePortrait.rawValue {
  84. numberOfCells = 1
  85. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  86. numberOfCells = 2
  87. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  88. numberOfCells = 3
  89. } else {
  90. numberOfCells = 4
  91. }
  92. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  93. //
  94. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  95. //
  96. let overallWidth = width - (2 * edgePadding)
  97. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  98. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  99. return CGSize(width: cellWidth, height: 80)
  100. }
  101. override func prepareForReuse() {
  102. super.prepareForReuse()
  103. titleLabel.text = ""
  104. descriptionLabel.text = ""
  105. thumbnailView.image = nil
  106. newLabel.isHidden = true
  107. }
  108. }