MediaCollectionViewCell.swift 6.6 KB

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