IGListKit/Examples/Examples-iOS/IGListKitExamples/SectionControllers/With Composable Layout/ExpandableComposableSectionController.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

58 lines
1.9 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
/// Like ExpandableSectionController, but supports UICollectionViewCompositionalLayout
final class ExpandableComposableSectionController: ListSectionController, CompositionLayoutCapable {
private var expanded = false
private var object: String?
override func sizeForItem(at index: Int) -> CGSize {
// Size handled by cell
return CGSizeZero
}
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell: CompositionLayoutCell = collectionContext.dequeueReusableCell(for: self, at: index)
cell.text = object
cell.expanded = expanded
return cell
}
override func didUpdate(to object: Any) {
self.object = object as? String
}
override func didSelectItem(at index: Int) {
expanded = !expanded
guard let cell = collectionContext.cellForItem(at: index, sectionController: self) as? CompositionLayoutCell else {
return
}
cell.expanded = expanded;
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.4,
initialSpringVelocity: 0.6,
options: [],
animations: {
self.collectionContext?.invalidateLayout(for: self)
})
}
// MARK: CompositionLayoutCapable
func collectionViewSectionLayout(layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? {
let config = UICollectionLayoutListConfiguration(appearance: .plain)
return NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment)
}
}