Skip to content

Commit e70ad0e

Browse files
committed
set delegate using protocol and default implementation.
1 parent a0bf386 commit e70ad0e

15 files changed

+92
-145
lines changed

RxCocoa/Common/DelegateProxyType.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,36 @@ extension DelegateProxyType
242242
}
243243
}
244244

245+
public protocol HasDelegate: AnyObject {
246+
associatedtype Delegate: AnyObject
247+
var delegate: Delegate? { get set }
248+
}
249+
250+
extension DelegateProxyType where ParentObject: HasDelegate, Self.Delegate == ParentObject.Delegate {
251+
public static func currentDelegate(for object: ParentObject) -> Delegate? {
252+
return object.delegate
253+
}
254+
255+
public static func setCurrentDelegate(_ delegate: Delegate?, to object: ParentObject) {
256+
object.delegate = delegate
257+
}
258+
}
259+
260+
public protocol HasDataSource: AnyObject {
261+
associatedtype DataSource: AnyObject
262+
var dataSource: DataSource? { get set }
263+
}
264+
265+
extension DelegateProxyType where ParentObject: HasDataSource, Self.Delegate == ParentObject.DataSource {
266+
public static func currentDelegate(for object: ParentObject) -> Delegate? {
267+
return object.dataSource
268+
}
269+
270+
public static func setCurrentDelegate(_ delegate: Delegate?, to object: ParentObject) {
271+
object.dataSource = delegate
272+
}
273+
}
274+
245275
#if os(iOS) || os(tvOS)
246276
import UIKit
247277

RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import UIKit
1313
import RxSwift
1414
#endif
1515

16+
extension UICollectionView: HasDataSource {
17+
public typealias DataSource = UICollectionViewDataSource
18+
}
19+
1620
let collectionViewDataSourceNotSet = CollectionViewDataSourceNotSet()
1721

1822
final class CollectionViewDataSourceNotSet
@@ -64,18 +68,6 @@ open class RxCollectionViewDataSourceProxy
6468
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
6569
return (_requiredMethodsDataSource ?? collectionViewDataSourceNotSet).collectionView(collectionView, cellForItemAt: indexPath)
6670
}
67-
68-
// MARK: proxy
69-
70-
/// For more information take a look at `DelegateProxyType`.
71-
open class func setCurrentDelegate(_ delegate: UICollectionViewDataSource?, to object: ParentObject) {
72-
object.dataSource = delegate
73-
}
74-
75-
/// For more information take a look at `DelegateProxyType`.
76-
open class func currentDelegate(for object: ParentObject) -> UICollectionViewDataSource? {
77-
return object.dataSource
78-
}
7971

8072
/// For more information take a look at `DelegateProxyType`.
8173
open override func setForwardToDelegate(_ forwardToDelegate: UICollectionViewDataSource?, retainDelegate: Bool) {

RxCocoa/iOS/Proxies/RxNavigationControllerDelegateProxy.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import RxSwift
1414
#endif
1515

16+
extension UINavigationController: HasDelegate {
17+
public typealias Delegate = UINavigationControllerDelegate
18+
}
19+
1620
/// For more information take a look at `DelegateProxyType`.
1721
open class RxNavigationControllerDelegateProxy
1822
: DelegateProxy<UINavigationController, UINavigationControllerDelegate>
@@ -32,15 +36,5 @@
3236
public static func registerKnownImplementations() {
3337
self.register { RxNavigationControllerDelegateProxy(parentObject: $0) }
3438
}
35-
36-
/// For more information take a look at `DelegateProxyType`.
37-
open class func currentDelegate(for object: ParentObject) -> UINavigationControllerDelegate? {
38-
return object.delegate
39-
}
40-
41-
/// For more information take a look at `DelegateProxyType`.
42-
open class func setCurrentDelegate(_ delegate: UINavigationControllerDelegate?, to object: ParentObject) {
43-
object.delegate = delegate
44-
}
4539
}
4640
#endif

RxCocoa/iOS/Proxies/RxPickerViewDataSourceProxy.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import RxSwift
1414
#endif
1515

16+
extension UIPickerView: HasDataSource {
17+
public typealias DataSource = UIPickerViewDataSource
18+
}
19+
1620
fileprivate let pickerViewDataSourceNotSet = PickerViewDataSourceNotSet()
1721

1822
final fileprivate class PickerViewDataSourceNotSet: NSObject, UIPickerViewDataSource {
@@ -59,18 +63,6 @@ public class RxPickerViewDataSourceProxy
5963
return (_requiredMethodsDataSource ?? pickerViewDataSourceNotSet).pickerView(pickerView, numberOfRowsInComponent: component)
6064
}
6165

62-
// MARK: proxy
63-
64-
/// For more information take a look at `DelegateProxyType`.
65-
public class func setCurrentDelegate(_ delegate: UIPickerViewDataSource?, to object: UIPickerView) {
66-
object.dataSource = delegate
67-
}
68-
69-
/// For more information take a look at `DelegateProxyType`.
70-
public class func currentDelegate(for object: UIPickerView) -> UIPickerViewDataSource? {
71-
return object.dataSource
72-
}
73-
7466
/// For more information take a look at `DelegateProxyType`.
7567
public override func setForwardToDelegate(_ forwardToDelegate: UIPickerViewDataSource?, retainDelegate: Bool) {
7668
_requiredMethodsDataSource = forwardToDelegate ?? pickerViewDataSourceNotSet

RxCocoa/iOS/Proxies/RxPickerViewDelegateProxy.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#endif
1414
import UIKit
1515

16+
extension UIPickerView: HasDelegate {
17+
public typealias Delegate = UIPickerViewDelegate
18+
}
19+
1620
open class RxPickerViewDelegateProxy
1721
: DelegateProxy<UIPickerView, UIPickerViewDelegate>
1822
, DelegateProxyType
@@ -31,15 +35,5 @@
3135
public static func registerKnownImplementations() {
3236
self.register { RxPickerViewDelegateProxy(parentObject: $0) }
3337
}
34-
35-
/// For more information take a look at `DelegateProxyType`.
36-
open class func setCurrentDelegate(_ delegate: UIPickerViewDelegate?, to object: ParentObject) {
37-
object.delegate = delegate
38-
}
39-
40-
/// For more information take a look at `DelegateProxyType`.
41-
open class func currentDelegate(for object: ParentObject) -> UIPickerViewDelegate? {
42-
return object.delegate
43-
}
4438
}
4539
#endif

RxCocoa/iOS/Proxies/RxScrollViewDelegateProxy.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
import RxSwift
1313
#endif
1414
import UIKit
15+
16+
extension UIScrollView: HasDelegate {
17+
public typealias Delegate = UIScrollViewDelegate
18+
}
1519

1620
/// For more information take a look at `DelegateProxyType`.
1721
open class RxScrollViewDelegateProxy
@@ -76,18 +80,6 @@ open class RxScrollViewDelegateProxy
7680
self._forwardToDelegate?.scrollViewDidScroll?(scrollView)
7781
}
7882

