AudioCollectionViewCell.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*****************************************************************************
  2. * AudioCollectionViewCell.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. class AudioCollectionViewCell: BaseCollectionViewCell {
  14. @IBOutlet weak var thumbnailView: UIImageView!
  15. @IBOutlet weak var titleLabel: UILabel!
  16. @IBOutlet weak var descriptionLabel: UILabel!
  17. @IBOutlet weak var newLabel: UILabel!
  18. override var media: VLCMLObject? {
  19. didSet {
  20. if let albumTrack = media as? VLCMLMedia, albumTrack.subtype() == .albumTrack {
  21. update(audiotrack:albumTrack)
  22. } else if let album = media as? VLCMLAlbum {
  23. update(album:album)
  24. } else if let artist = media as? VLCMLArtist {
  25. update(artist:artist)
  26. } else if let movie = media as? VLCMLMedia, movie.subtype() == .unknown {
  27. update(movie:movie)
  28. } else {
  29. fatalError("needs to be of Type VLCMLMedia or VLCMLAlbum")
  30. }
  31. }
  32. }
  33. override func awakeFromNib() {
  34. super.awakeFromNib()
  35. newLabel.text = NSLocalizedString("NEW", comment: "")
  36. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  37. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  38. themeDidChange()
  39. }
  40. @objc fileprivate func themeDidChange() {
  41. backgroundColor = PresentationTheme.current.colors.background
  42. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  43. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  44. }
  45. func update(audiotrack: VLCMLMedia) {
  46. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  47. titleLabel.text = audiotrack.title
  48. descriptionLabel.text = audiotrack.albumTrack.artist.name
  49. if audiotrack.isThumbnailGenerated() {
  50. thumbnailView.image = UIImage(contentsOfFile: audiotrack.thumbnail.absoluteString)
  51. }
  52. newLabel.isHidden = !audiotrack.isNew()
  53. }
  54. func update(album: VLCMLAlbum) {
  55. titleLabel.text = album.title
  56. descriptionLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
  57. }
  58. func update(artist: VLCMLArtist) {
  59. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  60. titleLabel.text = artist.name
  61. descriptionLabel.text = artist.numberOfTracksString()
  62. }
  63. func update(movie: VLCMLMedia) {
  64. titleLabel.text = movie.title
  65. descriptionLabel.text = movie.mediaDuration()
  66. if movie.isThumbnailGenerated() {
  67. thumbnailView.image = UIImage(contentsOfFile: movie.thumbnail.absoluteString)
  68. }
  69. newLabel.isHidden = !movie.isNew()
  70. }
  71. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  72. let numberOfCells: CGFloat
  73. if width <= DeviceWidth.iPhonePortrait.rawValue {
  74. numberOfCells = 1
  75. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  76. numberOfCells = 2
  77. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  78. numberOfCells = 3
  79. } else {
  80. numberOfCells = 4
  81. }
  82. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  83. //
  84. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  85. //
  86. let overallWidth = width - (2 * edgePadding)
  87. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  88. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  89. return CGSize(width: cellWidth, height: 80)
  90. }
  91. override func prepareForReuse() {
  92. super.prepareForReuse()
  93. titleLabel.text = ""
  94. descriptionLabel.text = ""
  95. thumbnailView.image = nil
  96. newLabel.isHidden = true
  97. }
  98. }