Skip to content

Commit fb2b3d3

Browse files
authored
add type-safe sections accessor for compositional layout (#8)
1 parent 1083a53 commit fb2b3d3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Sources/DynamicList/DynamicListView.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ public final class DynamicListView<Section: Hashable, Data: Hashable>: UIView,
137137

138138
private let contentPagingTrigger: ContentPagingTrigger
139139

140+
convenience public init(
141+
sectionProvider: @escaping (Section, NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection,
142+
contentInsetAdjustmentBehavior: UIScrollView.ContentInsetAdjustmentBehavior = .automatic
143+
) {
144+
weak var weakSelf: DynamicListView?
145+
self.init(
146+
layout: UICollectionViewCompositionalLayout.init(
147+
sectionProvider: { sectionIndex, environment in
148+
guard let `self` = weakSelf else {
149+
return nil
150+
}
151+
return sectionProvider(
152+
self.snapshot().sectionIdentifiers[sectionIndex],
153+
environment
154+
)
155+
}
156+
),
157+
contentInsetAdjustmentBehavior: contentInsetAdjustmentBehavior
158+
)
159+
weakSelf = self
160+
}
161+
140162
public init(
141163
layout: UICollectionViewCompositionalLayout,
142164
contentInsetAdjustmentBehavior: UIScrollView.ContentInsetAdjustmentBehavior = .automatic

0 commit comments

Comments
 (0)