79-
// MARK: delegate proxy
80-
81-
/// For more information take a look at `DelegateProxyType`.
82-
open class func setCurrentDelegate(_ delegate: UIScrollViewDelegate?, to object: ParentObject) {
83-
object.delegate = delegate
84-
}
85-
86-
/// For more information take a look at `DelegateProxyType`.
87-
open class func currentDelegate(for object: ParentObject) -> UIScrollViewDelegate? {
88-
return object.delegate
89-
}
90-
9183
deinit {
9284
if let subject = _contentOffsetBehaviorSubject {
9385
subject.on(.completed)

RxCocoa/iOS/Proxies/RxSearchBarDelegateProxy.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import UIKit
1313
import RxSwift
1414
#endif
1515

16+
extension UISearchBar: HasDelegate {
17+
public typealias Delegate = UISearchBarDelegate
18+
}
19+
1620
/// For more information take a look at `DelegateProxyType`.
1721
open class RxSearchBarDelegateProxy
1822
: DelegateProxy<UISearchBar, UISearchBarDelegate>
@@ -32,18 +36,6 @@ open class RxSearchBarDelegateProxy
3236
public static func registerKnownImplementations() {
3337
self.register { RxSearchBarDelegateProxy(parentObject: $0) }
3438
}
35-
36-
// MARK: Delegate proxy methods
37-
38-
/// For more information take a look at `DelegateProxyType`.
39-
open class func currentDelegate(for object: ParentObject) -> UISearchBarDelegate? {
40-
return object.delegate
41-
}
42-
43-
/// For more information take a look at `DelegateProxyType`.
44-
open class func setCurrentDelegate(_ delegate: UISearchBarDelegate?, to object: ParentObject) {
45-
object.delegate = delegate
46-
}
4739
}
4840

4941
#endif

RxCocoa/iOS/Proxies/RxSearchControllerDelegateProxy.swift

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#endif
1414
import UIKit
1515

16+
extension UISearchController: HasDelegate {
17+
public typealias Delegate = UISearchControllerDelegate
18+
}
19+
1620
/// For more information take a look at `DelegateProxyType`.
1721
@available(iOS 8.0, *)
1822
open class RxSearchControllerDelegateProxy
@@ -33,17 +37,6 @@ open class RxSearchControllerDelegateProxy
3337
public static func registerKnownImplementations() {
3438
self.register { RxSearchControllerDelegateProxy(parentObject: $0) }
3539
}
36-
37-
/// For more information take a look at `DelegateProxyType`.
38-
open class func setCurrentDelegate(_ delegate: UISearchControllerDelegate?, to object: ParentObject) {
39-
object.delegate = delegate
40-
}
41-
42-
/// For more information take a look at `DelegateProxyType`.
43-
open class func currentDelegate(for object: ParentObject) -> UISearchControllerDelegate? {
44-
return object.delegate
45-
}
46-
4740
}
4841

4942
#endif

RxCocoa/iOS/Proxies/RxTabBarControllerDelegateProxy.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import UIKit
1313
import RxSwift
1414
#endif
1515

16+
extension UITabBarController: HasDelegate {
17+
public typealias Delegate = UITabBarControllerDelegate
18+
}
19+
1620
/// For more information take a look at `DelegateProxyType`.
1721
open class RxTabBarControllerDelegateProxy
1822
: DelegateProxy<UITabBarController, UITabBarControllerDelegate>
@@ -32,16 +36,6 @@ open class RxTabBarControllerDelegateProxy
3236
public static func registerKnownImplementations() {
3337
self.register { RxTabBarControllerDelegateProxy(parentObject: $0) }
3438
}
35-
36-
/// For more information take a look at `DelegateProxyType`.
37-
open class func currentDelegate(for object: ParentObject) -> UITabBarControllerDelegate? {
38-
return object.delegate
39-
}
40-
41-
/// For more information take a look at `DelegateProxyType`.
42-
open class func setCurrentDelegate(_ delegate: UITabBarControllerDelegate?, to object: ParentObject) {
43-
object.delegate = delegate
44-
}
4539
}
4640

4741
#endif

RxCocoa/iOS/Proxies/RxTabBarDelegateProxy.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import UIKit
1313
import RxSwift
1414
#endif
1515

16+
extension UITabBar: HasDelegate {
17+
public typealias Delegate = UITabBarDelegate
18+
}
19+
1620
/// For more information take a look at `DelegateProxyType`.
1721
open class RxTabBarDelegateProxy
1822
: DelegateProxy<UITabBar, UITabBarDelegate>

RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import UIKit
1212
#if !RX_NO_MODULE
1313
import RxSwift
1414
#endif
15+
16+
extension UITableView: HasDataSource {
17+
public typealias DataSource = UITableViewDataSource
18+
}
1519

1620
let tableViewDataSourceNotSet = TableViewDataSourceNotSet()
1721

