Skip to content

Commit

Permalink
Merge pull request #13 from michzio/improve-collection-view-sections
Browse files Browse the repository at this point in the history
Improve supplementary views for sections
  • Loading branch information
michzio committed Sep 17, 2022
2 parents 4c960ab + 85ee659 commit d36c34d
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- SwiftUICollectionView (0.0.7)
- SwiftUICollectionView (0.0.9)

DEPENDENCIES:
- SwiftUICollectionView (from `../SwiftUICollectionView`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../SwiftUICollectionView"

SPEC CHECKSUMS:
SwiftUICollectionView: 7f9a87f1d369c2d769a93a551f073659e583016f
SwiftUICollectionView: 2a75c88b8b431e7b4c50411107e025479ba7b456

PODFILE CHECKSUM: 9a1452a7a2e894f254370980293834ce1f7f2aa1

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
20 changes: 11 additions & 9 deletions Example/SwiftUICollectionViewApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ struct ContentView: View {
.categories : Item.categoryItems
],
supplementaryKinds: [UICollectionView.elementKindSectionHeader, UICollectionView.elementKindSectionFooter],
supplementaryContent: { kind, indexPath, item in
switch kind {
case UICollectionView.elementKindSectionHeader:
return AnyView(Text("Header").font(.system(size: indexPath.section == 0 ? 30 : 16)))
case UICollectionView.elementKindSectionFooter:
return AnyView(Text("Footer"))
default:
return AnyView(EmptyView())
}
supplementaryContent: { kind, indexPath, section, item in
AnyView(Group {
switch kind {
case UICollectionView.elementKindSectionHeader:
Text("Header").font(.system(size: indexPath.section == 0 ? 30 : 16))
case UICollectionView.elementKindSectionFooter:
Text("Footer")
default:
EmptyView()
}
})
},
content: { indexPath, item in
let section = sections[indexPath.section]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies: [
.categories : Item.categoryItems
],
supplementaryKinds: [UICollectionView.elementKindSectionHeader, UICollectionView.elementKindSectionFooter],
supplementaryContent: { kind, indexPath, item in
supplementaryContent: { kind, indexPath, section, item in
switch kind {
case UICollectionView.elementKindSectionHeader:
return AnyView(Text("Header").font(.system(size: indexPath.section == 0 ? 30 : 16)))
Expand Down
2 changes: 1 addition & 1 deletion SwiftUICollectionView/SwiftUICollectionView.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SwiftUICollectionView"
s.version = "0.0.9"
s.version = "0.1.0"
s.summary = "SwiftUI representable of UIKit CollectionView that supports compositional layout."

s.swift_version = '5.6'
Expand Down
29 changes: 27 additions & 2 deletions SwiftUICollectionView/SwiftUICollectionView/CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct CollectionView<Section: Hashable, Item: Hashable>: UIViewControlle

// MARK: - Actions
private let content: (_ indexPath: IndexPath, _ item: Item) -> AnyView
private let supplementaryContent: ((_ kind: String, _ indexPath: IndexPath, _ item: Item?) -> AnyView)?
private let supplementaryContent: ((_ kind: String, _ indexPath: IndexPath, _ section: Section?, _ item: Item?) -> AnyView)?

private let snapshotProvider:(() -> NSDiffableDataSourceSnapshot<Section, Item>)?
private let selectionAction: ((_ collectionView: UICollectionView, _ item: Item, _ indexPath: IndexPath) -> Void)?
Expand All @@ -35,7 +35,7 @@ public struct CollectionView<Section: Hashable, Item: Hashable>: UIViewControlle
snapshotProvider: (() -> NSDiffableDataSourceSnapshot<Section, Item>)? = nil,
selectedItem: Binding<Item?>? = nil,
selectionAction: ((_ collectionView: UICollectionView, _ item: Item, _ indexPath: IndexPath) -> Void)? = nil,
supplementaryContent: ((_ kind: String, _ IndexPath: IndexPath, _ item: Item?) -> AnyView)? = nil,
@ViewBuilder supplementaryContent: @escaping (_ kind: String, _ IndexPath: IndexPath, _ section: Section?, _ item: Item?) -> AnyView,
@ViewBuilder content: @escaping (_ indexPath: IndexPath, _ item: Item) -> AnyView
) {
self.layout = layout
Expand All @@ -53,6 +53,31 @@ public struct CollectionView<Section: Hashable, Item: Hashable>: UIViewControlle
self.content = content
}

public init(
layout: UICollectionViewLayout,
sections: [Section],
items: [Section: [Item]],
animateChanges: Bool? = nil,
snapshotProvider: (() -> NSDiffableDataSourceSnapshot<Section, Item>)? = nil,
selectedItem: Binding<Item?>? = nil,
selectionAction: ((_ collectionView: UICollectionView, _ item: Item, _ indexPath: IndexPath) -> Void)? = nil,
@ViewBuilder content: @escaping (_ indexPath: IndexPath, _ item: Item) -> AnyView
) {
self.layout = layout

self.sections = sections
self.items = items
self.snapshotProvider = snapshotProvider
self.selectionAction = selectionAction
self.animateChanges = animateChanges
self.selectedItem = selectedItem

self.supplementaryKinds = []
self.supplementaryContent = nil

self.content = content
}

public func makeCoordinator() -> Coordinator {
Coordinator(self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CollectionViewController<Section, Item>: UIViewController, UICollec
var snapshot: NSDiffableDataSourceSnapshot<Section, Item>!

var supplementaryKinds: [String] = []
var supplementaryContent: ((_ kind: String, _ indexPath: IndexPath, _ item: Item?) -> AnyView)?
var supplementaryContent: ((_ kind: String, _ indexPath: IndexPath, _ section: Section?, _ item: Item?) -> AnyView)?
var content: ((_ indexPath: IndexPath, _ item: Item) -> AnyView)!

var onSelectItem: ((_ collectionView: UICollectionView, _ item: Item, _ indexPath: IndexPath) -> Void)?
Expand Down Expand Up @@ -157,9 +157,16 @@ extension CollectionViewController {
fatalError("Could not load supplementary view")
}

let section: Section?
if #available(iOS 15.0, *) {
section = dataSource.sectionIdentifier(for: indexPath.section)
} else {
section = nil
}

let item = dataSource.itemIdentifier(for: indexPath)

guard let content = supplementaryContent?(kind, indexPath, item) else {
guard let content = supplementaryContent?(kind, indexPath, section, 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.")
}

Expand Down

0 comments on commit d36c34d

Please sign in to comment.