Created
May 3, 2020 09:20
-
-
Save michzio/db330a825285275fe40da5d19d4f2ab5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CollectionViewController { | |
private func supplementaryViewProvider(collectionView: UICollectionView, kind: String, indexPath: IndexPath) -> UICollectionReusableView? { | |
if kind == "badge" { | |
return badgeViewProvider(collectionView, kind, indexPath) | |
} else { | |
return contentSupplementaryProvider(collectionView, kind, indexPath) | |
} | |
} | |
private func contentSupplementaryProvider(_ collectionView: UICollectionView, _ kind: String, _ indexPath: IndexPath) -> UICollectionReusableView? { | |
guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: HostingControllerCollectionReusableView<AnyView>.reuseIdentifier, for: indexPath) as? HostingControllerCollectionReusableView<AnyView> else { | |
fatalError("Could not load supplementary view") | |
} | |
let item = self.dataSource.itemIdentifier(for: indexPath) | |
guard let content = self.supplementaryContent?(kind, indexPath, item) else { | |
fatalError("Supplementary view content not provided for kind: \(kind), indexPath: \(indexPath). Please provide supplementaryContent closure parameter to CollectionView returning some View and provide kind in supplementaryKinds array parameter. Or remove this kind: \(kind) from layout definition.") | |
} | |
view.host(content) | |
return view | |
} | |
private func badgeViewProvider(_ collectionView: UICollectionView, _ kind: String, _ indexPath: IndexPath) -> UICollectionReusableView? { | |
if let item = self.dataSource.itemIdentifier(for: indexPath), | |
item is HasBadgeCount, | |
let badgeCount = item.badgeCount { | |
guard let badgeView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: BadgeSupplementaryView.reuseIdentifier, for: indexPath) as? BadgeSupplementaryView else { | |
fatalError("Cannot create badge supplementary") | |
} | |
badgeView.label.text = "\(badgeCount)" | |
return badgeView | |
} else { | |
guard let emptyView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: EmptySupplementaryView.reuseIdentifier, for: indexPath) as? EmptySupplementaryView else { | |
fatalError("Cannot create badge supplementary") | |
} | |
return emptyView | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment