metatext/Views/UIKit/Content Configurations/EmojiView.swift

98 lines
3.1 KiB
Swift
Raw Normal View History

2021-01-15 10:13:10 +00:00
// Copyright © 2021 Metabolist. All rights reserved.
2021-02-22 23:59:33 +00:00
import SDWebImage
2021-01-15 10:13:10 +00:00
import UIKit
final class EmojiView: UIView {
2021-02-22 23:59:33 +00:00
private let imageView = SDAnimatedImageView()
2021-01-15 10:13:10 +00:00
private let emojiLabel = UILabel()
private var emojiConfiguration: EmojiContentConfiguration
init(configuration: EmojiContentConfiguration) {
emojiConfiguration = configuration
super.init(frame: .zero)
initialSetup()
2021-02-02 04:44:56 +00:00
setupAccessibility()
2021-01-15 10:13:10 +00:00
applyEmojiConfiguration()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension EmojiView: UIContentView {
var configuration: UIContentConfiguration {
get { emojiConfiguration }
set {
guard let emojiConfiguration = newValue as? EmojiContentConfiguration else { return }
self.emojiConfiguration = emojiConfiguration
applyEmojiConfiguration()
}
}
}
private extension EmojiView {
func initialSetup() {
layoutMargins = .init(
top: .compactSpacing,
left: .compactSpacing,
bottom: .compactSpacing,
right: .compactSpacing)
addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
addSubview(emojiLabel)
emojiLabel.translatesAutoresizingMaskIntoConstraints = false
emojiLabel.textAlignment = .center
emojiLabel.adjustsFontSizeToFitWidth = true
emojiLabel.font = .preferredFont(forTextStyle: .largeTitle)
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
imageView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
imageView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
emojiLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
emojiLabel.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
emojiLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
emojiLabel.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
])
}
func applyEmojiConfiguration() {
if emojiConfiguration.viewModel.system {
2021-01-15 10:13:10 +00:00
emojiLabel.isHidden = false
emojiLabel.text = emojiConfiguration.viewModel.name
imageView.isHidden = true
} else {
emojiLabel.isHidden = true
emojiLabel.text = nil
imageView.isHidden = false
2021-02-27 21:29:30 +00:00
let url: URL?
if let urlString = emojiConfiguration.viewModel.url {
url = URL(stringEscapingPath: urlString)
} else {
url = nil
}
imageView.sd_setImage(with: url)
2021-01-15 10:13:10 +00:00
}
accessibilityLabel = emojiConfiguration.viewModel.name
2021-01-15 10:13:10 +00:00
}
2021-02-02 03:47:48 +00:00
func setupAccessibility() {
isAccessibilityElement = true
}
2021-01-15 10:13:10 +00:00
}