From 272fe9d9499e19fa3de591a623e740d7cad193a5 Mon Sep 17 00:00:00 2001 From: Apptek Studios <13492172+apptekstudios@users.noreply.github.com> Date: Sat, 2 May 2020 10:47:45 +1000 Subject: [PATCH] V1.6.2 (#141) * Attach views in layoutSubviews (as apple does in some of their UI), fixes #130 --- ASCollectionView-SwiftUI.podspec | 2 +- .../Cells/ASCollectionViewCell.swift | 15 +++++---- .../ASCollectionViewSupplementaryView.swift | 21 ++++++------- .../Cells/ASTableViewCell.swift | 20 ++++++------ .../Cells/ASTableViewSupplementaryView.swift | 31 ++++++------------- .../ASDiffableDataSourceCollectionView.swift | 2 +- .../Implementation/ASCollectionView.swift | 2 +- .../Implementation/ASTableView.swift | 2 +- 8 files changed, 40 insertions(+), 55 deletions(-) diff --git a/ASCollectionView-SwiftUI.podspec b/ASCollectionView-SwiftUI.podspec index 14410d0..630374a 100644 --- a/ASCollectionView-SwiftUI.podspec +++ b/ASCollectionView-SwiftUI.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'ASCollectionView-SwiftUI' - s.version = '1.6.0' + s.version = '1.6.2' s.summary = 'A SwiftUI collection view with support for custom layouts, preloading, and more. ' s.description = <<-DESC diff --git a/Sources/ASCollectionView/Cells/ASCollectionViewCell.swift b/Sources/ASCollectionView/Cells/ASCollectionViewCell.swift index 610e3d3..e270aa2 100644 --- a/Sources/ASCollectionView/Cells/ASCollectionViewCell.swift +++ b/Sources/ASCollectionView/Cells/ASCollectionViewCell.swift @@ -31,13 +31,8 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell { hostingController?.viewController.removeFromParent() hostingController.map { vc.addChild($0.viewController) } - attachView() hostingController?.viewController.didMove(toParent: vc) } - else - { - attachView() - } } func didDisappear() @@ -49,9 +44,10 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell { guard let hcView = hostingController?.viewController.view else { - contentView.subviews.forEach { $0.removeFromSuperview() } + detachViews() return } + guard !isHidden else { return } if hcView.superview != contentView { contentView.subviews.forEach { $0.removeFromSuperview() } @@ -60,6 +56,11 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell } } + private func detachViews() + { + contentView.subviews.forEach { $0.removeFromSuperview() } + } + var shouldSkipNextRefresh: Bool = true override func prepareForReuse() @@ -75,6 +76,8 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell { super.layoutSubviews() + attachView() + if hostingController?.viewController.view.frame != contentView.bounds { UIView.performWithoutAnimation { diff --git a/Sources/ASCollectionView/Cells/ASCollectionViewSupplementaryView.swift b/Sources/ASCollectionView/Cells/ASCollectionViewSupplementaryView.swift index 40b78a4..ff763f1 100644 --- a/Sources/ASCollectionView/Cells/ASCollectionViewSupplementaryView.swift +++ b/Sources/ASCollectionView/Cells/ASCollectionViewSupplementaryView.swift @@ -12,25 +12,14 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView var selfSizingConfig: ASSelfSizingConfig = .init(selfSizeHorizontally: true, selfSizeVertically: true) var maxSizeForSelfSizing: ASOptionalSize = .none - func setupForEmpty() - { - hostingController = nil - attachView() - } - func willAppear(in vc: UIViewController?) { if hostingController?.viewController.parent != vc { hostingController?.viewController.removeFromParent() hostingController.map { vc?.addChild($0.viewController) } - attachView() hostingController?.viewController.didMove(toParent: vc) } - else - { - attachView() - } } func didDisappear() @@ -42,9 +31,10 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView { guard let hcView = hostingController?.viewController.view else { - subviews.forEach { $0.removeFromSuperview() } + detachViews() return } + guard !isHidden else { return } if hcView.superview != self { subviews.forEach { $0.removeFromSuperview() } @@ -53,6 +43,11 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView } } + private func detachViews() + { + subviews.forEach { $0.removeFromSuperview() } + } + var shouldSkipNextRefresh: Bool = true override func prepareForReuse() { @@ -64,6 +59,8 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView { super.layoutSubviews() + attachView() + if hostingController?.viewController.view.frame != bounds { UIView.performWithoutAnimation { diff --git a/Sources/ASCollectionView/Cells/ASTableViewCell.swift b/Sources/ASCollectionView/Cells/ASTableViewCell.swift index 82ea7c9..82312fe 100644 --- a/Sources/ASCollectionView/Cells/ASTableViewCell.swift +++ b/Sources/ASCollectionView/Cells/ASTableViewCell.swift @@ -15,10 +15,6 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell { hostingController?.invalidateCellLayoutCallback = invalidateLayoutCallback hostingController?.tableViewScrollToCellCallback = scrollToCellCallback - if hostingController !== oldValue, hostingController != nil - { - attachView() - } } } @@ -52,19 +48,14 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell hostingController?.viewController.removeFromParent() } - override func didMoveToSuperview() - { - attachView() - } - private func attachView() { - guard superview != nil else { return } guard let hcView = hostingController?.viewController.view else { - contentView.subviews.forEach { $0.removeFromSuperview() } + detachViews() return } + guard !isHidden else { return } if hcView.superview != contentView { contentView.subviews.forEach { $0.removeFromSuperview() } @@ -73,6 +64,11 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell } } + private func detachViews() + { + contentView.subviews.forEach { $0.removeFromSuperview() } + } + var shouldSkipNextRefresh: Bool = true // This is used to avoid double-up in reloaded cells and our update from swiftUI override func prepareForReuse() { @@ -94,6 +90,8 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell { super.layoutSubviews() + attachView() + if hostingController?.viewController.view.frame != contentView.bounds { UIView.performWithoutAnimation { diff --git a/Sources/ASCollectionView/Cells/ASTableViewSupplementaryView.swift b/Sources/ASCollectionView/Cells/ASTableViewSupplementaryView.swift index 48d5173..31b8b6b 100644 --- a/Sources/ASCollectionView/Cells/ASTableViewSupplementaryView.swift +++ b/Sources/ASCollectionView/Cells/ASTableViewSupplementaryView.swift @@ -8,15 +8,6 @@ import UIKit class ASTableViewSupplementaryView: UITableViewHeaderFooterView { var hostingController: ASHostingControllerProtocol? - { - didSet - { - if hostingController !== oldValue, hostingController != nil - { - attachView() - } - } - } var sectionIDHash: Int? var supplementaryKind: String? @@ -32,12 +23,6 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView fatalError("init(coder:) has not been implemented") } - func setupForEmpty() - { - hostingController = nil - attachView() - } - func willAppear(in vc: UIViewController) { if hostingController?.viewController.parent != vc @@ -53,19 +38,14 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView hostingController?.viewController.removeFromParent() } - override func didMoveToSuperview() - { - attachView() - } - private func attachView() { - guard superview != nil else { return } guard let hcView = hostingController?.viewController.view else { - contentView.subviews.forEach { $0.removeFromSuperview() } + detachViews() return } + guard !isHidden else { return } if hcView.superview != contentView { contentView.subviews.forEach { $0.removeFromSuperview() } @@ -74,6 +54,11 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView } } + private func detachViews() + { + contentView.subviews.forEach { $0.removeFromSuperview() } + } + var shouldSkipNextRefresh: Bool = true override func prepareForReuse() { @@ -86,6 +71,8 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView { super.layoutSubviews() + attachView() + if hostingController?.viewController.view.frame != contentView.bounds { UIView.performWithoutAnimation { diff --git a/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift b/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift index f5502bb..de16855 100644 --- a/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift +++ b/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift @@ -74,7 +74,7 @@ class ASDiffableDataSourceCollectionView: ASDiffableDataSou guard let cell = supplementaryViewProvider?(collectionView, kind, indexPath) else { let empty = collectionView.dequeueReusableSupplementaryView(ofKind: supplementaryEmptyKind, withReuseIdentifier: supplementaryEmptyReuseID, for: indexPath) - (empty as? ASCollectionViewSupplementaryView)?.setupForEmpty() + (empty as? ASCollectionViewSupplementaryView)?.hostingController = nil return empty } return cell diff --git a/Sources/ASCollectionView/Implementation/ASCollectionView.swift b/Sources/ASCollectionView/Implementation/ASCollectionView.swift index 8edea8d..bc8d041 100644 --- a/Sources/ASCollectionView/Implementation/ASCollectionView.swift +++ b/Sources/ASCollectionView/Implementation/ASCollectionView.swift @@ -252,7 +252,7 @@ public struct ASCollectionView: UIViewControllerRepresentab else { return nil } let ifEmpty = { - reusableView.setupForEmpty() + reusableView.hostingController = nil } guard let section = self.parent.sections[safe: indexPath.section] else { ifEmpty(); return reusableView } diff --git a/Sources/ASCollectionView/Implementation/ASTableView.swift b/Sources/ASCollectionView/Implementation/ASTableView.swift index 72e4002..accdc16 100644 --- a/Sources/ASCollectionView/Implementation/ASTableView.swift +++ b/Sources/ASCollectionView/Implementation/ASTableView.swift @@ -601,7 +601,7 @@ public struct ASTableView: UIViewControllerRepresentable, C else { return } let ifEmpty = { - reusableView.setupForEmpty() + reusableView.hostingController = nil } guard let section = parent.sections[safe: sectionIndex] else { ifEmpty(); return }