IGListKit/Examples/Examples-iOS/IGListKitExamples/ViewControllers/CompositionLayoutViewController.swift
Maxime Ollivier 77a4582ce5 create UICollectionViewCompositionalLayout example
Summary:
Lets try using `UICollectionViewCompositionalLayout` with IGListKit! It does require some data plumbing, but maybe we can make that simpler in the future. For now, lets keep things simple and see how it feels.

This diff replicates the `MixedDataViewController` example starting with the expandable text.

Differential Revision: D52260529

fbshipit-source-id: 5579b8d6fe5d1f95ed282f821e6555a6a56b1c1c
2023-12-19 07:17:30 -08:00

79 lines
3 KiB
Swift

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import IGListKit
import UIKit
/// Enables SectionControllers to return their own layout. In the future, we might want IGListKit
/// to handle this, but for now, lets keep it simple.
protocol CompositionLayoutCapable {
func collectionViewSectionLayout(layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection?
}
/// Like MixedDataViewController, but using UICollectionViewCompositionalLayout
final class CompositionLayoutViewController: UIViewController, ListAdapterDataSource {
private var collectionView: UICollectionView?
private lazy var adapter: ListAdapter = {
return ListAdapter(updater: ListAdapterUpdater(), viewController: self)
}()
private let data: [Any] = [
"Maecenas faucibus mollis interdum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.",
"Praesent commodo cursus magna, vel scelerisque nisl consectetur et.",
"Nullam quis risus eget urna mollis ornare vel eu leo. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.",
"Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.",
]
override func viewDidLoad() {
super.viewDidLoad()
// Layout
let layout = UICollectionViewCompositionalLayout {[weak self] (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
guard let self = self else {
return nil
}
let controller = self.adapter.sectionController(forSection: sectionIndex)
guard let controller = controller as? CompositionLayoutCapable else {
return nil
}
return controller.collectionViewSectionLayout(layoutEnvironment: layoutEnvironment)
}
// Collection View
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
self.collectionView = collectionView
view.addSubview(collectionView)
adapter.collectionView = collectionView
adapter.dataSource = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView?.frame = view.bounds
}
// MARK: ListAdapterDataSource
func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
return data.map { $0 as! ListDiffable }
}
func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
switch object {
case is String:
return ExpandableComposableSectionController()
default:
return ListSectionController()
}
}
func emptyView(for listAdapter: ListAdapter) -> UIView? { return nil }
}