Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
V1.6.2 (#141)
Browse files Browse the repository at this point in the history
* Attach views in layoutSubviews (as apple does in some of their UI), fixes #130
  • Loading branch information
apptekstudios authored May 2, 2020
1 parent 44e46a9 commit 272fe9d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 55 deletions.
2 changes: 1 addition & 1 deletion ASCollectionView-SwiftUI.podspec
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 9 additions & 6 deletions Sources/ASCollectionView/Cells/ASCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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() }
Expand All @@ -60,6 +56,11 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
}
}

private func detachViews()
{
contentView.subviews.forEach { $0.removeFromSuperview() }
}

var shouldSkipNextRefresh: Bool = true

override func prepareForReuse()
Expand All @@ -75,6 +76,8 @@ class ASCollectionViewCell: UICollectionViewCell, ASDataSourceConfigurableCell
{
super.layoutSubviews()

attachView()

if hostingController?.viewController.view.frame != contentView.bounds
{
UIView.performWithoutAnimation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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() }
Expand All @@ -53,6 +43,11 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
}
}

private func detachViews()
{
subviews.forEach { $0.removeFromSuperview() }
}

var shouldSkipNextRefresh: Bool = true
override func prepareForReuse()
{
Expand All @@ -64,6 +59,8 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
{
super.layoutSubviews()

attachView()

if hostingController?.viewController.view.frame != bounds
{
UIView.performWithoutAnimation {
Expand Down
20 changes: 9 additions & 11 deletions Sources/ASCollectionView/Cells/ASTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
{
hostingController?.invalidateCellLayoutCallback = invalidateLayoutCallback
hostingController?.tableViewScrollToCellCallback = scrollToCellCallback
if hostingController !== oldValue, hostingController != nil
{
attachView()
}
}
}

Expand Down Expand Up @@ -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() }
Expand All @@ -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()
{
Expand All @@ -94,6 +90,8 @@ class ASTableViewCell: UITableViewCell, ASDataSourceConfigurableCell
{
super.layoutSubviews()

attachView()

if hostingController?.viewController.view.frame != contentView.bounds
{
UIView.performWithoutAnimation {
Expand Down
31 changes: 9 additions & 22 deletions Sources/ASCollectionView/Cells/ASTableViewSupplementaryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -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() }
Expand All @@ -74,6 +54,11 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
}
}

private func detachViews()
{
contentView.subviews.forEach { $0.removeFromSuperview() }
}

var shouldSkipNextRefresh: Bool = true
override func prepareForReuse()
{
Expand All @@ -86,6 +71,8 @@ class ASTableViewSupplementaryView: UITableViewHeaderFooterView
{
super.layoutSubviews()

attachView()

if hostingController?.viewController.view.frame != contentView.bounds
{
UIView.performWithoutAnimation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ASDiffableDataSourceCollectionView<SectionID: Hashable>: 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
else { return nil }

let ifEmpty = {
reusableView.setupForEmpty()
reusableView.hostingController = nil
}

guard let section = self.parent.sections[safe: indexPath.section] else { ifEmpty(); return reusableView }
Expand Down
2 changes: 1 addition & 1 deletion Sources/ASCollectionView/Implementation/ASTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
else { return }

let ifEmpty = {
reusableView.setupForEmpty()
reusableView.hostingController = nil
}

guard let section = parent.sections[safe: sectionIndex] else { ifEmpty(); return }
Expand Down

0 comments on commit 272fe9d

Please sign in to comment.