Skip to content

Commit 26867f8

Browse files
committed
Make closures private properties required to init
1 parent 4fc71f7 commit 26867f8

File tree

2 files changed

+97
-124
lines changed

2 files changed

+97
-124
lines changed

Example/Example5_UIPickerView.swift

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,38 @@ final class ReactivePickerViewControllerExample: UIViewController {
1919

2020
let disposeBag = DisposeBag()
2121

22-
private let stringPickerAdapter = RxPickerViewStringAdapter<[String]>(components: [])
23-
private let attributedStringPickerAdapter = RxPickerViewAttributedStringAdapter<[String]>(components: [])
24-
private let viewPickerAdapter = RxPickerViewViewAdapter<[String]>(components: [])
22+
private let stringPickerAdapter = RxPickerViewStringAdapter<[String]>(components: [],
23+
numberOfComponents: { _ in 1 },
24+
numberOfRowsInComponent: { (_, _, items, _) -> Int in
25+
return items.count
26+
},
27+
titleForRow: { (_, _, items, row, _) -> String? in
28+
return items[row]
29+
})
30+
private let attributedStringPickerAdapter = RxPickerViewAttributedStringAdapter<[String]>(components: [],
31+
numberOfComponents: { _ in 1 },
32+
numberOfRowsInComponent: { (_, _, items, _) -> Int in
33+
return items.count
34+
}) { (_, _, items, row, _) -> NSAttributedString? in
35+
return NSAttributedString(string: items[row],
36+
attributes: [
37+
NSForegroundColorAttributeName: UIColor.purple,
38+
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue,
39+
NSTextEffectAttributeName: NSTextEffectLetterpressStyle
40+
])
41+
}
42+
private let viewPickerAdapter = RxPickerViewViewAdapter<[String]>(components: [],
43+
numberOfComponents: { _ in 1 },
44+
numberOfRowsInComponent: { (_, _, items, _) -> Int in
45+
return items.count
46+
}) { (_, _, _, row, _, view) -> UIView in
47+
let componentView = view ?? UIView()
48+
componentView.backgroundColor = row % 2 == 0 ? UIColor.red : UIColor.blue
49+
return componentView
50+
}
2551

2652
override func viewDidLoad() {
2753
super.viewDidLoad()
28-
setupStringPickerAdapter()
29-
setupAttributedStringPickerAdapter()
30-
setupViewPickerAdapter()
3154

3255
Observable.just(["One", "Two", "Tree"])
3356
.bind(to: firstPickerView.rx.items(adapter: stringPickerAdapter))
@@ -41,54 +64,4 @@ final class ReactivePickerViewControllerExample: UIViewController {
4164
.bind(to: thirdPickerView.rx.items(adapter: viewPickerAdapter))
4265
.disposed(by: disposeBag)
4366
}
44-
45-
func setupStringPickerAdapter() {
46-
stringPickerAdapter.numberOfComponentsProvider = { _ in
47-
return 1
48-
}
49-
50-
stringPickerAdapter.numberOfRowsInComponentProvider = { _, _, components, _ in
51-
return components.count
52-
}
53-
54-
stringPickerAdapter.titleForRowProvider = { _, _, components, row, _ in
55-
return components[row]
56-
}
57-
}
58-
59-
func setupAttributedStringPickerAdapter() {
60-
attributedStringPickerAdapter.numberOfComponentsProvider = { _ in
61-
return 1
62-
}
63-
64-
attributedStringPickerAdapter.numberOfRowsInComponentProvider = { _, _, components, _ in
65-
return components.count
66-
}
67-
68-
attributedStringPickerAdapter.attributedTitleForRowProvider = {_, _, components, row, _ in
69-
let string = components[row]
70-
return NSAttributedString(string: string,
71-
attributes: [
72-
NSForegroundColorAttributeName: UIColor.purple,
73-
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleDouble.rawValue,
74-
NSTextEffectAttributeName: NSTextEffectLetterpressStyle
75-
])
76-
}
77-
}
78-
79-
func setupViewPickerAdapter() {
80-
viewPickerAdapter.numberOfComponentsProvider = { _ in
81-
return 1
82-
}
83-
84-
viewPickerAdapter.numberOfRowsInComponentProvider = { _, _, components, _ in
85-
return components.count
86-
}
87-
88-
viewPickerAdapter.viewForRowProvider = { _, _, components, row, _, view in
89-
let componentView = view ?? UIView()
90-
componentView.backgroundColor = row % 2 == 0 ? UIColor.red : UIColor.blue
91-
return componentView
92-
}
93-
}
9467
}

Sources/DataSources+Rx/RxPickerViewAdapter.swift

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,101 +13,101 @@ import UIKit
1313
import RxCocoa
1414
#endif
1515

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-
5616
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?
5818

59-
public var titleForRowProvider: TitleForRowProvider! = nil
19+
private let titleForRow: TitleForRow
6020

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)
6329
}
6430

6531
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)
6733
}
6834
}
6935

7036
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?
7238

73-
public var attributedTitleForRowProvider: AttributedTitleForRowProvider! = nil
39+
private let attributedTitleForRow: AttributedTitleForRow
7440

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)
7749
}
7850

7951
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)
8153
}
8254
}
8355

8456
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
8658

87-
public var viewForRowProvider: ViewForRowProvider!
59+
private let viewForRow: ViewForRow
8860

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)
9169
}
9270

9371
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)
9573
}
9674
}
9775

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)
111103
}
112104
}
113105

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

Comments
 (0)