Skip to content

Commit

Permalink
Footer
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanPodymov committed Apr 9, 2024
1 parent ca0811c commit 537e3da
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
34 changes: 31 additions & 3 deletions Sources/CommonAppleKit/CAListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
//

import Foundation

open class CAListView<Cell: CAListViewCell<CellRootView>, CellRootView, CellDataType>: CACollectionView, CACollectionViewDataSource, CACollectionViewDelegate {
import CoreGraphics

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

#if canImport(UIKit)
Expand All @@ -35,15 +42,19 @@ open class CAListView<Cell: CAListViewCell<CellRootView>, CellRootView, CellData
itemSize: CGSize,
minimumInteritemSpacing: CGFloat = 0,
minimumLineSpacing: CGFloat = 0,
footerReferenceSize: CGSize = .zero,
cellId: String = .init(describing: Cell.self),
cellDelegate: CAListViewCellDelegate? = nil
cellDelegate: CAListViewCellDelegate? = nil,
footerId: String = .init(describing: Footer.self)
) {
self.cellId = cellId
self.footerId = footerId
self.cellDelegate = cellDelegate
let layout = CACollectionViewFlowLayout()
layout.itemSize = itemSize
layout.minimumInteritemSpacing = minimumInteritemSpacing
layout.minimumLineSpacing = minimumLineSpacing
layout.footerReferenceSize = footerReferenceSize
#if canImport(AppKit)
super.init(frame: frame)
collectionViewLayout = layout
Expand All @@ -52,6 +63,11 @@ open class CAListView<Cell: CAListViewCell<CellRootView>, CellRootView, CellData
#endif

register(Cell.self, forCellWithReuseIdentifier: cellId)
register(
Footer.self,
forSupplementaryViewOfKind: CACollectionView.elementKindSectionFooter,
withReuseIdentifier: footerId
)

delegate = self
#if canImport(UIKit)
Expand All @@ -67,6 +83,18 @@ open class CAListView<Cell: CAListViewCell<CellRootView>, CellRootView, CellData
content.count
}

public func collectionView(_ collectionView: CACollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> CACollectionReusableView {
if kind == CACollectionView.elementKindSectionFooter {
let view = collectionView.dequeueReusableSupplementaryView(
ofKind: CACollectionView.elementKindSectionFooter,
withReuseIdentifier: footerId,
for: indexPath
)
return view
}
return .init()
}

#if canImport(AppKit)
public func collectionView(_ collectionView: CACollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> CACollectionViewCell {
collectionView.makeItem(withIdentifier: .init(cellId), for: indexPath)
Expand Down
39 changes: 34 additions & 5 deletions Sources/CommonAppleKit/CAScrollableListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,42 @@
//

import Foundation
import CoreGraphics

#if canImport(AppKit)
open class CAScrollableListView<Cell: CAListViewCell<CellRootView>, CellRootView, CellDataType>: CAScrollView {
open class CAScrollableListView<
Cell: CAListViewCell<CellRootView>,
Footer: CACollectionReusableView,
CellRootView,
CellDataType
>: CAScrollView {
public var content: [CellDataType] = [] {
didSet {
(documentView as? CAListView<Cell, CellRootView, CellDataType>)?.content = content
(documentView as? CAListView<Cell, Footer, CellRootView, CellDataType>)?.content = content
}
}

public init(frame: CGRect, itemSize: CGSize, cellId: String = .init(describing: Cell.self), cellDelegate: CAListViewCellDelegate? = nil) {
public init(
frame: CGRect,
itemSize: CGSize,
minimumInteritemSpacing: CGFloat = 0,
minimumLineSpacing: CGFloat = 0,
footerReferenceSize: CGSize = .zero,
cellId: String = .init(describing: Cell.self),
cellDelegate: CAListViewCellDelegate? = nil,
footerId: String = .init(describing: Footer.self)
) {
super.init(frame: frame)
let listView = CAListView<Cell, CellRootView, CellDataType>(frame: frame, itemSize: itemSize, cellId: cellId, cellDelegate: cellDelegate)
let listView = CAListView<Cell, Footer, CellRootView, CellDataType>(
frame: frame,
itemSize: itemSize,
minimumInteritemSpacing: minimumInteritemSpacing,
minimumLineSpacing: minimumLineSpacing,
footerReferenceSize: footerReferenceSize,
cellId: cellId,
cellDelegate: cellDelegate,
footerId: footerId
)
documentView = listView
}

Expand All @@ -27,7 +51,12 @@ open class CAScrollableListView<Cell: CAListViewCell<CellRootView>, CellRootView
}
}
#elseif canImport(UIKit)
open class CAScrollableListView<Cell: CAListViewCell<CellRootView>, CellRootView, CellDataType>: CAListView<Cell, CellRootView, CellDataType> {
open class CAScrollableListView<
Cell: CAListViewCell<CellRootView>,
Footer: CACollectionReusableView,
CellRootView,
CellDataType
>: CAListView<Cell, Footer, CellRootView, CellDataType> {
open var documentView: CAView? {
self
}
Expand Down
22 changes: 22 additions & 0 deletions Sources/CommonAppleKit/CommonAppleKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
public typealias CAScrollView = UIScrollView
public typealias CACollectionView = UICollectionView
public typealias CACollectionViewCell = UICollectionViewCell
public typealias CACollectionReusableView = UICollectionReusableView
public typealias CALabel = UILabel
public typealias CACollectionViewDataSource = UICollectionViewDataSource
public typealias CACollectionViewDelegate = UICollectionViewDelegate
Expand Down Expand Up @@ -165,6 +166,7 @@
public typealias CAScrollView = NSScrollView
public typealias CACollectionView = NSCollectionView
public typealias CACollectionViewCell = NSCollectionViewItem
public typealias CACollectionReusableView = NSView
public typealias CALabel = NSTextField
public typealias CACollectionViewDataSource = NSCollectionViewDataSource
public typealias CACollectionViewDelegate = NSCollectionViewDelegate
Expand Down Expand Up @@ -273,6 +275,26 @@
) {
register(cellClass, forItemWithIdentifier: .init(identifier))
}

func register(
_ viewClass: AnyClass?,
forSupplementaryViewOfKind elementKind: String,
withReuseIdentifier identifier: String
) {
register(
viewClass,
forSupplementaryViewOfKind: elementKind,
withIdentifier: .init(identifier)
)
}

func dequeueReusableSupplementaryView(
ofKind elementKind: String,
withReuseIdentifier identifier: String,
for indexPath: IndexPath
) -> CACollectionReusableView {
makeSupplementaryView(ofKind: elementKind, withIdentifier: .init(identifier), for: indexPath)
}
}

public extension CAWindow {
Expand Down

0 comments on commit 537e3da

Please sign in to comment.