446 lines
16 KiB
Swift
446 lines
16 KiB
Swift
//
|
|
// UICollectionView+Rx.swift
|
|
// RxCocoa
|
|
//
|
|
// Created by Krunoslav Zaher on 4/2/15.
|
|
// Copyright © 2015 Krunoslav Zaher. All rights reserved.
|
|
//
|
|
|
|
#if os(iOS) || os(tvOS)
|
|
|
|
import Foundation
|
|
#if !RX_NO_MODULE
|
|
import RxSwift
|
|
#endif
|
|
import UIKit
|
|
|
|
// Items
|
|
|
|
extension Reactive where Base: UICollectionView {
|
|
|
|
/**
|
|
Binds sequences of elements to collection view items.
|
|
|
|
- parameter source: Observable sequence of items.
|
|
- parameter cellFactory: Transform between sequence elements and view cells.
|
|
- returns: Disposable object that can be used to unbind.
|
|
|
|
Example
|
|
|
|
let items = Observable.just([
|
|
1,
|
|
2,
|
|
3
|
|
])
|
|
|
|
items
|
|
.bindTo(collectionView.rx.itemsWithCellFactory) { (collectionView, row, element) in
|
|
let indexPath = IndexPath(forItem: row, inSection: 0)
|
|
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NumberCell
|
|
cell.value?.text = "\(element) @ \(row)"
|
|
return cell
|
|
}
|
|
.addDisposableTo(disposeBag)
|
|
*/
|
|
@available(*, deprecated, renamed: "items(source:cellFactory:)")
|
|
public func itemsWithCellFactory<S: Sequence, O: ObservableType where O.E == S>
|
|
(_ source: O)
|
|
-> (cellFactory: (UICollectionView, Int, S.Iterator.Element) -> UICollectionViewCell)
|
|
-> Disposable {
|
|
return { cellFactory in
|
|
let dataSource = RxCollectionViewReactiveArrayDataSourceSequenceWrapper<S>(cellFactory: cellFactory)
|
|
return self.itemsWithDataSource(dataSource)(source: source)
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
Binds sequences of elements to collection view items.
|
|
|
|
- parameter source: Observable sequence of items.
|
|
- parameter cellFactory: Transform between sequence elements and view cells.
|
|
- returns: Disposable object that can be used to unbind.
|
|
|
|
Example
|
|
|
|
let items = Observable.just([
|
|
1,
|
|
2,
|
|
3
|
|
])
|
|
|
|
items
|
|
.bindTo(collectionView.rx.items) { (collectionView, row, element) in
|
|
let indexPath = IndexPath(forItem: row, inSection: 0)
|
|
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NumberCell
|
|
cell.value?.text = "\(element) @ \(row)"
|
|
return cell
|
|
}
|
|
.addDisposableTo(disposeBag)
|
|
*/
|
|
public func items<S: Sequence, O: ObservableType where O.E == S>
|
|
(source: O)
|
|
-> (cellFactory: (UICollectionView, Int, S.Iterator.Element) -> UICollectionViewCell)
|
|
-> Disposable {
|
|
return { cellFactory in
|
|
let dataSource = RxCollectionViewReactiveArrayDataSourceSequenceWrapper<S>(cellFactory: cellFactory)
|
|
return self.items(dataSource: dataSource)(source: source)
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
Binds sequences of elements to collection view items.
|
|
|
|
- parameter cellIdentifier: Identifier used to dequeue cells.
|
|
- parameter source: Observable sequence of items.
|
|
- parameter configureCell: Transform between sequence elements and view cells.
|
|
- parameter cellType: Type of table view cell.
|
|
- returns: Disposable object that can be used to unbind.
|
|
|
|
Example
|
|
|
|
let items = Observable.just([
|
|
1,
|
|
2,
|
|
3
|
|
])
|
|
|
|
items
|
|
.bindTo(collectionView.rx.itemsWithCellIdentifier("Cell", cellType: NumberCell.self)) { (row, element, cell) in
|
|
cell.value?.text = "\(element) @ \(row)"
|
|
}
|
|
.addDisposableTo(disposeBag)
|
|
*/
|
|
@available(*, deprecated, renamed: "items(cellIdentifier:cellType:source:configureCell:)")
|
|
public func itemsWithCellIdentifier<S: Sequence, Cell: UICollectionViewCell, O : ObservableType where O.E == S>
|
|
(_ cellIdentifier: String, cellType: Cell.Type = Cell.self)
|
|
-> (source: O)
|
|
-> (configureCell: (Int, S.Iterator.Element, Cell) -> Void)
|
|
-> Disposable {
|
|
return { source in
|
|
return { configureCell in
|
|
let dataSource = RxCollectionViewReactiveArrayDataSourceSequenceWrapper<S> { (cv, i, item) in
|
|
let indexPath = IndexPath(item: i, section: 0)
|
|
let cell = cv.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! Cell
|
|
configureCell(i, item, cell)
|
|
return cell
|
|
}
|
|
|
|
return self.itemsWithDataSource(dataSource)(source: source)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
Binds sequences of elements to collection view items.
|
|
|
|
- parameter cellIdentifier: Identifier used to dequeue cells.
|
|
- parameter source: Observable sequence of items.
|
|
- parameter configureCell: Transform between sequence elements and view cells.
|
|
- parameter cellType: Type of table view cell.
|
|
- returns: Disposable object that can be used to unbind.
|
|
|
|
Example
|
|
|
|
let items = Observable.just([
|
|
1,
|
|
2,
|
|
3
|
|
])
|
|
|
|
items
|
|
.bindTo(collectionView.rx.items(cellIdentifier: "Cell", cellType: NumberCell.self)) { (row, element, cell) in
|
|
cell.value?.text = "\(element) @ \(row)"
|
|
}
|
|
.addDisposableTo(disposeBag)
|
|
*/
|
|
public func items<S: Sequence, Cell: UICollectionViewCell, O : ObservableType where O.E == S>
|
|
(cellIdentifier: String, cellType: Cell.Type = Cell.self)
|
|
-> (source: O)
|
|
-> (configureCell: (Int, S.Iterator.Element, Cell) -> Void)
|
|
-> Disposable {
|
|
return { source in
|
|
return { configureCell in
|
|
let dataSource = RxCollectionViewReactiveArrayDataSourceSequenceWrapper<S> { (cv, i, item) in
|
|
let indexPath = IndexPath(item: i, section: 0)
|
|
let cell = cv.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! Cell
|
|
configureCell(i, item, cell)
|
|
return cell
|
|
}
|
|
|
|
return self.items(dataSource: dataSource)(source: source)
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
Binds sequences of elements to collection view items using a custom reactive data used to perform the transformation.
|
|
|
|
- parameter dataSource: Data source used to transform elements to view cells.
|
|
- parameter source: Observable sequence of items.
|
|
- returns: Disposable object that can be used to unbind.
|
|
|
|
Example
|
|
|
|
let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, Double>>()
|
|
|
|
let items = Observable.just([
|
|
SectionModel(model: "First section", items: [
|
|
1.0,
|
|
2.0,
|
|
3.0
|
|
]),
|
|
SectionModel(model: "Second section", items: [
|
|
1.0,
|
|
2.0,
|
|
3.0
|
|
]),
|
|
SectionModel(model: "Third section", items: [
|
|
1.0,
|
|
2.0,
|
|
3.0
|
|
])
|
|
])
|
|
|
|
dataSource.configureCell = { (dataSource, cv, indexPath, element) in
|
|
let cell = cv.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NumberCell
|
|
cell.value?.text = "\(element) @ row \(indexPath.row)"
|
|
return cell
|
|
}
|
|
|
|
items
|
|
.bindTo(collectionView.rx.itemsWithDataSource(dataSource))
|
|
.addDisposableTo(disposeBag)
|
|
*/
|
|
@available(*, deprecated, renamed: "items(dataSource:source:)")
|
|
public func itemsWithDataSource<
|
|
DataSource: RxCollectionViewDataSourceType & UICollectionViewDataSource,
|
|
O: ObservableType where DataSource.Element == O.E
|
|
>
|
|
(_ dataSource: DataSource)
|
|
-> (source: O)
|
|
-> Disposable {
|
|
return { source in
|
|
return source.subscribeProxyDataSource(ofObject: self.base, dataSource: dataSource, retainDataSource: true) { [weak collectionView = self.base] (_: RxCollectionViewDataSourceProxy, event) -> Void in
|
|
guard let collectionView = collectionView else {
|
|
return
|
|
}
|
|
dataSource.collectionView(collectionView, observedEvent: event)
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
Binds sequences of elements to collection view items using a custom reactive data used to perform the transformation.
|
|
|
|
- parameter dataSource: Data source used to transform elements to view cells.
|
|
- parameter source: Observable sequence of items.
|
|
- returns: Disposable object that can be used to unbind.
|
|
|
|
Example
|
|
|
|
let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, Double>>()
|
|
|
|
let items = Observable.just([
|
|
SectionModel(model: "First section", items: [
|
|
1.0,
|
|
2.0,
|
|
3.0
|
|
]),
|
|
SectionModel(model: "Second section", items: [
|
|
1.0,
|
|
2.0,
|
|
3.0
|
|
]),
|
|
SectionModel(model: "Third section", items: [
|
|
1.0,
|
|
2.0,
|
|
3.0
|
|
])
|
|
])
|
|
|
|
dataSource.configureCell = { (dataSource, cv, indexPath, element) in
|
|
let cell = cv.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NumberCell
|
|
cell.value?.text = "\(element) @ row \(indexPath.row)"
|
|
return cell
|
|
}
|
|
|
|
items
|
|
.bindTo(collectionView.rx.items(dataSource: dataSource))
|
|
.addDisposableTo(disposeBag)
|
|
*/
|
|
public func items<
|
|
DataSource: RxCollectionViewDataSourceType & UICollectionViewDataSource,
|
|
O: ObservableType where DataSource.Element == O.E
|
|
>
|
|
(dataSource: DataSource)
|
|
-> (source: O)
|
|
-> Disposable {
|
|
return { source in
|
|
// This is called for sideeffects only, and to make sure delegate proxy is in place when
|
|
// data source is being bound.
|
|
// This is needed because theoretically the data source subscription itself might
|
|
// call `self.rx_delegate`. If that happens, it might cause weird side effects since
|
|
// setting data source will set delegate, and UITableView might get into a weird state.
|
|
// Therefore it's better to set delegate proxy first, just to be sure.
|
|
_ = self.delegate
|
|
// Strong reference is needed because data source is in use until result subscription is disposed
|
|
return source.subscribeProxyDataSource(ofObject: self.base, dataSource: dataSource, retainDataSource: true) { [weak collectionView = self.base] (_: RxCollectionViewDataSourceProxy, event) -> Void in
|
|
guard let collectionView = collectionView else {
|
|
return
|
|
}
|
|
dataSource.collectionView(collectionView, observedEvent: event)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Reactive where Base: UICollectionView {
|
|
|
|
/**
|
|
Factory method that enables subclasses to implement their own `delegate`.
|
|
|
|
- returns: Instance of delegate proxy that wraps `delegate`.
|
|
*/
|
|
public func createDelegateProxy() -> RxScrollViewDelegateProxy {
|
|
return RxCollectionViewDelegateProxy(parentObject: self.base)
|
|
}
|
|
|
|
/**
|
|
Factory method that enables subclasses to implement their own `rx.dataSource`.
|
|
|
|
- returns: Instance of delegate proxy that wraps `dataSource`.
|
|
*/
|
|
public func createDataSourceProxy() -> RxCollectionViewDataSourceProxy {
|
|
return RxCollectionViewDataSourceProxy(parentObject: self.base)
|
|
}
|
|
|
|
/**
|
|
Reactive wrapper for `dataSource`.
|
|
|
|
For more information take a look at `DelegateProxyType` protocol documentation.
|
|
*/
|
|
public var dataSource: DelegateProxy {
|
|
return RxCollectionViewDataSourceProxy.proxyForObject(base)
|
|
}
|
|
|
|
/**
|
|
Installs data source as forwarding delegate on `rx.dataSource`.
|
|
Data source won't be retained.
|
|
|
|
It enables using normal delegate mechanism with reactive delegate mechanism.
|
|
|
|
- parameter dataSource: Data source object.
|
|
- returns: Disposable object that can be used to unbind the data source.
|
|
*/
|
|
public func setDataSource(_ dataSource: UICollectionViewDataSource)
|
|
-> Disposable {
|
|
return RxCollectionViewDataSourceProxy.installForwardDelegate(dataSource, retainDelegate: false, onProxyForObject: self.base)
|
|
}
|
|
|
|
/**
|
|
Reactive wrapper for `delegate` message `collectionView:didSelectItemAtIndexPath:`.
|
|
*/
|
|
public var itemSelected: ControlEvent<IndexPath> {
|
|
let source = delegate.observe(#selector(UICollectionViewDelegate.collectionView(_:didSelectItemAt:)))
|
|
.map { a in
|
|
return a[1] as! IndexPath
|
|
}
|
|
|
|
return ControlEvent(events: source)
|
|
}
|
|
|
|
/**
|
|
Reactive wrapper for `delegate` message `collectionView:didSelectItemAtIndexPath:`.
|
|
*/
|
|
public var itemDeselected: ControlEvent<IndexPath> {
|
|
let source = delegate.observe(#selector(UICollectionViewDelegate.collectionView(_:didDeselectItemAt:)))
|
|
.map { a in
|
|
return a[1] as! IndexPath
|
|
}
|
|
|
|
return ControlEvent(events: source)
|
|
}
|
|
|
|
/**
|
|
Reactive wrapper for `delegate` message `collectionView:didSelectItemAtIndexPath:`.
|
|
|
|
It can be only used when one of the `rx.itemsWith*` methods is used to bind observable sequence,
|
|
or any other data source conforming to `SectionedViewDataSourceType` protocol.
|
|
|
|
```
|
|
collectionView.rx.modelSelected(MyModel.self)
|
|
.map { ...
|
|
```
|
|
*/
|
|
public func modelSelected<T>(_ modelType: T.Type) -> ControlEvent<T> {
|
|
let source: Observable<T> = itemSelected.flatMap { [weak view = self.base as UICollectionView] indexPath -> Observable<T> in
|
|
guard let view = view else {
|
|
return Observable.empty()
|
|
}
|
|
|
|
return Observable.just(try view.rx.modelAtIndexPath(indexPath))
|
|
}
|
|
|
|
return ControlEvent(events: source)
|
|
}
|
|
|
|
/**
|
|
Reactive wrapper for `delegate` message `collectionView:didSelectItemAtIndexPath:`.
|
|
|
|
It can be only used when one of the `rx.itemsWith*` methods is used to bind observable sequence,
|
|
or any other data source conforming to `SectionedViewDataSourceType` protocol.
|
|
|
|
```
|
|
collectionView.rx.modelDeselected(MyModel.self)
|
|
.map { ...
|
|
```
|
|
*/
|
|
public func modelDeselected<T>(_ modelType: T.Type) -> ControlEvent<T> {
|
|
let source: Observable<T> = itemDeselected.flatMap { [weak view = self.base as UICollectionView] indexPath -> Observable<T> in
|
|
guard let view = view else {
|
|
return Observable.empty()
|
|
}
|
|
|
|
return Observable.just(try view.rx.modelAtIndexPath(indexPath))
|
|
}
|
|
|
|
return ControlEvent(events: source)
|
|
}
|
|
|
|
/**
|
|
Syncronous helper method for retrieving a model at indexPath through a reactive data source
|
|
*/
|
|
public func modelAtIndexPath<T>(_ indexPath: IndexPath) throws -> T {
|
|
let dataSource: SectionedViewDataSourceType = castOrFatalError(self.dataSource.forwardToDelegate(), message: "This method only works in case one of the `rx.itemsWith*` methods was used.")
|
|
|
|
let element = try dataSource.modelAtIndexPath(indexPath)
|
|
|
|
return element as! T
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if os(tvOS)
|
|
|
|
extension Reactive where Base: UICollectionView {
|
|
|
|
/**
|
|
Reactive wrapper for `delegate` message `collectionView:didUpdateFocusInContext:withAnimationCoordinator:`.
|
|
*/
|
|
public var didUpdateFocusInContextWithAnimationCoordinator: ControlEvent<(context: UIFocusUpdateContext, animationCoordinator: UIFocusAnimationCoordinator)> {
|
|
|
|
let source = delegate.observe(#selector(UICollectionViewDelegate.collectionView(_:didUpdateFocusIn:with:)))
|
|
.map { a -> (context: UIFocusUpdateContext, animationCoordinator: UIFocusAnimationCoordinator) in
|
|
let context = a[1] as! UIFocusUpdateContext
|
|
let animationCoordinator = a[2] as! UIFocusAnimationCoordinator
|
|
return (context: context, animationCoordinator: animationCoordinator)
|
|
}
|
|
|
|
return ControlEvent(events: source)
|
|
}
|
|
}
|
|
#endif
|