From 4288744ba484c1062c109c0f28d72b629d321d55 Mon Sep 17 00:00:00 2001 From: tbren Date: Wed, 22 Sep 2021 11:31:14 +1000 Subject: [PATCH] iOS 15 compatibility fixes --- .../ASDiffableDataSourceCollectionView.swift | 20 +++--------- .../SectionArrayBuilder.swift | 2 +- .../FunctionBuilders/ViewArrayBuilder.swift | 2 +- .../Implementation/ASCollectionView.swift | 32 +++++++++---------- .../UIKit/AS_UICollectionView.swift | 5 ++- 5 files changed, 24 insertions(+), 37 deletions(-) diff --git a/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift b/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift index 0d9335b..e78a955 100644 --- a/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift +++ b/Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift @@ -10,20 +10,20 @@ class ASDiffableDataSourceCollectionView: ASDiffableDataSou /// The type of closure providing the cell. public typealias Snapshot = ASDiffableDataSourceSnapshot public typealias CellProvider = (UICollectionView, IndexPath, ASCollectionViewItemUniqueID) -> ASCollectionViewCell? - public typealias SupplementaryProvider = (UICollectionView, String, IndexPath) -> ASCollectionViewSupplementaryView? + public typealias SupplementaryProvider = (UICollectionView, String, IndexPath) -> ASCollectionViewSupplementaryView private weak var collectionView: UICollectionView? var cellProvider: CellProvider - var supplementaryViewProvider: SupplementaryProvider? + var supplementaryViewProvider: SupplementaryProvider - public init(collectionView: UICollectionView, cellProvider: @escaping CellProvider) + public init(collectionView: UICollectionView, cellProvider: @escaping CellProvider, supplementaryViewProvider: @escaping SupplementaryProvider) { self.collectionView = collectionView self.cellProvider = cellProvider + self.supplementaryViewProvider = supplementaryViewProvider super.init() collectionView.dataSource = self - collectionView.register(ASCollectionViewSupplementaryView.self, forSupplementaryViewOfKind: supplementaryEmptyKind, withReuseIdentifier: supplementaryEmptyReuseID) } private var firstLoad: Bool = true @@ -73,18 +73,8 @@ class ASDiffableDataSourceCollectionView: ASDiffableDataSou return cell } - private let supplementaryEmptyKind = UUID().uuidString // Used to prevent crash if supplementaries defined in layout but not provided by the section - private let supplementaryEmptyReuseID = UUID().uuidString - func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { - guard let cell = supplementaryViewProvider?(collectionView, kind, indexPath) - else - { - let empty = collectionView.dequeueReusableSupplementaryView(ofKind: supplementaryEmptyKind, withReuseIdentifier: supplementaryEmptyReuseID, for: indexPath) - (empty as? ASCollectionViewSupplementaryView)?.setAsEmpty(supplementaryID: ASSupplementaryCellID(sectionIDHash: 0, supplementaryKind: supplementaryEmptyKind)) - return empty - } - return cell + return supplementaryViewProvider(collectionView, kind, indexPath) } } diff --git a/Sources/ASCollectionView/FunctionBuilders/SectionArrayBuilder.swift b/Sources/ASCollectionView/FunctionBuilders/SectionArrayBuilder.swift index 6cafebc..bb8321e 100644 --- a/Sources/ASCollectionView/FunctionBuilders/SectionArrayBuilder.swift +++ b/Sources/ASCollectionView/FunctionBuilders/SectionArrayBuilder.swift @@ -44,7 +44,7 @@ public func buildSectionArray(@SectionArrayBuilder
where SectionID: Hashable { public typealias Section = ASCollectionViewSection diff --git a/Sources/ASCollectionView/FunctionBuilders/ViewArrayBuilder.swift b/Sources/ASCollectionView/FunctionBuilders/ViewArrayBuilder.swift index a2894e6..35d898c 100644 --- a/Sources/ASCollectionView/FunctionBuilders/ViewArrayBuilder.swift +++ b/Sources/ASCollectionView/FunctionBuilders/ViewArrayBuilder.swift @@ -4,7 +4,7 @@ import Foundation import SwiftUI @available(iOS 13.0, *) -@_functionBuilder +@resultBuilder public struct ViewArrayBuilder { public enum Wrapper diff --git a/Sources/ASCollectionView/Implementation/ASCollectionView.swift b/Sources/ASCollectionView/Implementation/ASCollectionView.swift index 4cb5855..eaf4864 100644 --- a/Sources/ASCollectionView/Implementation/ASCollectionView.swift +++ b/Sources/ASCollectionView/Implementation/ASCollectionView.swift @@ -218,7 +218,7 @@ public struct ASCollectionView: UIViewControllerRepresentab cv.register(Cell.self, forCellWithReuseIdentifier: cellReuseID) - dataSource = .init(collectionView: cv) + dataSource = .init(collectionView: cv, cellProvider: { [weak self] collectionView, indexPath, itemID in guard let self = self else { return nil } @@ -253,32 +253,30 @@ public struct ASCollectionView: UIViewControllerRepresentab } return cell - } - dataSource?.supplementaryViewProvider = { [weak self] cv, kind, indexPath in - guard let self = self else { return nil } - - guard self.supplementaryKinds().contains(kind) - else - { - return nil + }, supplementaryViewProvider: { [weak self] cv, kind, indexPath in + guard let self = self else { fatalError("This shouldn't happen") } + if !self.supplementaryKinds().contains(kind) && !self.haveRegisteredForSupplementaryOfKind.contains(kind) { + cv.register(ASCollectionViewSupplementaryView.self, forSupplementaryViewOfKind: kind, withReuseIdentifier: self.supplementaryReuseID) + self.haveRegisteredForSupplementaryOfKind.insert(kind) + print("ASCOLLECTIONVIEW WARNING: Your collection View layout requested supplementary of type: \(kind) and you have not provided any content, assuming empty view") } - guard let reusableView = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.supplementaryReuseID, for: indexPath) as? ASCollectionViewSupplementaryView - else { return nil } - - guard let section = self.parent.sections[safe: indexPath.section] else { reusableView.setAsEmpty(supplementaryID: nil); return reusableView } - let supplementaryID = ASSupplementaryCellID(sectionIDHash: section.id.hashValue, supplementaryKind: kind) - reusableView.supplementaryID = supplementaryID - + let reusableView = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.supplementaryReuseID, for: indexPath) as! ASCollectionViewSupplementaryView //Force cast appropriate here + + guard let section = self.parent.sections[safe: indexPath.section] else { reusableView.setAsEmpty(supplementaryID: nil) + return reusableView + } + // Self Sizing Settings let selfSizingContext = ASSelfSizingContext(cellType: .supplementary(kind), indexPath: indexPath) reusableView.selfSizingConfig = section.dataSource.getSelfSizingSettings(context: selfSizingContext) ?? ASSelfSizingConfig() + let supplementaryID = ASSupplementaryCellID(sectionIDHash: section.id.hashValue, supplementaryKind: kind) reusableView.setContent(supplementaryID: supplementaryID, content: section.dataSource.content(supplementaryID: supplementaryID)) return reusableView - } + }) setupPrefetching() } diff --git a/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift b/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift index 2248c2d..b0324a7 100644 --- a/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift +++ b/Sources/ASCollectionView/UIKit/AS_UICollectionView.swift @@ -49,9 +49,6 @@ public class AS_CollectionViewController: UIViewController // Get current central cell self.coordinator?.prepareForOrientationChange() - super.viewWillTransition(to: size, with: coordinator) - // The following is a workaround to fix the interface rotation animation under SwiftUI - view.frame = CGRect(origin: view.frame.origin, size: size) coordinator.animate(alongsideTransition: { _ in self.view.setNeedsLayout() @@ -67,6 +64,8 @@ public class AS_CollectionViewController: UIViewController // Completion self.coordinator?.completedOrientationChange() } + + super.viewWillTransition(to: size, with: coordinator) } override public func viewSafeAreaInsetsDidChange()