AttachmentCell.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // AttachmentCell.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2020 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 10/4/17.
  26. //
  27. import UIKit
  28. open class AttachmentCell: UICollectionViewCell {
  29. // MARK: - Properties
  30. public class var reuseIdentifier: String {
  31. return "AttachmentCell"
  32. }
  33. public let containerView: UIView = {
  34. let view = UIView()
  35. view.translatesAutoresizingMaskIntoConstraints = false
  36. if #available(iOS 13, *) {
  37. view.backgroundColor = .systemGray6
  38. } else {
  39. view.backgroundColor = .groupTableViewBackground
  40. }
  41. view.layer.cornerRadius = 8
  42. view.clipsToBounds = true
  43. return view
  44. }()
  45. open var padding: UIEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5) {
  46. didSet {
  47. updateContainerPadding()
  48. }
  49. }
  50. open lazy var deleteButton: UIButton = { [weak self] in
  51. let button = UIButton()
  52. let textColor: UIColor
  53. if #available(iOS 13, *) {
  54. textColor = .systemBackground
  55. } else {
  56. textColor = .white
  57. }
  58. button.setAttributedTitle(NSMutableAttributedString().bold("X", fontSize: 15, textColor: textColor), for: .normal)
  59. button.setAttributedTitle(NSMutableAttributedString().bold("X", fontSize: 15, textColor: textColor.withAlphaComponent(0.5)), for: .highlighted)
  60. button.layer.cornerRadius = 10
  61. button.clipsToBounds = true
  62. button.backgroundColor = .systemBlue
  63. button.addTarget(self, action: #selector(deleteAttachment), for: .touchUpInside)
  64. return button
  65. }()
  66. open var attachment: AttachmentManager.Attachment?
  67. open var indexPath: IndexPath?
  68. open weak var manager: AttachmentManager?
  69. private var containerViewLayoutSet: NSLayoutConstraintSet?
  70. // MARK: - Initialization
  71. public override init(frame: CGRect) {
  72. super.init(frame: frame)
  73. setup()
  74. }
  75. required public init?(coder aDecoder: NSCoder) {
  76. super.init(coder: aDecoder)
  77. setup()
  78. }
  79. open override func prepareForReuse() {
  80. super.prepareForReuse()
  81. indexPath = nil
  82. manager = nil
  83. attachment = nil
  84. }
  85. // MARK: - Setup
  86. private func setup() {
  87. setupSubviews()
  88. setupConstraints()
  89. }
  90. private func setupSubviews() {
  91. contentView.addSubview(containerView)
  92. contentView.addSubview(deleteButton)
  93. }
  94. private func setupConstraints() {
  95. containerViewLayoutSet = NSLayoutConstraintSet(
  96. top: containerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: padding.top),
  97. bottom: containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -padding.bottom),
  98. left: containerView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: padding.left),
  99. right: containerView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -padding.right)
  100. ).activate()
  101. deleteButton.addConstraints(contentView.topAnchor, right: contentView.rightAnchor, widthConstant: 20, heightConstant: 20)
  102. }
  103. private func updateContainerPadding() {
  104. containerViewLayoutSet?.top?.constant = padding.top
  105. containerViewLayoutSet?.bottom?.constant = -padding.bottom
  106. containerViewLayoutSet?.left?.constant = padding.left
  107. containerViewLayoutSet?.right?.constant = -padding.right
  108. }
  109. // MARK: - User Actions
  110. @objc
  111. func deleteAttachment() {
  112. guard let index = indexPath?.row else { return }
  113. manager?.removeAttachment(at: index)
  114. }
  115. }