2022-04-13 14:43:16 +02:00
|
|
|
//
|
|
|
|
// NewsView.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-4-13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-05-11 12:39:34 +02:00
|
|
|
import Combine
|
2022-04-13 14:43:16 +02:00
|
|
|
import MastodonAsset
|
|
|
|
|
|
|
|
public final class NewsView: UIView {
|
2022-05-11 12:39:34 +02:00
|
|
|
|
|
|
|
static let imageViewWidth: CGFloat = 132
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
let container = UIStackView()
|
|
|
|
|
2022-04-18 11:14:49 +02:00
|
|
|
let providerFaviconImageView: UIImageView = {
|
|
|
|
let imageView = UIImageView()
|
|
|
|
imageView.contentMode = .scaleAspectFit
|
|
|
|
imageView.layer.masksToBounds = true
|
|
|
|
imageView.layer.cornerRadius = 2
|
|
|
|
imageView.layer.cornerCurve = .continuous
|
|
|
|
return imageView
|
|
|
|
}()
|
|
|
|
|
2022-04-13 14:43:16 +02:00
|
|
|
let providerNameLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: .systemFont(ofSize: 13, weight: .semibold))
|
|
|
|
label.textColor = Asset.Colors.Label.primary.color
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let headlineLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold))
|
|
|
|
label.textColor = Asset.Colors.Label.primary.color
|
|
|
|
label.numberOfLines = 0
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let footnoteLabel: UILabel = {
|
|
|
|
let label = UILabel()
|
|
|
|
label.font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .systemFont(ofSize: 12, weight: .medium))
|
|
|
|
label.textColor = Asset.Colors.Label.secondary.color
|
|
|
|
return label
|
|
|
|
}()
|
|
|
|
|
|
|
|
let imageView = MediaView()
|
2022-05-11 12:39:34 +02:00
|
|
|
|
|
|
|
// let imageView = UIImageView()
|
|
|
|
// var imageViewMediaConfiguration: MediaView.Configuration?
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
public func prepareForReuse() {
|
2022-04-18 11:14:49 +02:00
|
|
|
providerFaviconImageView.tag = (0..<Int.max).randomElement() ?? -1
|
2022-04-13 14:43:16 +02:00
|
|
|
imageView.prepareForReuse()
|
2022-05-11 12:39:34 +02:00
|
|
|
disposeBag.removeAll()
|
2022-04-13 14:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension NewsView {
|
|
|
|
private func _init() {
|
|
|
|
// container: H - [ textContainer | imageView ]
|
2022-05-11 12:39:34 +02:00
|
|
|
container.distribution = .fill
|
2022-04-13 14:43:16 +02:00
|
|
|
container.axis = .horizontal
|
|
|
|
container.spacing = 8
|
|
|
|
container.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(container)
|
2022-11-17 17:45:27 +01:00
|
|
|
container.pinToParent()
|
2022-04-13 14:43:16 +02:00
|
|
|
|
|
|
|
// textContainer: V - [ providerContainer | headlineLabel | (spacer) | footnoteLabel ]
|
|
|
|
let textContainer = UIStackView()
|
|
|
|
textContainer.axis = .vertical
|
|
|
|
textContainer.spacing = 4
|
|
|
|
container.addArrangedSubview(textContainer)
|
|
|
|
|
2022-04-18 11:14:49 +02:00
|
|
|
// providerContainer: H - [ providerFaviconImageView | providerNameLabel | (spacer) ]
|
2022-04-13 14:43:16 +02:00
|
|
|
let providerContainer = UIStackView()
|
|
|
|
providerContainer.axis = .horizontal
|
2022-04-18 11:14:49 +02:00
|
|
|
providerContainer.spacing = 4
|
2022-04-13 14:43:16 +02:00
|
|
|
textContainer.addArrangedSubview(providerContainer)
|
|
|
|
|
2022-04-18 11:14:49 +02:00
|
|
|
providerFaviconImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
providerContainer.addArrangedSubview(providerFaviconImageView)
|
2022-04-13 14:43:16 +02:00
|
|
|
providerContainer.addArrangedSubview(providerNameLabel)
|
2022-04-18 11:14:49 +02:00
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
providerFaviconImageView.heightAnchor.constraint(equalTo: providerNameLabel.heightAnchor, multiplier: 1.0).priority(.required - 10),
|
|
|
|
providerFaviconImageView.widthAnchor.constraint(equalTo: providerNameLabel.heightAnchor, multiplier: 1.0).priority(.required - 10),
|
|
|
|
])
|
|
|
|
// low priority for intrinsic size hugging
|
|
|
|
providerFaviconImageView.setContentHuggingPriority(.defaultLow - 10, for: .vertical)
|
|
|
|
providerFaviconImageView.setContentHuggingPriority(.defaultLow - 10, for: .horizontal)
|
|
|
|
// high priority but lower then layout constraint for size compression
|
|
|
|
providerFaviconImageView.setContentCompressionResistancePriority(.required - 11, for: .vertical)
|
|
|
|
providerFaviconImageView.setContentCompressionResistancePriority(.required - 11, for: .horizontal)
|
|
|
|
providerNameLabel.setContentCompressionResistancePriority(.required - 1, for: .vertical)
|
|
|
|
providerNameLabel.setContentHuggingPriority(.required - 1, for: .vertical)
|
|
|
|
|
2022-04-13 14:43:16 +02:00
|
|
|
// headlineLabel
|
|
|
|
textContainer.addArrangedSubview(headlineLabel)
|
|
|
|
let spacer = UIView()
|
|
|
|
spacer.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
textContainer.addArrangedSubview(spacer)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
spacer.heightAnchor.constraint(equalToConstant: 24).priority(.required - 1),
|
|
|
|
])
|
|
|
|
// footnoteLabel
|
|
|
|
textContainer.addArrangedSubview(footnoteLabel)
|
|
|
|
|
|
|
|
// imageView
|
|
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
container.addArrangedSubview(imageView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
imageView.widthAnchor.constraint(equalToConstant: 132).priority(.required - 1),
|
|
|
|
])
|
2022-05-11 12:39:34 +02:00
|
|
|
imageView.setContentHuggingPriority(.defaultLow - 100, for: .vertical)
|
|
|
|
imageView.setContentCompressionResistancePriority(.defaultLow - 100, for: .vertical)
|
|
|
|
imageView.imageView.setContentHuggingPriority(.defaultLow - 100, for: .vertical)
|
|
|
|
imageView.imageView.setContentCompressionResistancePriority(.defaultLow - 100, for: .vertical)
|
|
|
|
imageView.blurhashImageView.setContentHuggingPriority(.defaultLow - 100, for: .vertical)
|
|
|
|
imageView.blurhashImageView.setContentCompressionResistancePriority(.defaultLow - 100, for: .vertical)
|
2022-04-13 14:43:16 +02:00
|
|
|
imageView.isUserInteractionEnabled = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|