2022-11-12 03:35:18 +01:00
|
|
|
//
|
|
|
|
// OpenGraphView.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by Kyle Bashour on 11/11/22.
|
|
|
|
//
|
|
|
|
|
2022-11-14 22:26:25 +01:00
|
|
|
import AlamofireImage
|
|
|
|
import LinkPresentation
|
2022-11-12 03:35:18 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonCore
|
2022-11-24 06:51:39 +01:00
|
|
|
import CoreDataStack
|
2022-11-12 03:35:18 +01:00
|
|
|
import UIKit
|
|
|
|
|
2022-11-14 22:26:25 +01:00
|
|
|
public final class LinkPreviewButton: UIControl {
|
2022-11-12 03:35:18 +01:00
|
|
|
private let containerStackView = UIStackView()
|
|
|
|
private let labelStackView = UIStackView()
|
|
|
|
|
|
|
|
private let imageView = UIImageView()
|
|
|
|
private let titleLabel = UILabel()
|
2022-11-24 06:51:39 +01:00
|
|
|
private let linkLabel = UILabel()
|
|
|
|
|
|
|
|
private lazy var compactImageConstraints = [
|
|
|
|
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor),
|
|
|
|
imageView.heightAnchor.constraint(equalTo: heightAnchor),
|
|
|
|
containerStackView.heightAnchor.constraint(equalToConstant: 85),
|
|
|
|
]
|
|
|
|
|
|
|
|
private lazy var largeImageConstraints = [
|
|
|
|
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: 21 / 40),
|
|
|
|
]
|
2022-11-12 03:35:18 +01:00
|
|
|
|
|
|
|
public override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
|
|
|
|
clipsToBounds = true
|
|
|
|
layer.cornerCurve = .continuous
|
|
|
|
layer.cornerRadius = 10
|
|
|
|
layer.borderColor = ThemeService.shared.currentTheme.value.separator.cgColor
|
|
|
|
|
2022-11-14 22:26:25 +01:00
|
|
|
titleLabel.numberOfLines = 2
|
2022-11-12 03:35:18 +01:00
|
|
|
titleLabel.setContentCompressionResistancePriority(.defaultLow - 1, for: .horizontal)
|
|
|
|
titleLabel.text = "This is where I'd put a title... if I had one"
|
|
|
|
titleLabel.textColor = Asset.Colors.Label.primary.color
|
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
linkLabel.text = "Subtitle"
|
|
|
|
linkLabel.numberOfLines = 1
|
|
|
|
linkLabel.setContentCompressionResistancePriority(.defaultLow - 1, for: .horizontal)
|
|
|
|
linkLabel.textColor = Asset.Colors.Label.secondary.color
|
2022-11-12 03:35:18 +01:00
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
imageView.tintColor = Asset.Colors.Label.secondary.color
|
|
|
|
imageView.backgroundColor = ThemeService.shared.currentTheme.value.systemElevatedBackgroundColor
|
2022-11-14 22:26:25 +01:00
|
|
|
imageView.contentMode = .scaleAspectFill
|
|
|
|
imageView.clipsToBounds = true
|
2022-11-12 03:35:18 +01:00
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
labelStackView.addArrangedSubview(linkLabel)
|
2022-11-12 03:35:18 +01:00
|
|
|
labelStackView.addArrangedSubview(titleLabel)
|
|
|
|
labelStackView.layoutMargins = .init(top: 8, left: 10, bottom: 8, right: 10)
|
|
|
|
labelStackView.isLayoutMarginsRelativeArrangement = true
|
|
|
|
labelStackView.axis = .vertical
|
|
|
|
|
|
|
|
containerStackView.addArrangedSubview(imageView)
|
|
|
|
containerStackView.addArrangedSubview(labelStackView)
|
|
|
|
containerStackView.distribution = .fill
|
|
|
|
|
|
|
|
addSubview(containerStackView)
|
|
|
|
|
|
|
|
containerStackView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
containerStackView.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
containerStackView.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
containerStackView.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
containerStackView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
public func configure(card: Card) {
|
|
|
|
let isCompact = card.width == card.height
|
2022-11-14 22:26:25 +01:00
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
titleLabel.text = card.title
|
|
|
|
linkLabel.text = card.url?.host
|
|
|
|
imageView.contentMode = .center
|
2022-11-14 22:26:25 +01:00
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
imageView.sd_setImage(
|
|
|
|
with: card.imageURL,
|
|
|
|
placeholderImage: isCompact ? newsIcon : photoIcon
|
|
|
|
) { [weak imageView] image, _, _, _ in
|
|
|
|
if image != nil {
|
|
|
|
imageView?.contentMode = .scaleAspectFill
|
|
|
|
}
|
|
|
|
}
|
2022-11-14 22:26:25 +01:00
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
NSLayoutConstraint.deactivate(compactImageConstraints + largeImageConstraints)
|
2022-11-14 22:26:25 +01:00
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
if isCompact {
|
|
|
|
containerStackView.alignment = .center
|
|
|
|
containerStackView.axis = .horizontal
|
|
|
|
NSLayoutConstraint.activate(compactImageConstraints)
|
|
|
|
} else {
|
|
|
|
containerStackView.alignment = .fill
|
|
|
|
containerStackView.axis = .vertical
|
|
|
|
NSLayoutConstraint.activate(largeImageConstraints)
|
2022-11-14 22:26:25 +01:00
|
|
|
}
|
2022-11-12 03:35:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override func didMoveToWindow() {
|
|
|
|
super.didMoveToWindow()
|
|
|
|
|
|
|
|
if let window = window {
|
|
|
|
layer.borderWidth = 1 / window.screen.scale
|
|
|
|
}
|
|
|
|
}
|
2022-11-14 22:26:25 +01:00
|
|
|
|
2022-11-24 06:51:39 +01:00
|
|
|
private var newsIcon: UIImage? {
|
|
|
|
UIImage(systemName: "newspaper.fill")
|
|
|
|
}
|
|
|
|
|
|
|
|
private var photoIcon: UIImage? {
|
|
|
|
let configuration = UIImage.SymbolConfiguration(pointSize: 40)
|
|
|
|
return UIImage(systemName: "photo", withConfiguration: configuration)
|
2022-11-14 22:26:25 +01:00
|
|
|
}
|
2022-11-12 03:35:18 +01:00
|
|
|
}
|