@@ -61,18 +65,6 @@ open class RxTableViewDataSourceProxy
6165
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
6266
return (_requiredMethodsDataSource ?? tableViewDataSourceNotSet).tableView(tableView, cellForRowAt: indexPath)
6367
}
64-
65-
// MARK: proxy
66-
67-
/// For more information take a look at `DelegateProxyType`.
68-
open class func setCurrentDelegate(_ delegate: UITableViewDataSource?, to object: ParentObject) {
69-
object.dataSource = delegate
70-
}
71-
72-
/// For more information take a look at `DelegateProxyType`.
73-
open class func currentDelegate(for object: ParentObject) -> UITableViewDataSource? {
74-
return object.dataSource
75-
}
7668

7769
/// For more information take a look at `DelegateProxyType`.
7870
open override func setForwardToDelegate(_ forwardToDelegate: UITableViewDataSource?, retainDelegate: Bool) {

RxCocoa/iOS/Proxies/RxTextStorageDelegateProxy.swift

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
import RxSwift
1313
#endif
1414
import UIKit
15-
15+
16+
extension NSTextStorage: HasDelegate {
17+
public typealias Delegate = NSTextStorageDelegate
18+
}
19+
1620
open class RxTextStorageDelegateProxy
1721
: DelegateProxy<NSTextStorage, NSTextStorageDelegate>
1822
, DelegateProxyType
@@ -31,15 +35,5 @@
3135
public static func registerKnownImplementations() {
3236
self.register { RxTextStorageDelegateProxy(parentObject: $0) }
3337
}
34-
35-
/// For more information take a look at `DelegateProxyType`.
36-
open class func setCurrentDelegate(_ delegate: NSTextStorageDelegate?, to object: ParentObject) {
37-
object.delegate = delegate
38-
}
39-
40-
/// For more information take a look at `DelegateProxyType`.
41-
open class func currentDelegate(for object: ParentObject) -> NSTextStorageDelegate? {
42-
return object.delegate
43-
}
4438
}
4539
#endif

RxCocoa/iOS/Proxies/RxWebViewDelegateProxy.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import UIKit
1313
import RxSwift
1414
#endif
1515

16+
extension UIWebView: HasDelegate {
17+
public typealias Delegate = UIWebViewDelegate
18+
}
19+
1620
open class RxWebViewDelegateProxy
1721
: DelegateProxy<UIWebView, UIWebViewDelegate>
1822
, DelegateProxyType
@@ -31,16 +35,6 @@ open class RxWebViewDelegateProxy
3135
public static func registerKnownImplementations() {
3236
self.register { RxWebViewDelegateProxy(parentObject: $0) }
3337
}
34-
35-
/// For more information take a look at `DelegateProxyType`.
36-
open class func setCurrentDelegate(_ delegate: UIWebViewDelegate?, to object: UIWebView) {
37-
object.delegate = delegate
38-
}
39-
40-
/// For more information take a look at `DelegateProxyType`.
41-
open class func currentDelegate(for object: UIWebView) -> UIWebViewDelegate? {
42-
return object.delegate
43-
}
4438
}
4539

4640
#endif

RxExample/Extensions/RxCLLocationManagerDelegateProxy.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import CoreLocation
1212
import RxCocoa
1313
#endif
1414

15+
extension CLLocationManager: HasDelegate {
16+
public typealias Delegate = CLLocationManagerDelegate
17+
}
18+
1519
public class RxCLLocationManagerDelegateProxy
1620
: DelegateProxy<CLLocationManager, CLLocationManagerDelegate>
1721
, DelegateProxyType
@@ -28,14 +32,6 @@ public class RxCLLocationManagerDelegateProxy
2832
internal lazy var didUpdateLocationsSubject = PublishSubject<[CLLocation]>()
2933
internal lazy var didFailWithErrorSubject = PublishSubject<Error>()
3034

31-
public class func currentDelegate(for object: ParentObject) -> CLLocationManagerDelegate? {
32-
return object.delegate
33-
}
34-
35-
public class func setCurrentDelegate(_ delegate: CLLocationManagerDelegate?, to object: ParentObject) {
36-
object.delegate = delegate
37-
}
38-
3935
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
4036
_forwardToDelegate?.locationManager?(manager, didUpdateLocations: locations)
4137
didUpdateLocationsSubject.onNext(locations)

0 commit comments

Comments
 (0)