2021-05-27 07:56:55 +02:00
|
|
|
//
|
|
|
|
// ProfileFieldAddEntryCollectionViewCell.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-5-26.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
|
|
|
|
protocol ProfileFieldAddEntryCollectionViewCellDelegate: AnyObject {
|
|
|
|
func ProfileFieldAddEntryCollectionViewCellDidPressed(_ cell: ProfileFieldAddEntryCollectionViewCell)
|
|
|
|
}
|
|
|
|
|
|
|
|
final class ProfileFieldAddEntryCollectionViewCell: UICollectionViewCell {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
weak var delegate: ProfileFieldAddEntryCollectionViewCellDelegate?
|
|
|
|
|
|
|
|
let singleTagGestureRecognizer = UITapGestureRecognizer.singleTapGestureRecognizer
|
|
|
|
|
|
|
|
|
|
|
|
static let symbolConfiguration = ProfileFieldCollectionViewCell.symbolConfiguration
|
|
|
|
static let insertButtonImage = UIImage(systemName: "plus.circle.fill", withConfiguration: symbolConfiguration)
|
|
|
|
|
|
|
|
let containerStackView = UIStackView()
|
|
|
|
|
|
|
|
let fieldView = ProfileFieldView()
|
|
|
|
|
|
|
|
let editButton: UIButton = {
|
|
|
|
let button = HitTestExpandedButton(type: .custom)
|
|
|
|
button.setImage(ProfileFieldAddEntryCollectionViewCell.insertButtonImage, for: .normal)
|
|
|
|
button.contentMode = .center
|
|
|
|
button.tintColor = .systemGreen
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
2021-05-27 08:36:16 +02:00
|
|
|
var separatorLineToMarginLeadingLayoutConstraint: NSLayoutConstraint!
|
2021-05-27 07:56:55 +02:00
|
|
|
var separatorLineToEdgeTrailingLayoutConstraint: NSLayoutConstraint!
|
|
|
|
var separatorLineToMarginTrailingLayoutConstraint: NSLayoutConstraint!
|
|
|
|
let bottomSeparatorLine = UIView.separatorLine
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
|
|
|
|
disposeBag.removeAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileFieldAddEntryCollectionViewCell {
|
|
|
|
|
|
|
|
private func _init() {
|
|
|
|
containerStackView.axis = .horizontal
|
|
|
|
containerStackView.spacing = 8
|
|
|
|
|
|
|
|
containerStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(containerStackView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
containerStackView.topAnchor.constraint(equalTo: contentView.topAnchor),
|
2021-05-31 10:57:27 +02:00
|
|
|
containerStackView.leadingAnchor.constraint(equalTo: contentView.readableContentGuide.leadingAnchor),
|
|
|
|
containerStackView.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor),
|
2021-05-27 07:56:55 +02:00
|
|
|
containerStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
|
|
|
|
containerStackView.heightAnchor.constraint(equalToConstant: 44).priority(.defaultHigh),
|
|
|
|
])
|
2021-05-27 08:36:16 +02:00
|
|
|
containerStackView.isLayoutMarginsRelativeArrangement = true
|
2021-05-27 07:56:55 +02:00
|
|
|
|
|
|
|
containerStackView.addArrangedSubview(editButton)
|
|
|
|
containerStackView.addArrangedSubview(fieldView)
|
|
|
|
|
|
|
|
editButton.setContentCompressionResistancePriority(.required - 1, for: .horizontal)
|
|
|
|
editButton.setContentHuggingPriority(.required - 1, for: .horizontal)
|
|
|
|
|
|
|
|
bottomSeparatorLine.translatesAutoresizingMaskIntoConstraints = false
|
2021-05-27 08:36:16 +02:00
|
|
|
separatorLineToMarginLeadingLayoutConstraint = bottomSeparatorLine.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)
|
2021-05-27 07:56:55 +02:00
|
|
|
separatorLineToEdgeTrailingLayoutConstraint = bottomSeparatorLine.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
|
2021-05-27 08:36:16 +02:00
|
|
|
separatorLineToMarginTrailingLayoutConstraint = bottomSeparatorLine.trailingAnchor.constraint(equalTo: contentView.readableContentGuide.trailingAnchor)
|
2021-05-27 07:56:55 +02:00
|
|
|
|
|
|
|
addSubview(bottomSeparatorLine)
|
|
|
|
NSLayoutConstraint.activate([
|
2021-05-27 08:36:16 +02:00
|
|
|
separatorLineToMarginLeadingLayoutConstraint,
|
2021-05-27 07:56:55 +02:00
|
|
|
bottomSeparatorLine.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
bottomSeparatorLine.heightAnchor.constraint(equalToConstant: UIView.separatorLineHeight(of: self)).priority(.defaultHigh),
|
|
|
|
])
|
|
|
|
|
2021-05-27 08:57:20 +02:00
|
|
|
fieldView.titleActiveLabel.isHidden = false
|
|
|
|
fieldView.titleActiveLabel.configure(field: L10n.Scene.Profile.Fields.addRow, emojiDict: [:])
|
|
|
|
fieldView.titleTextField.isHidden = true
|
|
|
|
|
|
|
|
fieldView.valueActiveLabel.isHidden = false
|
2021-05-27 07:56:55 +02:00
|
|
|
fieldView.valueActiveLabel.configure(field: " ", emojiDict: [:])
|
2021-05-27 08:57:20 +02:00
|
|
|
fieldView.valueTextField.isHidden = true
|
2021-05-27 07:56:55 +02:00
|
|
|
|
|
|
|
addGestureRecognizer(singleTagGestureRecognizer)
|
|
|
|
singleTagGestureRecognizer.addTarget(self, action: #selector(ProfileFieldAddEntryCollectionViewCell.singleTapGestureRecognizerHandler(_:)))
|
|
|
|
|
|
|
|
editButton.addTarget(self, action: #selector(ProfileFieldAddEntryCollectionViewCell.addButtonDidPressed(_:)), for: .touchUpInside)
|
|
|
|
|
|
|
|
resetSeparatorLineLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
resetSeparatorLineLayout()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileFieldAddEntryCollectionViewCell {
|
|
|
|
|
|
|
|
@objc private func singleTapGestureRecognizerHandler(_ sender: UITapGestureRecognizer) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
delegate?.ProfileFieldAddEntryCollectionViewCellDidPressed(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func addButtonDidPressed(_ sender: UIButton) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
delegate?.ProfileFieldAddEntryCollectionViewCellDidPressed(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileFieldAddEntryCollectionViewCell {
|
|
|
|
private func resetSeparatorLineLayout() {
|
|
|
|
separatorLineToEdgeTrailingLayoutConstraint.isActive = false
|
|
|
|
separatorLineToMarginTrailingLayoutConstraint.isActive = false
|
|
|
|
|
|
|
|
if traitCollection.userInterfaceIdiom == .phone {
|
|
|
|
// to edge
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
separatorLineToEdgeTrailingLayoutConstraint,
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
if traitCollection.horizontalSizeClass == .compact {
|
|
|
|
// to edge
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
separatorLineToEdgeTrailingLayoutConstraint,
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
// to margin
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
separatorLineToMarginTrailingLayoutConstraint,
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ProfileFieldAddEntryCollectionViewCell_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
UIViewPreview(width: 375) {
|
|
|
|
ProfileFieldAddEntryCollectionViewCell()
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 44))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|