@@ -13,101 +13,101 @@ import UIKit
13
13
import RxCocoa
14
14
#endif
15
15
16
- public protocol RxPickerViewDataSourceType {
17
- associatedtype Element
18
-
19
- func pickerView( _ pickerView: UIPickerView , observedEvent: Event < Element > )
20
- }
21
-
22
- open class RxPickerViewDataSource < T> : NSObject , UIPickerViewDataSource {
23
- public typealias NumberOfComponentsProvider = ( RxPickerViewDataSource , UIPickerView , T ) -> Int
24
- public typealias NumberOfRowsInComponentProvider = ( RxPickerViewDataSource , UIPickerView , T , Int ) -> Int
25
-
26
- fileprivate var components : T
27
-
28
- init ( components: T ) {
29
- self . components = components
30
- super. init ( )
31
- }
32
-
33
- public var numberOfComponentsProvider : NumberOfComponentsProvider !
34
- public var numberOfRowsInComponentProvider : NumberOfRowsInComponentProvider !
35
-
36
- //MARK: UIPickerViewDataSource
37
-
38
- public func numberOfComponents( in pickerView: UIPickerView ) -> Int {
39
- return numberOfComponentsProvider ( self , pickerView, components)
40
- }
41
-
42
- public func pickerView( _ pickerView: UIPickerView , numberOfRowsInComponent component: Int ) -> Int {
43
- return numberOfRowsInComponentProvider ( self , pickerView, components, component)
44
- }
45
- }
46
-
47
- extension RxPickerViewDataSource : RxPickerViewDataSourceType {
48
- public func pickerView( _ pickerView: UIPickerView , observedEvent: Event < T > ) {
49
- UIBindingObserver ( UIElement: self ) { ( dataSource, components) in
50
- dataSource. components = components
51
- pickerView. reloadAllComponents ( )
52
- } . on ( observedEvent)
53
- }
54
- }
55
-
56
16
open class RxPickerViewStringAdapter < T> : RxPickerViewDataSource < T > , UIPickerViewDelegate {
57
- public typealias TitleForRowProvider = ( RxPickerViewStringAdapter < T > , UIPickerView , T , Int , Int ) -> String ?
17
+ public typealias TitleForRow = ( RxPickerViewStringAdapter < T > , UIPickerView , T , Int , Int ) -> String ?
58
18
59
- public var titleForRowProvider : TitleForRowProvider ! = nil
19
+ private let titleForRow : TitleForRow
60
20
61
- public override init ( components: T ) {
62
- super. init ( components: components)
21
+ public init ( components: T ,
22
+ numberOfComponents: @escaping NumberOfComponents ,
23
+ numberOfRowsInComponent: @escaping NumberOfRowsInComponent ,
24
+ titleForRow: @escaping TitleForRow ) {
25
+ self . titleForRow = titleForRow
26
+ super. init ( components: components,
27
+ numberOfComponents: numberOfComponents,
28
+ numberOfRowsInComponent: numberOfRowsInComponent)
63
29
}
64
30
65
31
open func pickerView( _ pickerView: UIPickerView , titleForRow row: Int , forComponent component: Int ) -> String ? {
66
- return titleForRowProvider ( self , pickerView, components, row, component)
32
+ return titleForRow ( self , pickerView, components, row, component)
67
33
}
68
34
}
69
35
70
36
open class RxPickerViewAttributedStringAdapter < T> : RxPickerViewDataSource < T > , UIPickerViewDelegate {
71
- public typealias AttributedTitleForRowProvider = ( RxPickerViewAttributedStringAdapter < T > , UIPickerView , T , Int , Int ) -> NSAttributedString ?
37
+ public typealias AttributedTitleForRow = ( RxPickerViewAttributedStringAdapter < T > , UIPickerView , T , Int , Int ) -> NSAttributedString ?
72
38
73
- public var attributedTitleForRowProvider : AttributedTitleForRowProvider ! = nil
39
+ private let attributedTitleForRow : AttributedTitleForRow
74
40
75
- public override init ( components: T ) {
76
- super. init ( components: components)
41
+ public init ( components: T ,
42
+ numberOfComponents: @escaping NumberOfComponents ,
43
+ numberOfRowsInComponent: @escaping NumberOfRowsInComponent ,
44
+ attributedTitleForRow: @escaping AttributedTitleForRow ) {
45
+ self . attributedTitleForRow = attributedTitleForRow
46
+ super. init ( components: components,
47
+ numberOfComponents: numberOfComponents,
48
+ numberOfRowsInComponent: numberOfRowsInComponent)
77
49
}
78
50
79
51
open func pickerView( _ pickerView: UIPickerView , attributedTitleForRow row: Int , forComponent component: Int ) -> NSAttributedString ? {
80
- return attributedTitleForRowProvider ( self , pickerView, components, row, component)
52
+ return attributedTitleForRow ( self , pickerView, components, row, component)
81
53
}
82
54
}
83
55
84
56
open class RxPickerViewViewAdapter < T> : RxPickerViewDataSource < T > , UIPickerViewDelegate {
85
- public typealias ViewForRowProvider = ( RxPickerViewViewAdapter < T > , UIPickerView , T , Int , Int , UIView ? ) -> UIView
57
+ public typealias ViewForRow = ( RxPickerViewViewAdapter < T > , UIPickerView , T , Int , Int , UIView ? ) -> UIView
86
58
87
- public var viewForRowProvider : ViewForRowProvider !
59
+ private let viewForRow : ViewForRow
88
60
89
- public override init ( components: T ) {
90
- super. init ( components: components)
61
+ public init ( components: T ,
62
+ numberOfComponents: @escaping NumberOfComponents ,
63
+ numberOfRowsInComponent: @escaping NumberOfRowsInComponent ,
64
+ viewForRow: @escaping ViewForRow ) {
65
+ self . viewForRow = viewForRow
66
+ super. init ( components: components,
67
+ numberOfComponents: numberOfComponents,
68
+ numberOfRowsInComponent: numberOfRowsInComponent)
91
69
}
92
70
93
71
open func pickerView( _ pickerView: UIPickerView , viewForRow row: Int , forComponent component: Int , reusing view: UIView ? ) -> UIView {
94
- return viewForRowProvider ( self , pickerView, components, row, component, view)
72
+ return viewForRow ( self , pickerView, components, row, component, view)
95
73
}
96
74
}
97
75
98
- extension Reactive where Base: UIPickerView {
99
- public func items< O: ObservableType ,
100
- Adapter: RxPickerViewDataSourceType & UIPickerViewDataSource & UIPickerViewDelegate > ( adapter: Adapter )
101
- -> ( _ source: O )
102
- -> Disposable where O. E == Adapter . Element {
103
- return { source in
104
- self . base. delegate = adapter
105
- self . base. dataSource = adapter
106
- return source. subscribe { [ weak pickerView = self . base] ( event) in
107
- guard let pickerView = pickerView else { return }
108
- adapter. pickerView ( pickerView, observedEvent: event)
109
- }
110
- }
76
+
77
+ open class RxPickerViewDataSource < T> : NSObject , UIPickerViewDataSource {
78
+ public typealias NumberOfComponents = ( RxPickerViewDataSource , UIPickerView , T ) -> Int
79
+ public typealias NumberOfRowsInComponent = ( RxPickerViewDataSource , UIPickerView , T , Int ) -> Int
80
+
81
+ fileprivate var components : T
82
+
83
+ init ( components: T ,
84
+ numberOfComponents: @escaping NumberOfComponents ,
85
+ numberOfRowsInComponent: @escaping NumberOfRowsInComponent ) {
86
+ self . components = components
87
+ self . numberOfComponents = numberOfComponents
88
+ self . numberOfRowsInComponent = numberOfRowsInComponent
89
+ super. init ( )
90
+ }
91
+
92
+ private let numberOfComponents : NumberOfComponents
93
+ private let numberOfRowsInComponent : NumberOfRowsInComponent
94
+
95
+ //MARK: UIPickerViewDataSource
96
+
97
+ public func numberOfComponents( in pickerView: UIPickerView ) -> Int {
98
+ return numberOfComponents ( self , pickerView, components)
99
+ }
100
+
101
+ public func pickerView( _ pickerView: UIPickerView , numberOfRowsInComponent component: Int ) -> Int {
102
+ return numberOfRowsInComponent ( self , pickerView, components, component)
111
103
}
112
104
}
113
105
106
+ extension RxPickerViewDataSource : RxPickerViewDataSourceType {
107
+ public func pickerView( _ pickerView: UIPickerView , observedEvent: Event < T > ) {
108
+ UIBindingObserver ( UIElement: self ) { ( dataSource, components) in
109
+ dataSource. components = components
110
+ pickerView. reloadAllComponents ( )
111
+ } . on ( observedEvent)
112
+ }
113
+ }
0 commit comments