MediaCollectionViewCell.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ?? ""
  54. if audiotrack.isThumbnailGenerated() {
  55. thumbnailView.image = UIImage(contentsOfFile: audiotrack.thumbnail.path)
  56. }
  57. newLabel.isHidden = !audiotrack.isNew
  58. }
  59. func update(album: VLCMLAlbum) {
  60. titleLabel.text = album.title
  61. descriptionLabel.text = album.albumArtist?.name ?? ""
  62. }
  63. func update(artist: VLCMLArtist) {
  64. thumbnailView.layer.masksToBounds = true
  65. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  66. titleLabel.text = artist.name
  67. descriptionLabel.text = artist.numberOfTracksString()
  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 = UIImage(contentsOfFile: playlist.artworkMrl())
  82. }
  83. func update(genre: VLCMLGenre) {
  84. newLabel.isHidden = true
  85. titleLabel.text = genre.name
  86. for track in genre.tracks() ?? [] {
  87. if track.isThumbnailGenerated() {
  88. thumbnailView.image = UIImage(contentsOfFile: track.thumbnail.path)
  89. break
  90. }
  91. }
  92. descriptionLabel.text = genre.numberOfTracksString()
  93. }
  94. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  95. let numberOfCells: CGFloat
  96. if width <= DeviceWidth.iPhonePortrait.rawValue {
  97. numberOfCells = 1
  98. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  99. numberOfCells = 2
  100. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  101. numberOfCells = 3
  102. } else {
  103. numberOfCells = 4
  104. }
  105. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  106. //
  107. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  108. //
  109. let overallWidth = width - (2 * edgePadding)
  110. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  111. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  112. return CGSize(width: cellWidth, height: 80)
  113. }
  114. override func prepareForReuse() {
  115. super.prepareForReuse()
  116. titleLabel.text = ""
  117. descriptionLabel.text = ""
  118. thumbnailView.image = nil
  119. newLabel.isHidden = true
  120. }
  121. }