MediaEditCell.swift 5.7 KB

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