diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index 6789b36c..84c713d8 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -127,6 +127,15 @@ class AttachmentView: GIFImageView { } } + var badges: Badges = [] + if attachment.description?.isEmpty == false { + badges.formUnion(.alt) + } + if attachment.kind == .gifv || attachment.url.pathExtension == "gif" { + badges.formUnion(.gif) + } + createBadgesView(badges) + switch attachment.kind { case .image: loadImage() @@ -302,6 +311,64 @@ class AttachmentView: GIFImageView { } } + private func createBadgesView(_ badges: Badges) { + guard !badges.isEmpty else { + return + } + + let stack = UIStackView() + stack.axis = .horizontal + stack.spacing = 2 + stack.translatesAutoresizingMaskIntoConstraints = false + + let font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .systemFont(ofSize: 13, weight: .bold)) + func makeBadgeView(text: String) { + let container = UIView() + container.backgroundColor = .secondarySystemBackground.resolvedColor(with: UITraitCollection(userInterfaceStyle: .dark)) + + let label = UILabel() + label.font = font + label.adjustsFontForContentSizeCategory = true + label.textColor = .white + label.text = text + label.translatesAutoresizingMaskIntoConstraints = false + container.addSubview(label) + NSLayoutConstraint.activate([ + label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 2), + label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -2), + label.topAnchor.constraint(equalTo: container.topAnchor, constant: 2), + label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -2), + ]) + stack.addArrangedSubview(container) + } + + if badges.contains(.gif) { + makeBadgeView(text: "GIF") + } + if badges.contains(.alt) { + makeBadgeView(text: "ALT") + } + + let first = stack.arrangedSubviews.first! + first.layer.masksToBounds = true + first.layer.cornerRadius = 4 + if stack.arrangedSubviews.count > 1 { + first.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner] + let last = stack.arrangedSubviews.last! + last.layer.masksToBounds = true + last.layer.cornerRadius = 4 + last.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner] + } + + addSubview(stack) + NSLayoutConstraint.activate([ + stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 4), + stack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -4), + ]) + } + + // MARK: Interaction + func showGallery() { if let delegate = delegate, let gallery = delegate.attachmentViewGallery(startingAt: index) { @@ -328,6 +395,13 @@ fileprivate extension AttachmentView { case gifData(URL, Data) case cgImage(URL, CGImage) } + + struct Badges: OptionSet { + static let gif = Badges(rawValue: 1 << 0) + static let alt = Badges(rawValue: 1 << 1) + + let rawValue: Int + } } extension AttachmentView: UIContextMenuInteractionDelegate {