metatext/Views/UIKit/AccountHeaderView.swift

410 lines
19 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
2021-02-04 01:50:25 +00:00
// swiftlint:disable file_length
2020-11-09 06:22:20 +00:00
final class AccountHeaderView: UIView {
2021-01-19 07:29:00 +00:00
let headerImageBackgroundView = UIView()
2020-10-22 22:16:06 +00:00
let headerImageView = AnimatedImageView()
let headerButton = UIButton()
let avatarBackgroundView = UIView()
2021-02-04 01:50:25 +00:00
let avatarImageView = AnimatedImageView()
2020-11-10 06:27:08 +00:00
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()
2021-01-31 18:43:37 +00:00
let followsYouLabel = FollowsYouLabel()
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()
2021-01-31 00:38:56 +00:00
let unavailableLabel = UILabel()
2020-09-22 06:53:11 +00:00
2021-01-31 01:43:48 +00:00
var viewModel: ProfileViewModel {
2020-09-22 06:53:11 +00:00
didSet {
2021-01-31 01:43:48 +00:00
if let accountViewModel = viewModel.accountViewModel {
2021-01-19 07:29:00 +00:00
headerImageView.kf.setImage(with: accountViewModel.headerURL) { [weak self] in
if case let .success(result) = $0, result.image.size != Self.missingHeaderImageSize {
self?.headerButton.isEnabled = true
}
}
2020-10-22 22:16:06 +00:00
headerImageView.tag = accountViewModel.headerURL.hashValue
2021-02-02 20:18:15 +00:00
headerButton.accessibilityLabel = String.localizedStringWithFormat(
NSLocalizedString("account.header.accessibility-label-%@", comment: ""),
accountViewModel.displayName)
2020-11-10 06:27:08 +00:00
avatarImageView.kf.setImage(with: accountViewModel.avatarURL(profile: true))
avatarImageView.tag = accountViewModel.avatarURL(profile: true).hashValue
2021-02-02 20:18:15 +00:00
avatarButton.accessibilityLabel = String.localizedStringWithFormat(
NSLocalizedString("account.avatar.accessibility-label-%@", comment: ""),
accountViewModel.displayName)
2020-11-10 06:27:08 +00:00
2020-11-12 08:32:18 +00:00
if !accountViewModel.isSelf, let relationship = accountViewModel.relationship {
2021-01-31 18:43:37 +00:00
followsYouLabel.isHidden = !relationship.followedBy
2020-11-12 08:32:18 +00:00
followButton.setTitle(
NSLocalizedString(
accountViewModel.isLocked ? "account.request" : "account.follow",
comment: ""),
for: .normal)
followButton.isHidden = relationship.following
unfollowButton.isHidden = !relationship.following
relationshipButtonsStackView.isHidden = false
2021-01-31 00:38:56 +00:00
unavailableLabel.isHidden = !relationship.blockedBy
2020-11-12 08:32:18 +00:00
} else {
relationshipButtonsStackView.isHidden = true
2021-01-31 00:38:56 +00:00
unavailableLabel.isHidden = true
2020-11-12 08:32:18 +00:00
}
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
2021-02-03 23:00:32 +00:00
var accountStackViewAccessibilityLabel = accountViewModel.accountName
if !lockedImageView.isHidden {
accountStackViewAccessibilityLabel
.appendWithSeparator(NSLocalizedString("account.locked.accessibility-label", comment: ""))
}
if !followsYouLabel.isHidden, let followsYouText = followsYouLabel.text {
accountStackViewAccessibilityLabel.appendWithSeparator(followsYouText)
}
accountStackView.accessibilityLabel = accountStackViewAccessibilityLabel
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)
}
2021-01-18 00:39:47 +00:00
fieldsStackView.isHidden = accountViewModel.fields.isEmpty && accountViewModel.identityProofs.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
}
}
}
2021-01-31 01:43:48 +00:00
init(viewModel: ProfileViewModel) {
self.viewModel = viewModel
// Initial size is to avoid unsatisfiable constraint warning
super.init(frame: .init(origin: .zero, size: .init(width: 300, height: 300)))
2020-09-22 06:53:11 +00:00
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:
2021-01-31 01:43:48 +00:00
viewModel.accountViewModel?.urlSelected(URL)
2020-09-26 07:45:39 +00:00
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
2021-01-19 07:29:00 +00:00
static let missingHeaderImageSize = CGSize(width: 1, height: 1)
2020-11-10 06:27:08 +00:00
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()
2021-01-19 07:29:00 +00:00
addSubview(headerImageBackgroundView)
headerImageBackgroundView.translatesAutoresizingMaskIntoConstraints = false
headerImageBackgroundView.backgroundColor = .secondarySystemBackground
2020-09-22 06:53:11 +00:00
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)
2021-01-31 01:43:48 +00:00
headerButton.addAction(UIAction { [weak self] _ in self?.viewModel.presentHeader() }, for: .touchUpInside)
2021-01-19 07:29:00 +00:00
headerButton.isEnabled = false
2020-11-10 06:27:08 +00:00
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)
2021-01-31 01:43:48 +00:00
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(
2021-01-31 01:43:48 +00:00
UIAction { [weak self] _ in self?.viewModel.accountViewModel?.follow() },
2020-11-12 08:32:18 +00:00
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
2021-01-31 01:43:48 +00:00
guard let accountViewModel = self?.viewModel.accountViewModel else { return }
2020-11-12 08:32:18 +00:00
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])
2021-01-31 16:41:06 +00:00
}])
2020-11-12 08:32:18 +00:00
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)
2021-01-31 18:43:37 +00:00
accountStackView.spacing = .compactSpacing
2021-02-03 23:00:32 +00:00
accountStackView.isAccessibilityElement = true
2020-11-11 00:53:31 +00:00
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
2021-01-31 18:43:37 +00:00
accountStackView.addArrangedSubview(followsYouLabel)
followsYouLabel.text = NSLocalizedString("account.follows-you", comment: "")
followsYouLabel.isHidden = true
2020-11-11 00:53:31 +00:00
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-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(
2021-01-31 01:43:48 +00:00
UIAction { [weak self] _ in self?.viewModel.accountViewModel?.followingSelected() },
2020-12-03 01:06:46 +00:00
for: .touchUpInside)
followStackView.addArrangedSubview(followingButton)
followersButton.addAction(
2021-01-31 01:43:48 +00:00
UIAction { [weak self] _ in self?.viewModel.accountViewModel?.followersSelected() },
2020-12-03 01:06:46 +00:00
for: .touchUpInside)
followStackView.addArrangedSubview(followersButton)
2021-01-31 01:43:48 +00:00
let statusWord = viewModel.identityContext.appPreferences.statusWord
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(
2021-01-31 01:43:48 +00:00
action: UIAction(title: collection.title(statusWord: statusWord)) { [weak self] _ in
self?.viewModel.collection = collection
self?.viewModel.request(maxId: nil, minId: nil, search: nil)
2020-09-22 06:53:11 +00:00
},
at: index,
animated: false)
}
segmentedControl.selectedSegmentIndex = 0
baseStackView.addArrangedSubview(segmentedControl)
2021-01-31 00:38:56 +00:00
baseStackView.addArrangedSubview(unavailableLabel)
unavailableLabel.adjustsFontForContentSizeCategory = true
unavailableLabel.font = .preferredFont(forTextStyle: .title3)
unavailableLabel.textAlignment = .center
unavailableLabel.numberOfLines = 0
unavailableLabel.text = NSLocalizedString("account.unavailable", comment: "")
unavailableLabel.isHidden = true
2020-09-22 06:53:11 +00:00
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),
2021-01-19 07:29:00 +00:00
headerImageBackgroundView.leadingAnchor.constraint(equalTo: headerImageView.leadingAnchor),
headerImageBackgroundView.topAnchor.constraint(equalTo: headerImageView.topAnchor),
headerImageBackgroundView.trailingAnchor.constraint(equalTo: headerImageView.trailingAnchor),
headerImageBackgroundView.bottomAnchor.constraint(equalTo: headerImageView.bottomAnchor),
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
])
}
}
2021-02-04 01:50:25 +00:00
// swiftlint:enable file_length