|
| 1 | +import InlineKit |
| 2 | +import UIKit |
| 3 | +class MessageReactionView: UIView { |
| 4 | + // MARK: - Properties |
| 5 | + |
| 6 | + private let emoji: String |
| 7 | + private let count: Int |
| 8 | + private let isSelected: Bool |
| 9 | + private let outgoing: Bool |
| 10 | + |
| 11 | + var onTap: ((String) -> Void)? |
| 12 | + |
| 13 | + // MARK: - UI Components |
| 14 | + |
| 15 | + private lazy var containerView: UIView = { |
| 16 | + let view = UIView() |
| 17 | + view.layer.cornerRadius = 12 |
| 18 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 19 | + return view |
| 20 | + }() |
| 21 | + |
| 22 | + private lazy var stackView: UIStackView = { |
| 23 | + let stack = UIStackView() |
| 24 | + stack.axis = .horizontal |
| 25 | + stack.spacing = 4 |
| 26 | + stack.alignment = .center |
| 27 | + stack.translatesAutoresizingMaskIntoConstraints = false |
| 28 | + return stack |
| 29 | + }() |
| 30 | + |
| 31 | + private lazy var emojiLabel: UILabel = { |
| 32 | + let label = UILabel() |
| 33 | + label.font = UIFont.systemFont(ofSize: 16) |
| 34 | + label.text = emoji |
| 35 | + return label |
| 36 | + }() |
| 37 | + |
| 38 | + private lazy var countLabel: UILabel = { |
| 39 | + let label = UILabel() |
| 40 | + label.font = UIFont.systemFont(ofSize: 12, weight: .medium) |
| 41 | + label.text = "\(count)" |
| 42 | + return label |
| 43 | + }() |
| 44 | + |
| 45 | + // MARK: - Initialization |
| 46 | + |
| 47 | + init(emoji: String, count: Int, isSelected: Bool, outgoing: Bool) { |
| 48 | + self.emoji = emoji |
| 49 | + self.count = count |
| 50 | + self.isSelected = isSelected |
| 51 | + self.outgoing = outgoing |
| 52 | + |
| 53 | + super.init(frame: .zero) |
| 54 | + setupView() |
| 55 | + } |
| 56 | + |
| 57 | + @available(*, unavailable) |
| 58 | + required init?(coder: NSCoder) { |
| 59 | + fatalError("init(coder:) has not been implemented") |
| 60 | + } |
| 61 | + |
| 62 | + // MARK: - Setup |
| 63 | + |
| 64 | + private func setupView() { |
| 65 | + // Configure container appearance |
| 66 | + containerView.backgroundColor = isSelected ? |
| 67 | + (outgoing ? UIColor.white.withAlphaComponent(0.3) : ColorManager.shared.selectedColor.withAlphaComponent(0.3)) : |
| 68 | + (outgoing ? UIColor.white.withAlphaComponent(0.15) : UIColor.systemGray6) |
| 69 | + |
| 70 | + // Configure text colors |
| 71 | + countLabel.textColor = outgoing ? .white.withAlphaComponent(0.9) : .darkGray |
| 72 | + |
| 73 | + // Add subviews |
| 74 | + addSubview(containerView) |
| 75 | + containerView.addSubview(stackView) |
| 76 | + |
| 77 | + stackView.addArrangedSubview(emojiLabel) |
| 78 | + stackView.addArrangedSubview(countLabel) |
| 79 | + |
| 80 | + // Setup constraints |
| 81 | + NSLayoutConstraint.activate([ |
| 82 | + containerView.topAnchor.constraint(equalTo: topAnchor), |
| 83 | + containerView.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 84 | + containerView.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 85 | + containerView.bottomAnchor.constraint(equalTo: bottomAnchor), |
| 86 | + |
| 87 | + stackView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 4), |
| 88 | + stackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 8), |
| 89 | + stackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -8), |
| 90 | + stackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -4) |
| 91 | + ]) |
| 92 | + |
| 93 | + // Add tap gesture |
| 94 | + let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap)) |
| 95 | + containerView.addGestureRecognizer(tapGesture) |
| 96 | + containerView.isUserInteractionEnabled = true |
| 97 | + } |
| 98 | + |
| 99 | + // MARK: - Actions |
| 100 | + |
| 101 | + @objc private func handleTap() { |
| 102 | + onTap?(emoji) |
| 103 | + } |
| 104 | + |
| 105 | + // MARK: - Layout |
| 106 | + |
| 107 | + override var intrinsicContentSize: CGSize { |
| 108 | + let stackSize = stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) |
| 109 | + return CGSize(width: stackSize.width + 16, height: stackSize.height + 8) |
| 110 | + } |
| 111 | + |
| 112 | + override func sizeThatFits(_ size: CGSize) -> CGSize { |
| 113 | + return intrinsicContentSize |
| 114 | + } |
| 115 | +} |
0 commit comments