-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add horizontal row to UICollectionViewCompositionalLayout example
Summary: Lets go even beyond the `MixedDataViewController` example by taking advantage of `orthogonalScrollingBehavior`. Really neat! Differential Revision: D52260531 fbshipit-source-id: aee8b38a38284104fa65ba8d33426a9e6216e503
- Loading branch information
1 parent
6117cda
commit 309e1d7
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Examples/Examples-iOS/IGListKitExamples/Models/HorizontalCardsSection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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 Foundation | ||
|
||
final class HorizontalCardsSection: NSObject { | ||
|
||
let cardCount: Int | ||
private(set) var items: [String] = [] | ||
|
||
init(cardCount: Int) { | ||
self.cardCount = cardCount | ||
super.init() | ||
self.items = computeItems() | ||
} | ||
|
||
private func computeItems() -> [String] { | ||
return [Int](1...cardCount).map { | ||
String(describing: $0) | ||
} | ||
} | ||
} | ||
|
||
extension HorizontalCardsSection: ListDiffable { | ||
|
||
func diffIdentifier() -> NSObjectProtocol { | ||
return self | ||
} | ||
|
||
func isEqual(toDiffableObject object: ListDiffable?) -> Bool { | ||
return self === object ? true : self.isEqual(object) | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...les/SectionControllers/With Composable Layout/HorizontalComposableSectionController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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 | ||
|
||
final class HorizontalComposableSectionController: ListSectionController, CompositionLayoutCapable { | ||
|
||
private var object: HorizontalCardsSection? | ||
|
||
override func numberOfItems() -> Int { | ||
return object?.cardCount ?? 0 | ||
} | ||
|
||
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?.items[index] ?? "undefined" | ||
cell.contentView.backgroundColor = UIColor.secondarySystemBackground | ||
cell.contentView.layer.cornerRadius = 8 | ||
return cell | ||
} | ||
|
||
override func didUpdate(to object: Any) { | ||
self.object = object as? HorizontalCardsSection | ||
} | ||
|
||
// MARK: CompositionLayoutCapable | ||
|
||
func collectionViewSectionLayout(layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? { | ||
// Item | ||
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), | ||
heightDimension: .fractionalHeight(1.0)) | ||
let item = NSCollectionLayoutItem(layoutSize: itemSize) | ||
item.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 16) | ||
|
||
// Group | ||
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(160)) | ||
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) | ||
|
||
// Section | ||
let section = NSCollectionLayoutSection(group: group) | ||
section.contentInsets = NSDirectionalEdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16) | ||
section.orthogonalScrollingBehavior = .continuousGroupLeadingBoundary | ||
return section | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters