MediaEditCell.swift 6.2 KB

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