Update documentation and polishing.

This commit is contained in:
Krunoslav Zaher 2015-07-05 18:40:06 +02:00
parent dc262916ec
commit d5c977109a
22 changed files with 227 additions and 249 deletions

View File

@ -126,7 +126,7 @@ Now something a little more interesting:
* bind results to label (resultLabel.rx_subscribeTextTo)
```swift
let subscription/*: Disposable */ = primeTextField.rx_text // type is Observable<String>
let subscription/*: Disposable */ = primeTextField.rx_text // type is Observable<String>
>- map { WolframAlphaIsPrime($0.toInt() ?? 0) } // type is Observable<Observable<Prime>>
>- concat // type is Observable<Prime>
>- map { "number \($0.n) is prime? \($0.isPrime)" } // type is Observable<String>
@ -245,6 +245,7 @@ Operators are stateless by default.
* [`distinctUntilChanged`](http://reactivex.io/documentation/operators/distinct.html)
* [`filter` / `where`](http://reactivex.io/documentation/operators/filter.html)
* [`sample`](http://reactivex.io/documentation/operators/sample.html)
* [`skip`](http://reactivex.io/documentation/operators/skip.html)
* [`take`](http://reactivex.io/documentation/operators/take.html)
#### Combining Observables
@ -325,6 +326,16 @@ extension NSNotificationCenter {
}
```
```swift
class DelegateProxy {
public func observe(selector: Selector) -> Observable<[AnyObject]> {}
public dispose() {}
}
```
**iOS**
```swift
@ -333,6 +344,8 @@ extension UIControl {
public func rx_controlEvents(controlEvents: UIControlEvents) -> Observable<Void> { }
public func rx_subscribeEnabledTo(source: Observable<Bool>) -> Disposable {}
}
```
@ -356,6 +369,8 @@ extension UITextField {
```swift
extension UISearchBar {
public var rx_delegate: DelegateProxy {}
public var rx_searchText: Observable<String> {}
}
@ -393,6 +408,10 @@ extension UIImageView {
```swift
extension UIScrollView {
public var rx_delegate: DelegateProxy {}
public func rx_setDelegate(delegate: UIScrollViewDelegate) {}
public var rx_contentOffset: Observable<CGPoint> {}
}
@ -416,51 +435,54 @@ extension UISlider {
```swift
extension UITableView {
public var rx_dataSource: DelegateProxy {}
public func rx_elementTap<E>() -> Observable<E> {}
public func rx_setDataSource(dataSource: UITableViewDataSource) -> Disposable {}
public func rx_rowTap() -> Observable<(UITableView, Int)> {}
public func rx_subscribeWithReactiveDataSource<DataSource: protocol<RxTableViewDataSourceType, UITableViewDataSource>>(dataSource: DataSource)
-> Observable<DataSource.Element> -> Disposable {}
public func rx_subscribeRowsTo<E where E: AnyObject>
(dataSource: TableViewDataSource)
(source: Observable<[E]>)
-> Disposable {}
public func rx_subscribeItemsTo<Item>(cellFactory: (UITableView, Int, Item) -> UITableViewCell)
-> Observable<[Item]> -> Disposable {}
public func rx_subscribeRowsTo<E where E : AnyObject>
(cellFactory: (UITableView, NSIndexPath, E) -> UITableViewCell)
(source: Observable<[E]>)
-> Disposable {}
public func rx_subscribeItemsToWithCellIdentifier<Item, Cell: UITableViewCell>(cellIdentifier: String, configureCell: (NSIndexPath, Item, Cell) -> Void)
-> Observable<[Item]> -> Disposable {}
public func rx_subscribeRowsToCellWithIdentifier<E, Cell where E : AnyObject, Cell: UITableViewCell>
(cellIdentifier: String, configureCell: (UITableView, NSIndexPath, E, Cell) -> Void)
(source: Observable<[E]>)
-> Disposable {}
public var rx_itemSelected: Observable<NSIndexPath> {}
public var rx_itemInserted: Observable<NSIndexPath> {}
public var rx_itemDeleted: Observable<NSIndexPath> {}
public var rx_itemMoved: Observable<ItemMovedEvent> {}
// This method only works in case one of the `rx_subscribeItemsTo` methods was used.
public func rx_modelSelected<T>() -> Observable<T> {}
}
```
```swift
extension UICollectionView {
public var rx_dataSource: DelegateProxy {}
public var rx_itemTap: -> Observable<(UICollectionView, Int)> {}
public func rx_setDataSource(dataSource: UICollectionViewDataSource) -> Disposable {}
public func rx_subscribeWithReactiveDataSource<DataSource: protocol<RxCollectionViewDataSourceType, UICollectionViewDataSource>>(dataSource: DataSource)
-> Observable<DataSource.Element> -> Disposable {}
public func rx_elementTap<E>() -> Observable<E> {}
public func rx_subscribeItemsTo<Item>(cellFactory: (UICollectionView, Int, Item) -> UICollectionViewCell)
-> Observable<[Item]> -> Disposable {}
public func rx_subscribeItemsTo<E where E: AnyObject>
(dataSource: CollectionViewDataSource)
(source: Observable<[E]>)
-> Disposable {}
public func rx_subscribeItemsToWithCellIdentifier<Item, Cell: UICollectionViewCell>(cellIdentifier: String, configureCell: (Int, Item, Cell) -> Void)
-> Observable<[Item]> -> Disposable {}
public func rx_subscribeItemsTo<E where E : AnyObject>
(cellFactory: (UICollectionView, NSIndexPath, E) -> UICollectionViewCell)
(source: Observable<[E]>)
-> Disposable {}
public func rx_subscribeItemsWithIdentifierTo<E, Cell where E : AnyObject, Cell : UICollectionViewCell>
(cellIdentifier: String, configureCell: (UICollectionView, NSIndexPath, E, Cell) -> Void)
(source: Observable<[E]>)
-> Disposable {}
public var rx_itemSelected: Observable<NSIndexPath> {}
// This method only works in case one of the `rx_subscribeItemsTo` methods was used.
public func rx_modelSelected<T>() -> Observable<T> {}
}
```
**OSX**
@ -504,10 +526,11 @@ extension NSImageView {
```swift
extension NSTextField {
public func rx_subscribeTextTo(source: Observable<String>) -> Disposable {}
public var rx_delegate: DelegateProxy {}
public var rx_text: Observable<String> {}
public func rx_subscribeTextTo(source: Observable<String>) -> Disposable {}
}
```

View File

@ -70,7 +70,7 @@ public class DelegateProxy : _RXDelegateProxy {
return self(parentObject: object)
}
public class func getAssignedProxyFor(object: AnyObject) -> Self? {
public class func assignedProxyFor(object: AnyObject) -> Self? {
let maybeDelegate: AnyObject! = objc_getAssociatedObject(object, self.delegateAssociatedObjectTag())
return castOptionalOrFatalError(maybeDelegate)
}
@ -85,7 +85,7 @@ public class DelegateProxy : _RXDelegateProxy {
self._setForwardToDelegate(delegate, retainDelegate: retainDelegate)
}
public func getForwardToDelegate() -> AnyObject? {
public func forwardToDelegate() -> AnyObject? {
return self._forwardToDelegate
}

View File

@ -84,16 +84,16 @@ public protocol DelegateProxyType : AnyObject {
// There can be only one registered proxy per object
// These functions control that.
static func assignedProxyFor(object: AnyObject) -> Self?
static func assignProxy(proxy: AnyObject, toObject object: AnyObject)
static func getAssignedProxyFor(object: AnyObject) -> Self?
// Set/Get current delegate for object
static func getCurrentDelegateFor(object: AnyObject) -> AnyObject?
static func currentDelegateFor(object: AnyObject) -> AnyObject?
static func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject)
// Set/Get current delegate on proxy
func forwardToDelegate() -> AnyObject?
func setForwardToDelegate(forwardToDelegate: AnyObject?, retainDelegate: Bool)
func getForwardToDelegate() -> AnyObject?
}
// future extensions :)
@ -102,25 +102,25 @@ public protocol DelegateProxyType : AnyObject {
func proxyForObject<P: DelegateProxyType>(object: AnyObject) -> P {
MainScheduler.ensureExecutingOnScheduler()
let maybeProxy = P.getAssignedProxyFor(object)
let maybeProxy = P.assignedProxyFor(object)
let proxy: P
if maybeProxy == nil {
proxy = P.createProxyForObject(object)
P.assignProxy(proxy, toObject: object)
assert(P.getAssignedProxyFor(object) === proxy)
assert(P.assignedProxyFor(object) === proxy)
}
else {
proxy = maybeProxy!
}
let currentDelegate: AnyObject? = P.getCurrentDelegateFor(object)
let currentDelegate: AnyObject? = P.currentDelegateFor(object)
if currentDelegate !== proxy {
proxy.setForwardToDelegate(currentDelegate, retainDelegate: false)
P.setCurrentDelegate(proxy, toObject: object)
assert(P.getCurrentDelegateFor(object) === proxy)
assert(proxy.getForwardToDelegate() === currentDelegate)
assert(P.currentDelegateFor(object) === proxy)
assert(proxy.forwardToDelegate() === currentDelegate)
}
return proxy
@ -129,7 +129,7 @@ func proxyForObject<P: DelegateProxyType>(object: AnyObject) -> P {
func installDelegate<P: DelegateProxyType>(proxy: P, delegate: AnyObject, retainDelegate: Bool, onProxyForObject object: AnyObject) -> Disposable {
//assert(proxy === proxyForObject(object))
assert(proxy.getForwardToDelegate() === nil, "There is already a set delegate \(proxy.getForwardToDelegate())")
assert(proxy.forwardToDelegate() === nil, "There is already a set delegate \(proxy.forwardToDelegate())")
proxy.setForwardToDelegate(delegate, retainDelegate: retainDelegate)
@ -138,12 +138,12 @@ func installDelegate<P: DelegateProxyType>(proxy: P, delegate: AnyObject, retain
P.setCurrentDelegate(nil, toObject: object)
P.setCurrentDelegate(proxy, toObject: object)
assert(proxy.getForwardToDelegate() === delegate, "Setting of delegate failed")
assert(proxy.forwardToDelegate() === delegate, "Setting of delegate failed")
return AnonymousDisposable {
MainScheduler.ensureExecutingOnScheduler()
assert(proxy.getForwardToDelegate() === delegate, "Delegate was changed from time it was first set. Current \(proxy.getForwardToDelegate()), and it should have been \(proxy)")
assert(proxy.forwardToDelegate() === delegate, "Delegate was changed from time it was first set. Current \(proxy.forwardToDelegate()), and it should have been \(proxy)")
proxy.setForwardToDelegate(nil, retainDelegate: retainDelegate)
}
@ -175,7 +175,7 @@ func setProxyDataSourceForObject<P: DelegateProxyType, Element>(object: AnyObjec
let subscription = source.subscribe(AnonymousObserver { event in
MainScheduler.ensureExecutingOnScheduler()
assert(proxy === P.getCurrentDelegateFor(object), "Proxy changed from the time it was first set.\nOriginal: \(proxy)\nExisting: \(P.getCurrentDelegateFor(object))")
assert(proxy === P.currentDelegateFor(object), "Proxy changed from the time it was first set.\nOriginal: \(proxy)\nExisting: \(P.currentDelegateFor(object))")
binding(proxy, event)
})

View File

@ -97,6 +97,16 @@ func castOptionalOrFatalError<T>(value: AnyObject?) -> T? {
return v
}
func castOrFatalError<T>(value: AnyObject!, message: String) -> T {
let result: RxResult<T> = castOrFail(value)
if result.isFailure {
rxFatalError(message)
}
return result.get()
}
func castOrFatalError<T>(value: AnyObject!) -> T {
let result: RxResult<T> = castOrFail(value)

View File

@ -31,7 +31,7 @@ class RxTextFieldDelegate : DelegateProxy
dispatchNext(nextValue, observers)
}
class func getCurrentDelegateFor(object: AnyObject) -> AnyObject? {
class func currentDelegateFor(object: AnyObject) -> AnyObject? {
let textField: NSTextField = castOrFatalError(object)
return textField.delegate
}

View File

@ -39,7 +39,7 @@ class RxCollectionViewReactiveArrayDataSource<ElementType> : _RxCollectionViewRe
, RxCollectionViewDataSourceType {
typealias Element = [ElementType]
typealias CellFactory = (UICollectionView, NSIndexPath, ElementType) -> UICollectionViewCell
typealias CellFactory = (UICollectionView, Int, ElementType) -> UICollectionViewCell
var itemModels: [ElementType]? = nil
@ -60,7 +60,7 @@ class RxCollectionViewReactiveArrayDataSource<ElementType> : _RxCollectionViewRe
}
override func _collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
return cellFactory(collectionView, indexPath, itemModels![indexPath.item])
return cellFactory(collectionView, indexPath.item, itemModels![indexPath.item])
}
// reactive

View File

@ -39,7 +39,7 @@ class RxTableViewReactiveArrayDataSource<ElementType> : _RxTableViewReactiveArra
, RxTableViewDataSourceType {
typealias Element = [ElementType]
typealias CellFactory = (UITableView, NSIndexPath, ElementType) -> UITableViewCell
typealias CellFactory = (UITableView, Int, ElementType) -> UITableViewCell
var itemModels: [ElementType]? = nil
@ -58,7 +58,7 @@ class RxTableViewReactiveArrayDataSource<ElementType> : _RxTableViewReactiveArra
}
override func _tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return cellFactory(tableView, indexPath, itemModels![indexPath.row])
return cellFactory(tableView, indexPath.item, itemModels![indexPath.row])
}
// reactive

View File

@ -9,4 +9,4 @@
import Foundation
import UIKit
public typealias ItemMovedEvent = (sourceIndexPath: NSIndexPath, destinationIndexPath: NSIndexPath)
public typealias ItemMovedEvent = (sourceIndex: NSIndexPath, destinationIndex: NSIndexPath)

View File

@ -61,7 +61,7 @@ class RxCollectionViewDataSourceProxy : DelegateProxy
collectionView.dataSource = castOptionalOrFatalError(delegate)
}
class func getCurrentDelegateFor(object: AnyObject) -> AnyObject? {
class func currentDelegateFor(object: AnyObject) -> AnyObject? {
let collectionView: UICollectionView = castOrFatalError(object)
return collectionView.dataSource
}

View File

@ -10,37 +10,8 @@ import Foundation
import UIKit
import RxSwift
let collectionViewDelegateNotSet = CollectionViewDelegateNotSet()
class CollectionViewDelegateNotSet : NSObject
, UICollectionViewDelegate {
}
// Please take a look at `DelegateProxyType.swift`
class RxCollectionViewDelegateProxy : RxScrollViewDelegateProxy
, UICollectionViewDelegate {
typealias ItemSelectedObserver = ObserverOf<NSIndexPath>
typealias ItemSelectedDisposeKey = Bag<ItemSelectedObserver>.KeyType
unowned let collectionView: UICollectionView
var itemSelectedObservers: Bag<ItemSelectedObserver> = Bag()
required init(parentObject: AnyObject) {
self.collectionView = parentObject as! UICollectionView
super.init(parentObject: parentObject)
}
func addItemSelectedObserver(observer: ItemSelectedObserver) -> ItemSelectedDisposeKey {
return itemSelectedObservers.put(observer)
}
func removeItemSelectedObserver(key: ItemSelectedDisposeKey) {
let element = itemSelectedObservers.removeKey(key)
if element == nil {
removingObserverFailed()
}
}
}

View File

@ -10,12 +10,6 @@ import Foundation
import RxSwift
import UIKit
let scrollViewDelegateNotSet = ScrollViewDelegateNotSet()
class ScrollViewDelegateNotSet : NSObject
, UIScrollViewDelegate {
}
// Please take a look at `DelegateProxyType.swift`
class RxScrollViewDelegateProxy : DelegateProxy
, UIScrollViewDelegate
@ -27,8 +21,6 @@ class RxScrollViewDelegateProxy : DelegateProxy
unowned let scrollView: UIScrollView
unowned var scrollViewDelegate: UIScrollViewDelegate = scrollViewDelegateNotSet
required init(parentObject: AnyObject) {
self.scrollView = parentObject as! UIScrollView
super.init(parentObject: parentObject)
@ -57,7 +49,7 @@ class RxScrollViewDelegateProxy : DelegateProxy
func scrollViewDidScroll(scrollView: UIScrollView) {
dispatchNext(scrollView.contentOffset, contentOffsetObservers)
scrollViewDelegate.scrollViewDidScroll?(scrollView)
self._forwardToDelegate?.scrollViewDidScroll?(scrollView)
}
// delegate proxy
@ -73,7 +65,7 @@ class RxScrollViewDelegateProxy : DelegateProxy
collectionView.delegate = castOptionalOrFatalError(delegate)
}
class func getCurrentDelegateFor(object: AnyObject) -> AnyObject? {
class func currentDelegateFor(object: AnyObject) -> AnyObject? {
let collectionView: UIScrollView = castOrFatalError(object)
return collectionView.delegate
}

View File

@ -14,7 +14,7 @@ class RxSearchBarDelegateProxy : DelegateProxy
, UISearchBarDelegate
, DelegateProxyType {
class func getCurrentDelegateFor(object: AnyObject) -> AnyObject? {
class func currentDelegateFor(object: AnyObject) -> AnyObject? {
let searchBar: UISearchBar = castOrFatalError(object)
return searchBar.delegate
}

View File

@ -66,7 +66,7 @@ class RxTableViewDataSourceProxy : DelegateProxy
collectionView.dataSource = castOptionalOrFatalError(delegate)
}
class func getCurrentDelegateFor(object: AnyObject) -> AnyObject? {
class func currentDelegateFor(object: AnyObject) -> AnyObject? {
let collectionView: UITableView = castOrFatalError(object)
return collectionView.dataSource
}

View File

@ -10,37 +10,8 @@ import Foundation
import UIKit
import RxSwift
let tableViewDelegateNotSet = TableViewDelegateNotSet()
class TableViewDelegateNotSet : NSObject
, UITableViewDelegate {
}
// Please take a look at `DelegateProxyType.swift`
class RxTableViewDelegateProxy : RxScrollViewDelegateProxy
, UITableViewDelegate {
typealias ItemSelectedObserver = ObserverOf<NSIndexPath>
typealias ItemSelectedDisposeKey = Bag<ItemSelectedObserver>.KeyType
unowned let tableView: UITableView
var itemSelectedObservers: Bag<ItemSelectedObserver> = Bag()
required init(parentObject: AnyObject) {
self.tableView = parentObject as! UITableView
super.init(parentObject: parentObject)
}
func addItemSelectedObserver(observer: ItemSelectedObserver) -> ItemSelectedDisposeKey {
return itemSelectedObservers.put(observer)
}
func removeItemSelectedObserver(key: ItemSelectedDisposeKey) {
let element = itemSelectedObservers.removeKey(key)
if element == nil {
removingObserverFailed()
}
}
}

View File

@ -26,6 +26,13 @@ extension UICollectionView {
}
}
// For more detailed explanations, take a look at `DelegateProxyType.swift`
public func rx_setDataSource(dataSource: UICollectionViewDataSource)
-> Disposable {
let proxy: RxCollectionViewDataSourceProxy = proxyForObject(self)
return installDelegate(proxy, dataSource, false, onProxyForObject: self)
}
// data source
// Registers reactive data source with collection view.
@ -40,32 +47,15 @@ extension UICollectionView {
public func rx_subscribeWithReactiveDataSource<DataSource: protocol<RxCollectionViewDataSourceType, UICollectionViewDataSource>>
(dataSource: DataSource)
-> Observable<DataSource.Element> -> Disposable {
return setProxyDataSourceForObject(self, dataSource, false) { (_: RxCollectionViewDataSourceProxy, event) -> Void in
dataSource.collectionView(self, observedEvent: event)
}
return setProxyDataSourceForObject(self, dataSource, false) { (_: RxCollectionViewDataSourceProxy, event) -> Void in
dataSource.collectionView(self, observedEvent: event)
}
}
// Registers `UICollectionViewDataSource`.
// For more detailed explanations, take a look at `RxCollectionViewDataSourceType.swift` and `DelegateProxyType.swift`
public func rx_setDataSource(dataSource: UICollectionViewDataSource)
-> Disposable {
let proxy: RxCollectionViewDataSourceProxy = proxyForObject(self)
return installDelegate(proxy, dataSource, false, onProxyForObject: self)
}
// delegate
// For more detailed explanations, take a look at `DelegateProxyType.swift`
public func rx_setDelegate(delegate: UICollectionViewDelegate)
-> Disposable {
let proxy: RxCollectionViewDelegateProxy = proxyForObject(self)
return installDelegate(proxy, delegate, false, onProxyForObject: self)
}
// `reloadData` - items subscription methods (it's assumed that there is one section, and it is typed `Void`)
public func rx_subscribeItemsTo<Item>
(cellFactory: (UICollectionView, NSIndexPath, Item) -> UICollectionViewCell)
(cellFactory: (UICollectionView, Int, Item) -> UICollectionViewCell)
-> Observable<[Item]> -> Disposable {
return { source in
let dataSource = RxCollectionViewReactiveArrayDataSource<Item>(cellFactory: cellFactory)
@ -74,12 +64,13 @@ extension UICollectionView {
}
public func rx_subscribeItemsToWithCellIdentifier<Item, Cell: UICollectionViewCell>
(cellIdentifier: String, configureCell: (NSIndexPath, Item, Cell) -> Void)
(cellIdentifier: String, configureCell: (Int, Item, Cell) -> Void)
-> Observable<[Item]> -> Disposable {
return { source in
let dataSource = RxCollectionViewReactiveArrayDataSource<Item> { (cv, indexPath, item) in
let dataSource = RxCollectionViewReactiveArrayDataSource<Item> { (cv, i, item) in
let indexPath = NSIndexPath(forItem: i, inSection: 0)
let cell = cv.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! Cell
configureCell(indexPath, item, cell)
configureCell(i, item, cell)
return cell
}
@ -90,32 +81,47 @@ extension UICollectionView {
// events
public var rx_itemSelected: Observable<NSIndexPath> {
return _proxyObservableForObject({ d, o in
return d.addItemSelectedObserver(o)
}, removeObserver: { (delegate, disposeKey) -> Void in
delegate.removeItemSelectedObserver(disposeKey)
})
return rx_delegate.observe("collectionView:didSelectItemAtIndexPath:")
>- map { a in
return a[1] as! NSIndexPath
}
}
// typed events
public func rx_modelSelected<T>() -> Observable<T> {
return rx_itemSelected >- map { indexPath in
let dataSource: RxCollectionViewReactiveArrayDataSource<T> = castOrFatalError(self.rx_dataSource.getForwardToDelegate())
let dataSource: RxCollectionViewReactiveArrayDataSource<T> = castOrFatalError(self.rx_dataSource.forwardToDelegate(), "This method only works in case one of the `rx_subscribeItemsTo` methods was used.")
return dataSource.modelAtIndex(indexPath.item)!
}
}
// private methods
private func _proxyObservableForObject<E, DisposeKey>(addObserver: (RxCollectionViewDelegateProxy, ObserverOf<E>) -> DisposeKey, removeObserver: (RxCollectionViewDelegateProxy, DisposeKey) -> Void) -> Observable<E> {
return proxyObservableForObject(self, addObserver, removeObserver)
}
// deprecated
extension UICollectionView {
@availability(*, deprecated=1.7, message="Replaced by `rx_subscribeItemsToWithCellIdentifier`")
public func rx_subscribeItemsWithIdentifierTo<E, Cell where E : AnyObject, Cell : UICollectionViewCell>
(cellIdentifier: String, configureCell: (UICollectionView, NSIndexPath, E, Cell) -> Void)
(source: Observable<[E]>)
-> Disposable {
let l = rx_subscribeItemsToWithCellIdentifier(cellIdentifier) { (i: Int, e: E, cell: Cell) in
return configureCell(self, NSIndexPath(forItem: i, inSection: 0), e, cell)
}
return l(source)
}
private func _dataSourceObservable<E, DisposeKey>(addObserver: (RxCollectionViewDataSourceProxy, ObserverOf<E>) -> DisposeKey,
removeObserver: (RxCollectionViewDataSourceProxy, DisposeKey) -> Void)
-> Observable<E> {
return proxyObservableForObject(self, addObserver, removeObserver)
@availability(*, deprecated=1.7, message="Replaced by `rx_itemSelected`")
public func rx_itemTap() -> Observable<(UICollectionView, Int)> {
return rx_itemSelected
>- map { i in
return (self, i.item)
}
}
@availability(*, deprecated=1.7, message="Replaced by `rx_modelSelected`")
public func rx_elementTap<E>() -> Observable<E> {
return rx_modelSelected()
}
}

View File

@ -37,9 +37,9 @@ extension UIScrollView {
// delegate
// For more detailed explanations, take a look at `DelegateProxyType.swift`
public func rx_setDelegate(delegate: UIScrollViewDelegate, retainDelegate: Bool)
public func rx_setDelegate(delegate: UIScrollViewDelegate)
-> Disposable {
let proxy: RxScrollViewDelegateProxy = proxyForObject(self)
return installDelegate(proxy, delegate, retainDelegate, onProxyForObject: self)
return installDelegate(proxy, delegate, false, onProxyForObject: self)
}
}

View File

@ -24,6 +24,13 @@ extension UITableView {
return proxyForObject(self) as RxTableViewDataSourceProxy
}
public func rx_setDataSource(dataSource: UITableViewDataSource)
-> Disposable {
let proxy: RxTableViewDataSourceProxy = proxyForObject(self)
return installDelegate(proxy, dataSource, false, onProxyForObject: self)
}
// data source
// Registers reactive data source with table view.
@ -43,29 +50,10 @@ extension UITableView {
}
}
// Registers `UITableViewDataSource`.
// For more detailed explanations, take a look at `RxTableViewDataSourceType.swift` and `DelegateProxyType.swift`
public func rx_setDataSource(dataSource: UITableViewDataSource)
-> Disposable {
let proxy: RxTableViewDataSourceProxy = proxyForObject(self)
return installDelegate(proxy, dataSource, false, onProxyForObject: self)
}
// delegate
// For more detailed explanations, take a look at `DelegateProxyType.swift`
public func rx_setDelegate(delegate: UITableViewDelegate)
-> Disposable {
let proxy: RxTableViewDelegateProxy = proxyForObject(self)
return installDelegate(proxy, delegate, false, onProxyForObject: self)
}
// `reloadData` - items subscription methods (it's assumed that there is one section, and it is typed `Void`)
public func rx_subscribeItemsTo<Item>
(cellFactory: (UITableView, NSIndexPath, Item) -> UITableViewCell)
(cellFactory: (UITableView, Int, Item) -> UITableViewCell)
-> Observable<[Item]> -> Disposable {
return { source in
let dataSource = RxTableViewReactiveArrayDataSource<Item>(cellFactory: cellFactory)
@ -78,7 +66,8 @@ extension UITableView {
(cellIdentifier: String, configureCell: (NSIndexPath, Item, Cell) -> Void)
-> Observable<[Item]> -> Disposable {
return { source in
let dataSource = RxTableViewReactiveArrayDataSource<Item> { (tv, indexPath, item) in
let dataSource = RxTableViewReactiveArrayDataSource<Item> { (tv, i, item) in
let indexPath = NSIndexPath(forItem: i, inSection: 0)
let cell = tv.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! Cell
configureCell(indexPath, item, cell)
return cell
@ -92,11 +81,10 @@ extension UITableView {
public var rx_itemSelected: Observable<NSIndexPath> {
return _proxyObservableForObject({ d, o in
return d.addItemSelectedObserver(o)
}, removeObserver: { (delegate, disposeKey) -> Void in
delegate.removeItemSelectedObserver(disposeKey)
})
return rx_delegate.observe("tableView:didSelectRowAtIndexPath:")
>- map { a in
return a[1] as! NSIndexPath
}
}
public var rx_itemInserted: Observable<NSIndexPath> {
@ -105,7 +93,7 @@ extension UITableView {
return UITableViewCellEditingStyle(rawValue: (a[1] as! NSNumber).integerValue) == .Insert
}
>- map { a in
return a[2] as! NSIndexPath
return (a[2] as! NSIndexPath)
}
}
@ -115,36 +103,72 @@ extension UITableView {
return UITableViewCellEditingStyle(rawValue: (a[1] as! NSNumber).integerValue) == .Delete
}
>- map { a in
return a[2] as! NSIndexPath
return (a[2] as! NSIndexPath)
}
}
public var rx_itemMoved: Observable<ItemMovedEvent> {
return rx_dataSource.observe("tableView:moveRowAtIndexPath:toIndexPath:")
>- map { a in
return (a[1] as! NSIndexPath, a[2] as! NSIndexPath)
return ((a[1] as! NSIndexPath), (a[2] as! NSIndexPath))
}
}
// typed events
// This method only works in case one of the `rx_subscribeItemsTo` methods was used.
public func rx_modelSelected<T>() -> Observable<T> {
return rx_itemSelected >- map { indexPath in
let dataSource: RxTableViewReactiveArrayDataSource<T> = castOrFatalError(self.rx_dataSource.getForwardToDelegate())
return rx_itemSelected >- map { ip in
let dataSource: RxTableViewReactiveArrayDataSource<T> = castOrFatalError(self.rx_dataSource.forwardToDelegate(), "This method only works in case one of the `rx_subscribeItemsTo` methods was used.")
return dataSource.modelAtIndex(indexPath.item)!
return dataSource.modelAtIndex(ip.item)!
}
}
// private methods
private func _proxyObservableForObject<E, DisposeKey>(addObserver: (RxTableViewDelegateProxy, ObserverOf<E>) -> DisposeKey, removeObserver: (RxTableViewDelegateProxy, DisposeKey) -> Void) -> Observable<E> {
return proxyObservableForObject(self, addObserver, removeObserver)
}
// deprecated
extension UITableView {
@availability(*, deprecated=1.7, message="Replaced by `rx_subscribeWithReactiveDataSource`")
public func rx_subscribeRowsTo<E where E: AnyObject>
(dataSource: UITableViewDataSource)
(source: Observable<[E]>)
-> Disposable {
return rx_setDataSource(dataSource)
}
private func _createDataSourceObservable<E, DisposeKey>(addObserver: (RxTableViewDataSourceProxy, ObserverOf<E>) -> DisposeKey,
removeObserver: (RxTableViewDataSourceProxy, DisposeKey) -> Void)
-> Observable<E> {
return proxyObservableForObject(self, addObserver, removeObserver)
@availability(*, deprecated=1.7, message="Replaced by `rx_setDataSource`")
public func rx_subscribeRowsTo<E where E : AnyObject>
(cellFactory: (UITableView, NSIndexPath, E) -> UITableViewCell)
(source: Observable<[E]>)
-> Disposable {
let l = rx_subscribeItemsTo { (tv: UITableView, i: Int, e: E) -> UITableViewCell in
return cellFactory(tv, NSIndexPath(forItem: i, inSection: 0), e)
}
return l(source)
}
@availability(*, deprecated=1.7, message="Replaced by `rx_subscribeItemsToWithCellIdentifier`")
public func rx_subscribeRowsToCellWithIdentifier<E, Cell where E : AnyObject, Cell: UITableViewCell>
(cellIdentifier: String, configureCell: (UITableView, NSIndexPath, E, Cell) -> Void)
(source: Observable<[E]>)
-> Disposable {
let l = rx_subscribeItemsToWithCellIdentifier(cellIdentifier) { (ip: NSIndexPath, e: E, c: Cell) -> Void in
configureCell(self, ip, e, c)
}
return l(source)
}
@availability(*, deprecated=1.7, message="Replaced by `rx_itemSelected`")
public func rx_rowTap() -> Observable<(UITableView, Int)> {
return rx_itemSelected
>- map { ip in
return (self, ip.item)
}
}
@availability(*, deprecated=1.7, message="Replaced by `rx_modelSelected`")
public func rx_elementTap<E>() -> Observable<E> {
return rx_modelSelected()
}
}

View File

@ -138,7 +138,20 @@ class PartialUpdatesViewController : ViewController {
updates
>- partialUpdatesCollectionViewOutlet.rx_subscribeWithReactiveDataSource(cvAnimatedDataSource)
>- disposeBag.addDisposable
// touches
partialUpdatesCollectionViewOutlet.rx_itemSelected
>- subscribeNext { [unowned self] i in
println("Let me guess, it's .... It's \(self.generator.sections[i.section].items[i.item]), isn't it? Yeah, I've got it.")
}
>- disposeBag.addDisposable
merge(from([partialUpdatesTableViewOutlet.rx_itemSelected, reloadTableViewOutlet.rx_itemSelected]))
>- subscribeNext { [unowned self] i in
println("I have a feeling it's .... \(self.generator.sections[i.section].items[i.item])?")
}
>- disposeBag.addDisposable
}
override func viewWillDisappear(animated: Bool) {

View File

@ -62,7 +62,7 @@ class TableViewController: ViewController, UITableViewDelegate {
// customization using delegate
// RxTableViewDelegateBridge will forward correct messages
tableView.rx_setDelegate(self, retainDelegate: false)
tableView.rx_setDelegate(self)
>- disposeBag.addDisposable
tableView.rx_itemSelected

View File

@ -24,6 +24,8 @@ class WikipediaSearchViewController: ViewController {
override func viewDidLoad() {
super.viewDidLoad()
let a = Subject<Int>()
sendNext(a, 1)
let resultsTableView = self.searchDisplayController!.searchResultsTableView
let searchBar = self.searchDisplayController!.searchBar

View File

@ -34,6 +34,13 @@ class Subscription<Element> : Disposable {
}
}
@availability(*, deprecated=1.7, message="Replaced by PublishSubject")
public class Subject<Element> : PublishSubject<Element> {
public override init() {
super.init()
}
}
public class PublishSubject<Element> : SubjectType<Element, Element>, Disposable {
typealias ObserverOf = Observer<Element>

View File

@ -73,12 +73,6 @@
C88BB8AE1B07E64B0064D411 /* VariableTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C814CEA21AF5622600E98087 /* VariableTest.swift */; };
C88BB8B01B07E64B0064D411 /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C811086A1AF50E43001C13E4 /* RxCocoa.framework */; };
C88BB8B11B07E64B0064D411 /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C811086B1AF50E43001C13E4 /* RxSwift.framework */; };
C88C78821B3EB0C50061C5AB /* RxTableViewSectionedAnimatedDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = C88C78781B3EB0C50061C5AB /* RxTableViewSectionedAnimatedDataSource.swift */; };
C88C78831B3EB0C50061C5AB /* RxTableViewSectionedDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = C88C78791B3EB0C50061C5AB /* RxTableViewSectionedDataSource.swift */; };
C88C78841B3EB0C50061C5AB /* RxTableViewSectionedReloadDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = C88C787A1B3EB0C50061C5AB /* RxTableViewSectionedReloadDataSource.swift */; };
C88C78881B3EB0C50061C5AB /* SectionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C88C787F1B3EB0C50061C5AB /* SectionModel.swift */; };
C88C78911B3F17C40061C5AB /* SectionedViewType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C88C78901B3F17C40061C5AB /* SectionedViewType.swift */; };
C88C78931B3F17CB0061C5AB /* Changeset.swift in Sources */ = {isa = PBXBuildFile; fileRef = C88C78921B3F17CB0061C5AB /* Changeset.swift */; };
C897EC3B1B10E000009C2CB0 /* BehaviorSubjectTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C897EC3A1B10E000009C2CB0 /* BehaviorSubjectTest.swift */; };
C897EC3C1B10E000009C2CB0 /* BehaviorSubjectTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C897EC3A1B10E000009C2CB0 /* BehaviorSubjectTest.swift */; };
C897EC471B112070009C2CB0 /* Observable+MultipleTest+Zip.tt in Resources */ = {isa = PBXBuildFile; fileRef = C897EC461B112070009C2CB0 /* Observable+MultipleTest+Zip.tt */; };
@ -134,12 +128,6 @@
C814CEA61AF642D600E98087 /* UI+RxTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UI+RxTests.swift"; sourceTree = "<group>"; };
C8633AE41B0A9FF300375D60 /* KVOObservableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KVOObservableTests.swift; sourceTree = "<group>"; };
C88BB8B71B07E64B0064D411 /* RxTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RxTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
C88C78781B3EB0C50061C5AB /* RxTableViewSectionedAnimatedDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTableViewSectionedAnimatedDataSource.swift; sourceTree = "<group>"; };
C88C78791B3EB0C50061C5AB /* RxTableViewSectionedDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTableViewSectionedDataSource.swift; sourceTree = "<group>"; };
C88C787A1B3EB0C50061C5AB /* RxTableViewSectionedReloadDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTableViewSectionedReloadDataSource.swift; sourceTree = "<group>"; };
C88C787F1B3EB0C50061C5AB /* SectionModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionModel.swift; sourceTree = "<group>"; };
C88C78901B3F17C40061C5AB /* SectionedViewType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SectionedViewType.swift; sourceTree = "<group>"; };
C88C78921B3F17CB0061C5AB /* Changeset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Changeset.swift; sourceTree = "<group>"; };
C897EC3A1B10E000009C2CB0 /* BehaviorSubjectTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BehaviorSubjectTest.swift; sourceTree = "<group>"; };
C897EC461B112070009C2CB0 /* Observable+MultipleTest+Zip.tt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "Observable+MultipleTest+Zip.tt"; sourceTree = "<group>"; };
C897EC491B1123DA009C2CB0 /* Observable+MultipleTest+Zip.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Observable+MultipleTest+Zip.swift"; sourceTree = "<group>"; };
@ -177,7 +165,6 @@
children = (
C811086A1AF50E43001C13E4 /* RxCocoa.framework */,
C811086B1AF50E43001C13E4 /* RxSwift.framework */,
C88C78761B3EB0C50061C5AB /* RxDataSourceStarterKit */,
C81108221AF50E11001C13E4 /* Tests */,
C81108211AF50E11001C13E4 /* Products */,
);
@ -296,28 +283,6 @@
path = Tests;
sourceTree = "<group>";
};
C88C78761B3EB0C50061C5AB /* RxDataSourceStarterKit */ = {
isa = PBXGroup;
children = (
C88C78771B3EB0C50061C5AB /* DataSources */,
C88C78921B3F17CB0061C5AB /* Changeset.swift */,
C88C78901B3F17C40061C5AB /* SectionedViewType.swift */,
C88C787F1B3EB0C50061C5AB /* SectionModel.swift */,
);
name = RxDataSourceStarterKit;
path = ../RxDataSourceStarterKit;
sourceTree = "<group>";
};
C88C78771B3EB0C50061C5AB /* DataSources */ = {
isa = PBXGroup;
children = (
C88C78781B3EB0C50061C5AB /* RxTableViewSectionedAnimatedDataSource.swift */,
C88C78791B3EB0C50061C5AB /* RxTableViewSectionedDataSource.swift */,
C88C787A1B3EB0C50061C5AB /* RxTableViewSectionedReloadDataSource.swift */,
);
path = DataSources;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -411,13 +376,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C88C78821B3EB0C50061C5AB /* RxTableViewSectionedAnimatedDataSource.swift in Sources */,
C8B787FA1AF55CDE00206D02 /* Observable+ConcurrencyTest.swift in Sources */,
C81108691AF50E2A001C13E4 /* RxTest.swift in Sources */,
C88C78831B3EB0C50061C5AB /* RxTableViewSectionedDataSource.swift in Sources */,
C81108581AF50E2A001C13E4 /* Result+Equatable.swift in Sources */,
C81108671AF50E2A001C13E4 /* Observable+TimeTest.swift in Sources */,
C88C78931B3F17CB0061C5AB /* Changeset.swift in Sources */,
C81108551AF50E2A001C13E4 /* TestObservable.swift in Sources */,
C8E3812B1B2083C2008CDC33 /* PrimitiveMockObserver.swift in Sources */,
C811085B1AF50E2A001C13E4 /* Subscription.swift in Sources */,
@ -430,7 +392,6 @@
C81108561AF50E2A001C13E4 /* TestObserver.swift in Sources */,
C811085A1AF50E2A001C13E4 /* VirtualTimeSchedulerBase.swift in Sources */,
C811085F1AF50E2A001C13E4 /* DisposableTest.swift in Sources */,
C88C78881B3EB0C50061C5AB /* SectionModel.swift in Sources */,
C8E381281B207D03008CDC33 /* PrimitiveHotObservable.swift in Sources */,
C81108661AF50E2A001C13E4 /* Observable+StandardSequenceOperatorsTest.swift in Sources */,
C81108641AF50E2A001C13E4 /* Observable+MultipleTest.swift in Sources */,
@ -448,9 +409,7 @@
C81108591AF50E2A001C13E4 /* TestScheduler.swift in Sources */,
C81108601AF50E2A001C13E4 /* Observable+AggregateTest.swift in Sources */,
C81108521AF50E2A001C13E4 /* MockObserver.swift in Sources */,
C88C78841B3EB0C50061C5AB /* RxTableViewSectionedReloadDataSource.swift in Sources */,
C8633AE51B0A9FF300375D60 /* KVOObservableTests.swift in Sources */,
C88C78911B3F17C40061C5AB /* SectionedViewType.swift in Sources */,
C811085C1AF50E2A001C13E4 /* TestExtensions.swift in Sources */,
C811085D1AF50E2A001C13E4 /* AssumptionsTest.swift in Sources */,
C81108501AF50E2A001C13E4 /* ConnectableObservable.swift in Sources */,