diff --git a/Example/Examples/Sources/Examples/ExampleList/ExampleListCoordinator+Presentable.swift b/Example/Examples/Sources/Examples/ExampleList/ExampleListCoordinator+Presentable.swift index 136a4178..01c05f70 100644 --- a/Example/Examples/Sources/Examples/ExampleList/ExampleListCoordinator+Presentable.swift +++ b/Example/Examples/Sources/Examples/ExampleList/ExampleListCoordinator+Presentable.swift @@ -37,6 +37,14 @@ extension ExampleListCoordinator: Presentable { presentable: VanillaSwiftExamples.EmojisCoordinator(navigationController: navigationController) ) ), + ExampleViewModel( + name: "Activities", + description: "An MVC example with compositional layout example using a SingleSectionCollectionViewAdapter that shows a single ListSectionController.", + navigation: ExampleCoordinator( + navigationController: navigationController, + presentable: ActivitiesViewController() + ) + ), ExampleViewModel( name: "Names", description: "An MVVM-C architecture with compositional layout example by using a ListCompositionalLayoutCollectionViewAdapter that shows multiple ListCompositionalLayoutSectionController.", diff --git a/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/Activities.swift b/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/Activities.swift new file mode 100644 index 00000000..c9a49d45 --- /dev/null +++ b/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/Activities.swift @@ -0,0 +1,53 @@ +enum Activity: String, CaseIterable { + case workingOut + + case practicingForASport + + case playingASport + + case swimmingLaps + + case yogaClass + + case running + + case pilates + + case homeRenovations + + case meditating + + case studyingForAnExam + + case completingAnAssignment + + case readingATextbook + + case writingAnArticle + + case editingAVideo + + case learningANewSkill + + case journalling + + case joiningASocialGroup + + case goingToARally + + case attendingAnImprovClass + + case goingToChurch + + case spendingTimeWithFriends + + var text: String { + rawValue + .replacingOccurrences( + of: "[A-Z]", + with: " $0", + options: .regularExpression + ) + .capitalized + } +} diff --git a/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesSectionController.swift b/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesSectionController.swift new file mode 100644 index 00000000..3f0c32b8 --- /dev/null +++ b/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesSectionController.swift @@ -0,0 +1,43 @@ +import SectionKit +import UIKit + +final class ActivitiesSectionController: ListSectionController<[String], String> { + override var layoutProvider: SectionLayoutProvider { + .compositionalLayout( + .init( + layoutSectionProvider: { _ in + let layoutSize = NSCollectionLayoutSize( + widthDimension: .fractionalWidth(1), + heightDimension: .estimated(50) + ) + + let item = NSCollectionLayoutItem(layoutSize: layoutSize) + let layout = NSCollectionLayoutSection( + group: .vertical( + layoutSize: layoutSize, + subitem: item, + count: 1 + ) + ) + return layout + } + ) + ) + } + + override func items(for model: [String]) -> [String] { + model + } + + override func cellForItem( + at indexPath: SectionIndexPath, + in context: CollectionViewContext + ) -> UICollectionViewCell { + let item = items[indexPath] + let cell: UICollectionViewListCell = context.dequeueReusableCell(for: indexPath) + var contentConfiguration = cell.defaultContentConfiguration() + contentConfiguration.text = item + cell.contentConfiguration = contentConfiguration + return cell + } +} diff --git a/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesViewController.swift b/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesViewController.swift new file mode 100644 index 00000000..67c2ecce --- /dev/null +++ b/Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesViewController.swift @@ -0,0 +1,33 @@ +import UIKit +import SectionKit + +public final class ActivitiesViewController: UIViewController { + private let collectionView: UICollectionView = { + let layout = SectionKitCompositionalLayout() + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) + collectionView.backgroundColor = .systemBackground + collectionView.alwaysBounceVertical = true + return collectionView + }() + + private lazy var collectionViewAdapter = SingleSectionCollectionViewAdapter( + collectionView: collectionView, + viewController: self + ) + + override public func loadView() { + view = collectionView + } + + override public func viewDidLoad() { + super.viewDidLoad() + title = "Activities" + + let activities = Activity.allCases.map(\.text) + collectionViewAdapter.section = Section( + id: UUID(), + model: activities, + controller: ActivitiesSectionController(model: activities) + ) + } +} diff --git a/SectionKit/Sources/CollectionViewAdapter/SingleSectionCollectionViewAdapter/SingleSectionCollectionViewAdapter.swift b/SectionKit/Sources/CollectionViewAdapter/SingleSectionCollectionViewAdapter/SingleSectionCollectionViewAdapter.swift index 2eb016fc..dfa71977 100644 --- a/SectionKit/Sources/CollectionViewAdapter/SingleSectionCollectionViewAdapter/SingleSectionCollectionViewAdapter.swift +++ b/SectionKit/Sources/CollectionViewAdapter/SingleSectionCollectionViewAdapter/SingleSectionCollectionViewAdapter.swift @@ -45,6 +45,15 @@ open class SingleSectionCollectionViewAdapter: NSObject, CollectionViewAdapter { collectionView.dragDelegate = self collectionView.dropDelegate = self } + if #available(iOS 13.0, *), + let layout = collectionView.collectionViewLayout as? SectionKitCompositionalLayout { + layout.sections = { [weak self] in + guard let section = self?.collectionViewSection else { + return [] + } + return [section] + } + } } /** @@ -86,6 +95,15 @@ open class SingleSectionCollectionViewAdapter: NSObject, CollectionViewAdapter { collectionView.dragDelegate = self collectionView.dropDelegate = self } + if #available(iOS 13.0, *), + let layout = collectionView.collectionViewLayout as? SectionKitCompositionalLayout { + layout.sections = { [weak self] in + guard let section = self?.collectionViewSection else { + return [] + } + return [section] + } + } } public let context: CollectionViewContext