MediaEditCell.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 media = media as? VLCMLMedia {
  24. updateForMedia(media: media)
  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 {
  34. fatalError("needs to be of a supported Type")
  35. }
  36. }
  37. }
  38. override func awakeFromNib() {
  39. super.awakeFromNib()
  40. thumbnailImageView.clipsToBounds = true
  41. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  42. themeDidChange()
  43. }
  44. @objc fileprivate func themeDidChange() {
  45. backgroundColor = PresentationTheme.current.colors.background
  46. titleLabel.textColor = PresentationTheme.current.colors.cellTextColor
  47. timeLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor
  48. sizeLabel.textColor = PresentationTheme.current.colors.cellTextColor
  49. }
  50. func updateForMedia(media: VLCMLMedia) {
  51. thumbnailImageView.layer.cornerRadius = 3
  52. AudioAspectRatio.isActive = false
  53. VideoAspectRatio.isActive = true
  54. titleLabel.text = media.title
  55. timeLabel.text = media.mediaDuration()
  56. sizeLabel.text = media.formatSize()
  57. if media.isThumbnailGenerated() {
  58. thumbnailImageView.image = UIImage(contentsOfFile: media.thumbnail.absoluteString)
  59. }
  60. }
  61. func updateForArtist(artist: VLCMLArtist) {
  62. //TODO: add size, number of tracks, thumbnail
  63. titleLabel.text = artist.name
  64. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  65. }
  66. func updateForAlbum(album: VLCMLAlbum) {
  67. titleLabel.text = album.title
  68. timeLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
  69. //TODO: add size, number of tracks, thumbnail
  70. }
  71. func updateForGenre(genre: VLCMLGenre) {
  72. titleLabel.text = genre.name
  73. timeLabel.text = genre.numberOfTracksString()
  74. thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.height / 2
  75. //TODO: add thumbnail
  76. }
  77. func updateForPlaylist(playlist: VLCMLPlaylist) {
  78. thumbnailImageView.layer.cornerRadius = 3
  79. AudioAspectRatio.isActive = false
  80. VideoAspectRatio.isActive = true
  81. titleLabel.text = playlist.name
  82. timeLabel.text = playlist.numberOfTracksString()
  83. //TODO: add thumbnail
  84. }
  85. var isChecked: Bool = false {
  86. didSet {
  87. checkboxImageView.image = isChecked ? UIImage(named: "checkboxSelected") : UIImage(named: "checkboxEmpty")
  88. }
  89. }
  90. override func prepareForReuse() {
  91. super.prepareForReuse()
  92. titleLabel.text = ""
  93. timeLabel.text = ""
  94. sizeLabel.text = ""
  95. thumbnailImageView.image = nil
  96. isChecked = false
  97. thumbnailImageView.layer.cornerRadius = 0
  98. AudioAspectRatio.isActive = true
  99. VideoAspectRatio.isActive = false
  100. }
  101. }