metatext/Views/AccountHeaderView.swift

352 lines
16 KiB
Swift
Raw Normal View History

2020-09-22 06:53:11 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Kingfisher
import UIKit
import ViewModels
2020-11-09 06:22:20 +00:00
final class AccountHeaderView: UIView {
2020-10-22 22:16:06 +00:00
let headerImageView = AnimatedImageView()
let headerButton = UIButton()
let avatarBackgroundView = UIView()
2020-11-10 06:27:08 +00:00
let avatarImageView = UIImageView()
let avatarButton = UIButton()
2020-11-12 08:32:18 +00:00
let relationshipButtonsStackView = UIStackView()
let followButton = UIButton(type: .system)
let unfollowButton = UIButton(type: .system)
2020-11-10 06:27:08 +00:00
let displayNameLabel = UILabel()
2020-11-11 00:53:31 +00:00
let accountStackView = UIStackView()
2020-11-10 06:27:08 +00:00
let accountLabel = UILabel()
2020-11-11 00:53:31 +00:00
let lockedImageView = UIImageView()
2020-11-10 06:27:08 +00:00
let fieldsStackView = UIStackView()
2020-09-22 06:53:11 +00:00
let noteTextView = TouchFallthroughTextView()
2020-12-03 01:06:46 +00:00
let followStackView = UIStackView()
let followingButton = UIButton()
let followersButton = UIButton()
2020-09-22 06:53:11 +00:00
let segmentedControl = UISegmentedControl()
2020-09-27 02:03:53 +00:00
var viewModel: ProfileViewModel? {
2020-09-22 06:53:11 +00:00
didSet {
2020-09-26 07:13:50 +00:00
if let accountViewModel = viewModel?.accountViewModel {
2020-10-22 22:16:06 +00:00
headerImageView.kf.setImage(with: accountViewModel.headerURL)
headerImageView.tag = accountViewModel.headerURL.hashValue
2020-11-10 06:27:08 +00:00
avatarImageView.kf.setImage(with: accountViewModel.avatarURL(profile: true))
avatarImageView.tag = accountViewModel.avatarURL(profile: true).hashValue
2020-11-12 08:32:18 +00:00
if !accountViewModel.isSelf, let relationship = accountViewModel.relationship {
followButton.setTitle(
NSLocalizedString(
accountViewModel.isLocked ? "account.request" : "account.follow",
comment: ""),
for: .normal)
followButton.isHidden = relationship.following
unfollowButton.isHidden = !relationship.following
relationshipButtonsStackView.isHidden = false
} else {
relationshipButtonsStackView.isHidden = true
}
2020-11-10 06:27:08 +00:00
if accountViewModel.displayName.isEmpty {
displayNameLabel.isHidden = true
} else {
let mutableDisplayName = NSMutableAttributedString(string: accountViewModel.displayName)
2021-01-12 07:33:35 +00:00
mutableDisplayName.insert(emojis: accountViewModel.emojis, view: displayNameLabel)
2020-11-10 06:27:08 +00:00
mutableDisplayName.resizeAttachments(toLineHeight: displayNameLabel.font.lineHeight)
displayNameLabel.attributedText = mutableDisplayName
}
accountLabel.text = accountViewModel.accountName
2020-11-11 00:53:31 +00:00
lockedImageView.isHidden = !accountViewModel.isLocked
2020-11-10 06:27:08 +00:00
for view in fieldsStackView.arrangedSubviews {
fieldsStackView.removeArrangedSubview(view)
view.removeFromSuperview()
}
for identityProof in accountViewModel.identityProofs {
let fieldView = AccountFieldView(
name: identityProof.provider,
value: NSAttributedString(
string: identityProof.providerUsername,
attributes: [.link: identityProof.profileUrl]),
verifiedAt: identityProof.updatedAt,
2021-01-12 07:33:35 +00:00
emojis: [])
fieldView.valueTextView.delegate = self
fieldsStackView.addArrangedSubview(fieldView)
}
2020-11-10 06:27:08 +00:00
for field in accountViewModel.fields {
let fieldView = AccountFieldView(
name: field.name,
value: field.value.attributed,
verifiedAt: field.verifiedAt,
2021-01-12 07:33:35 +00:00
emojis: accountViewModel.emojis)
2020-11-10 06:27:08 +00:00
fieldView.valueTextView.delegate = self
fieldsStackView.addArrangedSubview(fieldView)
}
fieldsStackView.isHidden = accountViewModel.fields.isEmpty
2020-09-22 06:53:11 +00:00
let noteFont = UIFont.preferredFont(forTextStyle: .callout)
2020-09-26 07:13:50 +00:00
let mutableNote = NSMutableAttributedString(attributedString: accountViewModel.note)
2020-09-22 06:53:11 +00:00
let noteRange = NSRange(location: 0, length: mutableNote.length)
mutableNote.removeAttribute(.font, range: noteRange)
mutableNote.addAttributes(
[.font: noteFont as Any,
.foregroundColor: UIColor.label],
range: noteRange)
2021-01-12 07:33:35 +00:00
mutableNote.insert(emojis: accountViewModel.emojis, view: noteTextView)
2020-09-22 06:53:11 +00:00
mutableNote.resizeAttachments(toLineHeight: noteFont.lineHeight)
noteTextView.attributedText = mutableNote
noteTextView.isHidden = false
2020-12-03 01:06:46 +00:00
followingButton.setAttributedLocalizedTitle(
localizationKey: "account.following-count",
count: accountViewModel.followingCount)
followersButton.setAttributedLocalizedTitle(
localizationKey: "account.followers-count",
count: accountViewModel.followersCount)
followStackView.isHidden = false
2020-09-22 06:53:11 +00:00
} else {
noteTextView.isHidden = true
2020-12-03 01:06:46 +00:00
followStackView.isHidden = true
2020-09-22 06:53:11 +00:00
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
2020-10-13 20:11:27 +00:00
initialSetup()
2020-09-22 06:53:11 +00:00
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2020-11-12 08:32:18 +00:00
override func layoutSubviews() {
super.layoutSubviews()
for button in [followButton, unfollowButton] {
let inset = (followButton.bounds.height - (button.titleLabel?.bounds.height ?? 0)) / 2
button.contentEdgeInsets = .init(top: 0, left: inset, bottom: 0, right: inset)
button.layer.cornerRadius = button.bounds.height / 2
}
}
2020-09-22 06:53:11 +00:00
}
2020-09-26 07:45:39 +00:00
extension AccountHeaderView: UITextViewDelegate {
func textView(
_ textView: UITextView,
shouldInteractWith URL: URL,
in characterRange: NSRange,
interaction: UITextItemInteraction) -> Bool {
switch interaction {
case .invokeDefaultAction:
viewModel?.accountViewModel?.urlSelected(URL)
return false
case .preview: return false
case .presentActions: return false
@unknown default: return false
}
}
}
2020-09-22 06:53:11 +00:00
private extension AccountHeaderView {
2020-11-10 06:27:08 +00:00
static let avatarDimension = CGFloat.avatarDimension * 2
2020-10-22 22:16:06 +00:00
// swiftlint:disable:next function_body_length
2020-10-13 20:11:27 +00:00
func initialSetup() {
2020-09-22 06:53:11 +00:00
let baseStackView = UIStackView()
addSubview(headerImageView)
headerImageView.translatesAutoresizingMaskIntoConstraints = false
2020-09-28 05:22:01 +00:00
headerImageView.contentMode = .scaleAspectFill
headerImageView.clipsToBounds = true
2020-10-22 22:16:06 +00:00
headerImageView.isUserInteractionEnabled = true
headerImageView.addSubview(headerButton)
headerButton.translatesAutoresizingMaskIntoConstraints = false
headerButton.setBackgroundImage(.highlightedButtonBackground, for: .highlighted)
2020-11-10 06:27:08 +00:00
headerButton.addAction(UIAction { [weak self] _ in self?.viewModel?.presentHeader() }, for: .touchUpInside)
let avatarBackgroundViewDimension = Self.avatarDimension + .compactSpacing * 2
addSubview(avatarBackgroundView)
avatarBackgroundView.translatesAutoresizingMaskIntoConstraints = false
avatarBackgroundView.backgroundColor = .systemBackground
avatarBackgroundView.layer.cornerRadius = avatarBackgroundViewDimension / 2
avatarBackgroundView.addSubview(avatarImageView)
2020-11-10 06:27:08 +00:00
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
avatarImageView.contentMode = .scaleAspectFill
avatarImageView.clipsToBounds = true
avatarImageView.isUserInteractionEnabled = true
avatarImageView.layer.cornerRadius = Self.avatarDimension / 2
avatarImageView.addSubview(avatarButton)
avatarButton.translatesAutoresizingMaskIntoConstraints = false
avatarButton.setBackgroundImage(.highlightedButtonBackground, for: .highlighted)
avatarButton.addAction(UIAction { [weak self] _ in self?.viewModel?.presentAvatar() }, for: .touchUpInside)
2020-10-22 22:16:06 +00:00
2020-11-12 08:32:18 +00:00
addSubview(relationshipButtonsStackView)
relationshipButtonsStackView.translatesAutoresizingMaskIntoConstraints = false
relationshipButtonsStackView.spacing = .defaultSpacing
relationshipButtonsStackView.addArrangedSubview(UIView())
for button in [followButton, unfollowButton] {
relationshipButtonsStackView.addArrangedSubview(button)
button.titleLabel?.font = .preferredFont(forTextStyle: .headline)
button.titleLabel?.adjustsFontForContentSizeCategory = true
button.backgroundColor = .secondarySystemBackground
}
followButton.setImage(
UIImage(
systemName: "person.badge.plus",
withConfiguration: UIImage.SymbolConfiguration(scale: .small)),
for: .normal)
followButton.addAction(
UIAction { [weak self] _ in self?.viewModel?.accountViewModel?.follow() },
for: .touchUpInside)
unfollowButton.setImage(
UIImage(
systemName: "checkmark",
withConfiguration: UIImage.SymbolConfiguration(scale: .small)),
for: .normal)
unfollowButton.setTitle(NSLocalizedString("account.following", comment: ""), for: .normal)
unfollowButton.showsMenuAsPrimaryAction = true
unfollowButton.menu = UIMenu(children: [UIDeferredMenuElement { [weak self] completion in
guard let accountViewModel = self?.viewModel?.accountViewModel else { return }
let unfollowAction = UIAction(
2020-11-22 20:20:36 +00:00
title: NSLocalizedString("account.unfollow", comment: ""),
2020-11-12 08:32:18 +00:00
image: UIImage(systemName: "person.badge.minus"),
attributes: .destructive) { _ in
accountViewModel.unfollow()
}
completion([unfollowAction])
},
UIAction(title: NSLocalizedString("cancel", comment: "")) { _ in }])
2020-10-22 22:16:06 +00:00
addSubview(baseStackView)
2020-09-22 06:53:11 +00:00
baseStackView.translatesAutoresizingMaskIntoConstraints = false
baseStackView.axis = .vertical
2020-11-10 06:27:08 +00:00
baseStackView.spacing = .defaultSpacing
baseStackView.addArrangedSubview(displayNameLabel)
displayNameLabel.numberOfLines = 0
displayNameLabel.font = .preferredFont(forTextStyle: .headline)
displayNameLabel.adjustsFontForContentSizeCategory = true
2020-11-11 00:53:31 +00:00
baseStackView.addArrangedSubview(accountStackView)
accountStackView.addArrangedSubview(accountLabel)
2020-11-10 06:27:08 +00:00
accountLabel.numberOfLines = 0
accountLabel.font = .preferredFont(forTextStyle: .subheadline)
accountLabel.adjustsFontForContentSizeCategory = true
accountLabel.textColor = .secondaryLabel
2020-11-11 00:53:31 +00:00
accountLabel.setContentHuggingPriority(.required, for: .horizontal)
accountLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
accountStackView.addArrangedSubview(lockedImageView)
lockedImageView.image = UIImage(
systemName: "lock.fill",
withConfiguration: UIImage.SymbolConfiguration(scale: .small))
lockedImageView.tintColor = .secondaryLabel
lockedImageView.contentMode = .scaleAspectFit
accountStackView.addArrangedSubview(UIView())
2020-11-10 06:27:08 +00:00
baseStackView.addArrangedSubview(fieldsStackView)
fieldsStackView.axis = .vertical
fieldsStackView.spacing = .hairline
fieldsStackView.backgroundColor = .separator
fieldsStackView.clipsToBounds = true
fieldsStackView.layer.borderColor = UIColor.separator.cgColor
fieldsStackView.layer.borderWidth = .hairline
fieldsStackView.layer.cornerRadius = .defaultCornerRadius
2020-09-22 06:53:11 +00:00
2020-11-10 06:27:08 +00:00
baseStackView.addArrangedSubview(noteTextView)
2020-09-22 06:53:11 +00:00
noteTextView.isScrollEnabled = false
2020-09-26 07:45:39 +00:00
noteTextView.delegate = self
2020-09-22 06:53:11 +00:00
2020-12-03 01:06:46 +00:00
baseStackView.addArrangedSubview(followStackView)
followStackView.distribution = .fillEqually
followingButton.addAction(
UIAction { [weak self] _ in self?.viewModel?.accountViewModel?.followingSelected() },
for: .touchUpInside)
followStackView.addArrangedSubview(followingButton)
followersButton.addAction(
UIAction { [weak self] _ in self?.viewModel?.accountViewModel?.followersSelected() },
for: .touchUpInside)
followStackView.addArrangedSubview(followersButton)
2020-09-27 02:03:53 +00:00
for (index, collection) in ProfileCollection.allCases.enumerated() {
2020-09-22 06:53:11 +00:00
segmentedControl.insertSegment(
action: UIAction(title: collection.title) { [weak self] _ in
self?.viewModel?.collection = collection
2020-10-05 22:50:05 +00:00
self?.viewModel?.request(maxId: nil, minId: nil)
2020-09-22 06:53:11 +00:00
},
at: index,
animated: false)
}
segmentedControl.selectedSegmentIndex = 0
baseStackView.addArrangedSubview(segmentedControl)
let headerImageAspectRatioConstraint = headerImageView.heightAnchor.constraint(
equalTo: headerImageView.widthAnchor,
2020-09-28 05:22:01 +00:00
multiplier: 1 / 3)
2020-09-22 06:53:11 +00:00
2020-10-12 05:37:34 +00:00
headerImageAspectRatioConstraint.priority = .justBelowMax
2020-09-22 06:53:11 +00:00
NSLayoutConstraint.activate([
headerImageAspectRatioConstraint,
headerImageView.topAnchor.constraint(equalTo: topAnchor),
headerImageView.leadingAnchor.constraint(equalTo: leadingAnchor),
headerImageView.trailingAnchor.constraint(equalTo: trailingAnchor),
2020-10-22 22:16:06 +00:00
headerButton.leadingAnchor.constraint(equalTo: headerImageView.leadingAnchor),
headerButton.topAnchor.constraint(equalTo: headerImageView.topAnchor),
headerButton.bottomAnchor.constraint(equalTo: headerImageView.bottomAnchor),
headerButton.trailingAnchor.constraint(equalTo: headerImageView.trailingAnchor),
avatarBackgroundView.heightAnchor.constraint(equalToConstant: avatarBackgroundViewDimension),
avatarBackgroundView.widthAnchor.constraint(equalToConstant: avatarBackgroundViewDimension),
avatarBackgroundView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
avatarBackgroundView.centerYAnchor.constraint(equalTo: headerImageView.bottomAnchor),
2020-11-10 06:27:08 +00:00
avatarImageView.heightAnchor.constraint(equalToConstant: Self.avatarDimension),
avatarImageView.widthAnchor.constraint(equalToConstant: Self.avatarDimension),
avatarImageView.centerXAnchor.constraint(equalTo: avatarBackgroundView.centerXAnchor),
avatarImageView.centerYAnchor.constraint(equalTo: avatarBackgroundView.centerYAnchor),
2020-11-10 06:27:08 +00:00
avatarButton.leadingAnchor.constraint(equalTo: avatarImageView.leadingAnchor),
avatarButton.topAnchor.constraint(equalTo: avatarImageView.topAnchor),
avatarButton.bottomAnchor.constraint(equalTo: avatarImageView.bottomAnchor),
avatarButton.trailingAnchor.constraint(equalTo: avatarImageView.trailingAnchor),
relationshipButtonsStackView.leadingAnchor.constraint(equalTo: avatarBackgroundView.trailingAnchor),
2020-11-12 08:32:18 +00:00
relationshipButtonsStackView.topAnchor.constraint(
equalTo: headerImageView.bottomAnchor,
constant: .defaultSpacing),
relationshipButtonsStackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
relationshipButtonsStackView.bottomAnchor.constraint(equalTo: avatarBackgroundView.bottomAnchor),
baseStackView.topAnchor.constraint(equalTo: avatarBackgroundView.bottomAnchor, constant: .defaultSpacing),
2020-09-22 06:53:11 +00:00
baseStackView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
baseStackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
2020-11-10 06:27:08 +00:00
baseStackView.bottomAnchor.constraint(equalTo: readableContentGuide.bottomAnchor)
2020-09-22 06:53:11 +00:00
])
}
}