| 1 | // |
| 2 | // CategoriesGridSection.swift |
| 3 | // JomloCollectionView Example |
| 4 | // |
| 5 | // Created by Seto Elkahfi on 2026-01-27. |
| 6 | // Copyright © 2026 Seto Elkahfi. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | import JomloCollectionView |
| 10 | import UIKit |
| 11 | |
| 12 | @MainActor |
| 13 | final class CategoriesGridSection: JomloSection { |
| 14 | |
| 15 | let identifier = JomloSectionID("categories") |
| 16 | var onReloadNeeded: (@MainActor @Sendable () -> Void)? |
| 17 | |
| 18 | private let categoryItems: [CategoryItem] |
| 19 | |
| 20 | init(items: [CategoryItem]) { |
| 21 | self.categoryItems = items |
| 22 | } |
| 23 | |
| 24 | func items() -> [any JomloItem] { |
| 25 | categoryItems |
| 26 | } |
| 27 | |
| 28 | func layoutSection( |
| 29 | environment: NSCollectionLayoutEnvironment, |
| 30 | sectionIndex: Int |
| 31 | ) -> NSCollectionLayoutSection { |
| 32 | let columns = environment.container.contentSize.width > 500 ? 4 : 2 |
| 33 | return JomloLayoutFactory.grid( |
| 34 | columns: columns, |
| 35 | itemHeight: .absolute(100), |
| 36 | interItemSpacing: 12, |
| 37 | headerHeight: .estimated(44) |
| 38 | ) |
| 39 | } |
| 40 | |
| 41 | func cell( |
| 42 | for item: any JomloItem, |
| 43 | in collectionView: UICollectionView, |
| 44 | at indexPath: IndexPath |
| 45 | ) -> UICollectionViewCell { |
| 46 | guard let categoryItem = item as? CategoryItem else { |
| 47 | return UICollectionViewCell() |
| 48 | } |
| 49 | return collectionView.dequeueJomloCell( |
| 50 | CategoryCell.self, for: indexPath, item: categoryItem) |
| 51 | } |
| 52 | |
| 53 | func supplementaryView( |
| 54 | ofKind kind: String, |
| 55 | in collectionView: UICollectionView, |
| 56 | at indexPath: IndexPath |
| 57 | ) -> UICollectionReusableView? { |
| 58 | guard kind == UICollectionView.elementKindSectionHeader else { return nil } |
| 59 | guard |
| 60 | let header = collectionView.dequeueReusableSupplementaryView( |
| 61 | ofKind: kind, |
| 62 | withReuseIdentifier: JomloSectionHeaderView.reuseIdentifier, |
| 63 | for: indexPath |
| 64 | ) as? JomloSectionHeaderView |
| 65 | else { |
| 66 | return nil |
| 67 | } |
| 68 | header.configure(title: "Categories") |
| 69 | return header |
| 70 | } |
| 71 | } |