MediaCollectionViewCell.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.cornerRadius = thumbnailView.frame.size.width / 2.0
  49. titleLabel.text = audiotrack.title
  50. descriptionLabel.text = audiotrack.albumTrack.artist.name
  51. if audiotrack.isThumbnailGenerated() {
  52. thumbnailView.image = UIImage(contentsOfFile: audiotrack.thumbnail.path)
  53. }
  54. newLabel.isHidden = !audiotrack.isNew()
  55. }
  56. func update(album: VLCMLAlbum) {
  57. titleLabel.text = album.title
  58. descriptionLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
  59. }
  60. func update(artist: VLCMLArtist) {
  61. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  62. titleLabel.text = artist.name
  63. descriptionLabel.text = artist.numberOfTracksString()
  64. }
  65. func update(movie: VLCMLMedia) {
  66. titleLabel.text = movie.title
  67. descriptionLabel.text = movie.mediaDuration()
  68. if movie.isThumbnailGenerated() {
  69. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail.path)
  70. }
  71. newLabel.isHidden = !movie.isNew()
  72. }
  73. func update(playlist: VLCMLPlaylist) {
  74. newLabel.isHidden = true
  75. titleLabel.text = playlist.name
  76. descriptionLabel.text = playlist.numberOfTracksString()
  77. thumbnailView.image = UIImage(contentsOfFile: playlist.artworkMrl())
  78. }
  79. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  80. let numberOfCells: CGFloat
  81. if width <= DeviceWidth.iPhonePortrait.rawValue {
  82. numberOfCells = 1
  83. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  84. numberOfCells = 2
  85. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  86. numberOfCells = 3
  87. } else {
  88. numberOfCells = 4
  89. }
  90. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  91. //
  92. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  93. //
  94. let overallWidth = width - (2 * edgePadding)
  95. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  96. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  97. return CGSize(width: cellWidth, height: 80)
  98. }
  99. override func prepareForReuse() {
  100. super.prepareForReuse()
  101. titleLabel.text = ""
  102. descriptionLabel.text = ""
  103. thumbnailView.image = nil
  104. newLabel.isHidden = true
  105. }
  106. }