ArtistCollectionViewCell.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 var media: VLCMLObject? {
  17. didSet {
  18. guard let artist = media as? VLCMLArtist else {
  19. fatalError("needs to be of Type VLCMLArtist")
  20. }
  21. update(artist:artist)
  22. }
  23. }
  24. override func awakeFromNib() {
  25. super.awakeFromNib()
  26. NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .VLCThemeDidChangeNotification, object: nil)
  27. themeDidChange()
  28. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  29. }
  30. @objc fileprivate func themeDidChange() {
  31. backgroundColor = PresentationTheme.current.colors.background
  32. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  33. }
  34. func update(artist: VLCMLArtist) {
  35. titleLabel.text = artist.name
  36. // if artist.isThumbnailGenerated() {
  37. // thumbnailView.image = UIImage(contentsOfFile: artist.thumbnail.absoluteString)
  38. // }
  39. }
  40. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  41. let numberOfCells: CGFloat
  42. if width <= DeviceWidth.iPhonePortrait.rawValue {
  43. numberOfCells = 1
  44. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  45. numberOfCells = 2
  46. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  47. numberOfCells = 3
  48. } else {
  49. numberOfCells = 4
  50. }
  51. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  52. //
  53. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  54. //
  55. let overallWidth = width - (2 * edgePadding)
  56. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  57. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  58. return CGSize(width: cellWidth, height: 80)
  59. }
  60. override func prepareForReuse() {
  61. super.prepareForReuse()
  62. titleLabel.text = ""
  63. thumbnailView.image = nil
  64. }
  65. }