Tusker/Tusker/Views/Status/TimelineStatusCollectionVie...

290 lines
11 KiB
Swift
Raw Normal View History

//
// TimelineStatusCollectionViewCell.swift
// Tusker
//
// Created by Shadowfacts on 10/1/22.
// Copyright © 2022 Shadowfacts. All rights reserved.
//
import UIKit
class TimelineStatusCollectionViewCell: UICollectionViewListCell {
private lazy var vStack = UIStackView(arrangedSubviews: [
reblogLabel,
reblogSpacer,
mainContainer,
actionsContainer,
]).configure {
$0.axis = .vertical
$0.alignment = .fill
}
private let reblogLabel = UILabel().configure {
$0.textColor = .secondaryLabel
// TODO: tap gesture
// $0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(reblogLabelPressed)))
}
private let reblogSpacer = UIView().configure {
$0.backgroundColor = nil
NSLayoutConstraint.activate([
$0.heightAnchor.constraint(equalToConstant: 4),
])
}
private lazy var mainContainer = UIView().configure {
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
$0.addSubview(avatarImageView)
contentVStack.translatesAutoresizingMaskIntoConstraints = false
$0.addSubview(contentVStack)
metaIndicatorsView.translatesAutoresizingMaskIntoConstraints = false
$0.addSubview(metaIndicatorsView)
NSLayoutConstraint.activate([
avatarImageView.leadingAnchor.constraint(equalTo: $0.leadingAnchor),
avatarImageView.topAnchor.constraint(equalTo: $0.topAnchor),
contentVStack.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: 8),
contentVStack.trailingAnchor.constraint(equalTo: $0.trailingAnchor),
contentVStack.topAnchor.constraint(equalTo: $0.topAnchor),
contentVStack.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
metaIndicatorsView.leadingAnchor.constraint(greaterThanOrEqualTo: $0.leadingAnchor),
metaIndicatorsView.trailingAnchor.constraint(equalTo: avatarImageView.trailingAnchor),
metaIndicatorsView.topAnchor.constraint(equalTo: avatarImageView.bottomAnchor, constant: 4),
metaIndicatorsView.bottomAnchor.constraint(lessThanOrEqualTo: $0.bottomAnchor),
])
}
private let avatarImageView = UIImageView().configure {
$0.layer.masksToBounds = true
NSLayoutConstraint.activate([
$0.heightAnchor.constraint(equalToConstant: 50),
$0.widthAnchor.constraint(equalToConstant: 50),
])
// TODO: context menu
// $0.addInteraction(UIContextMenuInteraction(delegate: self))
// TODO: drag gesture
// $0.addInteraction(UIDragInteraction(delegate: self))
// TODO: tap gesture
// $0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
}
private let metaIndicatorsView = StatusMetaIndicatorsView()
private lazy var contentVStack = UIStackView(arrangedSubviews: [
nameHStack,
contentWarningLabel,
collapseButton,
contentTextView,
cardView,
attachmentsView,
pollView,
]).configure {
$0.axis = .vertical
$0.spacing = 4
$0.alignment = .fill
}
private lazy var nameHStack = UIStackView(arrangedSubviews: [
displayNameLabel,
usernameLabel,
pinImageView,
timestampLabel,
]).configure {
$0.axis = .horizontal
$0.spacing = 4
// TODO: tap gesture
// $0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
}
private let displayNameLabel = EmojiLabel().configure {
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
.traits: [
UIFontDescriptor.TraitKey.weight: UIFont.Weight.semibold.rawValue,
]
]), size: 0)
$0.setContentHuggingPriority(.init(251), for: .horizontal)
$0.setContentCompressionResistancePriority(.init(749), for: .horizontal)
}
private let usernameLabel = UILabel().configure {
$0.textColor = .secondaryLabel
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
.traits: [
UIFontDescriptor.TraitKey.weight: UIFont.Weight.light.rawValue,
]
]), size: 0)
$0.setContentHuggingPriority(.init(249), for: .horizontal)
$0.setContentCompressionResistancePriority(.init(748), for: .horizontal)
}
private let pinImageView = UIImageView(image: UIImage(systemName: "pin.fill")).configure {
$0.tintColor = .secondaryLabel
$0.setContentHuggingPriority(.init(251), for: .horizontal)
}
private let timestampLabel = UILabel().configure {
$0.textColor = .secondaryLabel
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
.traits: [
UIFontDescriptor.TraitKey.weight: UIFont.Weight.light.rawValue,
]
]), size: 0)
}
private let contentWarningLabel = UILabel().configure {
$0.textColor = .secondaryLabel
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
.traits: [
UIFontDescriptor.TraitKey.weight: UIFont.Weight.bold.rawValue,
]
]), size: 0)
// TODO: tap gesture
// $0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(collapseButtonPressed)))
}
private let collapseButton = UIButton(configuration: {
var config = UIButton.Configuration.filled()
config.image = UIImage(systemName: "chevron.down")
return config
}(), primaryAction: nil).configure {
_ = $0
// TODO: masksToBounds and cornerRadius?
}
private let contentTextView = StatusContentTextView().configure {
$0.defaultFont = .systemFont(ofSize: 16)
$0.isScrollEnabled = false
$0.backgroundColor = nil
}
private let cardView = StatusCardView().configure {
NSLayoutConstraint.activate([
$0.heightAnchor.constraint(equalToConstant: 65),
])
}
private let attachmentsView = AttachmentsContainerView().configure {
NSLayoutConstraint.activate([
$0.heightAnchor.constraint(equalTo: $0.widthAnchor, multiplier: 9/16),
])
}
private let pollView = StatusPollView()
private var placeholderReplyButtonLeadingConstraint: NSLayoutConstraint!
private lazy var actionsContainer = UIView().configure {
replyButton.translatesAutoresizingMaskIntoConstraints = false
$0.addSubview(replyButton)
favoriteButton.translatesAutoresizingMaskIntoConstraints = false
$0.addSubview(favoriteButton)
reblogButton.translatesAutoresizingMaskIntoConstraints = false
$0.addSubview(reblogButton)
moreButton.translatesAutoresizingMaskIntoConstraints = false
$0.addSubview(moreButton)
placeholderReplyButtonLeadingConstraint = replyButton.leadingAnchor.constraint(equalTo: $0.leadingAnchor)
NSLayoutConstraint.activate([
favoriteButton.widthAnchor.constraint(equalTo: replyButton.widthAnchor),
reblogButton.widthAnchor.constraint(equalTo: replyButton.widthAnchor),
moreButton.widthAnchor.constraint(equalTo: replyButton.widthAnchor),
// TODO: gah
placeholderReplyButtonLeadingConstraint,
replyButton.topAnchor.constraint(equalTo: $0.topAnchor),
replyButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
favoriteButton.leadingAnchor.constraint(equalTo: replyButton.trailingAnchor),
favoriteButton.topAnchor.constraint(equalTo: $0.topAnchor),
favoriteButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
reblogButton.leadingAnchor.constraint(equalTo: favoriteButton.trailingAnchor),
reblogButton.topAnchor.constraint(equalTo: $0.topAnchor),
reblogButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
moreButton.leadingAnchor.constraint(equalTo: reblogButton.trailingAnchor),
moreButton.trailingAnchor.constraint(equalTo: $0.trailingAnchor),
moreButton.topAnchor.constraint(equalTo: $0.topAnchor),
moreButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
])
}
private let replyButton = UIButton().configure {
$0.setImage(UIImage(systemName: "arrowshape.turn.up.left.fill"), for: .normal)
}
private let favoriteButton = UIButton().configure {
$0.setImage(UIImage(systemName: "star.fill"), for: .normal)
}
private let reblogButton = UIButton().configure {
$0.setImage(UIImage(systemName: "repeat"), for: .normal)
}
private let moreButton = UIButton().configure {
$0.setImage(UIImage(systemName: "ellipsis"), for: .normal)
$0.showsMenuAsPrimaryAction = true
}
private var firstLayout = true
override init(frame: CGRect) {
super.init(frame: frame)
vStack.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(vStack)
NSLayoutConstraint.activate([
vStack.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
vStack.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
vStack.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
vStack.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
])
// TEMP
reblogLabel.text = "Reblogged by Person"
avatarImageView.backgroundColor = .red
displayNameLabel.text = "Display name"
usernameLabel.text = "@username"
timestampLabel.text = "2m"
contentWarningLabel.text = "Content Warning"
contentTextView.setTextFromHtml("<p>Weeeeeeeee</p>")
attachmentsView.backgroundColor = .red
metaIndicatorsView.placeholder()
// contentWarningLabel.isHidden = true
// collapseButton.isHidden = true
// cardView.isHidden = true
// pollView.isHidden = true
// attachmentsView.isHidden = true
// actionsContainer.isHidden = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
// the button's image view doesn't exist until after the first layout
// accessing it before that cause the button to layoutIfNeeded which generates a broken, intermediate layout and prints a bunch of unhelpful autolayout warnings
// so we wait until after the first layout pass to setup the reply button's real constraint
if firstLayout {
firstLayout = false
placeholderReplyButtonLeadingConstraint.isActive = false
replyButton.imageView!.leadingAnchor.constraint(equalTo: contentTextView.leadingAnchor).isActive = true
}
}
}
fileprivate protocol Configurable {
associatedtype T = Self
func configure(_ closure: (T) -> Void) -> T
}
extension Configurable where Self: UIView {
func configure(_ closure: (Self) -> Void) -> Self {
closure(self)
return self
}
}
extension UIView: Configurable {
}