Commit c4b2c7d8 authored by Apptek Studios's avatar Apptek Studios
Browse files

Refine header caching

parent ffddb7df
Showing with 39 additions and 39 deletions
+39 -39
......@@ -17,7 +17,7 @@ class ASCollectionViewDecoration<Content: Decoration>: ASCollectionViewSupplemen
{
super.init(frame: frame)
let view = Content()
setupFor(view: view)
hostingController = ASHostingController(view)
willAppear(in: nil)
}
......
......@@ -12,18 +12,6 @@ class ASCollectionViewSupplementaryView: UICollectionReusableView
var selfSizingConfig: ASSelfSizingConfig = .init(selfSizeHorizontally: true, selfSizeVertically: true)
var maxSizeForSelfSizing: ASOptionalSize = .none
func setupFor<Content: View>(view: Content)
{
if let hc = hostingController as? ASHostingController<Content>
{
hc.setView(view)
}
else
{
hostingController = ASHostingController<Content>(view)
}
}
func setupForEmpty()
{
hostingController = nil
......
......@@ -7,7 +7,11 @@ import UIKit
@available(iOS 13.0, *)
class ASTableViewSupplementaryView: UITableViewHeaderFooterView
{
var hostingController: ASHostingControllerProtocol?
var hostingController: ASHostingControllerProtocol? {
didSet {
setNeedsLayout()
}
}
var sectionIDHash: Int?
override init(reuseIdentifier: String?)
......
......@@ -326,9 +326,8 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
{ kind in
cv.indexPathsForVisibleSupplementaryElements(ofKind: kind).forEach
{
guard let supplementaryView = parent.sections[safe: $0.section]?.supplementary(ofKind: kind) else { return }
(cv.supplementaryView(forElementKind: kind, at: $0) as? ASCollectionViewSupplementaryView)?
.setupFor(view: supplementaryView)
guard let view = (cv.supplementaryView(forElementKind: kind, at: $0) as? ASCollectionViewSupplementaryView) else { return }
view.hostingController = parent.sections[safe: $0.section]?.dataSource.updateOrCreateHostController(forSupplementaryKind: kind, existingHC: view.hostingController)
}
}
}
......
......@@ -108,6 +108,8 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
private var hasDoneInitialSetup = false
private var hasSkippedFirstUpdate = false
private var visibleSupplementaries: [ASSupplementaryCellID<SectionID>: ASTableViewSupplementaryView] = [:]
// MARK: Caching
......@@ -254,23 +256,10 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
else { return }
self.section(forItemID: itemID)?.dataSource.update(hc, forItemID: itemID)
}
tv.visibleHeaderViews.forEach { view in
guard
let view = view as? ASTableViewSupplementaryView,
let sectionIDHash = view.sectionIDHash,
let hc = view.hostingController
else { return }
self.parent.sections.first(where: { $0.id.hashValue == sectionIDHash })?.dataSource.update(hc, forSupplementaryKind: UICollectionView.elementKindSectionHeader)
}
tv.visibleFooterViews.forEach { view in
guard
let view = view as? ASTableViewSupplementaryView,
let sectionIDHash = view.sectionIDHash,
let hc = view.hostingController
else { return }
self.parent.sections.first(where: { $0.id.hashValue == sectionIDHash })?.dataSource.update(hc, forSupplementaryKind: UICollectionView.elementKindSectionFooter)
visibleSupplementaries.forEach { (key, view) in
guard let section = self.parent.sections.first(where: { $0.id.hashValue == key.sectionID.hashValue }) else { return }
view.hostingController = section.dataSource.updateOrCreateHostController(forSupplementaryKind: key.supplementaryKind, existingHC: view.hostingController)
}
}
......@@ -306,7 +295,7 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
var lastContentSize: CGSize = .zero
func didUpdateContentSize(_ size: CGSize)
{
guard let tv = tableViewController?.tableView, tv.contentSize != lastContentSize else { return }
guard let tv = tableViewController?.tableView, tv.contentSize != lastContentSize, tv.contentSize.height != 0 else { return }
let firstSize = lastContentSize == .zero
lastContentSize = tv.contentSize
parent.contentSizeTracker?.contentSize = size
......@@ -357,22 +346,42 @@ public struct ASTableView<SectionID: Hashable>: UIViewControllerRepresentable, C
public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
tableViewController.map { (view as? ASTableViewSupplementaryView)?.willAppear(in: $0) }
guard let view = (view as? ASTableViewSupplementaryView) else { return }
if let section = parent.sections[safe: section] {
let supplementaryID = ASSupplementaryCellID(sectionID: section.id, supplementaryKind: UICollectionView.elementKindSectionHeader)
visibleSupplementaries[supplementaryID] = view
}
tableViewController.map { view.willAppear(in: $0) }
}
public func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int)
{
(view as? ASTableViewSupplementaryView)?.didDisappear()
guard let view = (view as? ASTableViewSupplementaryView) else { return }
if let section = parent.sections[safe: section] {
let supplementaryID = ASSupplementaryCellID(sectionID: section.id, supplementaryKind: UICollectionView.elementKindSectionHeader)
visibleSupplementaries.removeValue(forKey: supplementaryID)
}
view.didDisappear()
}
public func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int)
{
tableViewController.map { (view as? ASTableViewSupplementaryView)?.willAppear(in: $0) }
guard let view = (view as? ASTableViewSupplementaryView) else { return }
if let section = parent.sections[safe: section] {
let supplementaryID = ASSupplementaryCellID(sectionID: section.id, supplementaryKind: UICollectionView.elementKindSectionFooter)
visibleSupplementaries[supplementaryID] = view
}
tableViewController.map { view.willAppear(in: $0) }
}
public func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int)
{
(view as? ASTableViewSupplementaryView)?.didDisappear()
guard let view = (view as? ASTableViewSupplementaryView) else { return }
if let section = parent.sections[safe: section] {
let supplementaryID = ASSupplementaryCellID(sectionID: section.id, supplementaryKind: UICollectionView.elementKindSectionFooter)
visibleSupplementaries.removeValue(forKey: supplementaryID)
}
view.didDisappear()
}
public func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath])
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment