MediaEditCell.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 {
  39. assertionFailure("MediaEditCell: media: Needs to be of a supported Type.")
  40. }
  41. }
  42. }
  43. override func awakeFromNib() {
  44. super.awakeFromNib()
  45. if #available(iOS 11.0, *) {
  46. thumbnailImageView.accessibilityIgnoresInvertColors = true
  47. }
  48. thumbnailImageView.clipsToBounds = true
  49. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  50. themeDidChange()
  51. }
  52. @objc fileprivate func themeDidChange() {
  53. backgroundColor = PresentationTheme.current.colors.background
  54. titleLabel.textColor = PresentationTheme.current.colors.cellTextColor
  55. timeLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor
  56. sizeLabel.textColor = PresentationTheme.current.colors.cellTextColor
  57. dragImage.tintColor = PresentationTheme.current.colors.cellDetailTextColor
  58. }
  59. func updateForMovie(movie: VLCMLMedia) {
  60. thumbnailImageView.layer.cornerRadius = 3
  61. AudioAspectRatio.isActive = false
  62. VideoAspectRatio.isActive = true
  63. titleLabel.text = movie.title
  64. accessibilityLabel = movie.accessibilityText(editing: true)
  65. timeLabel.text = movie.mediaDuration()
  66. sizeLabel.text = movie.formatSize()
  67. thumbnailImageView.image = movie.thumbnailImage()
  68. }
  69. func updateForAudio(audio: VLCMLMedia) {
  70. titleLabel.text = audio.title
  71. accessibilityLabel = audio.accessibilityText(editing: true)
  72. timeLabel.text = audio.mediaDuration()
  73. sizeLabel.text = audio.formatSize()
  74. thumbnailImageView.image = audio.thumbnailImage()
  75. thumbnailImageView.layer.masksToBounds = true
  76. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  77. }
  78. func updateForArtist(artist: VLCMLArtist) {
  79. titleLabel.text = artist.artistName()
  80. accessibilityLabel = artist.accessibilityText()
  81. timeLabel.text = artist.numberOfTracksString()
  82. thumbnailImageView.layer.masksToBounds = true
  83. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  84. thumbnailImageView.image = artist.thumbnail()
  85. }
  86. func updateForAlbum(album: VLCMLAlbum) {
  87. titleLabel.text = album.albumName()
  88. accessibilityLabel = album.accessibilityText(editing: true)
  89. timeLabel.text = album.albumArtistName()
  90. sizeLabel.text = album.numberOfTracksString()
  91. thumbnailImageView.image = album.thumbnail()
  92. }
  93. func updateForGenre(genre: VLCMLGenre) {
  94. titleLabel.text = genre.name
  95. accessibilityLabel = genre.accessibilityText()
  96. timeLabel.text = genre.numberOfTracksString()
  97. thumbnailImageView.layer.masksToBounds = true
  98. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  99. thumbnailImageView.image = genre.thumbnail()
  100. }
  101. func updateForPlaylist(playlist: VLCMLPlaylist) {
  102. thumbnailImageView.layer.cornerRadius = 3
  103. AudioAspectRatio.isActive = false
  104. VideoAspectRatio.isActive = true
  105. titleLabel.text = playlist.name
  106. accessibilityLabel = playlist.accessibilityText()
  107. timeLabel.text = playlist.numberOfTracksString()
  108. thumbnailImageView.image = playlist.thumbnail()
  109. }
  110. var isChecked: Bool = false {
  111. didSet {
  112. checkboxImageView.image = isChecked ? UIImage(named: "checkboxSelected") : UIImage(named: "checkboxEmpty")
  113. }
  114. }
  115. override class func numberOfColumns(for width: CGFloat) -> CGFloat {
  116. if width <= DeviceWidth.iPhonePortrait.rawValue {
  117. return 1
  118. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  119. return 2
  120. } else {
  121. return 3
  122. }
  123. }
  124. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  125. let numberOfCells: CGFloat = numberOfColumns(for: width)
  126. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  127. //
  128. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  129. //
  130. let overallWidth = width - (2 * edgePadding)
  131. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  132. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  133. return CGSize(width: cellWidth, height: height)
  134. }
  135. override func prepareForReuse() {
  136. super.prepareForReuse()
  137. titleLabel.text = ""
  138. timeLabel.text = ""
  139. sizeLabel.text = ""
  140. accessibilityLabel = ""
  141. thumbnailImageView.image = nil
  142. isChecked = false
  143. thumbnailImageView.layer.cornerRadius = 0
  144. AudioAspectRatio.isActive = true
  145. VideoAspectRatio.isActive = false
  146. }
  147. }