2022-10-12 01:53:13 +00:00
|
|
|
//
|
2022-10-29 01:38:56 +00:00
|
|
|
// ProfileHeaderCollectionViewCell.swift
|
2022-10-12 01:53:13 +00:00
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/10/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2022-10-29 01:38:56 +00:00
|
|
|
class ProfileHeaderCollectionViewCell: UICollectionViewCell {
|
2022-10-12 01:53:13 +00:00
|
|
|
|
|
|
|
private var state: State = .unloaded
|
|
|
|
|
2022-10-29 19:08:03 +00:00
|
|
|
var view: ProfileHeaderView? {
|
|
|
|
if case .view(let view) = state {
|
|
|
|
return view
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 01:53:13 +00:00
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
|
2023-02-03 04:02:11 +00:00
|
|
|
contentView.backgroundColor = .appBackground
|
2022-10-12 01:53:13 +00:00
|
|
|
isOpaque = true
|
|
|
|
contentView.isOpaque = true
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addHeader(_ header: ProfileHeaderView) {
|
|
|
|
switch state {
|
|
|
|
case .unloaded, .placeholder(heightConstraint: _):
|
|
|
|
header.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.embedSubview(header)
|
|
|
|
self.state = .view(header)
|
|
|
|
case .view(_):
|
|
|
|
fatalError("profile header collection view cell already has view")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addConstraint(height: CGFloat) -> ProfileHeaderView? {
|
|
|
|
switch state {
|
|
|
|
case .unloaded:
|
|
|
|
let constraint = contentView.heightAnchor.constraint(equalToConstant: height)
|
|
|
|
constraint.isActive = true
|
|
|
|
state = .placeholder(heightConstraint: constraint)
|
|
|
|
return nil
|
|
|
|
case .placeholder(let heightConstraint):
|
|
|
|
heightConstraint.constant = height
|
|
|
|
return nil
|
|
|
|
case .view(let header):
|
|
|
|
let constraint = contentView.heightAnchor.constraint(equalToConstant: height)
|
|
|
|
constraint.isActive = true
|
|
|
|
state = .placeholder(heightConstraint: constraint)
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-20 18:57:51 +00:00
|
|
|
// overrides an internal method
|
|
|
|
// when the super impl is used, preferredLayoutAttributesFitting(_:) isn't called while the view is offscreen (i.e., window == nil)
|
|
|
|
// and so the collection view imposes a height of 44pts which breaks the layout
|
|
|
|
@objc func _preferredLayoutAttributesFittingAttributes(_ attributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
|
|
|
|
return preferredLayoutAttributesFitting(attributes)
|
|
|
|
}
|
|
|
|
|
2022-10-12 01:53:13 +00:00
|
|
|
enum State {
|
|
|
|
case unloaded
|
|
|
|
case placeholder(heightConstraint: NSLayoutConstraint)
|
|
|
|
case view(ProfileHeaderView)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|