ArtistCollectionViewCell.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*****************************************************************************
  2. * ArtistCollectionViewCell.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2019 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 ArtistCollectionViewCell: BaseCollectionViewCell {
  14. @IBOutlet weak var thumbnailView: UIImageView!
  15. @IBOutlet weak var titleLabel: UILabel!
  16. override class var cellPadding: CGFloat {
  17. return 5.0
  18. }
  19. override var media: VLCMLObject? {
  20. didSet {
  21. guard let artist = media as? VLCMLArtist else {
  22. fatalError("needs to be of Type VLCMLArtist")
  23. }
  24. update(artist:artist)
  25. }
  26. }
  27. override func awakeFromNib() {
  28. super.awakeFromNib()
  29. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  30. themeDidChange()
  31. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  32. }
  33. @objc fileprivate func themeDidChange() {
  34. backgroundColor = PresentationTheme.current.colors.background
  35. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  36. }
  37. func update(artist: VLCMLArtist) {
  38. titleLabel.text = artist.name
  39. // if artist.isThumbnailGenerated() {
  40. // thumbnailView.image = UIImage(contentsOfFile: artist.thumbnail.absoluteString)
  41. // }
  42. }
  43. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  44. let numberOfCells: CGFloat = round(width / 320)
  45. // We have the number of cells and we always have numberofCells + 1 padding spaces. -pad-[Cell]-pad-[Cell]-pad-
  46. // 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
  47. // since this might be an uneven number we ceil
  48. var cellWidth = width / numberOfCells
  49. cellWidth = cellWidth - ceil(((numberOfCells + 1) * cellPadding) / numberOfCells)
  50. return CGSize(width: cellWidth, height: 80)
  51. }
  52. override func prepareForReuse() {
  53. super.prepareForReuse()
  54. titleLabel.text = ""
  55. thumbnailView.image = nil
  56. }
  57. }