AttachmentCell.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // AttachmentCell.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 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. class var reuseIdentifier: String {
  31. return "AttachmentCell"
  32. }
  33. public let containerView: UIView = {
  34. let view = UIView()
  35. view.translatesAutoresizingMaskIntoConstraints = false
  36. view.backgroundColor = .groupTableViewBackground
  37. view.layer.cornerRadius = 8
  38. view.clipsToBounds = true
  39. return view
  40. }()
  41. open var padding: UIEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5) {
  42. didSet {
  43. updateContainerPadding()
  44. }
  45. }
  46. open lazy var deleteButton: UIButton = { [weak self] in
  47. let button = UIButton()
  48. button.setAttributedTitle(NSMutableAttributedString().bold("X", fontSize: 15, textColor: .white), for: .normal)
  49. button.setAttributedTitle(NSMutableAttributedString().bold("X", fontSize: 15, textColor: UIColor.white.withAlphaComponent(0.5)), for: .highlighted)
  50. button.layer.cornerRadius = 10
  51. button.clipsToBounds = true
  52. button.backgroundColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
  53. button.addTarget(self, action: #selector(deleteAttachment), for: .touchUpInside)
  54. return button
  55. }()
  56. open var attachment: AttachmentManager.Attachment?
  57. open var indexPath: IndexPath?
  58. open weak var manager: AttachmentManager?
  59. private var containerViewLayoutSet: NSLayoutConstraintSet?
  60. // MARK: - Initialization
  61. public override init(frame: CGRect) {
  62. super.init(frame: frame)
  63. setup()
  64. }
  65. required public init?(coder aDecoder: NSCoder) {
  66. super.init(coder: aDecoder)
  67. setup()
  68. }
  69. open override func prepareForReuse() {
  70. super.prepareForReuse()
  71. indexPath = nil
  72. manager = nil
  73. attachment = nil
  74. }
  75. // MARK: - Setup
  76. private func setup() {
  77. setupSubviews()
  78. setupConstraints()
  79. }
  80. private func setupSubviews() {
  81. contentView.addSubview(containerView)
  82. contentView.addSubview(deleteButton)
  83. }
  84. private func setupConstraints() {
  85. containerViewLayoutSet = NSLayoutConstraintSet(
  86. top: containerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: padding.top),
  87. bottom: containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -padding.bottom),
  88. left: containerView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: padding.left),
  89. right: containerView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -padding.right)
  90. ).activate()
  91. deleteButton.addConstraints(contentView.topAnchor, right: contentView.rightAnchor, widthConstant: 20, heightConstant: 20)
  92. }
  93. private func updateContainerPadding() {
  94. containerViewLayoutSet?.top?.constant = padding.top
  95. containerViewLayoutSet?.bottom?.constant = -padding.bottom
  96. containerViewLayoutSet?.left?.constant = padding.left
  97. containerViewLayoutSet?.right?.constant = -padding.right
  98. }
  99. // MARK: - User Actions
  100. @objc
  101. func deleteAttachment() {
  102. guard let index = indexPath?.row else { return }
  103. manager?.removeAttachment(at: index)
  104. }
  105. }