MediaCollectionViewCell.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 private weak var thumbnailView: UIImageView!
  15. @IBOutlet private weak var titleLabel: UILabel!
  16. @IBOutlet private weak var descriptionLabel: UILabel!
  17. @IBOutlet private weak var newLabel: UILabel!
  18. @IBOutlet private weak var thumbnailWidth: NSLayoutConstraint!
  19. override var media: VLCMLObject? {
  20. didSet {
  21. if let albumTrack = media as? VLCMLMedia, albumTrack.subtype() == .albumTrack {
  22. update(audiotrack:albumTrack)
  23. } else if let album = media as? VLCMLAlbum {
  24. update(album:album)
  25. } else if let artist = media as? VLCMLArtist {
  26. update(artist:artist)
  27. } else if let movie = media as? VLCMLMedia, movie.subtype() == .unknown {
  28. update(movie:movie)
  29. } else if let playlist = media as? VLCMLPlaylist {
  30. update(playlist: playlist)
  31. } else if let genre = media as? VLCMLGenre {
  32. update(genre: genre)
  33. } else {
  34. fatalError("needs to be of Type VLCMLMedia or VLCMLAlbum")
  35. }
  36. }
  37. }
  38. override func awakeFromNib() {
  39. super.awakeFromNib()
  40. if #available(iOS 11.0, *) {
  41. thumbnailView.accessibilityIgnoresInvertColors = true
  42. }
  43. newLabel.text = NSLocalizedString("NEW", comment: "")
  44. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  45. let isIpad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
  46. thumbnailWidth.constant = isIpad ? 72 : 56
  47. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  48. themeDidChange()
  49. }
  50. @objc fileprivate func themeDidChange() {
  51. backgroundColor = PresentationTheme.current.colors.background
  52. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  53. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  54. }
  55. func update(audiotrack: VLCMLMedia) {
  56. var title = audiotrack.title
  57. if UserDefaults.standard.bool(forKey: kVLCOptimizeItemNamesForDisplay) == true {
  58. title = (title as NSString).deletingPathExtension
  59. }
  60. titleLabel.text = title
  61. accessibilityLabel = audiotrack.accessibilityText(editing: false)
  62. descriptionLabel.text = audiotrack.albumTrackArtistName()
  63. newLabel.isHidden = !audiotrack.isNew
  64. thumbnailView.image = audiotrack.thumbnailImage()
  65. }
  66. func update(album: VLCMLAlbum) {
  67. titleLabel.text = album.albumName()
  68. accessibilityLabel = album.accessibilityText(editing: false)
  69. descriptionLabel.text = album.albumArtistName()
  70. thumbnailView.image = album.thumbnail()
  71. }
  72. func update(artist: VLCMLArtist) {
  73. thumbnailView.layer.masksToBounds = true
  74. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  75. titleLabel.text = artist.artistName()
  76. accessibilityLabel = artist.accessibilityText()
  77. descriptionLabel.text = artist.numberOfTracksString()
  78. thumbnailView.image = artist.thumbnail()
  79. }
  80. func update(movie: VLCMLMedia) {
  81. var title = movie.title
  82. if UserDefaults.standard.bool(forKey: kVLCOptimizeItemNamesForDisplay) == true {
  83. title = (title as NSString).deletingPathExtension
  84. }
  85. titleLabel.text = title
  86. accessibilityLabel = movie.accessibilityText(editing: false)
  87. descriptionLabel.text = movie.mediaDuration()
  88. if movie.isThumbnailGenerated() {
  89. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail()?.path ?? "")
  90. }
  91. newLabel.isHidden = !movie.isNew
  92. }
  93. func update(playlist: VLCMLPlaylist) {
  94. newLabel.isHidden = true
  95. titleLabel.text = playlist.name
  96. accessibilityLabel = playlist.accessibilityText()
  97. descriptionLabel.text = playlist.numberOfTracksString()
  98. thumbnailView.image = playlist.thumbnail()
  99. }
  100. func update(genre: VLCMLGenre) {
  101. newLabel.isHidden = true
  102. titleLabel.text = genre.name
  103. accessibilityLabel = genre.accessibilityText()
  104. thumbnailView.image = genre.thumbnail()
  105. descriptionLabel.text = genre.numberOfTracksString()
  106. }
  107. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  108. let numberOfCells: CGFloat
  109. if width <= DeviceWidth.iPhonePortrait.rawValue {
  110. numberOfCells = 1
  111. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  112. numberOfCells = 2
  113. } else {
  114. numberOfCells = 3
  115. }
  116. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  117. //
  118. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  119. //
  120. let overallWidth = width - (2 * edgePadding)
  121. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  122. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  123. let isIpad = UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
  124. return CGSize(width: cellWidth, height: isIpad ? 94 : 80)
  125. }
  126. override func prepareForReuse() {
  127. super.prepareForReuse()
  128. titleLabel.text = ""
  129. accessibilityLabel = ""
  130. descriptionLabel.text = ""
  131. thumbnailView.image = nil
  132. newLabel.isHidden = true
  133. }
  134. }