AudioCollectionViewCell.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 class var cellPadding: CGFloat {
  19. return 5.0
  20. }
  21. override var media: VLCMLObject? {
  22. didSet {
  23. if let albumTrack = media as? VLCMLMedia {
  24. update(audiotrack:albumTrack)
  25. } else if let album = media as? VLCMLAlbum {
  26. update(album:album)
  27. } else {
  28. fatalError("needs to be of Type VLCMLMedia or VLCMLAlbum")
  29. }
  30. }
  31. }
  32. override func awakeFromNib() {
  33. super.awakeFromNib()
  34. newLabel.text = NSLocalizedString("NEW", comment: "")
  35. newLabel.textColor = PresentationTheme.current.colors.orangeUI
  36. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  37. themeDidChange()
  38. }
  39. @objc fileprivate func themeDidChange() {
  40. backgroundColor = PresentationTheme.current.colors.background
  41. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  42. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  43. }
  44. func update(audiotrack: VLCMLMedia) {
  45. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  46. titleLabel.text = audiotrack.title
  47. descriptionLabel.text = audiotrack.albumTrack.artist.name
  48. if audiotrack.isThumbnailGenerated() {
  49. thumbnailView.image = UIImage(contentsOfFile: audiotrack.thumbnail.absoluteString)
  50. }
  51. newLabel.isHidden = !audiotrack.isNew()
  52. }
  53. func update(album: VLCMLAlbum) {
  54. titleLabel.text = album.title
  55. descriptionLabel.text = album.albumArtist.name
  56. }
  57. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  58. let numberOfCells: CGFloat = round(width / 320)
  59. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  60. // we then have the entire padding, we divide the entire padding by the number of Cells to know how much needs to be substracted from ech cell
  61. // since this might be an uneven number we ceil
  62. var cellWidth = width / numberOfCells
  63. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  64. return CGSize(width: cellWidth, height: 80)
  65. }
  66. override func prepareForReuse() {
  67. super.prepareForReuse()
  68. titleLabel.text = ""
  69. descriptionLabel.text = ""
  70. thumbnailView.image = nil
  71. newLabel.isHidden = true
  72. }
  73. }