|
53 | 53 | return RxPickerViewDelegateProxy.installForwardDelegate(delegate, retainDelegate: false, onProxyForObject: self.base)
|
54 | 54 | }
|
55 | 55 |
|
| 56 | + /** |
| 57 | + Reactive wrapper for `dataSource`. |
| 58 | + |
| 59 | + For more information take a look at `DelegateProxyType` protocol documentation. |
| 60 | + */ |
| 61 | + public var dataSource: DelegateProxy { |
| 62 | + return RxPickerViewDataSourceProxy.proxyForObject(base) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + Reactive wrapper for `delegate` message `pickerView:didSelectRow:inComponent:`. |
| 67 | + */ |
56 | 68 | public var itemSelected: ControlEvent<(Int, Int)> {
|
57 | 69 | let source = delegate
|
58 | 70 | .methodInvoked(#selector(UIPickerViewDelegate.pickerView(_:didSelectRow:inComponent:)))
|
|
62 | 74 | return ControlEvent(events: source)
|
63 | 75 | }
|
64 | 76 |
|
| 77 | + /** |
| 78 | + Reactive wrapper for `delegate` message `pickerView:didSelectRow:inComponent:`. |
| 79 | + |
| 80 | + It can be only used when one of the `rx.itemTitles, rx.itemAttributedTitles, items(_ source: O)` methods is used to bind observable sequence, |
| 81 | + or any other data source conforming to a `ViewDataSourceType` protocol. |
| 82 | + |
| 83 | + ``` |
| 84 | + pickerView.rx.modelSelected(MyModel.self) |
| 85 | + .map { ... |
| 86 | + ``` |
| 87 | + - parameter modelType: Type of a Model which bound to the dataSource |
| 88 | + */ |
| 89 | + public func modelSelected<T>(_ modelType: T.Type) -> ControlEvent<T> { |
| 90 | + let source = itemSelected.flatMap { [weak view = self.base as UIPickerView] (row, _) -> Observable<T> in |
| 91 | + guard let view = view else { |
| 92 | + return Observable.empty() |
| 93 | + } |
| 94 | + return Observable.just(try view.rx.model(at: row)) |
| 95 | + } |
| 96 | + |
| 97 | + return ControlEvent(events: source) |
| 98 | + } |
| 99 | + |
65 | 100 | /**
|
66 | 101 | Binds sequences of elements to picker view rows.
|
67 | 102 |
|
|
191 | 226 | return Disposables.create(delegateSubscription, dataSourceSubscription)
|
192 | 227 | }
|
193 | 228 | }
|
| 229 | + |
| 230 | + /** |
| 231 | + Synchronous helper method for retrieving a model at indexPath through a reactive data source. |
| 232 | + */ |
| 233 | + public func model<T>(at index: Int) throws -> T { |
| 234 | + let dataSource: ViewDataSourceType = castOrFatalError(self.dataSource.forwardToDelegate(), message: "This method only works in case one of the `rx.itemTitles, rx.itemAttributedTitles, items(_ source: O)` methods was used.") |
| 235 | + |
| 236 | + return castOrFatalError(try dataSource.model(at: index)) |
| 237 | + } |
194 | 238 | }
|
195 | 239 |
|
196 | 240 | #endif
|
0 commit comments