forked from ReactiveX/RxSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RxPickerViewDataSourceProxy, RxPickerViewDataSourceType and UIPic…
…kerView reactive extensions
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// RxPickerViewDataSourceType.swift | ||
// RxCocoa | ||
// | ||
// Created by Sergey Shulga on 05/07/2017. | ||
// Copyright © 2017 Krunoslav Zaher. All rights reserved. | ||
// | ||
|
||
#if os(iOS) || os(tvOS) | ||
|
||
import UIKit | ||
#if !RX_NO_MODULE | ||
import RxSwift | ||
#endif | ||
|
||
/// Marks data source as `UIPickerView` reactive data source enabling it to be used with one of the `bindTo` methods. | ||
public protocol RxPickerViewDataSourceType { | ||
/// Type of elements that can be bound to picker view. | ||
associatedtype Element | ||
|
||
/// New observable sequence event observed. | ||
/// | ||
/// - parameter pickerView: Bound picker view. | ||
/// - parameter observedEvent: Event | ||
func pickerView(_ pickerView: UIPickerView, observedEvent: Event<Element>) | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// RxPickerViewDataSourceProxy.swift | ||
// RxCocoa | ||
// | ||
// Created by Sergey Shulga on 05/07/2017. | ||
// Copyright © 2017 Krunoslav Zaher. All rights reserved. | ||
// | ||
|
||
#if os(iOS) || os(tvOS) | ||
|
||
import UIKit | ||
#if !RX_NO_MODULE | ||
import RxSwift | ||
#endif | ||
|
||
let pickerViewDataSourceNotSet = PickerViewDataSourceNotSet() | ||
|
||
final class PickerViewDataSourceNotSet: NSObject, UIPickerViewDataSource { | ||
func numberOfComponents(in pickerView: UIPickerView) -> Int { | ||
rxAbstractMethod() | ||
} | ||
|
||
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | ||
rxAbstractMethod() | ||
} | ||
} | ||
|
||
public class RxPickerViewDataSourceProxy | ||
: DelegateProxy | ||
, UIPickerViewDataSource | ||
, DelegateProxyType { | ||
public weak fileprivate(set) var pickerView: UIPickerView? | ||
private weak var _requiredMethodsDataSource: UIPickerViewDataSource? = pickerViewDataSourceNotSet | ||
|
||
public required init(parentObject: AnyObject) { | ||
self.pickerView = castOrFatalError(parentObject) | ||
super.init(parentObject: parentObject) | ||
} | ||
|
||
|
||
// MARK: UIPickerViewDataSource | ||
|
||
public func numberOfComponents(in pickerView: UIPickerView) -> Int { | ||
return (_requiredMethodsDataSource ?? pickerViewDataSourceNotSet).numberOfComponents(in: pickerView) | ||
} | ||
|
||
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | ||
return (_requiredMethodsDataSource ?? pickerViewDataSourceNotSet).pickerView(pickerView, numberOfRowsInComponent: component) | ||
} | ||
|
||
// MARK: proxy | ||
|
||
/// For more information take a look at `DelegateProxyType`. | ||
public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { | ||
let pickerView: UIPickerView = castOrFatalError(object) | ||
return pickerView.createRxDataSourceProxy() | ||
} | ||
|
||
/// For more information take a look at `DelegateProxyType`. | ||
public override class func delegateAssociatedObjectTag() -> UnsafeRawPointer { | ||
return dataSourceAssociatedTag | ||
} | ||
|
||
/// For more information take a look at `DelegateProxyType`. | ||
public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { | ||
let pickerView: UIPickerView = castOrFatalError(object) | ||
pickerView.dataSource = castOptionalOrFatalError(delegate) | ||
} | ||
|
||
/// For more information take a look at `DelegateProxyType`. | ||
public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { | ||
let tableView: UITableView = castOrFatalError(object) | ||
return tableView.dataSource | ||
} | ||
|
||
/// For more information take a look at `DelegateProxyType`. | ||
public override func setForwardToDelegate(_ forwardToDelegate: AnyObject?, retainDelegate: Bool) { | ||
let requiredMethodsDataSource: UIPickerViewDataSource? = castOptionalOrFatalError(forwardToDelegate) | ||
_requiredMethodsDataSource = requiredMethodsDataSource ?? pickerViewDataSourceNotSet | ||
super.setForwardToDelegate(forwardToDelegate, retainDelegate: retainDelegate) | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters