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

Commit

Permalink
iOS 15 compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tbren committed Sep 22, 2021
1 parent 8698712 commit 4288744
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ class ASDiffableDataSourceCollectionView<SectionID: Hashable>: ASDiffableDataSou
/// The type of closure providing the cell.
public typealias Snapshot = ASDiffableDataSourceSnapshot<SectionID>
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
Expand Down Expand Up @@ -73,18 +73,8 @@ class ASDiffableDataSourceCollectionView<SectionID: Hashable>: 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public func buildSectionArray<SectionID: Hashable>(@SectionArrayBuilder <Section
}

@available(iOS 13.0, *)
@_functionBuilder
@resultBuilder
public struct SectionArrayBuilder<SectionID> where SectionID: Hashable
{
public typealias Section = ASCollectionViewSection<SectionID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
import SwiftUI

@available(iOS 13.0, *)
@_functionBuilder
@resultBuilder
public struct ViewArrayBuilder
{
public enum Wrapper
Expand Down
32 changes: 15 additions & 17 deletions Sources/ASCollectionView/Implementation/ASCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public struct ASCollectionView<SectionID: Hashable>: 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 }

Expand Down Expand Up @@ -253,32 +253,30 @@ public struct ASCollectionView<SectionID: Hashable>: 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()
}

Expand Down
5 changes: 2 additions & 3 deletions Sources/ASCollectionView/UIKit/AS_UICollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -67,6 +64,8 @@ public class AS_CollectionViewController: UIViewController
// Completion
self.coordinator?.completedOrientationChange()
}

super.viewWillTransition(to: size, with: coordinator)
}

override public func viewSafeAreaInsetsDidChange()
Expand Down

0 comments on commit 4288744

Please sign in to comment.