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

add: UITableView, UICollectionView prefetching publisher #83

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
32 changes: 32 additions & 0 deletions Sources/CombineCocoa/Controls/UICollectionView+Combine.swift
Original file line number Diff line number Diff line change
@@ -77,10 +77,29 @@ public extension UICollectionView {
.map { ($0[1] as! UICollectionReusableView, $0[2] as! String, $0[3] as! IndexPath) }
.eraseToAnyPublisher()
}

/// Combine wrapper for `tableView(_:prefetchRowsAt:)`
var prefetchItemsAtPublisher: AnyPublisher<[IndexPath], Never> {
return (prefetchingDelegateProxy as! CollectionViewDataSourcePrefetchingProxy)
.prefetchItemsSubject
.eraseToAnyPublisher()
}

/// Combine wrapper for `tableView(_:cancelPrefetchingForRowsAt:)`
var cancelPrefetchingForItemsAtPublisher: AnyPublisher<[IndexPath], Never> {
let selector = #selector(UICollectionViewDataSourcePrefetching.collectionView(_:cancelPrefetchingForItemsAt:))
return prefetchingDelegateProxy.interceptSelectorPublisher(selector)
.map { ($0[1]) as! [IndexPath] }
.eraseToAnyPublisher()
}

override var delegateProxy: DelegateProxy {
CollectionViewDelegateProxy.createDelegateProxy(for: self)
}

var prefetchingDelegateProxy: DelegateProxy {
CollectionViewDataSourcePrefetchingProxy.createDelegateProxy(for: self)
}
}

@available(iOS 13.0, *)
@@ -89,5 +108,18 @@ private class CollectionViewDelegateProxy: DelegateProxy, UICollectionViewDelega
object.delegate = self
}
}

@available(iOS 13.0, *)
internal class CollectionViewDataSourcePrefetchingProxy: DelegateProxy, UICollectionViewDataSourcePrefetching, DelegateProxyType {
internal let prefetchItemsSubject = PassthroughSubject<[IndexPath], Never>()

func setDelegate(to object: UICollectionView) {
object.prefetchDataSource = self
}

func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
prefetchItemsSubject.send(indexPaths)
}
}
#endif
// swiftlint:enable force_cast
32 changes: 32 additions & 0 deletions Sources/CombineCocoa/Controls/UITableView+Combine.swift
Original file line number Diff line number Diff line change
@@ -117,10 +117,29 @@ public extension UITableView {
.map { $0[1] as! IndexPath }
.eraseToAnyPublisher()
}

/// Combine wrapper for `tableView(_:prefetchRowsAt:)`
var prefetchItemsAtPublisher: AnyPublisher<[IndexPath], Never> {
return (prefetchingDelegateProxy as! TableViewDataSourcePrefetchingProxy)
.prefetchItemsSubject
.eraseToAnyPublisher()
}

/// Combine wrapper for `tableView(_:cancelPrefetchingForRowsAt:)`
var cancelPrefetchingForItemsAtPublisher: AnyPublisher<[IndexPath], Never> {
let selector = #selector(UITableViewDataSourcePrefetching.tableView(_:cancelPrefetchingForRowsAt:))
return prefetchingDelegateProxy.interceptSelectorPublisher(selector)
.map { ($0[1]) as! [IndexPath] }
.eraseToAnyPublisher()
}

override var delegateProxy: DelegateProxy {
TableViewDelegateProxy.createDelegateProxy(for: self)
}

var prefetchingDelegateProxy: DelegateProxy {
TableViewDataSourcePrefetchingProxy.createDelegateProxy(for: self)
}
}

@available(iOS 13.0, *)
@@ -129,5 +148,18 @@ private class TableViewDelegateProxy: DelegateProxy, UITableViewDelegate, Delega
object.delegate = self
}
}

@available(iOS 13.0, *)
internal class TableViewDataSourcePrefetchingProxy: DelegateProxy, UITableViewDataSourcePrefetching, DelegateProxyType {
internal let prefetchItemsSubject = PassthroughSubject<[IndexPath], Never>()

func setDelegate(to object: UITableView) {
object.prefetchDataSource = self
}

func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
prefetchItemsSubject.send(indexPaths)
}
}
#endif
// swiftlint:enable force_cast