Skip to content

Commit

Permalink
Store & keep current Grid sections updated
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarkowitz committed Oct 27, 2024
1 parent cb5224c commit 5f5ff4e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Example/Sources/Grid/GridViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@ final class GridViewController: ExampleViewController, CellEventCoordinator {
}
}

private static let inset = CGFloat(4)
private let inset = CGFloat(4)

// MARK: Init

init() {
super.init(collectionViewLayout: Self.makeLayout())
super.init(collectionViewLayout: .init())
collectionView.setCollectionViewLayout(makeLayout(), animated: false)
}

// MARK: View lifecycle

override func viewDidLoad() {
super.viewDidLoad()

let viewModel = self.makeViewModel()
self.driver.update(viewModel: viewModel)
}
Expand Down Expand Up @@ -82,9 +84,11 @@ final class GridViewController: ExampleViewController, CellEventCoordinator {

// MARK: Private

private static func makeLayout() -> UICollectionViewCompositionalLayout {
return UICollectionViewCompositionalLayout { sectionIndex, _ in
let sectionType = Section.allCases[sectionIndex]
private func makeLayout() -> UICollectionViewCompositionalLayout {
return UICollectionViewCompositionalLayout { [weak self] sectionIndex, _ in
guard let self else { return nil }

let sectionType = model.gridSections[sectionIndex]

// Supplementary Item
let offset = 0.15
Expand Down Expand Up @@ -129,7 +133,7 @@ final class GridViewController: ExampleViewController, CellEventCoordinator {
}
}

private static func makeGridSection(with badge: NSCollectionLayoutSupplementaryItem) -> NSCollectionLayoutSection {
private func makeGridSection(with badge: NSCollectionLayoutSupplementaryItem) -> NSCollectionLayoutSection {
let fractionalWidth = CGFloat(0.5)

// Item
Expand All @@ -150,7 +154,7 @@ final class GridViewController: ExampleViewController, CellEventCoordinator {
}


private static func makeHorizontalScrollingSection(with badge: NSCollectionLayoutSupplementaryItem) -> NSCollectionLayoutSection {
private func makeHorizontalScrollingSection(with badge: NSCollectionLayoutSupplementaryItem) -> NSCollectionLayoutSection {
let size = NSCollectionLayoutSize(widthDimension: .absolute(150),
heightDimension: .absolute(150))

Expand Down
26 changes: 26 additions & 0 deletions Example/Sources/Main/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct Model {

private(set) var planets = PlanetModel.makePlanets()

private(set) var gridSections: [GridSection] = GridSection.allCases

mutating func shuffle() {
self.people.shuffle()
self.colors.shuffle()
Expand All @@ -37,6 +39,8 @@ struct Model {
if let index = self.planets.firstIndex(where: { $0.id == id }) {
self.planets.remove(at: index)
}

updateSections()
}

mutating func toggleFavorite(id: UniqueIdentifier) {
Expand All @@ -50,6 +54,20 @@ struct Model {
self.planets[index].isFavorite.toggle()
}
}

private mutating func updateSections() {
gridSections = []

if people.isEmpty == false {
gridSections.append(.people)
}
if colors.isEmpty == false {
gridSections.append(.colors)
}
if planets.isEmpty == false {
gridSections.append(.planets)
}
}
}

extension Model: CustomDebugStringConvertible {
Expand All @@ -69,3 +87,11 @@ extension Model: CustomDebugStringConvertible {
"""
}
}

extension Model {
enum GridSection: CaseIterable {
case people
case colors
case planets
}
}

0 comments on commit 5f5ff4e

Please sign in to comment.