Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinChangCC committed Jun 26, 2024
1 parent 4b5d4d5 commit 2eff101
Show file tree
Hide file tree
Showing 4 changed files with 137 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)
)
}
}

0 comments on commit 2eff101

Please sign in to comment.