2020-09-23 03:00:56 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Kingfisher
|
2021-01-20 03:47:21 +01:00
|
|
|
import Mastodon
|
2020-09-23 03:00:56 +02:00
|
|
|
import UIKit
|
|
|
|
|
2020-11-09 07:22:20 +01:00
|
|
|
final class AccountView: UIView {
|
2020-09-23 03:00:56 +02:00
|
|
|
let avatarImageView = AnimatedImageView()
|
2020-09-26 02:52:59 +02:00
|
|
|
let displayNameLabel = UILabel()
|
|
|
|
let accountLabel = UILabel()
|
2020-09-23 03:00:56 +02:00
|
|
|
let noteTextView = TouchFallthroughTextView()
|
|
|
|
|
|
|
|
private var accountConfiguration: AccountContentConfiguration
|
|
|
|
|
|
|
|
init(configuration: AccountContentConfiguration) {
|
|
|
|
self.accountConfiguration = configuration
|
|
|
|
|
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
initialSetup()
|
2020-10-13 22:11:27 +02:00
|
|
|
applyAccountConfiguration()
|
2020-09-23 03:00:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 03:47:21 +01:00
|
|
|
extension AccountView {
|
|
|
|
static func estimatedHeight(width: CGFloat, account: Account) -> CGFloat {
|
|
|
|
.defaultSpacing * 2
|
|
|
|
+ .compactSpacing * 2
|
|
|
|
+ account.displayName.height(width: width, font: .preferredFont(forTextStyle: .headline))
|
|
|
|
+ account.acct.height(width: width, font: .preferredFont(forTextStyle: .subheadline))
|
|
|
|
+ account.note.attributed.string.height(width: width, font: .preferredFont(forTextStyle: .callout))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 03:00:56 +02:00
|
|
|
extension AccountView: UIContentView {
|
|
|
|
var configuration: UIContentConfiguration {
|
|
|
|
get { accountConfiguration }
|
|
|
|
set {
|
|
|
|
guard let accountConfiguration = newValue as? AccountContentConfiguration else { return }
|
|
|
|
|
|
|
|
self.accountConfiguration = accountConfiguration
|
|
|
|
|
|
|
|
applyAccountConfiguration()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 02:57:35 +02:00
|
|
|
extension AccountView: UITextViewDelegate {
|
|
|
|
func textView(
|
|
|
|
_ textView: UITextView,
|
|
|
|
shouldInteractWith URL: URL,
|
|
|
|
in characterRange: NSRange,
|
|
|
|
interaction: UITextItemInteraction) -> Bool {
|
|
|
|
switch interaction {
|
|
|
|
case .invokeDefaultAction:
|
|
|
|
accountConfiguration.viewModel.urlSelected(URL)
|
|
|
|
return false
|
|
|
|
case .preview: return false
|
|
|
|
case .presentActions: return false
|
|
|
|
@unknown default: return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 03:00:56 +02:00
|
|
|
private extension AccountView {
|
|
|
|
func initialSetup() {
|
2020-09-26 02:52:59 +02:00
|
|
|
let stackView = UIStackView()
|
2020-09-23 03:00:56 +02:00
|
|
|
|
2020-09-26 02:52:59 +02:00
|
|
|
addSubview(avatarImageView)
|
|
|
|
addSubview(stackView)
|
|
|
|
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
|
2020-10-12 07:37:34 +02:00
|
|
|
avatarImageView.layer.cornerRadius = .avatarDimension / 2
|
2020-09-26 02:52:59 +02:00
|
|
|
avatarImageView.clipsToBounds = true
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.axis = .vertical
|
2020-09-29 03:32:28 +02:00
|
|
|
stackView.spacing = .compactSpacing
|
2020-09-26 02:52:59 +02:00
|
|
|
stackView.addArrangedSubview(displayNameLabel)
|
|
|
|
stackView.addArrangedSubview(accountLabel)
|
|
|
|
stackView.addArrangedSubview(noteTextView)
|
|
|
|
displayNameLabel.numberOfLines = 0
|
|
|
|
displayNameLabel.font = .preferredFont(forTextStyle: .headline)
|
|
|
|
displayNameLabel.adjustsFontForContentSizeCategory = true
|
|
|
|
accountLabel.numberOfLines = 0
|
|
|
|
accountLabel.font = .preferredFont(forTextStyle: .subheadline)
|
|
|
|
accountLabel.adjustsFontForContentSizeCategory = true
|
|
|
|
accountLabel.textColor = .secondaryLabel
|
|
|
|
noteTextView.backgroundColor = .clear
|
2020-09-26 02:57:35 +02:00
|
|
|
noteTextView.delegate = self
|
2020-09-23 03:00:56 +02:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
2020-10-12 07:37:34 +02:00
|
|
|
avatarImageView.widthAnchor.constraint(equalToConstant: .avatarDimension),
|
|
|
|
avatarImageView.heightAnchor.constraint(equalToConstant: .avatarDimension),
|
2020-09-26 02:52:59 +02:00
|
|
|
avatarImageView.topAnchor.constraint(equalTo: readableContentGuide.topAnchor),
|
|
|
|
avatarImageView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
|
|
|
|
avatarImageView.bottomAnchor.constraint(lessThanOrEqualTo: readableContentGuide.bottomAnchor),
|
2020-09-29 03:32:28 +02:00
|
|
|
stackView.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: .defaultSpacing),
|
2020-09-26 02:52:59 +02:00
|
|
|
stackView.topAnchor.constraint(equalTo: readableContentGuide.topAnchor),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
|
|
|
|
stackView.bottomAnchor.constraint(equalTo: readableContentGuide.bottomAnchor)
|
2020-09-23 03:00:56 +02:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyAccountConfiguration() {
|
2020-10-23 00:16:06 +02:00
|
|
|
avatarImageView.kf.setImage(with: accountConfiguration.viewModel.avatarURL(profile: false))
|
2020-09-26 02:52:59 +02:00
|
|
|
|
2020-12-03 23:32:15 +01:00
|
|
|
if accountConfiguration.viewModel.displayName.isEmpty {
|
2020-09-26 02:52:59 +02:00
|
|
|
displayNameLabel.isHidden = true
|
|
|
|
} else {
|
|
|
|
let mutableDisplayName = NSMutableAttributedString(string: accountConfiguration.viewModel.displayName)
|
|
|
|
|
2021-01-12 08:33:35 +01:00
|
|
|
mutableDisplayName.insert(emojis: accountConfiguration.viewModel.emojis, view: displayNameLabel)
|
2020-09-26 02:52:59 +02:00
|
|
|
mutableDisplayName.resizeAttachments(toLineHeight: displayNameLabel.font.lineHeight)
|
|
|
|
displayNameLabel.attributedText = mutableDisplayName
|
|
|
|
}
|
|
|
|
|
|
|
|
accountLabel.text = accountConfiguration.viewModel.accountName
|
|
|
|
|
|
|
|
let noteFont = UIFont.preferredFont(forTextStyle: .callout)
|
|
|
|
let mutableNote = NSMutableAttributedString(attributedString: accountConfiguration.viewModel.note)
|
|
|
|
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 08:33:35 +01:00
|
|
|
mutableNote.insert(emojis: accountConfiguration.viewModel.emojis, view: noteTextView)
|
2020-09-26 02:52:59 +02:00
|
|
|
mutableNote.resizeAttachments(toLineHeight: noteFont.lineHeight)
|
|
|
|
|
|
|
|
noteTextView.attributedText = mutableNote
|
2020-09-23 03:00:56 +02:00
|
|
|
}
|
|
|
|
}
|