refactored to have a public contentSize publisher
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
73c7e23b99
commit
6aa09da464
@ -36,7 +36,15 @@ public final class SelfSizingCollectionView: UICollectionView {
|
||||
//--------------------------------------------------
|
||||
private var collectionViewHeight: NSLayoutConstraint?
|
||||
private var anyCancellable: AnyCancellable?
|
||||
|
||||
private var contentSizeSubject = CurrentValueSubject<CGSize, Never>(.zero)
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Public Properties
|
||||
//--------------------------------------------------
|
||||
public var contentSizePublisher: AnyPublisher<CGSize, Never> {
|
||||
contentSizeSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
//--------------------------------------------------
|
||||
// MARK: - Overrides
|
||||
//--------------------------------------------------
|
||||
@ -67,17 +75,13 @@ public final class SelfSizingCollectionView: UICollectionView {
|
||||
setContentCompressionResistancePriority(.required, for: .vertical)
|
||||
collectionViewHeight = heightAnchor.constraint(equalToConstant: 0).activate()
|
||||
|
||||
anyCancellable = self.publisher(for: \.contentSize, options: [.old, .new])
|
||||
.scan((old: CGSize.zero, new: CGSize.zero)) { accumulator, newValue in
|
||||
// accumulator.old contains the old contentSize value
|
||||
// accumulator.new contains the new contentSize value before the current update
|
||||
// newValue is the current update to contentSize
|
||||
return (old: accumulator.new, new: newValue)
|
||||
}
|
||||
anyCancellable = self.publisher(for: \.contentSize, options: [.new])
|
||||
.sink { [weak self] compare in
|
||||
if compare.old != compare.new {
|
||||
self?.invalidateIntrinsicContentSize()
|
||||
self?.collectionViewHeight?.constant = compare.new.height
|
||||
guard let self else { return }
|
||||
if compare.height != self.collectionViewHeight?.constant {
|
||||
self.invalidateIntrinsicContentSize()
|
||||
self.collectionViewHeight?.constant = compare.height
|
||||
self.contentSizeSubject.send(compare)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user