MediaEditCell.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if movie.isThumbnailGenerated() {
  60. thumbnailImageView.image = UIImage(contentsOfFile: movie.thumbnail.absoluteString)
  61. }
  62. }
  63. func updateForAudio(audio: VLCMLMedia) {
  64. titleLabel.text = audio.title
  65. timeLabel.text = audio.mediaDuration()
  66. sizeLabel.text = audio.formatSize()
  67. if audio.isThumbnailGenerated() {
  68. thumbnailImageView.image = UIImage(contentsOfFile: audio.thumbnail.absoluteString)
  69. }
  70. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  71. }
  72. func updateForArtist(artist: VLCMLArtist) {
  73. //TODO: add size, number of tracks, thumbnail
  74. titleLabel.text = artist.name
  75. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  76. }
  77. func updateForAlbum(album: VLCMLAlbum) {
  78. titleLabel.text = album.title
  79. timeLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
  80. //TODO: add size, number of tracks, thumbnail
  81. }
  82. func updateForGenre(genre: VLCMLGenre) {
  83. titleLabel.text = genre.name
  84. timeLabel.text = genre.numberOfTracksString()
  85. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  86. //TODO: add thumbnail
  87. }
  88. func updateForPlaylist(playlist: VLCMLPlaylist) {
  89. thumbnailImageView.layer.cornerRadius = 3
  90. AudioAspectRatio.isActive = false
  91. VideoAspectRatio.isActive = true
  92. titleLabel.text = playlist.name
  93. timeLabel.text = playlist.numberOfTracksString()
  94. //TODO: add thumbnail
  95. }
  96. var isChecked: Bool = false {
  97. didSet {
  98. checkboxImageView.image = isChecked ? UIImage(named: "checkboxSelected") : UIImage(named: "checkboxEmpty")
  99. }
  100. }
  101. override func prepareForReuse() {
  102. super.prepareForReuse()
  103. titleLabel.text = ""
  104. timeLabel.text = ""
  105. sizeLabel.text = ""
  106. thumbnailImageView.image = nil
  107. isChecked = false
  108. thumbnailImageView.layer.cornerRadius = 0
  109. AudioAspectRatio.isActive = true
  110. VideoAspectRatio.isActive = false
  111. }
  112. }