2022-10-04 04:02:41 +00:00
|
|
|
//
|
|
|
|
// StatusContentContainer.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/2/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2023-12-04 22:06:10 +00:00
|
|
|
class StatusContentContainer: UIView {
|
|
|
|
// TODO: this is a weird place for this
|
|
|
|
static var cardViewHeight: CGFloat { 90 }
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2023-12-04 22:06:10 +00:00
|
|
|
private let arrangedSubviews: [any StatusContentView]
|
2022-10-05 02:48:42 +00:00
|
|
|
|
|
|
|
private var isHiddenObservations: [NSKeyValueObservation] = []
|
|
|
|
|
2023-07-06 03:30:55 +00:00
|
|
|
private var visibleSubviews = IndexSet()
|
2022-10-05 02:48:42 +00:00
|
|
|
private var verticalConstraints: [NSLayoutConstraint] = []
|
2023-07-06 03:30:55 +00:00
|
|
|
private var lastSubviewBottomConstraint: (UIView, NSLayoutConstraint)?
|
2022-10-04 04:02:41 +00:00
|
|
|
private var zeroHeightConstraint: NSLayoutConstraint!
|
2022-12-04 16:14:19 +00:00
|
|
|
|
|
|
|
private var isCollapsed = false
|
2022-12-25 19:13:59 +00:00
|
|
|
|
|
|
|
var visibleSubviewHeight: CGFloat {
|
|
|
|
subviews.filter { !$0.isHidden }.map(\.bounds.height).reduce(0, +)
|
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2023-12-04 22:06:10 +00:00
|
|
|
init(arrangedSubviews: [any StatusContentView], useTopSpacer: Bool) {
|
|
|
|
var arrangedSubviews = arrangedSubviews
|
|
|
|
if useTopSpacer {
|
|
|
|
arrangedSubviews.insert(TopSpacerView(), at: 0)
|
|
|
|
}
|
|
|
|
self.arrangedSubviews = arrangedSubviews
|
2023-01-21 16:25:07 +00:00
|
|
|
|
|
|
|
super.init(frame: .zero)
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
for subview in arrangedSubviews {
|
2022-10-04 04:02:41 +00:00
|
|
|
subview.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(subview)
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
subview.leadingAnchor.constraint(equalTo: leadingAnchor),
|
|
|
|
subview.trailingAnchor.constraint(equalTo: trailingAnchor),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
// this constraint needs to have low priority so that during the collapse/expand animation, the content container is the view that shrinks/expands
|
2022-10-04 04:02:41 +00:00
|
|
|
zeroHeightConstraint = heightAnchor.constraint(equalToConstant: 0)
|
|
|
|
zeroHeightConstraint.priority = .defaultLow
|
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
setNeedsUpdateConstraints()
|
2022-10-08 19:31:08 +00:00
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
isHiddenObservations = arrangedSubviews.map {
|
2023-12-04 22:06:10 +00:00
|
|
|
$0.observeIsHidden { [unowned self] in
|
2022-10-05 02:48:42 +00:00
|
|
|
self.setNeedsUpdateConstraints()
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
override func updateConstraints() {
|
2023-07-06 03:30:55 +00:00
|
|
|
let visibleSubviews = IndexSet(arrangedSubviews.indices.filter { !arrangedSubviews[$0].isHidden })
|
|
|
|
if self.visibleSubviews != visibleSubviews {
|
|
|
|
self.visibleSubviews = visibleSubviews
|
|
|
|
NSLayoutConstraint.deactivate(verticalConstraints)
|
|
|
|
verticalConstraints = []
|
|
|
|
var lastVisibleSubview: UIView?
|
2022-10-05 02:48:42 +00:00
|
|
|
|
2023-07-06 03:30:55 +00:00
|
|
|
for subviewIndex in visibleSubviews {
|
|
|
|
let subview = arrangedSubviews[subviewIndex]
|
|
|
|
if let lastVisibleSubview {
|
|
|
|
verticalConstraints.append(subview.topAnchor.constraint(equalTo: lastVisibleSubview.bottomAnchor, constant: 4))
|
|
|
|
} else {
|
|
|
|
verticalConstraints.append(subview.topAnchor.constraint(equalTo: topAnchor))
|
|
|
|
}
|
|
|
|
lastVisibleSubview = subview
|
2022-10-05 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 03:30:55 +00:00
|
|
|
NSLayoutConstraint.activate(verticalConstraints)
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastSubviewBottomConstraint == nil || arrangedSubviews[visibleSubviews.last!] !== lastSubviewBottomConstraint?.0 {
|
|
|
|
lastSubviewBottomConstraint?.1.isActive = false
|
|
|
|
// this constraint needs to have low priority so that during the collapse/expand animation, the content container is the view that shrinks/expands
|
|
|
|
let lastVisibleSubview = arrangedSubviews[visibleSubviews.last!]
|
|
|
|
let constraint = lastVisibleSubview.bottomAnchor.constraint(equalTo: bottomAnchor)
|
|
|
|
constraint.isActive = !isCollapsed
|
|
|
|
constraint.priority = .defaultLow
|
|
|
|
lastSubviewBottomConstraint = (lastVisibleSubview, constraint)
|
2022-10-05 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 16:14:19 +00:00
|
|
|
zeroHeightConstraint.isActive = isCollapsed
|
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
super.updateConstraints()
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:02:41 +00:00
|
|
|
func setCollapsed(_ collapsed: Bool) {
|
2022-12-04 16:14:19 +00:00
|
|
|
guard collapsed != isCollapsed else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
isCollapsed = collapsed
|
|
|
|
|
2023-05-14 19:53:24 +00:00
|
|
|
// don't call setNeedsUpdateConstraints b/c that destroys/recreates a bunch of other constraints
|
|
|
|
// if there is no lastSubviewBottomConstraint, then we already need a constraint update, so we don't need to do anything here
|
|
|
|
if let lastSubviewBottomConstraint {
|
2023-07-06 03:30:55 +00:00
|
|
|
lastSubviewBottomConstraint.1.isActive = !collapsed
|
2023-05-14 19:53:24 +00:00
|
|
|
zeroHeightConstraint.isActive = collapsed
|
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 19:00:03 +00:00
|
|
|
// used only for collapsing automatically based on height, doesn't need to be accurate
|
|
|
|
// just roughly inline with the content height
|
|
|
|
func estimateVisibleSubviewHeight(effectiveWidth: CGFloat) -> CGFloat {
|
|
|
|
var height: CGFloat = 0
|
2023-12-04 22:06:10 +00:00
|
|
|
for view in arrangedSubviews where !view.isHidden {
|
|
|
|
height += view.estimateHeight(effectiveWidth: effectiveWidth)
|
2023-05-13 19:00:03 +00:00
|
|
|
}
|
|
|
|
return height
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:02:41 +00:00
|
|
|
}
|
2023-12-04 22:06:10 +00:00
|
|
|
|
|
|
|
extension StatusContentContainer {
|
|
|
|
private class TopSpacerView: UIView, StatusContentView {
|
|
|
|
init() {
|
|
|
|
super.init(frame: .zero)
|
|
|
|
|
|
|
|
backgroundColor = .clear
|
|
|
|
// other 4pt is provided by this view's own spacing
|
|
|
|
heightAnchor.constraint(equalToConstant: 4).isActive = true
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func estimateHeight(effectiveWidth: CGFloat) -> CGFloat {
|
|
|
|
4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension UIView {
|
|
|
|
func observeIsHidden(_ f: @escaping () -> Void) -> NSKeyValueObservation {
|
|
|
|
self.observe(\.isHidden) { _, _ in
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol StatusContentView: UIView {
|
|
|
|
func estimateHeight(effectiveWidth: CGFloat) -> CGFloat
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ContentTextView: StatusContentView {
|
|
|
|
func estimateHeight(effectiveWidth: CGFloat) -> CGFloat {
|
|
|
|
sizeThatFits(CGSize(width: effectiveWidth, height: UIView.layoutFittingCompressedSize.height)).height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension StatusCardView: StatusContentView {
|
|
|
|
func estimateHeight(effectiveWidth: CGFloat) -> CGFloat {
|
|
|
|
StatusContentContainer.cardViewHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AttachmentsContainerView: StatusContentView {
|
|
|
|
func estimateHeight(effectiveWidth: CGFloat) -> CGFloat {
|
|
|
|
effectiveWidth / aspectRatio
|
|
|
|
}
|
|
|
|
}
|