metatext/Views/Status/CardView.swift

124 lines
4.9 KiB
Swift
Raw Normal View History

2020-09-29 01:14:43 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Kingfisher
2021-01-19 00:46:38 +00:00
import Mastodon
2020-09-29 01:14:43 +00:00
import UIKit
import ViewModels
final class CardView: UIView {
let imageView = UIImageView()
let titleLabel = UILabel()
let descriptionLabel = UILabel()
let urlLabel = UILabel()
let button = UIButton()
var viewModel: CardViewModel? {
didSet {
guard let viewModel = viewModel else { return }
imageView.isHidden = viewModel.imageURL == nil
imageView.kf.setImage(with: viewModel.imageURL)
titleLabel.text = viewModel.title
descriptionLabel.text = viewModel.description
descriptionLabel.isHidden = descriptionLabel.text == "" || descriptionLabel.text == titleLabel.text
if
let host = viewModel.url.host, host.hasPrefix("www."),
let withoutWww = host.components(separatedBy: "www.").last {
urlLabel.text = withoutWww
} else {
urlLabel.text = viewModel.url.host
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
2020-10-13 20:11:27 +00:00
initialSetup()
2020-09-29 01:14:43 +00:00
}
2020-10-13 20:11:27 +00:00
@available(*, unavailable)
2020-09-29 01:14:43 +00:00
required init?(coder: NSCoder) {
2020-10-13 20:11:27 +00:00
fatalError("init(coder:) has not been implemented")
2020-09-29 01:14:43 +00:00
}
}
2021-01-19 00:46:38 +00:00
extension CardView {
static func estimatedHeight(width: CGFloat,
2021-01-26 00:06:35 +00:00
identityContext: IdentityContext,
2021-01-19 00:46:38 +00:00
status: Status,
configuration: CollectionItem.StatusConfiguration) -> CGFloat {
if status.displayStatus.card != nil {
return round(UIFont.preferredFont(forTextStyle: .headline).lineHeight
+ UIFont.preferredFont(forTextStyle: .subheadline).lineHeight
+ UIFont.preferredFont(forTextStyle: .footnote).lineHeight
+ .defaultSpacing * 2
+ .compactSpacing * 2)
} else {
return 0
}
}
}
2020-09-29 01:14:43 +00:00
private extension CardView {
// swiftlint:disable:next function_body_length
2020-10-13 20:11:27 +00:00
func initialSetup() {
2020-09-29 01:14:43 +00:00
backgroundColor = .secondarySystemBackground
layer.cornerRadius = .defaultCornerRadius
clipsToBounds = true
let stackView = UIStackView()
let innerStackView = UIStackView()
addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.setBackgroundImage(.highlightedButtonBackground, for: .highlighted)
stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(innerStackView)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
imageView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
innerStackView.axis = .vertical
innerStackView.isLayoutMarginsRelativeArrangement = true
innerStackView.directionalLayoutMargins =
.init(top: .defaultSpacing, leading: .defaultSpacing, bottom: .defaultSpacing, trailing: .defaultSpacing)
innerStackView.spacing = .compactSpacing
innerStackView.addArrangedSubview(titleLabel)
innerStackView.addArrangedSubview(descriptionLabel)
innerStackView.addArrangedSubview(urlLabel)
titleLabel.font = .preferredFont(forTextStyle: .headline)
titleLabel.adjustsFontForContentSizeCategory = true
titleLabel.setContentCompressionResistancePriority(.required, for: .vertical)
descriptionLabel.font = .preferredFont(forTextStyle: .subheadline)
descriptionLabel.adjustsFontForContentSizeCategory = true
descriptionLabel.setContentCompressionResistancePriority(.required, for: .vertical)
urlLabel.font = .preferredFont(forTextStyle: .footnote)
urlLabel.adjustsFontForContentSizeCategory = true
urlLabel.setContentCompressionResistancePriority(.required, for: .vertical)
urlLabel.textColor = .secondaryLabel
NSLayoutConstraint.activate([
stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: trailingAnchor),
stackView.topAnchor.constraint(equalTo: topAnchor),
stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
button.leadingAnchor.constraint(equalTo: leadingAnchor),
button.trailingAnchor.constraint(equalTo: trailingAnchor),
button.topAnchor.constraint(equalTo: topAnchor),
button.bottomAnchor.constraint(equalTo: bottomAnchor),
imageView.heightAnchor.constraint(equalTo: innerStackView.heightAnchor),
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor)
])
}
}