MediaCollectionViewCell.swift 5.1 KB

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