Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompositionalLayout support for SingleSectionCollectionViewAdapter #124

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading