2021-03-17 11:09:38 +01:00
|
|
|
//
|
|
|
|
// ComposeStatusAttachmentTableViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-17.
|
|
|
|
//
|
|
|
|
|
2021-03-18 08:16:35 +01:00
|
|
|
import os.log
|
2021-03-17 11:09:38 +01:00
|
|
|
import UIKit
|
2021-03-18 08:16:35 +01:00
|
|
|
import Combine
|
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
protocol ComposeStatusAttachmentCollectionViewCellDelegate: class {
|
|
|
|
func composeStatusAttachmentCollectionViewCell(_ cell: ComposeStatusAttachmentCollectionViewCell, removeButtonDidPressed button: UIButton)
|
2021-03-18 08:16:35 +01:00
|
|
|
}
|
2021-03-17 11:09:38 +01:00
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
final class ComposeStatusAttachmentCollectionViewCell: UICollectionViewCell {
|
2021-03-17 11:09:38 +01:00
|
|
|
|
2021-03-18 08:16:35 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
static let verticalMarginHeight: CGFloat = ComposeStatusAttachmentCollectionViewCell.removeButtonSize.height * 0.5
|
2021-03-18 08:16:35 +01:00
|
|
|
static let removeButtonSize = CGSize(width: 22, height: 22)
|
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
weak var delegate: ComposeStatusAttachmentCollectionViewCellDelegate?
|
2021-03-17 11:09:38 +01:00
|
|
|
|
|
|
|
let attachmentContainerView = AttachmentContainerView()
|
2021-03-18 08:16:35 +01:00
|
|
|
let removeButton: UIButton = {
|
|
|
|
let button = HighlightDimmableButton()
|
|
|
|
button.expandEdgeInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)
|
|
|
|
let image = UIImage(systemName: "minus")!.withConfiguration(UIImage.SymbolConfiguration(pointSize: 14, weight: .bold))
|
|
|
|
button.tintColor = .white
|
|
|
|
button.setImage(image, for: .normal)
|
|
|
|
button.setBackgroundImage(.placeholder(color: Asset.Colors.Background.danger.color), for: .normal)
|
|
|
|
button.layer.masksToBounds = true
|
2021-03-22 10:48:35 +01:00
|
|
|
button.layer.cornerRadius = ComposeStatusAttachmentCollectionViewCell.removeButtonSize.width * 0.5
|
2021-03-18 08:16:35 +01:00
|
|
|
button.layer.borderColor = Asset.Colors.Background.dangerBorder.color.cgColor
|
|
|
|
button.layer.borderWidth = 1
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
|
|
|
|
attachmentContainerView.activityIndicatorView.startAnimating()
|
2021-03-22 10:48:35 +01:00
|
|
|
attachmentContainerView.previewImageView.af.cancelImageRequest()
|
|
|
|
attachmentContainerView.previewImageView.image = .placeholder(color: .systemFill)
|
2021-03-18 08:16:35 +01:00
|
|
|
delegate = nil
|
2021-03-22 10:48:35 +01:00
|
|
|
disposeBag.removeAll()
|
2021-03-18 08:16:35 +01:00
|
|
|
}
|
2021-03-17 11:09:38 +01:00
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
2021-03-17 11:09:38 +01:00
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
extension ComposeStatusAttachmentCollectionViewCell {
|
2021-03-17 11:09:38 +01:00
|
|
|
|
|
|
|
private func _init() {
|
2021-03-22 10:48:35 +01:00
|
|
|
// selectionStyle = .none
|
2021-03-18 08:16:35 +01:00
|
|
|
|
2021-03-17 11:09:38 +01:00
|
|
|
attachmentContainerView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(attachmentContainerView)
|
|
|
|
NSLayoutConstraint.activate([
|
2021-03-22 10:48:35 +01:00
|
|
|
attachmentContainerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: ComposeStatusAttachmentCollectionViewCell.verticalMarginHeight),
|
2021-03-17 11:09:38 +01:00
|
|
|
attachmentContainerView.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor),
|
|
|
|
attachmentContainerView.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor),
|
2021-03-22 10:48:35 +01:00
|
|
|
contentView.bottomAnchor.constraint(equalTo: attachmentContainerView.bottomAnchor, constant: ComposeStatusAttachmentCollectionViewCell.verticalMarginHeight),
|
2021-03-17 11:09:38 +01:00
|
|
|
attachmentContainerView.heightAnchor.constraint(equalToConstant: 205).priority(.defaultHigh),
|
|
|
|
])
|
|
|
|
|
2021-03-18 08:16:35 +01:00
|
|
|
removeButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(removeButton)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
removeButton.centerXAnchor.constraint(equalTo: attachmentContainerView.trailingAnchor),
|
|
|
|
removeButton.centerYAnchor.constraint(equalTo: attachmentContainerView.topAnchor),
|
2021-03-22 10:48:35 +01:00
|
|
|
removeButton.widthAnchor.constraint(equalToConstant: ComposeStatusAttachmentCollectionViewCell.removeButtonSize.width).priority(.defaultHigh),
|
|
|
|
removeButton.heightAnchor.constraint(equalToConstant: ComposeStatusAttachmentCollectionViewCell.removeButtonSize.height).priority(.defaultHigh),
|
2021-03-18 08:16:35 +01:00
|
|
|
])
|
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
removeButton.addTarget(self, action: #selector(ComposeStatusAttachmentCollectionViewCell.removeButtonDidPressed(_:)), for: .touchUpInside)
|
2021-03-17 11:09:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-18 08:16:35 +01:00
|
|
|
|
2021-03-22 10:48:35 +01:00
|
|
|
extension ComposeStatusAttachmentCollectionViewCell {
|
2021-03-18 08:16:35 +01:00
|
|
|
|
|
|
|
@objc private func removeButtonDidPressed(_ sender: UIButton) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
2021-03-22 10:48:35 +01:00
|
|
|
delegate?.composeStatusAttachmentCollectionViewCell(self, removeButtonDidPressed: sender)
|
2021-03-18 08:16:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|