Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO-JIRA Highlight Unhighlight Event Modifier를 추가해요. #46

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4) was + p.p 문법으로 봤을때는 다음과 같이 변경해야되지 않을까요?ㅎㅎ

Suggested change
/// Register a callback handler that will be called when the cell was unhighlight.
/// Register a callback handler that will be called when the cell was unhighlighted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋ aa79209
�위 커밋에서 수정했어요 감사해요~

///
/// - 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