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

Commit 4288744

Browse files
author
tbren
committed
iOS 15 compatibility fixes
1 parent 8698712 commit 4288744

File tree

5 files changed

+24
-37
lines changed

5 files changed

+24
-37
lines changed

Sources/ASCollectionView/Datasource/ASDiffableDataSourceCollectionView.swift

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ class ASDiffableDataSourceCollectionView<SectionID: Hashable>: ASDiffableDataSou
1010
/// The type of closure providing the cell.
1111
public typealias Snapshot = ASDiffableDataSourceSnapshot<SectionID>
1212
public typealias CellProvider = (UICollectionView, IndexPath, ASCollectionViewItemUniqueID) -> ASCollectionViewCell?
13-
public typealias SupplementaryProvider = (UICollectionView, String, IndexPath) -> ASCollectionViewSupplementaryView?
13+
public typealias SupplementaryProvider = (UICollectionView, String, IndexPath) -> ASCollectionViewSupplementaryView
1414

1515
private weak var collectionView: UICollectionView?
1616
var cellProvider: CellProvider
17-
var supplementaryViewProvider: SupplementaryProvider?
17+
var supplementaryViewProvider: SupplementaryProvider
1818

19-
public init(collectionView: UICollectionView, cellProvider: @escaping CellProvider)
19+
public init(collectionView: UICollectionView, cellProvider: @escaping CellProvider, supplementaryViewProvider: @escaping SupplementaryProvider)
2020
{
2121
self.collectionView = collectionView
2222
self.cellProvider = cellProvider
23+
self.supplementaryViewProvider = supplementaryViewProvider
2324
super.init()
2425

2526
collectionView.dataSource = self
26-
collectionView.register(ASCollectionViewSupplementaryView.self, forSupplementaryViewOfKind: supplementaryEmptyKind, withReuseIdentifier: supplementaryEmptyReuseID)
2727
}
2828

2929
private var firstLoad: Bool = true
@@ -73,18 +73,8 @@ class ASDiffableDataSourceCollectionView<SectionID: Hashable>: ASDiffableDataSou
7373
return cell
7474
}
7575

76-
private let supplementaryEmptyKind = UUID().uuidString // Used to prevent crash if supplementaries defined in layout but not provided by the section
77-
private let supplementaryEmptyReuseID = UUID().uuidString
78-
7976
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
8077
{
81-
guard let cell = supplementaryViewProvider?(collectionView, kind, indexPath)
82-
else
83-
{
84-
let empty = collectionView.dequeueReusableSupplementaryView(ofKind: supplementaryEmptyKind, withReuseIdentifier: supplementaryEmptyReuseID, for: indexPath)
85-
(empty as? ASCollectionViewSupplementaryView)?.setAsEmpty(supplementaryID: ASSupplementaryCellID(sectionIDHash: 0, supplementaryKind: supplementaryEmptyKind))
86-
return empty
87-
}
88-
return cell
78+
return supplementaryViewProvider(collectionView, kind, indexPath)
8979
}
9080
}

Sources/ASCollectionView/FunctionBuilders/SectionArrayBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public func buildSectionArray<SectionID: Hashable>(@SectionArrayBuilder <Section
4444
}
4545

4646
@available(iOS 13.0, *)
47-
@_functionBuilder
47+
@resultBuilder
4848
public struct SectionArrayBuilder<SectionID> where SectionID: Hashable
4949
{
5050
public typealias Section = ASCollectionViewSection<SectionID>

Sources/ASCollectionView/FunctionBuilders/ViewArrayBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
import SwiftUI
55

66
@available(iOS 13.0, *)
7-
@_functionBuilder
7+
@resultBuilder
88
public struct ViewArrayBuilder
99
{
1010
public enum Wrapper

Sources/ASCollectionView/Implementation/ASCollectionView.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
218218

219219
cv.register(Cell.self, forCellWithReuseIdentifier: cellReuseID)
220220

221-
dataSource = .init(collectionView: cv)
221+
dataSource = .init(collectionView: cv, cellProvider:
222222
{ [weak self] collectionView, indexPath, itemID in
223223
guard let self = self else { return nil }
224224

@@ -253,32 +253,30 @@ public struct ASCollectionView<SectionID: Hashable>: UIViewControllerRepresentab
253253
}
254254

255255
return cell
256-
}
257-
dataSource?.supplementaryViewProvider = { [weak self] cv, kind, indexPath in
258-
guard let self = self else { return nil }
259-
260-
guard self.supplementaryKinds().contains(kind)
261-
else
262-
{
263-
return nil
256+
}, supplementaryViewProvider: { [weak self] cv, kind, indexPath in
257+
guard let self = self else { fatalError("This shouldn't happen") }
258+
if !self.supplementaryKinds().contains(kind) && !self.haveRegisteredForSupplementaryOfKind.contains(kind) {
259+
cv.register(ASCollectionViewSupplementaryView.self, forSupplementaryViewOfKind: kind, withReuseIdentifier: self.supplementaryReuseID)
260+
self.haveRegisteredForSupplementaryOfKind.insert(kind)
261+
print("ASCOLLECTIONVIEW WARNING: Your collection View layout requested supplementary of type: \(kind) and you have not provided any content, assuming empty view")
264262
}
265-
guard let reusableView = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.supplementaryReuseID, for: indexPath) as? ASCollectionViewSupplementaryView
266-
else { return nil }
267-
268-
guard let section = self.parent.sections[safe: indexPath.section] else { reusableView.setAsEmpty(supplementaryID: nil); return reusableView }
269-
let supplementaryID = ASSupplementaryCellID(sectionIDHash: section.id.hashValue, supplementaryKind: kind)
270-
reusableView.supplementaryID = supplementaryID
271-
263+
let reusableView = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.supplementaryReuseID, for: indexPath) as! ASCollectionViewSupplementaryView //Force cast appropriate here
264+
265+
guard let section = self.parent.sections[safe: indexPath.section] else { reusableView.setAsEmpty(supplementaryID: nil)
266+
return reusableView
267+
}
268+
272269
// Self Sizing Settings
273270
let selfSizingContext = ASSelfSizingContext(cellType: .supplementary(kind), indexPath: indexPath)
274271
reusableView.selfSizingConfig =
275272
section.dataSource.getSelfSizingSettings(context: selfSizingContext)
276273
?? ASSelfSizingConfig()
277274

275+
let supplementaryID = ASSupplementaryCellID(sectionIDHash: section.id.hashValue, supplementaryKind: kind)
278276
reusableView.setContent(supplementaryID: supplementaryID, content: section.dataSource.content(supplementaryID: supplementaryID))
279277

280278
return reusableView
281-
}
279+
})
282280
setupPrefetching()
283281
}
284282

Sources/ASCollectionView/UIKit/AS_UICollectionView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ public class AS_CollectionViewController: UIViewController
4949
// Get current central cell
5050
self.coordinator?.prepareForOrientationChange()
5151

52-
super.viewWillTransition(to: size, with: coordinator)
53-
// The following is a workaround to fix the interface rotation animation under SwiftUI
54-
view.frame = CGRect(origin: view.frame.origin, size: size)
5552

5653
coordinator.animate(alongsideTransition: { _ in
5754
self.view.setNeedsLayout()
@@ -67,6 +64,8 @@ public class AS_CollectionViewController: UIViewController
6764
// Completion
6865
self.coordinator?.completedOrientationChange()
6966
}
67+
68+
super.viewWillTransition(to: size, with: coordinator)
7069
}
7170

7271
override public func viewSafeAreaInsetsDidChange()

0 commit comments

Comments
 (0)