MediaCollectionViewCell.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 if let genre = media as? VLCMLGenre {
  31. update(genre: genre)
  32. } else {
  33. fatalError("needs to be of Type VLCMLMedia or VLCMLAlbum")
  34. }
  35. }
  36. }
  37. override func awakeFromNib() {
  38. super.awakeFromNib()
  39. newLabel.text = NSLocalizedString("NEW", comment: "")
  40. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  41. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  42. themeDidChange()
  43. }
  44. @objc fileprivate func themeDidChange() {
  45. backgroundColor = PresentationTheme.current.colors.background
  46. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  47. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  48. }
  49. func update(audiotrack: VLCMLMedia) {
  50. thumbnailView.layer.masksToBounds = true
  51. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  52. titleLabel.text = audiotrack.title
  53. descriptionLabel.text = audiotrack.albumTrack?.artist?.name ?? NSLocalizedString("UNKNOWN_ARTIST", comment: "")
  54. newLabel.isHidden = !audiotrack.isNew
  55. thumbnailView.image = audiotrack.thumbnailImage()
  56. }
  57. func update(album: VLCMLAlbum) {
  58. titleLabel.text = album.title != "" ? album.title : NSLocalizedString("UNKNOWN_TITLE", comment: "")
  59. descriptionLabel.text = album.albumArtist?.name != "" ? album.albumArtist?.name : NSLocalizedString("UNKNOWN_ARTIST", comment: "")
  60. thumbnailView.image = album.thumbnail()
  61. }
  62. func update(artist: VLCMLArtist) {
  63. thumbnailView.layer.masksToBounds = true
  64. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  65. titleLabel.text = artist.name
  66. descriptionLabel.text = artist.numberOfTracksString()
  67. thumbnailView.image = artist.thumbnail()
  68. }
  69. func update(movie: VLCMLMedia) {
  70. titleLabel.text = movie.title
  71. descriptionLabel.text = movie.mediaDuration()
  72. if movie.isThumbnailGenerated() {
  73. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail.path)
  74. }
  75. newLabel.isHidden = !movie.isNew
  76. }
  77. func update(playlist: VLCMLPlaylist) {
  78. newLabel.isHidden = true
  79. titleLabel.text = playlist.name
  80. descriptionLabel.text = playlist.numberOfTracksString()
  81. thumbnailView.image = playlist.thumbnail()
  82. }
  83. func update(genre: VLCMLGenre) {
  84. newLabel.isHidden = true
  85. titleLabel.text = genre.name
  86. thumbnailView.image = genre.thumbnail()
  87. descriptionLabel.text = genre.numberOfTracksString()
  88. }
  89. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  90. let numberOfCells: CGFloat
  91. if width <= DeviceWidth.iPhonePortrait.rawValue {
  92. numberOfCells = 1
  93. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  94. numberOfCells = 2
  95. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  96. numberOfCells = 3
  97. } else {
  98. numberOfCells = 4
  99. }
  100. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  101. //
  102. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  103. //
  104. let overallWidth = width - (2 * edgePadding)
  105. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  106. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  107. return CGSize(width: cellWidth, height: 80)
  108. }
  109. override func prepareForReuse() {
  110. super.prepareForReuse()
  111. titleLabel.text = ""
  112. descriptionLabel.text = ""
  113. thumbnailView.image = nil
  114. newLabel.isHidden = true
  115. }
  116. }