2022-04-14 15:15:21 +02:00
|
|
|
//
|
|
|
|
// ProfileCardView.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-14.
|
|
|
|
//
|
|
|
|
|
2022-04-15 11:17:39 +02:00
|
|
|
import os.log
|
2022-04-14 15:15:21 +02:00
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import MetaTextKit
|
|
|
|
import MastodonAsset
|
|
|
|
|
2022-04-15 11:17:39 +02:00
|
|
|
public protocol ProfileCardViewDelegate: AnyObject {
|
|
|
|
func profileCardView(_ profileCardView: ProfileCardView, relationshipButtonDidPressed button: ProfileRelationshipActionButton)
|
2022-05-17 12:49:29 +02:00
|
|
|
func profileCardView(_ profileCardView: ProfileCardView, familiarFollowersDashboardViewDidPressed view: FamiliarFollowersDashboardView)
|
2022-04-15 11:17:39 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
public final class ProfileCardView: UIView {
|
|
|
|
|
2022-05-17 12:49:29 +02:00
|
|
|
let logger = Logger(subsystem: "ProfileCardView", category: "View")
|
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
static let avatarSize = CGSize(width: 56, height: 56)
|
|
|
|
static let friendshipActionButtonSize = CGSize(width: 108, height: 34)
|
|
|
|
static let contentMargin: CGFloat = 16
|
|
|
|
|
2022-04-15 11:17:39 +02:00
|
|
|
weak var delegate: ProfileCardViewDelegate?
|
2022-04-14 15:15:21 +02:00
|
|
|
private var _disposeBag = Set<AnyCancellable>()
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
let container = UIStackView()
|
|
|
|
|
|
|
|
let bannerImageView: UIImageView = {
|
|
|
|
let imageView = UIImageView()
|
|
|
|
imageView.contentMode = .scaleAspectFill
|
|
|
|
imageView.layer.masksToBounds = true
|
|
|
|
imageView.layer.cornerRadius = 3
|
|
|
|
imageView.layer.cornerCurve = .continuous
|
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
|
|
|
// avatar
|
2022-04-15 11:17:39 +02:00
|
|
|
public let avatarButtonBackgroundView = UIView()
|
2022-04-14 15:15:21 +02:00
|
|
|
public let avatarButton = AvatarButton()
|
|
|
|
|
|
|
|
// author name
|
|
|
|
public let authorNameLabel = MetaLabel(style: .profileCardName)
|
|
|
|
|
|
|
|
// author username
|
|
|
|
public let authorUsernameLabel = MetaLabel(style: .profileCardUsername)
|
|
|
|
|
2022-04-19 14:57:21 +02:00
|
|
|
// bio
|
|
|
|
let bioMetaTextAdaptiveMarginContainerView = AdaptiveMarginContainerView()
|
2022-04-14 15:15:21 +02:00
|
|
|
let bioMetaText: MetaText = {
|
|
|
|
let metaText = MetaText()
|
|
|
|
metaText.textView.backgroundColor = .clear
|
|
|
|
metaText.textView.isEditable = false
|
|
|
|
metaText.textView.isSelectable = true
|
|
|
|
metaText.textView.isScrollEnabled = false
|
|
|
|
//metaText.textView.textContainer.lineFragmentPadding = 0
|
|
|
|
//metaText.textView.textContainerInset = .zero
|
|
|
|
metaText.textView.layer.masksToBounds = false
|
|
|
|
metaText.textView.textDragInteraction?.isEnabled = false // disable drag for link and attachment
|
|
|
|
|
|
|
|
metaText.textView.layer.masksToBounds = true
|
|
|
|
metaText.textView.layer.cornerCurve = .continuous
|
|
|
|
metaText.textView.layer.cornerRadius = 10
|
|
|
|
|
|
|
|
metaText.paragraphStyle = {
|
|
|
|
let style = NSMutableParagraphStyle()
|
|
|
|
style.lineSpacing = 5
|
|
|
|
style.paragraphSpacing = 8
|
|
|
|
return style
|
|
|
|
}()
|
|
|
|
metaText.textAttributes = [
|
|
|
|
.font: UIFont.preferredFont(forTextStyle: .body),
|
|
|
|
.foregroundColor: Asset.Colors.Label.primary.color,
|
|
|
|
]
|
|
|
|
metaText.linkAttributes = [
|
|
|
|
.font: UIFont.preferredFont(forTextStyle: .body),
|
2022-06-02 11:31:23 +02:00
|
|
|
.foregroundColor: Asset.Colors.brand.color,
|
2022-04-14 15:15:21 +02:00
|
|
|
]
|
|
|
|
return metaText
|
|
|
|
}()
|
|
|
|
|
2022-04-19 14:57:21 +02:00
|
|
|
let infoContainerAdaptiveMarginContainerView = AdaptiveMarginContainerView()
|
2022-04-18 04:47:55 +02:00
|
|
|
let infoContainer = UIStackView()
|
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
let statusDashboardView = ProfileStatusDashboardView()
|
|
|
|
|
|
|
|
let relationshipActionButtonShadowContainer = ShadowBackgroundContainer()
|
|
|
|
let relationshipActionButton: ProfileRelationshipActionButton = {
|
|
|
|
let button = ProfileRelationshipActionButton()
|
|
|
|
button.titleLabel?.font = .systemFont(ofSize: 17, weight: .semibold)
|
|
|
|
button.titleLabel?.adjustsFontSizeToFitWidth = true
|
|
|
|
button.titleLabel?.minimumScaleFactor = 0.5
|
|
|
|
return button
|
|
|
|
}()
|
|
|
|
|
2022-05-16 13:42:03 +02:00
|
|
|
let familiarFollowersDashboardViewAdaptiveMarginContainerView = AdaptiveMarginContainerView()
|
|
|
|
let familiarFollowersDashboardView = FamiliarFollowersDashboardView()
|
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
public private(set) lazy var viewModel: ViewModel = {
|
|
|
|
let viewModel = ViewModel()
|
|
|
|
viewModel.bind(view: self)
|
|
|
|
return viewModel
|
|
|
|
}()
|
|
|
|
|
|
|
|
public func prepareForReuse() {
|
|
|
|
disposeBag.removeAll()
|
|
|
|
bannerImageView.af.cancelImageRequest()
|
|
|
|
bannerImageView.image = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileCardView {
|
|
|
|
private func _init() {
|
|
|
|
avatarButton.isUserInteractionEnabled = false
|
|
|
|
authorNameLabel.isUserInteractionEnabled = false
|
|
|
|
authorUsernameLabel.isUserInteractionEnabled = false
|
|
|
|
bioMetaText.textView.isUserInteractionEnabled = false
|
|
|
|
statusDashboardView.isUserInteractionEnabled = false
|
|
|
|
|
2022-05-16 13:42:03 +02:00
|
|
|
// container: V - [ bannerContainer | authorContainer | bioMetaText | infoContainer | familiarFollowersDashboardView ]
|
2022-04-14 15:15:21 +02:00
|
|
|
container.axis = .vertical
|
|
|
|
container.spacing = 8
|
|
|
|
container.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(container)
|
2022-11-17 17:45:27 +01:00
|
|
|
container.pinToParent()
|
2022-04-14 15:15:21 +02:00
|
|
|
|
|
|
|
// bannerContainer
|
|
|
|
let bannerContainer = UIView()
|
|
|
|
bannerContainer.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
container.addArrangedSubview(bannerContainer)
|
|
|
|
container.setCustomSpacing(6, after: bannerContainer)
|
|
|
|
|
|
|
|
// bannerImageView
|
|
|
|
bannerImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
bannerContainer.addSubview(bannerImageView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
bannerImageView.topAnchor.constraint(equalTo: bannerContainer.topAnchor, constant: 4),
|
|
|
|
bannerImageView.leadingAnchor.constraint(equalTo: bannerContainer.leadingAnchor, constant: 4),
|
|
|
|
bannerContainer.trailingAnchor.constraint(equalTo: bannerImageView.trailingAnchor, constant: 4),
|
|
|
|
bannerImageView.bottomAnchor.constraint(equalTo: bannerContainer.bottomAnchor),
|
|
|
|
bannerImageView.widthAnchor.constraint(equalTo: bannerImageView.heightAnchor, multiplier: 335.0/128.0).priority(.required - 1),
|
|
|
|
])
|
|
|
|
|
|
|
|
// authorContainer: H - [ avatarPlaceholder | authorInfoContainer ]
|
|
|
|
let authorContainer = UIStackView()
|
|
|
|
authorContainer.axis = .horizontal
|
|
|
|
authorContainer.spacing = 16
|
|
|
|
let authorContainerAdaptiveMarginContainerView = AdaptiveMarginContainerView()
|
|
|
|
authorContainerAdaptiveMarginContainerView.contentView = authorContainer
|
|
|
|
authorContainerAdaptiveMarginContainerView.margin = ProfileCardView.contentMargin
|
|
|
|
container.addArrangedSubview(authorContainerAdaptiveMarginContainerView)
|
2022-05-17 11:40:19 +02:00
|
|
|
container.setCustomSpacing(6, after: bannerContainer)
|
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
// avatarPlaceholder
|
|
|
|
let avatarPlaceholder = UIView()
|
|
|
|
avatarPlaceholder.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
authorContainer.addArrangedSubview(avatarPlaceholder)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
avatarPlaceholder.widthAnchor.constraint(equalToConstant: ProfileCardView.avatarSize.width).priority(.required - 1),
|
|
|
|
avatarPlaceholder.heightAnchor.constraint(equalToConstant: ProfileCardView.avatarSize.height - 14).priority(.required - 1),
|
|
|
|
])
|
|
|
|
|
|
|
|
avatarButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
authorContainer.addSubview(avatarButton)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
avatarButton.leadingAnchor.constraint(equalTo: avatarPlaceholder.leadingAnchor),
|
|
|
|
avatarButton.trailingAnchor.constraint(equalTo: avatarPlaceholder.trailingAnchor),
|
|
|
|
avatarButton.bottomAnchor.constraint(equalTo: avatarPlaceholder.bottomAnchor),
|
|
|
|
avatarButton.heightAnchor.constraint(equalToConstant: ProfileCardView.avatarSize.height).priority(.required - 1),
|
|
|
|
])
|
|
|
|
|
|
|
|
avatarButtonBackgroundView.layer.masksToBounds = true
|
|
|
|
avatarButtonBackgroundView.layer.cornerCurve = .continuous
|
2022-04-18 11:18:24 +02:00
|
|
|
avatarButtonBackgroundView.layer.cornerRadius = 12 + 1
|
2022-04-14 15:15:21 +02:00
|
|
|
avatarButtonBackgroundView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
authorContainer.insertSubview(avatarButtonBackgroundView, belowSubview: avatarButton)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
avatarButtonBackgroundView.centerXAnchor.constraint(equalTo: avatarButton.centerXAnchor),
|
|
|
|
avatarButtonBackgroundView.centerYAnchor.constraint(equalTo: avatarButton.centerYAnchor),
|
|
|
|
avatarButtonBackgroundView.widthAnchor.constraint(equalToConstant: ProfileCardView.avatarSize.width + 4).priority(.required - 1),
|
|
|
|
avatarButtonBackgroundView.heightAnchor.constraint(equalToConstant: ProfileCardView.avatarSize.height + 4).priority(.required - 1),
|
|
|
|
])
|
|
|
|
|
|
|
|
// authorInfoContainer: V - [ authorNameLabel | authorUsernameLabel ]
|
|
|
|
let authorInfoContainer = UIStackView()
|
|
|
|
authorInfoContainer.axis = .vertical
|
2022-04-19 14:57:21 +02:00
|
|
|
// authorInfoContainer.spacing = 2
|
2022-04-14 15:15:21 +02:00
|
|
|
authorContainer.addArrangedSubview(authorInfoContainer)
|
|
|
|
|
|
|
|
authorInfoContainer.addArrangedSubview(authorNameLabel)
|
|
|
|
authorInfoContainer.addArrangedSubview(authorUsernameLabel)
|
|
|
|
|
|
|
|
// bioMetaText
|
|
|
|
bioMetaTextAdaptiveMarginContainerView.contentView = bioMetaText.textView
|
|
|
|
bioMetaTextAdaptiveMarginContainerView.margin = ProfileCardView.contentMargin
|
2022-04-18 11:44:19 +02:00
|
|
|
bioMetaText.textView.setContentHuggingPriority(.required - 1, for: .vertical)
|
|
|
|
bioMetaText.textView.setContentCompressionResistancePriority(.required - 1, for: .vertical)
|
2022-04-14 15:15:21 +02:00
|
|
|
container.addArrangedSubview(bioMetaTextAdaptiveMarginContainerView)
|
|
|
|
container.setCustomSpacing(16, after: bioMetaTextAdaptiveMarginContainerView)
|
2022-04-19 14:57:21 +02:00
|
|
|
|
2022-05-16 13:42:03 +02:00
|
|
|
// infoContainer: H - [ statusDashboardView | (spacer) | relationshipActionButton]
|
2022-04-14 15:15:21 +02:00
|
|
|
infoContainer.axis = .horizontal
|
2022-04-18 04:47:55 +02:00
|
|
|
infoContainer.spacing = 8
|
2022-04-14 15:15:21 +02:00
|
|
|
infoContainerAdaptiveMarginContainerView.contentView = infoContainer
|
|
|
|
infoContainerAdaptiveMarginContainerView.margin = ProfileCardView.contentMargin
|
|
|
|
container.addArrangedSubview(infoContainerAdaptiveMarginContainerView)
|
2022-05-17 11:40:19 +02:00
|
|
|
container.setCustomSpacing(16, after: infoContainerAdaptiveMarginContainerView)
|
2022-04-19 14:57:21 +02:00
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
infoContainer.addArrangedSubview(statusDashboardView)
|
2022-04-19 14:57:21 +02:00
|
|
|
let infoContainerSpacer = UIView()
|
2022-04-14 15:15:21 +02:00
|
|
|
infoContainer.addArrangedSubview(UIView())
|
2022-04-19 14:57:21 +02:00
|
|
|
infoContainerSpacer.setContentHuggingPriority(.defaultLow - 100, for: .vertical)
|
|
|
|
infoContainerSpacer.setContentHuggingPriority(.defaultLow - 100, for: .horizontal)
|
2022-04-14 15:15:21 +02:00
|
|
|
let relationshipActionButtonShadowContainer = ShadowBackgroundContainer()
|
2022-04-19 14:57:21 +02:00
|
|
|
relationshipActionButtonShadowContainer.translatesAutoresizingMaskIntoConstraints = false
|
2022-04-14 15:15:21 +02:00
|
|
|
infoContainer.addArrangedSubview(relationshipActionButtonShadowContainer)
|
2022-04-19 14:57:21 +02:00
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
relationshipActionButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
relationshipActionButtonShadowContainer.addSubview(relationshipActionButton)
|
2022-11-17 17:45:27 +01:00
|
|
|
relationshipActionButton.pinToParent()
|
2022-04-14 15:15:21 +02:00
|
|
|
NSLayoutConstraint.activate([
|
2022-04-19 14:57:21 +02:00
|
|
|
relationshipActionButtonShadowContainer.widthAnchor.constraint(greaterThanOrEqualToConstant: ProfileCardView.friendshipActionButtonSize.width).priority(.required - 1),
|
|
|
|
relationshipActionButtonShadowContainer.heightAnchor.constraint(equalToConstant: ProfileCardView.friendshipActionButtonSize.height).priority(.required - 1),
|
2022-04-14 15:15:21 +02:00
|
|
|
])
|
2022-05-16 13:42:03 +02:00
|
|
|
|
|
|
|
familiarFollowersDashboardViewAdaptiveMarginContainerView.contentView = familiarFollowersDashboardView
|
|
|
|
familiarFollowersDashboardViewAdaptiveMarginContainerView.margin = ProfileCardView.contentMargin
|
|
|
|
container.addArrangedSubview(familiarFollowersDashboardViewAdaptiveMarginContainerView)
|
2022-04-19 14:57:21 +02:00
|
|
|
|
2022-04-14 15:15:21 +02:00
|
|
|
let bottomPadding = UIView()
|
|
|
|
bottomPadding.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
container.addArrangedSubview(bottomPadding)
|
|
|
|
NSLayoutConstraint.activate([
|
2022-05-17 11:40:19 +02:00
|
|
|
bottomPadding.heightAnchor.constraint(equalToConstant: 8).priority(.required - 10),
|
2022-04-14 15:15:21 +02:00
|
|
|
])
|
2022-04-15 11:17:39 +02:00
|
|
|
|
|
|
|
relationshipActionButton.addTarget(self, action: #selector(ProfileCardView.relationshipActionButtonDidPressed(_:)), for: .touchUpInside)
|
2022-05-17 12:49:29 +02:00
|
|
|
|
|
|
|
let familiarFollowersDashboardViewTapGestureRecognizer = UITapGestureRecognizer.singleTapGestureRecognizer
|
|
|
|
familiarFollowersDashboardViewTapGestureRecognizer.addTarget(self, action: #selector(ProfileCardView.familiarFollowersDashboardViewDidPressed(_:)))
|
|
|
|
familiarFollowersDashboardView.addGestureRecognizer(familiarFollowersDashboardViewTapGestureRecognizer)
|
2022-04-15 11:17:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
|
|
|
|
super.traitCollectionDidChange(previousTraitCollection)
|
|
|
|
|
|
|
|
viewModel.userInterfaceStyle = traitCollection.userInterfaceStyle
|
2022-04-14 15:15:21 +02:00
|
|
|
}
|
2022-04-18 04:47:55 +02:00
|
|
|
|
|
|
|
public override func layoutSubviews() {
|
2022-04-18 11:44:19 +02:00
|
|
|
updateInfoContainerLayout()
|
2022-04-18 04:47:55 +02:00
|
|
|
super.layoutSubviews()
|
2022-04-18 11:44:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileCardView {
|
2022-04-19 14:57:21 +02:00
|
|
|
public func setupLayoutFrame(_ rect: CGRect) {
|
|
|
|
frame.size.width = rect.width
|
|
|
|
bioMetaTextAdaptiveMarginContainerView.frame.size.width = frame.width
|
|
|
|
bioMetaTextAdaptiveMarginContainerView.contentView?.frame.size.width = frame.width - 2 * bioMetaTextAdaptiveMarginContainerView.margin
|
|
|
|
infoContainerAdaptiveMarginContainerView.frame.size.width = frame.width
|
|
|
|
infoContainerAdaptiveMarginContainerView.contentView?.frame.size.width = frame.width - 2 * infoContainerAdaptiveMarginContainerView.margin
|
|
|
|
}
|
|
|
|
|
2022-04-18 11:44:19 +02:00
|
|
|
private func updateInfoContainerLayout() {
|
2022-04-18 04:47:55 +02:00
|
|
|
let isCompactAdaptive = bounds.width < 350
|
|
|
|
infoContainer.axis = isCompactAdaptive ? .vertical : .horizontal
|
|
|
|
}
|
2022-04-14 15:15:21 +02:00
|
|
|
}
|
2022-04-15 11:17:39 +02:00
|
|
|
|
|
|
|
extension ProfileCardView {
|
|
|
|
@objc private func relationshipActionButtonDidPressed(_ sender: UIButton) {
|
2022-05-17 12:49:29 +02:00
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
2022-04-15 11:17:39 +02:00
|
|
|
assert(sender === relationshipActionButton)
|
|
|
|
delegate?.profileCardView(self, relationshipButtonDidPressed: relationshipActionButton)
|
|
|
|
}
|
2022-05-17 12:49:29 +02:00
|
|
|
|
|
|
|
@objc private func familiarFollowersDashboardViewDidPressed(_ sender: UITapGestureRecognizer) {
|
|
|
|
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public)")
|
|
|
|
assert(sender.view === familiarFollowersDashboardView)
|
|
|
|
delegate?.profileCardView(self, familiarFollowersDashboardViewDidPressed: familiarFollowersDashboardView)
|
|
|
|
}
|
2022-04-15 11:17:39 +02:00
|
|
|
}
|