-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CompositionalLayout support for SingleSectionCollectionViewAdapter (#124
- Loading branch information
Showing
5 changed files
with
155 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
53 changes: 53 additions & 0 deletions
53
Example/VanillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/Activities.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,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 | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...laSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesSectionController.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,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 | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...nillaSwiftExamples/Sources/VanillaSwiftExamples/Activities/ActivitiesViewController.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,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) | ||
) | ||
} | ||
} |
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