2021-01-31 06:43:40 +01:00
|
|
|
// Copyright © 2021 Metabolist. All rights reserved.
|
|
|
|
|
2021-02-23 00:59:33 +01:00
|
|
|
import SDWebImage
|
2021-01-31 06:43:40 +01:00
|
|
|
import UIKit
|
|
|
|
|
|
|
|
final class InstanceView: UIView {
|
2021-02-23 00:59:33 +01:00
|
|
|
private let imageView = SDAnimatedImageView()
|
2021-01-31 06:43:40 +01:00
|
|
|
private let titleLabel = UILabel()
|
|
|
|
private let uriLabel = UILabel()
|
|
|
|
private var instanceConfiguration: InstanceContentConfiguration
|
|
|
|
|
|
|
|
init(configuration: InstanceContentConfiguration) {
|
|
|
|
instanceConfiguration = configuration
|
|
|
|
|
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
initialSetup()
|
2021-02-02 05:44:56 +01:00
|
|
|
setupAccessibility()
|
2021-01-31 06:43:40 +01:00
|
|
|
applyInstanceConfiguration()
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension InstanceView: UIContentView {
|
|
|
|
var configuration: UIContentConfiguration {
|
|
|
|
get { instanceConfiguration }
|
|
|
|
set {
|
|
|
|
guard let instanceConfiguration = newValue as? InstanceContentConfiguration else { return }
|
|
|
|
|
|
|
|
self.instanceConfiguration = instanceConfiguration
|
|
|
|
|
|
|
|
applyInstanceConfiguration()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension InstanceView {
|
|
|
|
func initialSetup() {
|
|
|
|
let stackView = UIStackView()
|
|
|
|
|
|
|
|
addSubview(stackView)
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.axis = .vertical
|
|
|
|
stackView.spacing = .defaultSpacing
|
|
|
|
|
|
|
|
stackView.addArrangedSubview(imageView)
|
|
|
|
imageView.layer.cornerRadius = .defaultCornerRadius
|
|
|
|
imageView.clipsToBounds = true
|
|
|
|
imageView.contentMode = .scaleAspectFill
|
|
|
|
|
|
|
|
stackView.addArrangedSubview(titleLabel)
|
|
|
|
titleLabel.adjustsFontSizeToFitWidth = true
|
|
|
|
titleLabel.font = .preferredFont(forTextStyle: .headline)
|
|
|
|
titleLabel.numberOfLines = 0
|
|
|
|
titleLabel.textAlignment = .center
|
|
|
|
|
|
|
|
stackView.addArrangedSubview(uriLabel)
|
|
|
|
uriLabel.adjustsFontSizeToFitWidth = true
|
|
|
|
uriLabel.font = .preferredFont(forTextStyle: .subheadline)
|
|
|
|
uriLabel.numberOfLines = 0
|
|
|
|
uriLabel.textAlignment = .center
|
|
|
|
uriLabel.textColor = .secondaryLabel
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
stackView.leadingAnchor.constraint(equalTo: readableContentGuide.leadingAnchor),
|
|
|
|
stackView.topAnchor.constraint(equalTo: readableContentGuide.topAnchor),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: readableContentGuide.trailingAnchor),
|
|
|
|
stackView.bottomAnchor.constraint(equalTo: readableContentGuide.bottomAnchor),
|
|
|
|
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: 16 / 9)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInstanceConfiguration() {
|
|
|
|
let viewModel = instanceConfiguration.viewModel
|
|
|
|
|
2021-02-23 00:59:33 +01:00
|
|
|
imageView.sd_setImage(with: viewModel.instance.thumbnail)
|
|
|
|
imageView.autoPlayAnimatedImage = !UIAccessibility.isReduceMotionEnabled
|
2021-01-31 06:43:40 +01:00
|
|
|
|
|
|
|
titleLabel.text = viewModel.instance.title
|
|
|
|
uriLabel.text = viewModel.instance.uri
|
2021-02-02 04:39:32 +01:00
|
|
|
|
2021-02-02 05:36:16 +01:00
|
|
|
accessibilityLabel = viewModel.instance.title.appendingWithSeparator(viewModel.instance.uri)
|
2021-02-02 04:39:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupAccessibility() {
|
|
|
|
isAccessibilityElement = true
|
2021-01-31 06:43:40 +01:00
|
|
|
}
|
|
|
|
}
|