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
|
|
|
|
|
|
|
|
class StatusContentContainer: UIView {
|
2023-01-21 16:25:07 +00:00
|
|
|
|
|
|
|
private var useTopSpacer = false
|
|
|
|
private let topSpacer = UIView().configure {
|
|
|
|
$0.backgroundColor = .clear
|
|
|
|
// other 4pt is provided by this view's own spacing
|
|
|
|
$0.heightAnchor.constraint(equalToConstant: 4).isActive = true
|
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2023-01-21 16:25:07 +00:00
|
|
|
let contentTextView = StatusContentTextView().configure {
|
2022-11-04 02:15:54 +00:00
|
|
|
$0.adjustsFontForContentSizeCategory = true
|
2022-10-05 21:40:00 +00:00
|
|
|
$0.isScrollEnabled = false
|
|
|
|
$0.backgroundColor = nil
|
|
|
|
$0.isEditable = false
|
|
|
|
$0.isSelectable = false
|
2022-10-04 04:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let cardView = StatusCardView().configure {
|
|
|
|
NSLayoutConstraint.activate([
|
2023-01-20 16:22:28 +00:00
|
|
|
$0.heightAnchor.constraint(equalToConstant: 90),
|
2022-10-04 04:02:41 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2022-12-25 19:13:59 +00:00
|
|
|
let attachmentsView = AttachmentsContainerView()
|
2022-10-04 04:02:41 +00:00
|
|
|
|
|
|
|
let pollView = StatusPollView()
|
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
private var arrangedSubviews: [UIView] {
|
2023-01-21 16:25:07 +00:00
|
|
|
if useTopSpacer {
|
|
|
|
return [topSpacer, contentTextView, cardView, attachmentsView, pollView]
|
|
|
|
} else {
|
|
|
|
return [contentTextView, cardView, attachmentsView, pollView]
|
|
|
|
}
|
2022-10-05 02:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var isHiddenObservations: [NSKeyValueObservation] = []
|
|
|
|
|
|
|
|
private var verticalConstraints: [NSLayoutConstraint] = []
|
|
|
|
private var lastSubviewBottomConstraint: 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-01-21 16:25:07 +00:00
|
|
|
init(useTopSpacer: Bool) {
|
|
|
|
self.useTopSpacer = useTopSpacer
|
|
|
|
|
|
|
|
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 {
|
|
|
|
$0.observe(\.isHidden) { [unowned self] _, _ in
|
|
|
|
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() {
|
|
|
|
NSLayoutConstraint.deactivate(verticalConstraints)
|
|
|
|
verticalConstraints = []
|
|
|
|
var lastVisibleSubview: UIView?
|
|
|
|
|
|
|
|
for subview in arrangedSubviews {
|
|
|
|
guard !subview.isHidden else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if let lastVisibleSubview {
|
|
|
|
verticalConstraints.append(subview.topAnchor.constraint(equalTo: lastVisibleSubview.bottomAnchor, constant: 4))
|
|
|
|
} else {
|
|
|
|
verticalConstraints.append(subview.topAnchor.constraint(equalTo: topAnchor))
|
|
|
|
}
|
|
|
|
|
|
|
|
lastVisibleSubview = subview
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate(verticalConstraints)
|
|
|
|
|
|
|
|
lastSubviewBottomConstraint?.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
|
|
|
|
lastSubviewBottomConstraint = subviews.last(where: { !$0.isHidden })!.bottomAnchor.constraint(equalTo: bottomAnchor)
|
2022-12-04 16:14:19 +00:00
|
|
|
lastSubviewBottomConstraint!.isActive = !isCollapsed
|
2022-10-05 02:48:42 +00:00
|
|
|
lastSubviewBottomConstraint!.priority = .defaultLow
|
|
|
|
|
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
|
|
|
|
|
2022-10-05 02:48:42 +00:00
|
|
|
// ensure that we have a lastSubviewBottomConstraint
|
|
|
|
updateConstraintsIfNeeded()
|
|
|
|
// force unwrap because the content container should always have at least one view
|
|
|
|
lastSubviewBottomConstraint!.isActive = !collapsed
|
2022-10-04 04:02:41 +00:00
|
|
|
zeroHeightConstraint.isActive = collapsed
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|