Skip to content

Commit deed0bc

Browse files
Nolan Warnerkzaher
Nolan Warner
authored andcommitted
Add missing control events to UICollectionView+Rx
1 parent 39730ef commit deed0bc

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

RxCocoa/iOS/UICollectionView+Rx.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ extension UICollectionView {
179179
}
180180

181181
extension Reactive where Base: UICollectionView {
182+
public typealias DisplayCollectionViewCellEvent = (cell: UICollectionViewCell, indexPath: IndexPath)
183+
public typealias DisplayCollectionReusableViewEvent = (reuseableView: UICollectionReusableView, elementKind: String, indexPath: IndexPath)
182184

183185
/// Reactive wrapper for `dataSource`.
184186
///
@@ -219,6 +221,70 @@ extension Reactive where Base: UICollectionView {
219221
return ControlEvent(events: source)
220222
}
221223

224+
/// Reactive wrapper for `delegate` message `collectionView:didHighlightItemAt:`.
225+
public var itemHighlighted: ControlEvent<IndexPath> {
226+
let source = delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:didHighlightItemAt:)))
227+
.map { a in
228+
return try castOrThrow(IndexPath.self, a[1])
229+
}
230+
231+
return ControlEvent(events: source)
232+
}
233+
234+
/// Reactive wrapper for `delegate` message `collectionView:didUnhighlightItemAt:`.
235+
public var itemUnhighlighted: ControlEvent<IndexPath> {
236+
let source = delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:didUnhighlightItemAt:)))
237+
.map { a in
238+
return try castOrThrow(IndexPath.self, a[1])
239+
}
240+
241+
return ControlEvent(events: source)
242+
}
243+
244+
/// Reactive wrapper for `delegate` message `collectionView:willDisplay:forItemAt:`.
245+
public var willDisplayCell: ControlEvent<DisplayCollectionViewCellEvent> {
246+
let source: Observable<DisplayCollectionViewCellEvent> = self.delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:willDisplay:forItemAt:)))
247+
.map { a in
248+
return (try castOrThrow(UICollectionViewCell.self, a[1]), try castOrThrow(IndexPath.self, a[2]))
249+
}
250+
251+
return ControlEvent(events: source)
252+
}
253+
254+
/// Reactive wrapper for `delegate` message `collectionView:willDisplaySupplementaryView:forElementKind:at:`.
255+
public var willDisplaySupplementaryView: ControlEvent<DisplayCollectionReusableViewEvent> {
256+
let source: Observable<DisplayCollectionReusableViewEvent> = self.delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:willDisplaySupplementaryView:forElementKind:at:)))
257+
.map { a in
258+
return (try castOrThrow(UICollectionReusableView.self, a[1]),
259+
try castOrThrow(String.self, a[2]),
260+
try castOrThrow(IndexPath.self, a[3]))
261+
}
262+
263+
return ControlEvent(events: source)
264+
}
265+
266+
/// Reactive wrapper for `delegate` message `collectionView:didEndDisplaying:forItemAt:`.
267+
public var didEndDisplayingCell: ControlEvent<DisplayCollectionViewCellEvent> {
268+
let source: Observable<DisplayCollectionViewCellEvent> = self.delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:didEndDisplaying:forItemAt:)))
269+
.map { a in
270+
return (try castOrThrow(UICollectionViewCell.self, a[1]), try castOrThrow(IndexPath.self, a[2]))
271+
}
272+
273+
return ControlEvent(events: source)
274+
}
275+
276+
/// Reactive wrapper for `delegate` message `collectionView:didEndDisplayingSupplementaryView:forElementOfKind:at:`.
277+
public var didEndDisplayingSupplementaryView: ControlEvent<DisplayCollectionReusableViewEvent> {
278+
let source: Observable<DisplayCollectionReusableViewEvent> = self.delegate.methodInvoked(#selector(UICollectionViewDelegate.collectionView(_:didEndDisplayingSupplementaryView:forElementOfKind:at:)))
279+
.map { a in
280+
return (try castOrThrow(UICollectionReusableView.self, a[1]),
281+
try castOrThrow(String.self, a[2]),
282+
try castOrThrow(IndexPath.self, a[3]))
283+
}
284+
285+
return ControlEvent(events: source)
286+
}
287+
222288
/// Reactive wrapper for `delegate` message `collectionView:didSelectItemAtIndexPath:`.
223289
///
224290
/// It can be only used when one of the `rx.itemsWith*` methods is used to bind observable sequence,

0 commit comments

Comments
 (0)