metatext-app-ios-iphone-ipad/Views/AccountHeaderView.swift

342 lines
15 KiB
Swift
Raw Normal View History

2020-09-22 08:53:11 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Kingfisher
import UIKit
import ViewModels
2020-11-09 07:22:20 +01:00
final class AccountHeaderView: UIView {
2020-10-23 00:16:06 +02:00
let headerImageView = AnimatedImageView()
let headerButton = UIButton()
2020-11-10 07:27:08 +01:00
let avatarImageView = UIImageView()
let avatarButton = UIButton()
2020-11-12 09:32:18 +01:00
let relationshipButtonsStackView = UIStackView()
let followButton = UIButton(type: .system)
let unfollowButton = UIButton(type: .system)
2020-11-10 07:27:08 +01:00
let displayNameLabel = UILabel()
2020-11-11 01:53:31 +01:00
let accountStackView = UIStackView()
2020-11-10 07:27:08 +01:00
let accountLabel = UILabel()
2020-11-11 01:53:31 +01:00
let lockedImageView = UIImageView()
2020-11-10 07:27:08 +01:00
let fieldsStackView = UIStackView()
2020-09-22 08:53:11 +02:00
let noteTextView = TouchFallthroughTextView()
2020-12-03 02:06:46 +01:00
let followStackView = UIStackView()
let followingButton = UIButton()
let followersButton = UIButton()
2020-09-22 08:53:11 +02:00
let segmentedControl = UISegmentedControl()
2020-09-27 04:03:53 +02:00
var viewModel: ProfileViewModel? {
2020-09-22 08:53:11 +02:00
didSet {
2020-09-26 09:13:50 +02:00
if let accountViewModel = viewModel?.accountViewModel {
2020-10-23 00:16:06 +02:00
headerImageView.kf.setImage(with: accountViewModel.headerURL)
headerImageView.tag = accountViewModel.headerURL.hashValue
2020-11-10 07:27:08 +01:00
avatarImageView.kf.setImage(with: accountViewModel.avatarURL(profile: true))
avatarImageView.tag = accountViewModel.avatarURL(profile: true).hashValue
2020-11-12 09:32:18 +01: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 07:27:08 +01:00
if accountViewModel.displayName.isEmpty {
displayNameLabel.isHidden = true
} else {
let mutableDisplayName = NSMutableAttributedString(string: accountViewModel.displayName)
mutableDisplayName.insert(emoji: accountViewModel.emoji, view: displayNameLabel)
mutableDisplayName.resizeAttachments(toLineHeight: displayNameLabel.font.lineHeight)
displayNameLabel.attributedText = mutableDisplayName
}
accountLabel.text = accountViewModel.accountName
2020-11-11 01:53:31 +01:00
lockedImageView.isHidden = !accountViewModel.isLocked
2020-11-10 07:27:08 +01: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,
emoji: [])
fieldView.valueTextView.delegate = self
fieldsStackView.addArrangedSubview(fieldView)
}
2020-11-10 07:27:08 +01:00
for field in accountViewModel.fields {
let fieldView = AccountFieldView(
name: field.name,
value: field.value.attributed,
verifiedAt: field.verifiedAt,
emoji: accountViewModel.emoji)
2020-11-10 07:27:08 +01:00
fieldView.valueTextView.delegate = self
fieldsStackView.addArrangedSubview(fieldView)
}
fieldsStackView.isHidden = accountViewModel.fields.isEmpty
2020-09-22 08:53:11 +02:00
let noteFont = UIFont.preferredFont(forTextStyle: .callout)
2020-09-26 09:13:50 +02:00
let mutableNote = NSMutableAttributedString(attributedString: accountViewModel.note)
2020-09-22 08:53:11 +02: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)
2020-09-26 09:13:50 +02:00
mutableNote.insert(emoji: accountViewModel.emoji, view: noteTextView)
2020-09-22 08:53:11 +02:00
mutableNote.resizeAttachments(toLineHeight: noteFont.lineHeight)
noteTextView.attributedText = mutableNote
noteTextView.isHidden = false
2020-12-03 02:06:46 +01: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 08:53:11 +02:00
} else {
noteTextView.isHidden = true
2020-12-03 02:06:46 +01:00
followStackView.isHidden = true
2020-09-22 08:53:11 +02:00
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
2020-10-13 22:11:27 +02:00
initialSetup()
2020-09-22 08:53:11 +02:00
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2020-11-12 09:32:18 +01: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 08:53:11 +02:00
}
2020-09-26 09:45:39 +02: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 08:53:11 +02:00
private extension AccountHeaderView {
2020-11-10 07:27:08 +01:00
static let avatarDimension = CGFloat.avatarDimension * 2
2020-10-23 00:16:06 +02:00
// swiftlint:disable:next function_body_length
2020-10-13 22:11:27 +02:00
func initialSetup() {
2020-09-22 08:53:11 +02:00
let baseStackView = UIStackView()
addSubview(headerImageView)
headerImageView.translatesAutoresizingMaskIntoConstraints = false
2020-09-28 07:22:01 +02:00
headerImageView.contentMode = .scaleAspectFill
headerImageView.clipsToBounds = true
2020-10-23 00:16:06 +02:00
headerImageView.isUserInteractionEnabled = true
headerImageView.addSubview(headerButton)
headerButton.translatesAutoresizingMaskIntoConstraints = false
headerButton.setBackgroundImage(.highlightedButtonBackground, for: .highlighted)
2020-11-10 07:27:08 +01:00
headerButton.addAction(UIAction { [weak self] _ in self?.viewModel?.presentHeader() }, for: .touchUpInside)
addSubview(avatarImageView)
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
avatarImageView.contentMode = .scaleAspectFill
avatarImageView.clipsToBounds = true
avatarImageView.isUserInteractionEnabled = true
avatarImageView.layer.cornerRadius = Self.avatarDimension / 2
avatarImageView.layer.borderWidth = .compactSpacing
avatarImageView.layer.borderColor = UIColor.systemBackground.cgColor
avatarImageView.addSubview(avatarButton)
avatarButton.translatesAutoresizingMaskIntoConstraints = false
avatarButton.setBackgroundImage(.highlightedButtonBackground, for: .highlighted)
avatarButton.addAction(UIAction { [weak self] _ in self?.viewModel?.presentAvatar() }, for: .touchUpInside)
2020-10-23 00:16:06 +02:00
2020-11-12 09:32:18 +01: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 21:20:36 +01:00
title: NSLocalizedString("account.unfollow", comment: ""),
2020-11-12 09:32:18 +01:00
image: UIImage(systemName: "person.badge.minus"),
attributes: .destructive) { _ in
accountViewModel.unfollow()
}
completion([unfollowAction])
},
UIAction(title: NSLocalizedString("cancel", comment: "")) { _ in }])
2020-10-23 00:16:06 +02:00
addSubview(baseStackView)
2020-09-22 08:53:11 +02:00
baseStackView.translatesAutoresizingMaskIntoConstraints = false
baseStackView.axis = .vertical
2020-11-10 07:27:08 +01:00
baseStackView.spacing = .defaultSpacing
baseStackView.addArrangedSubview(displayNameLabel)
displayNameLabel.numberOfLines = 0
displayNameLabel.font = .preferredFont(forTextStyle: .headline)
displayNameLabel.adjustsFontForContentSizeCategory = true
2020-11-11 01:53:31 +01:00
baseStackView.addArrangedSubview(accountStackView)
accountStackView.addArrangedSubview(accountLabel)
2020-11-10 07:27:08 +01:00
accountLabel.numberOfLines = 0
accountLabel.font = .preferredFont(forTextStyle: .subheadline)
accountLabel.adjustsFontForContentSizeCategory = true
accountLabel.textColor = .secondaryLabel
2020-11-11 01:53:31 +01: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 07:27:08 +01: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 08:53:11 +02:00
2020-11-10 07:27:08 +01:00
baseStackView.addArrangedSubview(noteTextView)
2020-09-22 08:53:11 +02:00
noteTextView.isScrollEnabled = false
2020-09-26 09:45:39 +02:00
noteTextView.delegate = self
2020-09-22 08:53:11 +02:00
2020-12-03 02:06:46 +01: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 04:03:53 +02:00
for (index, collection) in ProfileCollection.allCases.enumerated() {
2020-09-22 08:53:11 +02:00
segmentedControl.insertSegment(
action: UIAction(title: collection.title) { [weak self] _ in
self?.viewModel?.collection = collection
2020-10-06 00:50:05 +02:00
self?.viewModel?.request(maxId: nil, minId: nil)
2020-09-22 08:53:11 +02:00
},
at: index,
animated: false)
}
segmentedControl.selectedSegmentIndex = 0
baseStackView.addArrangedSubview(segmentedControl)
let headerImageAspectRatioConstraint = headerImageView.heightAnchor.constraint(
equalTo: headerImageView.widthAnchor,
2020-09-28 07:22:01 +02:00
multiplier: 1 / 3)
2020-09-22 08:53:11 +02:00
2020-10-12 07:37:34 +02:00
headerImageAspectRatioConstraint.priority = .justBelowMax
2020-09-22 08:53:11 +02:00
NSLayoutConstraint.activate([
headerImageAspectRatioConstraint,
headerImageView.topAnchor.constraint(equalTo: topAnchor),
headerImageView.leadingAnchor.constraint(equalTo: leadingAnchor),
headerImageView.trailingAnchor.constraint(equalTo: trailingAnchor),
2020-10-23 00:16:06 +02: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),
2020-11-10 07:27:08 +01:00
avatarImageView.heightAnchor.constraint(equalToConstant: Self.avatarDimension),
avatarImageView.widthAnchor.constraint(equalToConstant: Self.avatarDimension),
avatarImageView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
avatarImageView.centerYAnchor.constraint(equalTo: headerImageView.bottomAnchor),
avatarButton.leadingAnchor.constraint(equalTo: avatarImageView.leadingAnchor),
avatarButton.topAnchor.constraint(equalTo: avatarImageView.topAnchor),
avatarButton.bottomAnchor.constraint(equalTo: avatarImageView.bottomAnchor),
avatarButton.trailingAnchor.constraint(equalTo: avatarImageView.trailingAnchor),
2020-11-12 09:32:18 +01:00
relationshipButtonsStackView.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor),
relationshipButtonsStackView.topAnchor.constraint(
equalTo: headerImageView.bottomAnchor,
constant: .defaultSpacing),
relationshipButtonsStackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
relationshipButtonsStackView.bottomAnchor.constraint(equalTo: avatarImageView.bottomAnchor),
2020-11-10 07:27:08 +01:00
baseStackView.topAnchor.constraint(equalTo: avatarImageView.bottomAnchor, constant: .defaultSpacing),
2020-09-22 08:53:11 +02:00
baseStackView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
baseStackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
2020-11-10 07:27:08 +01:00
baseStackView.bottomAnchor.constraint(equalTo: readableContentGuide.bottomAnchor)
2020-09-22 08:53:11 +02:00
])
}
}