Skip to content

Commit

Permalink
feat: add DidEndDeceleratingEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
ppth0608 committed Apr 22, 2024
1 parent 89b22c3 commit dd881c6
Show file tree
Hide file tree
Showing 4 changed files with 60 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 @@ -486,6 +486,18 @@ extension CollectionViewAdapter {
)
)
}

public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
guard let collectionView else {
return
}

list?.event(for: DidEndDeceleratingEvent.self)?.handler(
.init(
collectionView: collectionView
)
)
}
}

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

import UIKit

/// This structure encapsulates the did end decelerating event information and contains a closure object for handling the did end decelerating event.
public struct DidEndDeceleratingEvent: ListingViewEvent {
public struct EventContext {

/// The collectionView object that’s decelerating the scrolling of the content view.
public let collectionView: UICollectionView
}

/// A closure that's called when the collection view ended decelerating the scrolling movement.
let handler: (EventContext) -> Void
}
8 changes: 8 additions & 0 deletions Sources/KarrotListKit/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ extension List {
public func willBeginDecelerating(_ handler: @escaping (WillBeginDeceleratingEvent.EventContext) -> Void) -> Self {
registerEvent(WillBeginDeceleratingEvent(handler: handler))
}

/// Register a callback handler that will be called when the scroll view ended decelerating the scrolling movement.
///
/// - Parameters:
/// - handler: The callback handler for did end decelerating event
public func didEndDecelerating(_ handler: @escaping (DidEndDeceleratingEvent.EventContext) -> Void) -> Self {
registerEvent(DidEndDeceleratingEvent(handler: handler))
}
}
23 changes: 23 additions & 0 deletions Tests/KarrotListKitTests/CollectionViewAdapterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,29 @@ extension CollectionViewAdapterTests {
// then
XCTAssertIdentical(eventContext.collectionView, collectionView)
}

func test_given_didEndDeceleratingHandler_when_didEndDecelerating_then_handleEvent() {
// given
var eventContext: DidEndDeceleratingEvent.EventContext!
let collectionView = UICollectionView(layoutAdapter: CollectionViewLayoutAdapter())
let sut = sut(collectionView: collectionView)
sut.list = List(
sections: []
).didEndDecelerating { context in
eventContext = context
}

// when
collectionView
.delegate?
.scrollViewDidEndDecelerating?(
collectionView
)

// then
XCTAssertIdentical(eventContext.collectionView, collectionView)
}

func test_given_refreshControlEnabled_and_handler_when_pullToRefresh_then_handleEvent() {
// given
var eventContext: PullToRefreshEvent.EventContext!
Expand Down

0 comments on commit dd881c6

Please # to comment.