Skip to content

Commit

Permalink
Header
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Apr 12, 2024
1 parent 0fe1367 commit 736b746
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
35 changes: 30 additions & 5 deletions Sources/CommonAppleKit/CAListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import CoreGraphics

open class CAListView<
Cell: CAListViewCell<CellRootView>,
Header: CACollectionReusableView,
Footer: CACollectionReusableView,
CellRootView,
CellDataType
>: CACollectionView, CACollectionViewDataSource, CACollectionViewDelegate {
private let cellId: String
private let headerId: String
private let footerId: String
private weak var cellDelegate: CAListViewCellDelegate?

Expand All @@ -42,18 +44,22 @@ open class CAListView<
itemSize: CGSize,
minimumInteritemSpacing: CGFloat = 0,
minimumLineSpacing: CGFloat = 0,
headerReferenceSize: CGSize = .zero,
footerReferenceSize: CGSize = .zero,
cellId: String = .init(describing: Cell.self),
cellDelegate: CAListViewCellDelegate? = nil,
footerId: String = .init(describing: Footer.self)
headerId: String = .init(describing: Header.self),
footerId: String = .init(describing: Footer.self),
cellDelegate: CAListViewCellDelegate? = nil
) {
self.cellId = cellId
self.headerId = headerId
self.footerId = footerId
self.cellDelegate = cellDelegate
let layout = CACollectionViewFlowLayout()
layout.itemSize = itemSize
layout.minimumInteritemSpacing = minimumInteritemSpacing
layout.minimumLineSpacing = minimumLineSpacing
layout.headerReferenceSize = headerReferenceSize
layout.footerReferenceSize = footerReferenceSize
#if canImport(AppKit)
super.init(frame: frame)
Expand All @@ -62,7 +68,15 @@ open class CAListView<
super.init(frame: frame, collectionViewLayout: layout)
#endif

register(Cell.self, forCellWithReuseIdentifier: cellId)
register(
Cell.self,
forCellWithReuseIdentifier: cellId
)
register(
Header.self,
forSupplementaryViewOfKind: CACollectionView.elementKindSectionHeader,
withReuseIdentifier: headerId
)
register(
Footer.self,
forSupplementaryViewOfKind: CACollectionView.elementKindSectionFooter,
Expand All @@ -84,7 +98,14 @@ open class CAListView<
}

public func collectionView(_ collectionView: CACollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> CACollectionReusableView {
if kind == CACollectionView.elementKindSectionFooter {
if kind == CACollectionView.elementKindSectionHeader {
let view = collectionView.dequeueReusableSupplementaryView(
ofKind: CACollectionView.elementKindSectionHeader,
withReuseIdentifier: headerId,
for: indexPath
)
return view
} else if kind == CACollectionView.elementKindSectionFooter {
let view = collectionView.dequeueReusableSupplementaryView(
ofKind: CACollectionView.elementKindSectionFooter,
withReuseIdentifier: footerId,
Expand All @@ -96,7 +117,11 @@ open class CAListView<
}

public func collectionView(_ collectionView: CACollectionView, willDisplaySupplementaryView view: CACollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {
cellDelegate?.onPageEndReached()
if elementKind == CACollectionView.elementKindSectionHeader {
cellDelegate?.onHeaderWillBeDisplayed()
} else if elementKind == CACollectionView.elementKindSectionFooter {
cellDelegate?.onFooterWillBeDisplayed()
}
}

#if canImport(AppKit)
Expand Down
3 changes: 2 additions & 1 deletion Sources/CommonAppleKit/CAListViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Foundation
public protocol CAListViewCellDelegate: AnyObject {
func onCellTap(data: Any?)
func onAction(data: Any?)
func onPageEndReached()
func onHeaderWillBeDisplayed()
func onFooterWillBeDisplayed()
}

open class CAListViewCell<RootView: CAView>: CACollectionViewCell {
Expand Down
3 changes: 2 additions & 1 deletion Sources/CommonAppleKit/CAScrollableListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ open class CAScrollableListView<
#elseif canImport(UIKit)
open class CAScrollableListView<
Cell: CAListViewCell<CellRootView>,
Header: CACollectionReusableView,
Footer: CACollectionReusableView,
CellRootView,
CellDataType
>: CAListView<Cell, Footer, CellRootView, CellDataType> {
>: CAListView<Cell, Header, Footer, CellRootView, CellDataType> {
open var documentView: CAView? {
self
}
Expand Down
39 changes: 36 additions & 3 deletions Tests/CommonAppleKitTests/CommonAppleKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ private final class CellARootView: CAView {
fileprivate weak var cell: CellA?
}

private final class HeaderA: CACollectionReusableView { }

private final class FooterA: CACollectionReusableView { }

private final class ViewA: CAView {
Expand Down Expand Up @@ -130,12 +132,39 @@ final class CommonAppleKitTests: XCTestCase {
}

func testListView() {
let listView = CAListView<CellA, FooterA, CellARootView, String>(frame: .init(x: 0, y: 0, width: 300, height: 500), itemSize: .init(width: 200, height: 100), cellId: "id", cellDelegate: self)
let listView = CAListView<CellA, HeaderA, FooterA, CellARootView, String>(
frame: .init(
x: 0,
y: 0,
width: 300,
height: 500
),
itemSize: .init(
width: 200,
height: 100
),
cellId: "id",
cellDelegate: self
)
listView.content = ["one", "two"]
}

func testScrollableListView() {
let listView = CAScrollableListView<CellA, FooterA, CellARootView, Int>(frame: .zero, itemSize: .init(width: 200, height: 100))
let listView = CAScrollableListView<CellA, HeaderA, FooterA, CellARootView, Int>(
frame: .zero,
itemSize: .init(
width: 200,
height: 100
),
headerReferenceSize: .init(
width: 300,
height: 400
),
footerReferenceSize: .init(
width: 300,
height: 400
)
)
listView.content = [1, 2]
}
}
Expand All @@ -149,7 +178,11 @@ extension CommonAppleKitTests: CAListViewCellDelegate {

}

func onPageEndReached() {
func onHeaderWillBeDisplayed() {

}

func onFooterWillBeDisplayed() {

}
}
Expand Down

0 comments on commit 736b746

Please sign in to comment.