MediaEditCell.swift 3.7 KB

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