AudioCollectionViewCell.swift 3.0 KB

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