Skip to content

Commit

Permalink
Merge pull request #47 from daangn/impl-should-scroll-to-top-modifier
Browse files Browse the repository at this point in the history
NO-JIRA `scrollViewShouldScrollToTop`event modifier 추가해요.
  • Loading branch information
jaxtynSong authored Jan 17, 2025
2 parents 9d7a508 + 6d81ba9 commit 2bdee75
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/KarrotListKit/Adapter/CollectionViewAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,18 @@ extension CollectionViewAdapter {
)
)
}

public func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
guard let collectionView else {
return true
}

return list?.event(for: ShouldScrollToTopEvent.self)?.handler(
.init(
collectionView: collectionView
)
) ?? true
}
}

// MARK: - UICollectionViewDataSourcePrefetching
Expand Down
16 changes: 16 additions & 0 deletions Sources/KarrotListKit/Event/List/ShouldScrollToTopEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright (c) 2025 Danggeun Market Inc.
//

import UIKit

/// This structure encapsulates the shouldScrollToTop event information and contains a closure object for shouldScrollToTop event.
public struct ShouldScrollToTopEvent: ListingViewEvent {
public struct EventContext {
/// The collectionView object requesting this information.
public let collectionView: UICollectionView
}

/// A closure that's called when if the scroll view should scroll to the top of the content.
let handler: (EventContext) -> Bool
}
8 changes: 8 additions & 0 deletions Sources/KarrotListKit/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,12 @@ extension List {
public func didEndDecelerating(_ handler: @escaping (DidEndDeceleratingEvent.EventContext) -> Void) -> Self {
registerEvent(DidEndDeceleratingEvent(handler: handler))
}

/// Register a callback handler that will be called when the scroll view should scroll to the top of the content
///
/// - Parameters:
/// - handler: The callback handler for shouldScrollToTop event
public func shouldScrollToTop(_ handler: @escaping (ShouldScrollToTopEvent.EventContext) -> Bool) -> Self {
registerEvent(ShouldScrollToTopEvent(handler: handler))
}
}
26 changes: 26 additions & 0 deletions Tests/KarrotListKitTests/CollectionViewAdapterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,32 @@ extension CollectionViewAdapterTests {
// then
XCTAssertNotNil(eventContext)
}

func test_given_scrollToTopValue_when_try_scrollToTop_then_handleShouldScrollToTopEvent() {
[true, false].forEach { value in
// given
var eventContext: ShouldScrollToTopEvent.EventContext!
let collectionView = UICollectionView(layoutAdapter: CollectionViewLayoutAdapter())
let sut = sut(
configuration: .init(),
collectionView: collectionView
)
sut.list = List(
sections: []
).shouldScrollToTop { context in
eventContext = context
return value
}

// when
let shouldScrollToTop = collectionView
.delegate?.scrollViewShouldScrollToTop?(collectionView)

// then
XCTAssertEqual(shouldScrollToTop, value)
XCTAssertNotNil(eventContext)
}
}
}

// MARK: - UICollectionViewDataSourcePrefetching
Expand Down

0 comments on commit 2bdee75

Please sign in to comment.