ActionSheetCell.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*****************************************************************************
  2. * ActionSheetCell.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. class ActionSheetCellImageView: UIImageView {
  12. override var image: UIImage? {
  13. didSet {
  14. super.image = image
  15. isHidden = false
  16. }
  17. }
  18. override init(image: UIImage? = nil) {
  19. super.init(image: image)
  20. isHidden = true
  21. }
  22. required init?(coder aDecoder: NSCoder) {
  23. fatalError("init(coder:) has not been implemented")
  24. }
  25. }
  26. @objc(VLCActionSheetCell)
  27. class ActionSheetCell: UICollectionViewCell {
  28. @objc static var identifier: String {
  29. return String(describing: self)
  30. }
  31. override var isSelected: Bool {
  32. didSet {
  33. updateColors()
  34. checkmark.isHidden = !isSelected
  35. }
  36. }
  37. let icon: ActionSheetCellImageView = {
  38. let icon = ActionSheetCellImageView()
  39. icon.translatesAutoresizingMaskIntoConstraints = false
  40. icon.contentMode = .scaleAspectFit
  41. return icon
  42. }()
  43. let name: UILabel = {
  44. let name = UILabel()
  45. name.textColor = PresentationTheme.current.colors.cellTextColor
  46. name.font = UIFont.systemFont(ofSize: 15)
  47. name.translatesAutoresizingMaskIntoConstraints = false
  48. name.setContentHuggingPriority(.defaultLow, for: .horizontal)
  49. return name
  50. }()
  51. let checkmark: UILabel = {
  52. let checkmark = UILabel()
  53. checkmark.text = "✓"
  54. checkmark.font = UIFont.systemFont(ofSize: 18)
  55. checkmark.textColor = PresentationTheme.current.colors.orangeUI
  56. checkmark.translatesAutoresizingMaskIntoConstraints = false
  57. checkmark.isHidden = true
  58. return checkmark
  59. }()
  60. let stackView: UIStackView = {
  61. let stackView = UIStackView()
  62. stackView.spacing = 15.0
  63. stackView.axis = .horizontal
  64. stackView.alignment = .center
  65. stackView.translatesAutoresizingMaskIntoConstraints = false
  66. return stackView
  67. }()
  68. override init(frame: CGRect) {
  69. super.init(frame: frame)
  70. setupViews()
  71. }
  72. required init?(coder aDecoder: NSCoder) {
  73. super.init(coder: aDecoder)
  74. setupViews()
  75. }
  76. private func updateColors() {
  77. let colors = PresentationTheme.current.colors
  78. name.textColor = isSelected ? colors.orangeUI : colors.cellTextColor
  79. tintColor = isSelected ? colors.orangeUI : colors.cellDetailTextColor
  80. }
  81. override func prepareForReuse() {
  82. super.prepareForReuse()
  83. updateColors()
  84. }
  85. private func setupViews() {
  86. backgroundColor = PresentationTheme.current.colors.background
  87. stackView.addArrangedSubview(icon)
  88. stackView.addArrangedSubview(name)
  89. stackView.addArrangedSubview(checkmark)
  90. addSubview(stackView)
  91. var guide: LayoutAnchorContainer = self
  92. if #available(iOS 11.0, *) {
  93. guide = safeAreaLayoutGuide
  94. }
  95. NSLayoutConstraint.activate([
  96. icon.heightAnchor.constraint(equalToConstant: 25),
  97. icon.widthAnchor.constraint(equalTo: icon.heightAnchor),
  98. stackView.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 20),
  99. stackView.trailingAnchor.constraint(equalTo: guide.trailingAnchor, constant: -20),
  100. stackView.heightAnchor.constraint(equalTo: heightAnchor),
  101. stackView.topAnchor.constraint(equalTo: topAnchor)
  102. ])
  103. }
  104. }