MediaEditCell.swift 6.3 KB

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