MediaEditCell.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*****************************************************************************
  2. * VLCMediaViewEditCell.swift
  3. *
  4. * Copyright © 2018 VLC authors and VideoLAN
  5. * Copyright © 2018 Videolabs
  6. *
  7. * Authors: Soomin Lee <bubu@mikan.io>
  8. * Carola Nitz <nitz.carola@googlemail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. class MediaEditCell: BaseCollectionViewCell {
  13. static let height: CGFloat = 88
  14. @IBOutlet weak var checkboxImageView: UIImageView!
  15. @IBOutlet weak var thumbnailImageView: UIImageView!
  16. @IBOutlet weak var titleLabel: UILabel!
  17. @IBOutlet weak var timeLabel: UILabel!
  18. @IBOutlet weak var sizeLabel: UILabel!
  19. @IBOutlet weak var dragImage: UIImageView!
  20. @IBOutlet weak var VideoAspectRatio: NSLayoutConstraint!
  21. @IBOutlet weak var AudioAspectRatio: NSLayoutConstraint!
  22. override var media: VLCMLObject? {
  23. didSet {
  24. if let media = media as? VLCMLMedia {
  25. if media.type() == .audio {
  26. updateForAudio(audio: media)
  27. } else {
  28. updateForMovie(movie: media)
  29. }
  30. } else if let artist = media as? VLCMLArtist {
  31. updateForArtist(artist: artist)
  32. } else if let album = media as? VLCMLAlbum {
  33. updateForAlbum(album: album)
  34. } else if let genre = media as? VLCMLGenre {
  35. updateForGenre(genre: genre)
  36. } else if let playlist = media as? VLCMLPlaylist {
  37. updateForPlaylist(playlist: playlist)
  38. } else if let videoGroup = media as? VLCMLVideoGroup {
  39. update(videoGroup: videoGroup)
  40. } else {
  41. assertionFailure("MediaEditCell: media: Needs to be of a supported Type.")
  42. }
  43. }
  44. }
  45. override func awakeFromNib() {
  46. super.awakeFromNib()
  47. if #available(iOS 11.0, *) {
  48. thumbnailImageView.accessibilityIgnoresInvertColors = true
  49. }
  50. thumbnailImageView.clipsToBounds = true
  51. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  52. themeDidChange()
  53. }
  54. @objc fileprivate func themeDidChange() {
  55. backgroundColor = PresentationTheme.current.colors.background
  56. titleLabel.textColor = PresentationTheme.current.colors.cellTextColor
  57. timeLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor
  58. sizeLabel.textColor = PresentationTheme.current.colors.cellTextColor
  59. dragImage.tintColor = PresentationTheme.current.colors.cellDetailTextColor
  60. }
  61. func updateForMovie(movie: VLCMLMedia) {
  62. thumbnailImageView.layer.cornerRadius = 3
  63. AudioAspectRatio.isActive = false
  64. VideoAspectRatio.isActive = true
  65. titleLabel.text = movie.title
  66. accessibilityLabel = movie.accessibilityText(editing: true)
  67. timeLabel.text = movie.mediaDuration()
  68. sizeLabel.text = movie.formatSize()
  69. thumbnailImageView.image = movie.thumbnailImage()
  70. }
  71. func updateForAudio(audio: VLCMLMedia) {
  72. titleLabel.text = audio.title
  73. accessibilityLabel = audio.accessibilityText(editing: true)
  74. timeLabel.text = audio.mediaDuration()
  75. sizeLabel.text = audio.formatSize()
  76. thumbnailImageView.image = audio.thumbnailImage()
  77. thumbnailImageView.layer.masksToBounds = true
  78. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  79. }
  80. func updateForArtist(artist: VLCMLArtist) {
  81. titleLabel.text = artist.artistName()
  82. accessibilityLabel = artist.accessibilityText()
  83. timeLabel.text = artist.numberOfTracksString()
  84. thumbnailImageView.layer.masksToBounds = true
  85. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  86. thumbnailImageView.image = artist.thumbnail()
  87. }
  88. func updateForAlbum(album: VLCMLAlbum) {
  89. titleLabel.text = album.albumName()
  90. accessibilityLabel = album.accessibilityText(editing: true)
  91. timeLabel.text = album.albumArtistName()
  92. sizeLabel.text = album.numberOfTracksString()
  93. thumbnailImageView.image = album.thumbnail()
  94. }
  95. func updateForGenre(genre: VLCMLGenre) {
  96. titleLabel.text = genre.name
  97. accessibilityLabel = genre.accessibilityText()
  98. timeLabel.text = genre.numberOfTracksString()
  99. thumbnailImageView.layer.masksToBounds = true
  100. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  101. thumbnailImageView.image = genre.thumbnail()
  102. }
  103. func updateForPlaylist(playlist: VLCMLPlaylist) {
  104. thumbnailImageView.layer.cornerRadius = 3
  105. AudioAspectRatio.isActive = false
  106. VideoAspectRatio.isActive = true
  107. titleLabel.text = playlist.name
  108. accessibilityLabel = playlist.accessibilityText()
  109. timeLabel.text = playlist.numberOfTracksString()
  110. thumbnailImageView.image = playlist.thumbnail()
  111. }
  112. func update(videoGroup: VLCMLVideoGroup) {
  113. thumbnailImageView.layer.cornerRadius = 3
  114. AudioAspectRatio.isActive = false
  115. VideoAspectRatio.isActive = true
  116. titleLabel.text = videoGroup.name()
  117. accessibilityLabel = videoGroup.accessibilityText()
  118. timeLabel.text = videoGroup.numberOfTracksString()
  119. thumbnailImageView.image = videoGroup.thumbnail()
  120. }
  121. var isChecked: Bool = false {
  122. didSet {
  123. checkboxImageView.image = isChecked ? UIImage(named: "checkboxSelected") : UIImage(named: "checkboxEmpty")
  124. }
  125. }
  126. override class func numberOfColumns(for width: CGFloat) -> CGFloat {
  127. if width <= DeviceWidth.iPhonePortrait.rawValue {
  128. return 1
  129. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  130. return 2
  131. } else {
  132. return 3
  133. }
  134. }
  135. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  136. let numberOfCells: CGFloat = numberOfColumns(for: width)
  137. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  138. //
  139. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  140. //
  141. let overallWidth = width - (2 * edgePadding)
  142. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  143. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  144. return CGSize(width: cellWidth, height: height)
  145. }
  146. override func prepareForReuse() {
  147. super.prepareForReuse()
  148. titleLabel.text = ""
  149. timeLabel.text = ""
  150. sizeLabel.text = ""
  151. accessibilityLabel = ""
  152. thumbnailImageView.image = nil
  153. isChecked = false
  154. thumbnailImageView.layer.cornerRadius = 0
  155. AudioAspectRatio.isActive = true
  156. VideoAspectRatio.isActive = false
  157. }
  158. }