-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
UIPickerView+Rx.swift
224 lines (184 loc) · 9.13 KB
/
UIPickerView+Rx.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// UIPickerView+Rx.swift
// RxCocoa
//
// Created by Segii Shulga on 5/12/16.
// Copyright © 2016 Krunoslav Zaher. All rights reserved.
//
#if os(iOS) || os(visionOS)
import RxSwift
import UIKit
extension Reactive where Base: UIPickerView {
/// Reactive wrapper for `delegate`.
/// For more information take a look at `DelegateProxyType` protocol documentation.
public var delegate: DelegateProxy<UIPickerView, UIPickerViewDelegate> {
return RxPickerViewDelegateProxy.proxy(for: base)
}
/// Installs delegate as forwarding delegate on `delegate`.
/// Delegate won't be retained.
///
/// It enables using normal delegate mechanism with reactive delegate mechanism.
///
/// - parameter delegate: Delegate object.
/// - returns: Disposable object that can be used to unbind the delegate.
public func setDelegate(_ delegate: UIPickerViewDelegate)
-> Disposable {
return RxPickerViewDelegateProxy.installForwardDelegate(delegate, retainDelegate: false, onProxyForObject: self.base)
}
/**
Reactive wrapper for `dataSource`.
For more information take a look at `DelegateProxyType` protocol documentation.
*/
public var dataSource: DelegateProxy<UIPickerView, UIPickerViewDataSource> {
return RxPickerViewDataSourceProxy.proxy(for: base)
}
/**
Reactive wrapper for `delegate` message `pickerView:didSelectRow:inComponent:`.
*/
public var itemSelected: ControlEvent<(row: Int, component: Int)> {
let source = delegate
.methodInvoked(#selector(UIPickerViewDelegate.pickerView(_:didSelectRow:inComponent:)))
.map {
return (row: try castOrThrow(Int.self, $0[1]), component: try castOrThrow(Int.self, $0[2]))
}
return ControlEvent(events: source)
}
/**
Reactive wrapper for `delegate` message `pickerView:didSelectRow:inComponent:`.
It can be only used when one of the `rx.itemTitles, rx.itemAttributedTitles, items(_ source: O)` methods is used to bind observable sequence,
or any other data source conforming to a `ViewDataSourceType` protocol.
```
pickerView.rx.modelSelected(MyModel.self)
.map { ...
```
- parameter modelType: Type of a Model which bound to the dataSource
*/
public func modelSelected<T>(_ modelType: T.Type) -> ControlEvent<[T]> {
let source = itemSelected.flatMap { [weak view = self.base as UIPickerView] _, component -> Observable<[T]> in
guard let view = view else {
return Observable.empty()
}
let model: [T] = try (0 ..< view.numberOfComponents).map { component in
let row = view.selectedRow(inComponent: component)
return try view.rx.model(at: IndexPath(row: row, section: component))
}
return Observable.just(model)
}
return ControlEvent(events: source)
}
/**
Binds sequences of elements to picker view rows.
- parameter source: Observable sequence of items.
- parameter titleForRow: Transform between sequence elements and row titles.
- returns: Disposable object that can be used to unbind.
Example:
let items = Observable.just([
"First Item",
"Second Item",
"Third Item"
])
items
.bind(to: pickerView.rx.itemTitles) { (row, element) in
return element
}
.disposed(by: disposeBag)
*/
public func itemTitles<Sequence: Swift.Sequence, Source: ObservableType>
(_ source: Source)
-> (_ titleForRow: @escaping (Int, Sequence.Element) -> String?)
-> Disposable where Source.Element == Sequence {
return { titleForRow in
let adapter = RxStringPickerViewAdapter<Sequence>(titleForRow: titleForRow)
return self.items(adapter: adapter)(source)
}
}
/**
Binds sequences of elements to picker view rows.
- parameter source: Observable sequence of items.
- parameter attributedTitleForRow: Transform between sequence elements and row attributed titles.
- returns: Disposable object that can be used to unbind.
Example:
let items = Observable.just([
"First Item",
"Second Item",
"Third Item"
])
items
.bind(to: pickerView.rx.itemAttributedTitles) { (row, element) in
return NSAttributedString(string: element)
}
.disposed(by: disposeBag)
*/
public func itemAttributedTitles<Sequence: Swift.Sequence, Source: ObservableType>
(_ source: Source)
-> (_ attributedTitleForRow: @escaping (Int, Sequence.Element) -> NSAttributedString?)
-> Disposable where Source.Element == Sequence {
return { attributedTitleForRow in
let adapter = RxAttributedStringPickerViewAdapter<Sequence>(attributedTitleForRow: attributedTitleForRow)
return self.items(adapter: adapter)(source)
}
}
/**
Binds sequences of elements to picker view rows.
- parameter source: Observable sequence of items.
- parameter viewForRow: Transform between sequence elements and row views.
- returns: Disposable object that can be used to unbind.
Example:
let items = Observable.just([
"First Item",
"Second Item",
"Third Item"
])
items
.bind(to: pickerView.rx.items) { (row, element, view) in
guard let myView = view as? MyView else {
let view = MyView()
view.configure(with: element)
return view
}
myView.configure(with: element)
return myView
}
.disposed(by: disposeBag)
*/
public func items<Sequence: Swift.Sequence, Source: ObservableType>
(_ source: Source)
-> (_ viewForRow: @escaping (Int, Sequence.Element, UIView?) -> UIView)
-> Disposable where Source.Element == Sequence {
return { viewForRow in
let adapter = RxPickerViewAdapter<Sequence>(viewForRow: viewForRow)
return self.items(adapter: adapter)(source)
}
}
/**
Binds sequences of elements to picker view rows using a custom reactive adapter used to perform the transformation.
This method will retain the adapter for as long as the subscription isn't disposed (result `Disposable`
being disposed).
In case `source` observable sequence terminates successfully, the adapter will present latest element
until the subscription isn't disposed.
- parameter adapter: Adapter used to transform elements to picker components.
- parameter source: Observable sequence of items.
- returns: Disposable object that can be used to unbind.
*/
public func items<Source: ObservableType,
Adapter: RxPickerViewDataSourceType & UIPickerViewDataSource & UIPickerViewDelegate>(adapter: Adapter)
-> (_ source: Source)
-> Disposable where Source.Element == Adapter.Element {
return { source in
let delegateSubscription = self.setDelegate(adapter)
let dataSourceSubscription = source.subscribeProxyDataSource(ofObject: self.base, dataSource: adapter, retainDataSource: true, binding: { [weak pickerView = self.base] (_: RxPickerViewDataSourceProxy, event) in
guard let pickerView = pickerView else { return }
adapter.pickerView(pickerView, observedEvent: event)
})
return Disposables.create(delegateSubscription, dataSourceSubscription)
}
}
/**
Synchronous helper method for retrieving a model at indexPath through a reactive data source.
*/
public func model<T>(at indexPath: IndexPath) throws -> T {
let dataSource: SectionedViewDataSourceType = castOrFatalError(self.dataSource.forwardToDelegate(), message: "This method only works in case one of the `rx.itemTitles, rx.itemAttributedTitles, items(_ source: O)` methods was used.")
return castOrFatalError(try dataSource.model(at: indexPath))
}
}
#endif