VLCMediaViewEditCell.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*****************************************************************************
  2. * VLCMediaViewEditCell.swift
  3. *
  4. * Copyright © 2018 VLC authors and VideoLAN
  5. * Copyright © 2018 Videolabs
  6. *
  7. * Authors: Soomin Lee <bubu@mikan.io>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. struct VLCCheckView {
  12. var isEnabled: Bool {
  13. didSet {
  14. let backgroundColor: UIColor = isEnabled ? .orange : .clear
  15. let borderColor: UIColor = isEnabled ? .clear : .lightGray
  16. view.backgroundColor = backgroundColor
  17. view.layer.borderColor = borderColor.cgColor
  18. }
  19. }
  20. var view: UIView = {
  21. let view = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
  22. view.translatesAutoresizingMaskIntoConstraints = false
  23. view.clipsToBounds = true
  24. view.layer.cornerRadius = view.frame.width / 2
  25. view.layer.borderColor = UIColor.lightGray.cgColor
  26. view.layer.borderWidth = 1
  27. return view
  28. }()
  29. init(isEnabled: Bool) {
  30. self.isEnabled = isEnabled
  31. }
  32. }
  33. class VLCMediaViewEditCell: UICollectionViewCell {
  34. static let identifier = String(describing: VLCMediaViewEditCell.self)
  35. static let height: CGFloat = 56
  36. var checkView = VLCCheckView(isEnabled: false)
  37. let thumbnailImageView: UIImageView = {
  38. let thumbnailImageView = UIImageView()
  39. thumbnailImageView.translatesAutoresizingMaskIntoConstraints = false
  40. thumbnailImageView.contentMode = .scaleAspectFit
  41. thumbnailImageView.clipsToBounds = true
  42. thumbnailImageView.layer.cornerRadius = 3
  43. return thumbnailImageView
  44. }()
  45. let titleLabel: UILabel = {
  46. let titleLabel = UILabel()
  47. titleLabel.textColor = PresentationTheme.current.colors.cellTextColor
  48. titleLabel.font = UIFont.systemFont(ofSize: 17)
  49. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  50. return titleLabel
  51. }()
  52. let subInfoLabel: UILabel = {
  53. let subInfoLabel = UILabel()
  54. subInfoLabel.textColor = PresentationTheme.current.colors.cellTextColor
  55. subInfoLabel.font = UIFont.systemFont(ofSize: 13)
  56. subInfoLabel.translatesAutoresizingMaskIntoConstraints = false
  57. return subInfoLabel
  58. }()
  59. let sizeLabel: UILabel = {
  60. let sizeLabel = UILabel()
  61. sizeLabel.textColor = PresentationTheme.current.colors.cellTextColor
  62. sizeLabel.font = UIFont.systemFont(ofSize: 11)
  63. sizeLabel.translatesAutoresizingMaskIntoConstraints = false
  64. return sizeLabel
  65. }()
  66. let mainStackView: UIStackView = {
  67. let mainStackView = UIStackView()
  68. mainStackView.spacing = 20.0
  69. mainStackView.axis = .horizontal
  70. mainStackView.alignment = .center
  71. mainStackView.translatesAutoresizingMaskIntoConstraints = false
  72. return mainStackView
  73. }()
  74. let mediaInfoStackView: UIStackView = {
  75. let mediaInfoStackView = UIStackView()
  76. mediaInfoStackView.spacing = 5.0
  77. mediaInfoStackView.axis = .vertical
  78. mediaInfoStackView.alignment = .leading
  79. mediaInfoStackView.translatesAutoresizingMaskIntoConstraints = false
  80. return mediaInfoStackView
  81. }()
  82. override init(frame: CGRect) {
  83. super.init(frame: frame)
  84. setupViews()
  85. }
  86. required init?(coder aDecoder: NSCoder) {
  87. super.init(coder: aDecoder)
  88. setupViews()
  89. }
  90. private func setupViews() {
  91. mediaInfoStackView.addArrangedSubview(titleLabel)
  92. mediaInfoStackView.addArrangedSubview(subInfoLabel)
  93. mediaInfoStackView.addArrangedSubview(sizeLabel)
  94. mainStackView.addArrangedSubview(checkView.view)
  95. mainStackView.addArrangedSubview(thumbnailImageView)
  96. mainStackView.addArrangedSubview(mediaInfoStackView)
  97. addSubview(mainStackView)
  98. var guide: LayoutAnchorContainer = self
  99. if #available(iOS 11.0, *) {
  100. guide = safeAreaLayoutGuide
  101. }
  102. NSLayoutConstraint.activate([
  103. checkView.view.heightAnchor.constraint(equalToConstant: 20),
  104. checkView.view.widthAnchor.constraint(equalTo: checkView.view.heightAnchor),
  105. thumbnailImageView.heightAnchor.constraint(equalToConstant: VLCMediaViewEditCell.height),
  106. thumbnailImageView.widthAnchor.constraint(equalTo: thumbnailImageView.heightAnchor),
  107. mainStackView.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 20),
  108. mainStackView.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -20),
  109. mainStackView.heightAnchor.constraint(equalTo: heightAnchor),
  110. mainStackView.topAnchor.constraint(equalTo: topAnchor)
  111. ])
  112. }
  113. }