2020-10-31 01:29:43 +01:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import ViewModels
|
|
|
|
|
|
|
|
final class StatusBodyView: UIView {
|
|
|
|
let spoilerTextLabel = UILabel()
|
|
|
|
let toggleShowContentButton = UIButton(type: .system)
|
|
|
|
let contentTextView = TouchFallthroughTextView()
|
2021-01-08 07:11:33 +01:00
|
|
|
let attachmentsView = AttachmentsView()
|
2020-10-31 01:29:43 +01:00
|
|
|
let pollView = PollView()
|
|
|
|
let cardView = CardView()
|
|
|
|
|
|
|
|
var viewModel: StatusViewModel? {
|
|
|
|
didSet {
|
|
|
|
guard let viewModel = viewModel else { return }
|
|
|
|
|
|
|
|
let isContextParent = viewModel.configuration.isContextParent
|
|
|
|
let mutableContent = NSMutableAttributedString(attributedString: viewModel.content)
|
|
|
|
let mutableSpoilerText = NSMutableAttributedString(string: viewModel.spoilerText)
|
|
|
|
let contentFont = UIFont.preferredFont(forTextStyle: isContextParent ? .title3 : .callout)
|
|
|
|
let contentRange = NSRange(location: 0, length: mutableContent.length)
|
|
|
|
|
|
|
|
contentTextView.shouldFallthrough = !isContextParent
|
|
|
|
|
|
|
|
mutableContent.removeAttribute(.font, range: contentRange)
|
|
|
|
mutableContent.addAttributes(
|
|
|
|
[.font: contentFont, .foregroundColor: UIColor.label],
|
|
|
|
range: contentRange)
|
2021-01-12 08:33:35 +01:00
|
|
|
mutableContent.insert(emojis: viewModel.contentEmojis, view: contentTextView)
|
2020-10-31 01:29:43 +01:00
|
|
|
mutableContent.resizeAttachments(toLineHeight: contentFont.lineHeight)
|
|
|
|
contentTextView.attributedText = mutableContent
|
2020-12-03 23:32:15 +01:00
|
|
|
contentTextView.isHidden = contentTextView.text.isEmpty
|
2020-10-31 01:29:43 +01:00
|
|
|
|
2021-01-12 08:33:35 +01:00
|
|
|
mutableSpoilerText.insert(emojis: viewModel.contentEmojis, view: spoilerTextLabel)
|
2020-10-31 01:29:43 +01:00
|
|
|
mutableSpoilerText.resizeAttachments(toLineHeight: spoilerTextLabel.font.lineHeight)
|
|
|
|
spoilerTextLabel.font = contentFont
|
|
|
|
spoilerTextLabel.attributedText = mutableSpoilerText
|
|
|
|
spoilerTextLabel.isHidden = spoilerTextLabel.text == ""
|
|
|
|
toggleShowContentButton.setTitle(
|
|
|
|
viewModel.shouldShowContent
|
|
|
|
? NSLocalizedString("status.show-less", comment: "")
|
|
|
|
: NSLocalizedString("status.show-more", comment: ""),
|
|
|
|
for: .normal)
|
2020-12-03 23:32:15 +01:00
|
|
|
toggleShowContentButton.isHidden = viewModel.spoilerText.isEmpty
|
2020-10-31 01:29:43 +01:00
|
|
|
|
|
|
|
contentTextView.isHidden = !viewModel.shouldShowContent
|
|
|
|
|
2020-12-03 23:32:15 +01:00
|
|
|
attachmentsView.isHidden = viewModel.attachmentViewModels.isEmpty
|
2020-10-31 01:29:43 +01:00
|
|
|
attachmentsView.viewModel = viewModel
|
|
|
|
|
2020-12-03 23:32:15 +01:00
|
|
|
pollView.isHidden = viewModel.pollOptions.isEmpty
|
2020-10-31 01:29:43 +01:00
|
|
|
pollView.viewModel = viewModel
|
|
|
|
|
|
|
|
cardView.viewModel = viewModel.cardViewModel
|
|
|
|
cardView.isHidden = viewModel.cardViewModel == nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
|
|
|
|
initialSetup()
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension StatusBodyView: UITextViewDelegate {
|
|
|
|
func textView(
|
|
|
|
_ textView: UITextView,
|
|
|
|
shouldInteractWith URL: URL,
|
|
|
|
in characterRange: NSRange,
|
|
|
|
interaction: UITextItemInteraction) -> Bool {
|
|
|
|
switch interaction {
|
|
|
|
case .invokeDefaultAction:
|
|
|
|
viewModel?.urlSelected(URL)
|
|
|
|
return false
|
|
|
|
case .preview: return false
|
|
|
|
case .presentActions: return false
|
|
|
|
@unknown default: return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension StatusBodyView {
|
|
|
|
func initialSetup() {
|
|
|
|
let stackView = UIStackView()
|
|
|
|
|
|
|
|
addSubview(stackView)
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.axis = .vertical
|
|
|
|
stackView.spacing = .compactSpacing
|
|
|
|
|
|
|
|
spoilerTextLabel.numberOfLines = 0
|
|
|
|
spoilerTextLabel.adjustsFontForContentSizeCategory = true
|
|
|
|
stackView.addArrangedSubview(spoilerTextLabel)
|
|
|
|
|
|
|
|
toggleShowContentButton.titleLabel?.font = .preferredFont(forTextStyle: .headline)
|
|
|
|
toggleShowContentButton.titleLabel?.adjustsFontForContentSizeCategory = true
|
|
|
|
toggleShowContentButton.addAction(
|
|
|
|
UIAction { [weak self] _ in self?.viewModel?.toggleShowContent() },
|
|
|
|
for: .touchUpInside)
|
|
|
|
stackView.addArrangedSubview(toggleShowContentButton)
|
|
|
|
|
|
|
|
contentTextView.adjustsFontForContentSizeCategory = true
|
|
|
|
contentTextView.isScrollEnabled = false
|
|
|
|
contentTextView.backgroundColor = .clear
|
|
|
|
contentTextView.delegate = self
|
|
|
|
stackView.addArrangedSubview(contentTextView)
|
|
|
|
|
|
|
|
stackView.addArrangedSubview(attachmentsView)
|
|
|
|
|
|
|
|
stackView.addArrangedSubview(pollView)
|
|
|
|
|
|
|
|
cardView.button.addAction(
|
|
|
|
UIAction { [weak self] _ in
|
|
|
|
guard
|
|
|
|
let viewModel = self?.viewModel,
|
|
|
|
let url = viewModel.cardViewModel?.url
|
|
|
|
else { return }
|
|
|
|
|
|
|
|
viewModel.urlSelected(url)
|
|
|
|
},
|
|
|
|
for: .touchUpInside)
|
|
|
|
stackView.addArrangedSubview(cardView)
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
stackView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
stackView.bottomAnchor.constraint(equalTo: bottomAnchor)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|