Tusker/Tusker/Views/Status/StatusContentContainer.swift

79 lines
2.7 KiB
Swift
Raw Normal View History

//
// StatusContentContainer.swift
// Tusker
//
// Created by Shadowfacts on 10/2/22.
// Copyright © 2022 Shadowfacts. All rights reserved.
//
import UIKit
class StatusContentContainer: UIView {
let contentTextView = StatusContentTextView().configure {
$0.defaultFont = .systemFont(ofSize: 16)
$0.isScrollEnabled = false
$0.backgroundColor = nil
}
let cardView = StatusCardView().configure {
NSLayoutConstraint.activate([
$0.heightAnchor.constraint(equalToConstant: 65),
])
}
let attachmentsView = AttachmentsContainerView().configure {
NSLayoutConstraint.activate([
$0.heightAnchor.constraint(equalTo: $0.widthAnchor, multiplier: 9/16),
])
}
let pollView = StatusPollView()
private var lastSubviewBottomConstraint: NSLayoutConstraint!
private var zeroHeightConstraint: NSLayoutConstraint!
override init(frame: CGRect) {
super.init(frame: frame)
let subviews = [contentTextView, cardView, attachmentsView, pollView]
for (index, subview) in subviews.enumerated() {
subview.translatesAutoresizingMaskIntoConstraints = false
addSubview(subview)
let topConstraint: NSLayoutConstraint
if index == 0 {
topConstraint = subview.topAnchor.constraint(equalTo: topAnchor)
} else {
topConstraint = subview.topAnchor.constraint(equalTo: subviews[index - 1].bottomAnchor, constant: 4)
}
NSLayoutConstraint.activate([
topConstraint,
subview.leadingAnchor.constraint(equalTo: leadingAnchor),
subview.trailingAnchor.constraint(equalTo: trailingAnchor),
])
}
// these constraints need to have low priority so that during the collapse/expand animation, the content container is the view that shrinks/expands
lastSubviewBottomConstraint = subviews.last!.bottomAnchor.constraint(equalTo: bottomAnchor)
lastSubviewBottomConstraint.isActive = true
lastSubviewBottomConstraint.priority = .defaultLow
zeroHeightConstraint = heightAnchor.constraint(equalToConstant: 0)
zeroHeightConstraint.priority = .defaultLow
// mask to bounds so that the during the expand/collapse animation, subviews are clipped
layer.masksToBounds = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setCollapsed(_ collapsed: Bool) {
lastSubviewBottomConstraint.isActive = !collapsed
zeroHeightConstraint.isActive = collapsed
}
}