AudioCollectionViewCell.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 {
  21. update(audiotrack:albumTrack)
  22. } else if let album = media as? VLCMLAlbum {
  23. update(album:album)
  24. } else {
  25. fatalError("needs to be of Type VLCMLMedia or VLCMLAlbum")
  26. }
  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. }
  36. @objc fileprivate func themeDidChange() {
  37. backgroundColor = PresentationTheme.current.colors.background
  38. titleLabel?.textColor = PresentationTheme.current.colors.cellTextColor
  39. descriptionLabel?.textColor = PresentationTheme.current.colors.cellDetailTextColor
  40. }
  41. func update(audiotrack: VLCMLMedia) {
  42. thumbnailView.layer.cornerRadius = thumbnailView.frame.size.width / 2.0
  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. func update(album: VLCMLAlbum) {
  51. titleLabel.text = album.title
  52. descriptionLabel.text = album.albumArtist != nil ? album.albumArtist.name : ""
  53. }
  54. override class func cellSizeForWidth(_ width: CGFloat) -> CGSize {
  55. let numberOfCells: CGFloat
  56. if width <= DeviceWidth.iPhonePortrait.rawValue {
  57. numberOfCells = 1
  58. } else if width <= DeviceWidth.iPhoneLandscape.rawValue {
  59. numberOfCells = 2
  60. } else if width <= DeviceWidth.iPadLandscape.rawValue {
  61. numberOfCells = 3
  62. } else {
  63. numberOfCells = 4
  64. }
  65. // We have the number of cells and we always have numberofCells + 1 interItemPadding spaces.
  66. //
  67. // edgePadding-interItemPadding-[Cell]-interItemPadding-[Cell]-interItemPadding-edgePadding
  68. //
  69. let overallWidth = width - (2 * edgePadding)
  70. let overallCellWidthWithoutPadding = overallWidth - (numberOfCells + 1) * interItemPadding
  71. let cellWidth = floor(overallCellWidthWithoutPadding / numberOfCells)
  72. return CGSize(width: cellWidth, height: 80)
  73. }
  74. override func prepareForReuse() {
  75. super.prepareForReuse()
  76. titleLabel.text = ""
  77. descriptionLabel.text = ""
  78. thumbnailView.image = nil
  79. newLabel.isHidden = true
  80. }
  81. }