Skip to content

Commit

Permalink
feat: add HighlightEvent and UnhighlightEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxtynSong committed Dec 11, 2024
1 parent 7b2c060 commit 2ed3bf2
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/KarrotListKit/Adapter/CollectionViewAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,40 @@ extension CollectionViewAdapter: UICollectionViewDelegate {
return
}
}

public func collectionView(
_ collectionView: UICollectionView,
didHighlightItemAt indexPath: IndexPath
) {
guard let item = item(at: indexPath) else {
return
}

item.event(for: HighlightEvent.self)?.handler(
.init(
indexPath: indexPath,
anyComponent: item.component,
content: (collectionView.cellForItem(at: indexPath) as? ComponentRenderable)?.renderedContent
)
)
}

public func collectionView(
_ collectionView: UICollectionView,
didUnhighlightItemAt indexPath: IndexPath
) {
guard let item = item(at: indexPath) else {
return
}

item.event(for: UnhighlightEvent.self)?.handler(
.init(
indexPath: indexPath,
anyComponent: item.component,
content: (collectionView.cellForItem(at: indexPath) as? ComponentRenderable)?.renderedContent
)
)
}
}

// MARK: - UIScrollViewDelegate
Expand Down
16 changes: 16 additions & 0 deletions Sources/KarrotListKit/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ extension Cell {
public func didEndDisplay(_ handler: @escaping (DidEndDisplayingEvent.EventContext) -> Void) -> Self {
registerEvent(DidEndDisplayingEvent(handler: handler))
}

/// Register a callback handler that will be called when the cell was highlighted.
///
/// - Parameters:
/// - handler: The callback handler for highlight event
public func onHighlight(_ handler: @escaping (HighlightEvent.EventContext) -> Void) -> Self {
registerEvent(HighlightEvent(handler: handler))
}

/// Register a callback handler that will be called when the cell was unhighlight.
///
/// - Parameters:
/// - handler: The callback handler for unhighlight event
public func onUnhighlight(_ handler: @escaping (UnhighlightEvent.EventContext) -> Void) -> Self {
registerEvent(UnhighlightEvent(handler: handler))
}
}

// MARK: - Hashable
Expand Down
24 changes: 24 additions & 0 deletions Sources/KarrotListKit/Event/Cell/HighlightEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Copyright (c) 2024 Danggeun Market Inc.
//

import UIKit

/// This structure encapsulates the highlight event information and contains a closure object for handling the highlight event.
public struct HighlightEvent: ListingViewEvent {

public struct EventContext {

/// The index path of the view that was highlighted.
public let indexPath: IndexPath

/// The component owned by the view that was highlighted.
public let anyComponent: AnyComponent

/// The content owned by the view that was highlighted.
public let content: UIView?
}

/// A closure that's called when the cell was highlighted
let handler: (EventContext) -> Void
}
25 changes: 25 additions & 0 deletions Sources/KarrotListKit/Event/Cell/UnhighlightEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright (c) 2024 Danggeun Market Inc.
//

import UIKit

/// This structure encapsulates the unhighlight event information and contains a closure object for handling the unhighlight event.
public struct UnhighlightEvent: ListingViewEvent {

public struct EventContext {

/// The index path of the view that was unhighlight.
public let indexPath: IndexPath

/// The component owned by the view that was unhighlight.
public let anyComponent: AnyComponent

/// The content owned by the view that was unhighlight.
public let content: UIView?
}

/// A closure that's called when the cell was unhighlight
let handler: (EventContext) -> Void
}

54 changes: 54 additions & 0 deletions Tests/KarrotListKitTests/CollectionViewAdapterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,60 @@ extension CollectionViewAdapterTests {
// then
XCTAssertEqual(eventContext.indexPath, IndexPath(item: 0, section: 0))
}

func test_given_highlightHandler_when_highlightCell_then_handleEvent() {
// given
var eventContext: HighlightEvent.EventContext!
let collectionView = UICollectionView(layoutAdapter: CollectionViewLayoutAdapter())
let component = DummyComponent()
let sut = sut(collectionView: collectionView)
sut.list = List {
Section(id: UUID()) {
Cell(id: UUID(), component: component)
.onHighlight { context in
eventContext = context
}
}
}

// when
collectionView
.delegate?
.collectionView?(
collectionView,
didHighlightItemAt: IndexPath(item: 0, section: 0)
)

// then
XCTAssertEqual(eventContext.indexPath, IndexPath(item: 0, section: 0))
}

func test_given_unhighlightHandler_when_unhighlightCell_then_handleEvent() {
// given
var eventContext: UnhighlightEvent.EventContext!
let collectionView = UICollectionView(layoutAdapter: CollectionViewLayoutAdapter())
let component = DummyComponent()
let sut = sut(collectionView: collectionView)
sut.list = List {
Section(id: UUID()) {
Cell(id: UUID(), component: component)
.onUnhighlight { context in
eventContext = context
}
}
}

// when
collectionView
.delegate?
.collectionView?(
collectionView,
didUnhighlightItemAt: IndexPath(item: 0, section: 0)
)

// then
XCTAssertEqual(eventContext.indexPath, IndexPath(item: 0, section: 0))
}
}

// MARK: - UIScrollViewDelegate
Expand Down

0 comments on commit 2ed3bf2

Please sign in to comment.