|
| 1 | +// |
| 2 | +// GenericCollectionLayoutProtocol.swift |
| 3 | +// GenericCollectionViewKit |
| 4 | +// |
| 5 | +// Created by Engin Gülek on 11.11.2025. |
| 6 | +// |
| 7 | + |
| 8 | +import UIKit |
| 9 | +import Foundation |
| 10 | + |
| 11 | +// MARK: - GenericCollectionLayoutProviderProtocol |
| 12 | +/// A protocol that defines how layout configurations are provided for each section |
| 13 | +/// of a collection view. Each section can have its own distinct layout style by returning |
| 14 | +/// a `LayoutSource` instance. |
| 15 | +public protocol GenericCollectionLayoutProviderProtocol { |
| 16 | + |
| 17 | + ///Returns a layout configuration (`LayoutSource`) for the specified section index. |
| 18 | + /// - Parameter sectionIndex: The index of the section whose layout is being requested. |
| 19 | + /// - Returns: A `LayoutSource` instance describing the layout configuration for that section. |
| 20 | + func layout(for sectionIndex: Int) -> LayoutSource |
| 21 | +} |
| 22 | + |
| 23 | +// MARK: - GenericCollectionLayoutProvider |
| 24 | +/// A generic class that builds a `UICollectionViewCompositionalLayout` |
| 25 | +/// using the configurations provided by a `GenericCollectionLayoutProviderProtocol` conforming source. |
| 26 | +public class GenericCollectionLayoutProvider<Source: GenericCollectionLayoutProviderProtocol> { |
| 27 | + |
| 28 | + ///The source responsible for providing layout configurations per section. |
| 29 | + private let source: Source |
| 30 | + |
| 31 | + /// Initializes a new instance of `GenericCollectionLayoutProvider` with a layout source. |
| 32 | + /// - Parameter source: An object conforming to `GenericCollectionLayoutProtocol`. |
| 33 | + public init(source: Source) { |
| 34 | + self.source = source |
| 35 | + } |
| 36 | + |
| 37 | + // MARK: - Layout Creation |
| 38 | + /// Creates and returns a `UICollectionViewCompositionalLayout` |
| 39 | + /// based on the layout information provided by the source. |
| 40 | + /// This method dynamically constructs section layouts using each section’s `LayoutSource`. |
| 41 | + /// It handles item sizing, grouping, spacing, section insets, scroll direction, and headers. |
| 42 | + /// - Returns: A fully configured `UICollectionViewCompositionalLayout`. |
| 43 | + @MainActor |
| 44 | + public func createLayout() -> UICollectionViewCompositionalLayout { |
| 45 | + UICollectionViewCompositionalLayout { [weak self] sectionIndex, _ in |
| 46 | + guard let self = self else { return nil } |
| 47 | + |
| 48 | + // Retrieve layout configuration for the current section |
| 49 | + let layoutSource = self.source.layout(for: sectionIndex) |
| 50 | + |
| 51 | + // MARK: Item Configuration |
| 52 | + /// Define how individual items are sized (fractional or absolute). |
| 53 | + |
| 54 | + let itemSize = NSCollectionLayoutSize( |
| 55 | + widthDimension: layoutSource.itemSize.width.type == .fractional |
| 56 | + ? .fractionalWidth(layoutSource.itemSize.width.value) |
| 57 | + : .absolute(layoutSource.itemSize.width.value), |
| 58 | + heightDimension: layoutSource.itemSize.height.type == .fractional |
| 59 | + ? .fractionalHeight(layoutSource.itemSize.height.value) |
| 60 | + : .absolute(layoutSource.itemSize.height.value)) |
| 61 | + |
| 62 | + let item = NSCollectionLayoutItem(layoutSize: itemSize) |
| 63 | + |
| 64 | + // MARK: Group Configuration |
| 65 | + /// Define the layout and size of item groups (either horizontal or vertical). |
| 66 | + let groupSize = NSCollectionLayoutSize( |
| 67 | + widthDimension: layoutSource.groupSize.width.type == .absolute |
| 68 | + ? .absolute(layoutSource.groupSize.width.value) |
| 69 | + : .fractionalWidth(layoutSource.groupSize.width.value), |
| 70 | + heightDimension: layoutSource.groupSize.height.type == .absolute |
| 71 | + ? .absolute(layoutSource.groupSize.height.value) |
| 72 | + : .fractionalHeight(layoutSource.groupSize.height.value)) |
| 73 | + |
| 74 | + let group: NSCollectionLayoutGroup |
| 75 | + if layoutSource.groupOrientation == .horizontal { |
| 76 | + group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) |
| 77 | + } else { |
| 78 | + group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [item]) |
| 79 | + } |
| 80 | + |
| 81 | + group.interItemSpacing = .fixed(layoutSource.interItemSpacing) |
| 82 | + |
| 83 | + // MARK: Section Configuration |
| 84 | + /// Defines how groups are arranged within each section, including |
| 85 | + /// scrolling behavior, content insets, spacing, and supplementary views. |
| 86 | + let section = NSCollectionLayoutSection(group: group) |
| 87 | + section.orthogonalScrollingBehavior = layoutSource.scrollDirection == .horizontal ? .continuous : .none |
| 88 | + section.contentInsets = NSDirectionalEdgeInsets( |
| 89 | + top: layoutSource.sectionInsets.top, |
| 90 | + leading: layoutSource.sectionInsets.leading, |
| 91 | + bottom: layoutSource.sectionInsets.bottom, |
| 92 | + trailing: layoutSource.sectionInsets.trailing |
| 93 | + ) |
| 94 | + section.interGroupSpacing = layoutSource.interGroupSpacing |
| 95 | + |
| 96 | + // MARK: Header Configuration |
| 97 | + /// Adds a header supplementary view at the top of each section. |
| 98 | + let headerSize = NSCollectionLayoutSize( |
| 99 | + widthDimension: .fractionalWidth(1.0), |
| 100 | + heightDimension: .absolute(40) |
| 101 | + ) |
| 102 | + let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem( |
| 103 | + layoutSize: headerSize, |
| 104 | + elementKind: UICollectionView.elementKindSectionHeader, |
| 105 | + alignment: .top |
| 106 | + ) |
| 107 | + section.boundarySupplementaryItems = [sectionHeader] |
| 108 | + |
| 109 | + return section |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments