metatext-app-ios-iphone-ipad/Views/UIKit/SecondaryNavigationTitleVie...

93 lines
3.7 KiB
Swift
Raw Normal View History

2021-01-28 02:13:17 +01:00
// Copyright © 2021 Metabolist. All rights reserved.
2021-02-23 00:59:33 +01:00
import SDWebImage
2021-01-28 02:13:17 +01:00
import UIKit
import ViewModels
final class SecondaryNavigationTitleView: UIView {
private let viewModel: NavigationViewModel
2021-02-23 00:59:33 +01:00
private let avatarImageView = SDAnimatedImageView()
2021-02-22 08:10:34 +01:00
private let displayNameLabel = AnimatedAttachmentLabel()
2021-01-28 02:13:17 +01:00
private let accountLabel = UILabel()
private let stackView = UIStackView()
init(viewModel: NavigationViewModel) {
self.viewModel = viewModel
super.init(frame: .zero)
initialSetup()
applyViewModel()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension SecondaryNavigationTitleView {
func initialSetup() {
addSubview(avatarImageView)
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
avatarImageView.layer.cornerRadius = .barButtonItemDimension / 2
avatarImageView.contentMode = .scaleAspectFill
avatarImageView.clipsToBounds = true
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = .ultraCompactSpacing
stackView.addArrangedSubview(displayNameLabel)
displayNameLabel.adjustsFontForContentSizeCategory = true
displayNameLabel.adjustsFontSizeToFitWidth = true
displayNameLabel.minimumScaleFactor = 0.5
displayNameLabel.font = .preferredFont(forTextStyle: .headline)
stackView.addArrangedSubview(accountLabel)
accountLabel.adjustsFontForContentSizeCategory = true
accountLabel.adjustsFontSizeToFitWidth = true
accountLabel.minimumScaleFactor = 0.5
accountLabel.font = .preferredFont(forTextStyle: .footnote)
accountLabel.textColor = .secondaryLabel
NSLayoutConstraint.activate([
avatarImageView.widthAnchor.constraint(equalToConstant: .barButtonItemDimension),
avatarImageView.heightAnchor.constraint(equalToConstant: .barButtonItemDimension),
avatarImageView.leadingAnchor.constraint(equalTo: leadingAnchor),
avatarImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
stackView.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: .defaultSpacing),
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
func applyViewModel() {
2021-02-04 02:50:25 +01:00
let avatarURL = viewModel.identityContext.appPreferences.animateAvatars == .everywhere
? viewModel.identityContext.identity.account?.avatar
: viewModel.identityContext.identity.account?.avatarStatic
2021-03-29 08:04:14 +02:00
avatarImageView.sd_setImage(with: avatarURL?.url)
2021-01-28 02:13:17 +01:00
if let displayName = viewModel.identityContext.identity.account?.displayName,
!displayName.isEmpty {
let mutableDisplayName = NSMutableAttributedString(string: displayName)
if let emojis = viewModel.identityContext.identity.account?.emojis {
2021-02-22 08:10:34 +01:00
mutableDisplayName.insert(emojis: emojis,
view: displayNameLabel,
identityContext: viewModel.identityContext)
2021-01-28 02:13:17 +01:00
mutableDisplayName.resizeAttachments(toLineHeight: displayNameLabel.font.lineHeight)
}
displayNameLabel.attributedText = mutableDisplayName
} else {
displayNameLabel.isHidden = true
}
accountLabel.text = viewModel.identityContext.identity.handle
}
}