IGListKit/Examples/Examples-iOS/IGListKitExamples/SectionControllers/PostSectionController.swift
Abhyas Mall 01bd505d8b 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-06-28 10:28:37 -07:00

67 lines
2.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
// MARK: - ListSectionController
// In IGListKit, section controllers manage a single type of data object
// and are responsible for:
// - Creating and configuring cells
// - Handling cell selection
// - Determining cell sizes
// - Managing section-specific actions
final class PostSectionController: ListSectionController {
var post: Post?
weak var delegate: PostSectionControllerDelegate?
override init() {
super.init()
// Setting insets for the entire section
self.inset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
}
// Determines the size of cells in this section
// IGListKit calls this method to calculate cell dimensions
override func sizeForItem(at index: Int) -> CGSize {
// collectionContext is a bridge that connects the section controller
// to the actual UICollectionView it exists within
let width = collectionContext?.containerSize.width ?? 0
return CGSize(width: width, height: width + 200)
}
// Creates and configures a cell for this section
// Similar to cellForRowAt in UICollectionViewDataSource
override func cellForItem(at index: Int) -> UICollectionViewCell {
guard let cell = collectionContext?.dequeueReusableCell(of: PostCell.self, for: self, at: index) as? PostCell,
let post = post else {
fatalError("Failed to dequeue PostCell")
}
cell.configure(with: post)
cell.optionsButtonTapped = { [weak self] (button: UIButton) in
guard let self = self, let post = self.post else { return }
self.delegate?.postSectionController(self, didSelectOptionsFor: post, from: button)
}
return cell
}
// Handles selection of cells in this section
override func didSelectItem(at index: Int) {
guard let post = post else { return }
print("Post ID:\(post.id) was tapped.")
}
// Called when the data object for this section controller is updated
// This is where you store a reference to your model object
override func didUpdate(to object: Any) {
post = object as? Post
}
}