Skip to content

Commit

Permalink
update flow layout section controller
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinChangCC committed May 22, 2024
1 parent 6ab82ff commit 6c8df36
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 68 deletions.
16 changes: 3 additions & 13 deletions SectionKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@
5AA9D95126AAA5E000679D88 /* MockCollectionViewCell.swift */,
5A8D3E5327E8D0E200073712 /* MockCollectionViewContext.swift */,
5AA9D95526AAA61700679D88 /* MockErrorHandler.swift */,
D91425802BBBF8670005E8CD /* MockFlowLayoutSectionController.swift */,
5A0B776F26B19EB300B054D4 /* MockListCollectionViewAdapterDataSource.swift */,
5A0B775326AFF4D500B054D4 /* MockScrollViewDelegate.swift */,
5AB6644A26A86F7A004DC230 /* MockSectionController.swift */,
Expand Down Expand Up @@ -544,8 +545,9 @@
5AE175A92667DA3E00D4DCE1 /* SectionController */ = {
isa = PBXGroup;
children = (
D9CCE9B62BADF2FD00EED204 /* Generic */,
D9CCE9B22BADED3D00EED204 /* FlowLayout */,
5AE175AD2667DA3E00D4DCE1 /* Update */,
5AE175B42667DA3E00D4DCE1 /* Generic */,
5AE175B22667DA3E00D4DCE1 /* SectionIndexPath.swift */,
5AE175BA2667DA3F00D4DCE1 /* SectionController.swift */,
5AE175AB2667DA3E00D4DCE1 /* SectionDataSource.swift */,
Expand All @@ -568,18 +570,6 @@
path = Update;
sourceTree = "<group>";
};
5AE175B42667DA3E00D4DCE1 /* Generic */ = {
isa = PBXGroup;
children = (
5AE175B52667DA3E00D4DCE1 /* BaseSectionController.swift */,
5AE175B62667DA3E00D4DCE1 /* ListSectionController.swift */,
5AE175B72667DA3F00D4DCE1 /* SingleItemSectionController.swift */,
5AE175B82667DA3F00D4DCE1 /* SingleModelSectionController.swift */,
5AE175B92667DA3F00D4DCE1 /* FoundationDiffingListSectionController.swift */,
);
path = Generic;
sourceTree = "<group>";
};
5AE175BC2667DA3F00D4DCE1 /* CollectionViewAdapter */ = {
isa = PBXGroup;
children = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import UIKit

/// This is a foundational implementation of `FlowLayoutSectionController`, implementing the flow layout delegate while inheriting from the `BaseSectionController`.
@MainActor
open class BaseFlowLayoutSectionController: BaseSectionController,
SectionFlowDelegate,
FlowLayoutSectionController {
open var flowDelegate: SectionFlowDelegate? { self }

// MARK: - SectionFlowDelegate

open func sizeForItem(
at indexPath: SectionIndexPath,
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGSize {
(layout as? UICollectionViewFlowLayout)?.itemSize ?? CGSize(width: 50, height: 50)
}

open func inset(using layout: UICollectionViewLayout, in context: CollectionViewContext) -> UIEdgeInsets {
(layout as? UICollectionViewFlowLayout)?.sectionInset ?? .zero
}

open func minimumLineSpacing(using layout: UICollectionViewLayout, in context: CollectionViewContext) -> CGFloat {
(layout as? UICollectionViewFlowLayout)?.minimumLineSpacing ?? 10
}

open func minimumInteritemSpacing(
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGFloat {
(layout as? UICollectionViewFlowLayout)?.minimumInteritemSpacing ?? 10
}

open func referenceSizeForHeader(
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGSize {
(layout as? UICollectionViewFlowLayout)?.headerReferenceSize ?? .zero
}

open func referenceSizeForFooter(
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGSize {
(layout as? UICollectionViewFlowLayout)?.footerReferenceSize ?? .zero
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import UIKit
of a model to be displayed and the list of items (almost) never changes or should not perform animated updates.
*/
@MainActor
open class ListSectionController<Model, Item>: BaseSectionController {
open class ListSectionController<Model, Item>: BaseFlowLayoutSectionController {
/**
Initialise an instance of `ListSectionController`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit
- Warning: If `numberOfItems` is overridden, `calculateUpdate(from:to:)` needs to be overridden as well.
*/
@MainActor
open class SingleItemSectionController<Model, Item>: BaseSectionController {
open class SingleItemSectionController<Model, Item>: BaseFlowLayoutSectionController {
private let areItemsEqual: @MainActor (Item, Item) -> Bool

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit
it is recommended to use `ListSectionController` instead.
*/
@MainActor
open class SingleModelSectionController<Model>: BaseSectionController {
open class SingleModelSectionController<Model>: BaseFlowLayoutSectionController {
/**
Initialise an instance of `SingleModelSectionController`.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import UIKit

/**
A base implementation of all `SectionController` datasource and delegate protocols.

Every declaration is marked `open` and can be overridden.
*/
/// This serves as a fundamental `SectionController` implementation,
/// encompassing data source and delegate protocols, with the exception of the flow layout delegate.
/// Each access control is designated as` open`, allowing for easy customization through overrides.
@MainActor
open class BaseSectionController: SectionController,
SectionDataSource,
SectionDataSourcePrefetchingDelegate,
SectionDelegate,
SectionFlowDelegate,
SectionDragDelegate,
SectionDropDelegate {
// MARK: - Init
Expand All @@ -28,8 +25,6 @@ open class BaseSectionController: SectionController,

open var delegate: SectionDelegate? { self }

open var flowDelegate: SectionFlowDelegate? { self }

@available(iOS 11.0, *)
open var dragDelegate: SectionDragDelegate? { self }

Expand Down Expand Up @@ -263,43 +258,4 @@ open class BaseSectionController: SectionController,
) -> UIDragPreviewParameters? {
nil
}

// MARK: - SectionFlowDelegate

open func sizeForItem(
at indexPath: SectionIndexPath,
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGSize {
(layout as? UICollectionViewFlowLayout)?.itemSize ?? CGSize(width: 50, height: 50)
}

open func inset(using layout: UICollectionViewLayout, in context: CollectionViewContext) -> UIEdgeInsets {
(layout as? UICollectionViewFlowLayout)?.sectionInset ?? .zero
}

open func minimumLineSpacing(using layout: UICollectionViewLayout, in context: CollectionViewContext) -> CGFloat {
(layout as? UICollectionViewFlowLayout)?.minimumLineSpacing ?? 10
}

open func minimumInteritemSpacing(
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGFloat {
(layout as? UICollectionViewFlowLayout)?.minimumInteritemSpacing ?? 10
}

open func referenceSizeForHeader(
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGSize {
(layout as? UICollectionViewFlowLayout)?.headerReferenceSize ?? .zero
}

open func referenceSizeForFooter(
using layout: UICollectionViewLayout,
in context: CollectionViewContext
) -> CGSize {
(layout as? UICollectionViewFlowLayout)?.footerReferenceSize ?? .zero
}
}
11 changes: 6 additions & 5 deletions SectionKit/Sources/SectionController/SectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public protocol SectionController: AnyObject {
/// The delegate of this section.
var delegate: SectionDelegate? { get }

/// The delegate for the `UICollectionViewFlowLayout` of this section.
var flowDelegate: SectionFlowDelegate? { get }

/// The drag delegate of this section.
@available(iOS 11.0, *)
var dragDelegate: SectionDragDelegate? { get }
Expand All @@ -37,11 +34,15 @@ extension SectionController {

public var delegate: SectionDelegate? { nil }

public var flowDelegate: SectionFlowDelegate? { nil }

@available(iOS 11.0, *)
public var dragDelegate: SectionDragDelegate? { nil }

@available(iOS 11.0, *)
public var dropDelegate: SectionDropDelegate? { nil }
}

@MainActor
public protocol FlowLayoutSectionController: SectionController {
/// The delegate for the `UICollectionViewFlowLayout` of this section.
var flowDelegate: SectionFlowDelegate? { get }
}

0 comments on commit 6c8df36

Please sign in to comment.