IGListKit/Examples/Examples-iOS/IGListKitExamples/ViewControllers/CompositionLayoutViewController.swift
Abhyas Mall 305a7bce0a Feed View Controller Demo and Improve UI Testing Infrastructure (#1629)
Summary:
## Changes in this pull request

This PR adds a new feed view controller demo to the IGListKit example app, showcasing how to implement a social media feed with IGListKit. The demo features infinite scrolling with pagination, post deletion, and interactive elements. Also, I've improved the UI testing infrastructure by adding helper methods for more reliable tests across different device types. The new helpers make the tests more resilient when dealing with iPad split views and ensure elements are properly visible before interacting with them.

### Checklist

- [X] All tests pass. Demo project builds and runs.
- [X] I added tests, an experiment, or detailed why my change isn't tested.
- [X] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes.
- [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/main/.github/CONTRIBUTING.md)

Pull Request resolved: https://github.com/Instagram/IGListKit/pull/1629

Reviewed By: fabiomassimo

Differential Revision: D77482204

Pulled By: TimOliver

fbshipit-source-id: 44ac86ed253e8452388aad50a1ed05bc6f3a31ab
2025-12-10 17:18:43 +09:00

109 lines
5.1 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] = [
ActivityItem(bodyText: "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", header: "Activity Start"),
ActivityItem(bodyText: "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore."),
ActivityItem(bodyText: "Excepteur sint occaecat cupidatat non proident."),
ActivityItem(bodyText: "Dominus", footer: "Activity End"),
SelectionModel(options: ["Leverage agile", "frameworks", "robust synopsis", "high level", "overviews",
"Iterative approaches", "corporate strategy", "foster collaborative",
"overall value", "proposition", "Organically grow", "holistic world view",
"disruptive", "innovation", "workplace diversity", "empowerment"]),
"Maecenas faucibus mollis interdum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.",
GridItem(color: UIColor(red: 237 / 255.0, green: 73 / 255.0, blue: 86 / 255.0, alpha: 1), itemCount: 6),
User(pk: 2, name: "Ryan Olson", handle: "ryanolsonk"),
HorizontalCardsSection(cardCount: 10),
SwipeActionSection(),
"Praesent commodo cursus magna, vel scelerisque nisl consectetur et.",
User(pk: 4, name: "Oliver Rickard", handle: "ocrickard"),
HorizontalCardsSection(cardCount: 2),
GridItem(color: UIColor(red: 56 / 255.0, green: 151 / 255.0, blue: 240 / 255.0, alpha: 1), itemCount: 5),
"Nullam quis risus eget urna mollis ornare vel eu leo. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.",
User(pk: 3, name: "Jesse Squires", handle: "jesse_squires"),
GridItem(color: UIColor(red: 112 / 255.0, green: 192 / 255.0, blue: 80 / 255.0, alpha: 1), itemCount: 3),
"Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.",
GridItem(color: UIColor(red: 163 / 255.0, green: 42 / 255.0, blue: 186 / 255.0, alpha: 1), itemCount: 7),
User(pk: 1, name: "Ryan Nystrom", handle: "_ryannystrom")
]
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()
case is GridItem:
return GridComposableSectionController()
case is User:
return UserComposableSectionController()
case is HorizontalCardsSection:
return HorizontalComposableSectionController()
case is SwipeActionSection:
return SwipeActionComposabelSectionController()
case is ActivityItem:
return ActivityComposableSectionController()
case is SelectionModel:
return SelectionComposableSectionController()
default:
return ListSectionController()
}
}
func emptyView(for listAdapter: ListAdapter) -> UIView? { return nil }
}