metatext/Views/AccountHeaderView.swift

121 lines
4.4 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
class AccountHeaderView: UIView {
let headerImageView = UIImageView()
let noteTextView = TouchFallthroughTextView()
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-15 07:44:01 +00:00
let appPreferences = accountViewModel.identification.appPreferences
let headerURL: URL
if !appPreferences.shouldReduceMotion, appPreferences.animateHeaders {
headerURL = accountViewModel.headerURL
} else {
headerURL = accountViewModel.headerStaticURL
}
headerImageView.kf.setImage(with: headerURL)
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)
2020-09-26 07:13:50 +00:00
mutableNote.insert(emoji: accountViewModel.emoji, view: noteTextView)
2020-09-22 06:53:11 +00:00
mutableNote.resizeAttachments(toLineHeight: noteFont.lineHeight)
noteTextView.attributedText = mutableNote
noteTextView.isHidden = false
} else {
noteTextView.isHidden = true
}
}
}
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-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-10-13 20:11:27 +00:00
func initialSetup() {
2020-09-22 06:53:11 +00:00
let baseStackView = UIStackView()
addSubview(headerImageView)
addSubview(baseStackView)
headerImageView.translatesAutoresizingMaskIntoConstraints = false
2020-09-28 05:22:01 +00:00
headerImageView.contentMode = .scaleAspectFill
headerImageView.clipsToBounds = true
2020-09-22 06:53:11 +00:00
baseStackView.translatesAutoresizingMaskIntoConstraints = false
baseStackView.axis = .vertical
noteTextView.isScrollEnabled = false
2020-09-26 07:45:39 +00:00
noteTextView.delegate = self
2020-09-22 06:53:11 +00:00
baseStackView.addArrangedSubview(noteTextView)
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),
baseStackView.topAnchor.constraint(equalTo: headerImageView.bottomAnchor),
baseStackView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
baseStackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
baseStackView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
}