Skip to content

Commit

Permalink
CompositionalLayout support for SingleSectionCollectionViewAdapter (#124
Browse files Browse the repository at this point in the history
)

Add section kit compositional layout support to the single section
collection view adapter
  • Loading branch information
CalvinChangCC authored Jun 26, 2024
2 parents 1b380a5 + 2eff101 commit 8eb8410
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8eb8410

Please sign in to comment.