From 3fba185b96b901480d84c1556ac985d316da1f39 Mon Sep 17 00:00:00 2001 From: tarunon Date: Sat, 17 Jun 2017 01:20:54 +0900 Subject: [PATCH] implement DelegateProxy factory --- RxCocoa/Common/DelegateProxy.swift | 7 --- RxCocoa/Common/DelegateProxyType.swift | 27 +++++--- RxCocoa/iOS/NSTextStorage+Rx.swift | 9 --- .../RxCollectionViewDataSourceProxy.swift | 12 ++-- .../RxNavigationControllerDelegateProxy.swift | 10 ++- .../Proxies/RxPickerViewDelegateProxy.swift | 10 ++- .../Proxies/RxScrollViewDelegateProxy.swift | 13 ++-- .../Proxies/RxSearchBarDelegateProxy.swift | 17 ++---- .../RxSearchControllerDelegateProxy.swift | 12 ++-- .../RxTabBarControllerDelegateProxy.swift | 10 ++- .../iOS/Proxies/RxTabBarDelegateProxy.swift | 11 ++-- .../Proxies/RxTableViewDataSourceProxy.swift | 10 ++- .../Proxies/RxTextStorageDelegateProxy.swift | 12 ++-- .../iOS/Proxies/RxWebViewDelegateProxy.swift | 10 ++- RxCocoa/iOS/UICollectionView+Rx.swift | 11 ---- RxCocoa/iOS/UINavigationController+Rx.swift | 9 --- RxCocoa/iOS/UIPickerView+Rx.swift | 11 ---- RxCocoa/iOS/UIScrollView+Rx.swift | 20 ------ RxCocoa/iOS/UISearchBar+Rx.swift | 13 ---- RxCocoa/iOS/UISearchController+Rx.swift | 9 --- RxCocoa/iOS/UITabBar+Rx.swift | 10 --- RxCocoa/iOS/UITabBarController+Rx.swift | 10 --- RxCocoa/iOS/UITableView+Rx.swift | 13 ---- RxCocoa/iOS/UIWebView+Rx.swift | 11 ---- RxCocoa/macOS/NSTextField+Rx.swift | 20 ++---- .../RxCLLocationManagerDelegateProxy.swift | 4 ++ .../DelegateProxyTest+Cocoa.swift | 4 -- .../DelegateProxyTest+UIKit.swift | 61 +------------------ Tests/RxCocoaTests/DelegateProxyTest.swift | 61 +++++++++++++++++++ 29 files changed, 141 insertions(+), 296 deletions(-) diff --git a/RxCocoa/Common/DelegateProxy.swift b/RxCocoa/Common/DelegateProxy.swift index 8b3e850d..9a40690e 100644 --- a/RxCocoa/Common/DelegateProxy.swift +++ b/RxCocoa/Common/DelegateProxy.swift @@ -188,13 +188,6 @@ open class DelegateProxy : _RXDelegateProxy { return delegateAssociatedTag } - /// Initializes new instance of delegate proxy. - /// - /// - returns: Initialized instance of `self`. - open class func createProxyForObject(_ object: AnyObject) -> AnyObject { - return self.init(parentObject: object) - } - /// Returns assigned proxy for object. /// /// - parameter object: Object that can have assigned delegate proxy. diff --git a/RxCocoa/Common/DelegateProxyType.swift b/RxCocoa/Common/DelegateProxyType.swift index f964784b..c5ceccde 100644 --- a/RxCocoa/Common/DelegateProxyType.swift +++ b/RxCocoa/Common/DelegateProxyType.swift @@ -69,22 +69,25 @@ every view has a corresponding delegate virtual factory method. In case of UITableView / UIScrollView, there is - extension UIScrollView { - public func createRxDelegateProxy() -> RxScrollViewDelegateProxy { - return RxScrollViewDelegateProxy(parentObject: base) - } + RxScrollViewDelegateProxy has factories that contains RxScrollViewDelegateProxy(parentObject: parentObject) .... -and override in UITableView +and extend it - extension UITableView { - public override func createRxDelegateProxy() -> RxScrollViewDelegateProxy { - .... + RxScrollViewDelegateProxy.extend { (parentObject: UITableView) in + RxTableViewDelegateProxy(parentObject: parentObject) + } */ public protocol DelegateProxyType : AnyObject { + /// DelegateProxy factory + static var factories: [((AnyObject) -> AnyObject?)] { get set } + + /// Extend DelegateProxy for specific subclass + static func extend(with factory: @escaping ((Object) -> AnyObject)) + /// Creates new proxy for target object. static func createProxyForObject(_ object: AnyObject) -> AnyObject @@ -210,6 +213,14 @@ extension DelegateProxyType { proxy.setForwardToDelegate(nil, retainDelegate: retainDelegate) } } + + public static func extend(with factory: @escaping ((Object) -> AnyObject)) { + factories.append({ ($0 as? Object).map(factory) }) + } + + public static func createProxyForObject(_ object: AnyObject) -> AnyObject { + return factories.reversed().reduce(AnyObject?.none) { $0 ?? $1(object) }! + } } #if os(iOS) || os(tvOS) diff --git a/RxCocoa/iOS/NSTextStorage+Rx.swift b/RxCocoa/iOS/NSTextStorage+Rx.swift index 8b30f393..18835d6e 100644 --- a/RxCocoa/iOS/NSTextStorage+Rx.swift +++ b/RxCocoa/iOS/NSTextStorage+Rx.swift @@ -11,15 +11,6 @@ import RxSwift #endif import UIKit - - extension NSTextStorage { - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxTextStorageDelegateProxy { - return RxTextStorageDelegateProxy(parentObject: self) - } - } extension Reactive where Base: NSTextStorage { diff --git a/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift b/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift index 65af53c8..dec9ac59 100644 --- a/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift +++ b/RxCocoa/iOS/Proxies/RxCollectionViewDataSourceProxy.swift @@ -35,7 +35,11 @@ final class CollectionViewDataSourceNotSet public class RxCollectionViewDataSourceProxy : DelegateProxy , UICollectionViewDataSource - , DelegateProxyType { +, DelegateProxyType { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxCollectionViewDataSourceProxy(parentObject: $0) } + ] /// Typed parent object. public weak private(set) var collectionView: UICollectionView? @@ -64,12 +68,6 @@ public class RxCollectionViewDataSourceProxy // MARK: proxy - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let collectionView: UICollectionView = castOrFatalError(object) - return collectionView.createRxDataSourceProxy() - } - /// For more information take a look at `DelegateProxyType`. public override class func delegateAssociatedObjectTag() -> UnsafeRawPointer { return dataSourceAssociatedTag diff --git a/RxCocoa/iOS/Proxies/RxNavigationControllerDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxNavigationControllerDelegateProxy.swift index 34260714..b418ab55 100644 --- a/RxCocoa/iOS/Proxies/RxNavigationControllerDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxNavigationControllerDelegateProxy.swift @@ -18,6 +18,10 @@ : DelegateProxy , UINavigationControllerDelegate , DelegateProxyType { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxNavigationControllerDelegateProxy(parentObject: $0) } + ] /// For more information take a look at `DelegateProxyType`. public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { @@ -30,12 +34,6 @@ let navigationController: UINavigationController = castOrFatalError(object) navigationController.delegate = castOptionalOrFatalError(delegate) } - - /// For more information take a look at `DelegateProxyType`. - open override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let navigationController: UINavigationController = castOrFatalError(object) - return navigationController.createRxDelegateProxy() - } } #if os(iOS) diff --git a/RxCocoa/iOS/Proxies/RxPickerViewDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxPickerViewDelegateProxy.swift index 95cecc6e..e8dd30b6 100644 --- a/RxCocoa/iOS/Proxies/RxPickerViewDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxPickerViewDelegateProxy.swift @@ -17,12 +17,10 @@ : DelegateProxy , DelegateProxyType , UIPickerViewDelegate { - - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let pickerView: UIPickerView = castOrFatalError(object) - return pickerView.createRxDelegateProxy() - } + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxPickerViewDelegateProxy(parentObject: $0) } + ] /// For more information take a look at `DelegateProxyType`. public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { diff --git a/RxCocoa/iOS/Proxies/RxScrollViewDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxScrollViewDelegateProxy.swift index 59f80df1..29683315 100644 --- a/RxCocoa/iOS/Proxies/RxScrollViewDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxScrollViewDelegateProxy.swift @@ -18,6 +18,13 @@ public class RxScrollViewDelegateProxy : DelegateProxy , UIScrollViewDelegate , DelegateProxyType { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxScrollViewDelegateProxy(parentObject: $0) }, + { ($0 as? UITableView).map { RxTableViewDelegateProxy(parentObject: $0) } }, + { ($0 as? UICollectionView).map { RxCollectionViewDelegateProxy(parentObject: $0) } }, + { ($0 as? UITextView).map { RxTextViewDelegateProxy(parentObject: $0) } } + ] fileprivate var _contentOffsetBehaviorSubject: BehaviorSubject? fileprivate var _contentOffsetPublishSubject: PublishSubject<()>? @@ -72,12 +79,6 @@ public class RxScrollViewDelegateProxy // MARK: delegate proxy - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let scrollView: UIScrollView = castOrFatalError(object) - return scrollView.createRxDelegateProxy() - } - /// For more information take a look at `DelegateProxyType`. public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { let scrollView: UIScrollView = castOrFatalError(object) diff --git a/RxCocoa/iOS/Proxies/RxSearchBarDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxSearchBarDelegateProxy.swift index 9dfc3f95..cd8f4475 100644 --- a/RxCocoa/iOS/Proxies/RxSearchBarDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxSearchBarDelegateProxy.swift @@ -19,6 +19,12 @@ public class RxSearchBarDelegateProxy , UISearchBarDelegate , DelegateProxyType { + // MARK: Delegate proxy methods + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxSearchBarDelegateProxy(parentObject: $0) } + ] + /// For more information take a look at `DelegateProxyType`. public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { let searchBar: UISearchBar = castOrFatalError(object) @@ -30,17 +36,6 @@ public class RxSearchBarDelegateProxy let searchBar: UISearchBar = castOrFatalError(object) searchBar.delegate = castOptionalOrFatalError(delegate) } - - // MARK: Delegate proxy methods - -#if os(iOS) - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let searchBar: UISearchBar = castOrFatalError(object) - return searchBar.createRxDelegateProxy() - } -#endif - } #endif diff --git a/RxCocoa/iOS/Proxies/RxSearchControllerDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxSearchControllerDelegateProxy.swift index 5c01ecb4..3d5e57f0 100644 --- a/RxCocoa/iOS/Proxies/RxSearchControllerDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxSearchControllerDelegateProxy.swift @@ -18,13 +18,11 @@ public class RxSearchControllerDelegateProxy : DelegateProxy , DelegateProxyType - , UISearchControllerDelegate { - - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let pickerView: UISearchController = castOrFatalError(object) - return pickerView.createRxDelegateProxy() - } +, UISearchControllerDelegate { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxSearchControllerDelegateProxy(parentObject: $0) } + ] /// For more information take a look at `DelegateProxyType`. public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { diff --git a/RxCocoa/iOS/Proxies/RxTabBarControllerDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxTabBarControllerDelegateProxy.swift index fe284d8b..4dedd5d3 100644 --- a/RxCocoa/iOS/Proxies/RxTabBarControllerDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxTabBarControllerDelegateProxy.swift @@ -19,6 +19,10 @@ public class RxTabBarControllerDelegateProxy , UITabBarControllerDelegate , DelegateProxyType { + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxTabBarControllerDelegateProxy(parentObject: $0) } + ] + /// For more information take a look at `DelegateProxyType`. public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { let tabBarController: UITabBarController = castOrFatalError(object) @@ -30,12 +34,6 @@ public class RxTabBarControllerDelegateProxy let tabBarController: UITabBarController = castOrFatalError(object) tabBarController.delegate = castOptionalOrFatalError(delegate) } - - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let tabBarController: UITabBarController = castOrFatalError(object) - return tabBarController.createRxDelegateProxy() - } } #endif diff --git a/RxCocoa/iOS/Proxies/RxTabBarDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxTabBarDelegateProxy.swift index 7a0c83fe..a32fddcc 100644 --- a/RxCocoa/iOS/Proxies/RxTabBarDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxTabBarDelegateProxy.swift @@ -18,6 +18,10 @@ public class RxTabBarDelegateProxy : DelegateProxy , UITabBarDelegate , DelegateProxyType { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxTabBarDelegateProxy(parentObject: $0) } + ] /// For more information take a look at `DelegateProxyType`. public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { @@ -30,13 +34,6 @@ public class RxTabBarDelegateProxy let tabBar: UITabBar = castOrFatalError(object) tabBar.delegate = castOptionalOrFatalError(delegate) } - - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let tabBar: UITabBar = castOrFatalError(object) - return tabBar.createRxDelegateProxy() - } - } #endif diff --git a/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift b/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift index 4351beae..3a432620 100644 --- a/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift +++ b/RxCocoa/iOS/Proxies/RxTableViewDataSourceProxy.swift @@ -33,6 +33,10 @@ public class RxTableViewDataSourceProxy : DelegateProxy , UITableViewDataSource , DelegateProxyType { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxTableViewDataSourceProxy(parentObject: $0) } + ] /// Typed parent object. public weak fileprivate(set) var tableView: UITableView? @@ -61,12 +65,6 @@ public class RxTableViewDataSourceProxy // MARK: proxy - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let tableView: UITableView = castOrFatalError(object) - return tableView.createRxDataSourceProxy() - } - /// For more information take a look at `DelegateProxyType`. public override class func delegateAssociatedObjectTag() -> UnsafeRawPointer { return dataSourceAssociatedTag diff --git a/RxCocoa/iOS/Proxies/RxTextStorageDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxTextStorageDelegateProxy.swift index 86d3a853..ddb1cea4 100644 --- a/RxCocoa/iOS/Proxies/RxTextStorageDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxTextStorageDelegateProxy.swift @@ -16,13 +16,11 @@ public class RxTextStorageDelegateProxy : DelegateProxy , DelegateProxyType - , NSTextStorageDelegate { - - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let pickerView: NSTextStorage = castOrFatalError(object) - return pickerView.createRxDelegateProxy() - } + , NSTextStorageDelegate { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxTextStorageDelegateProxy(parentObject: $0) } + ] /// For more information take a look at `DelegateProxyType`. public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { diff --git a/RxCocoa/iOS/Proxies/RxWebViewDelegateProxy.swift b/RxCocoa/iOS/Proxies/RxWebViewDelegateProxy.swift index 04e99822..d7498445 100644 --- a/RxCocoa/iOS/Proxies/RxWebViewDelegateProxy.swift +++ b/RxCocoa/iOS/Proxies/RxWebViewDelegateProxy.swift @@ -17,13 +17,11 @@ public class RxWebViewDelegateProxy : DelegateProxy , DelegateProxyType , UIWebViewDelegate { - - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let pickerView: UIWebView = castOrFatalError(object) - return pickerView.createRxDelegateProxy() - } + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxWebViewDelegateProxy(parentObject: $0) } + ] + /// For more information take a look at `DelegateProxyType`. public class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) { let webView: UIWebView = castOrFatalError(object) diff --git a/RxCocoa/iOS/UICollectionView+Rx.swift b/RxCocoa/iOS/UICollectionView+Rx.swift index e7a5c0bc..5015bbbc 100644 --- a/RxCocoa/iOS/UICollectionView+Rx.swift +++ b/RxCocoa/iOS/UICollectionView+Rx.swift @@ -160,17 +160,6 @@ extension Reactive where Base: UICollectionView { } } -extension UICollectionView { - - /// Factory method that enables subclasses to implement their own `rx.dataSource`. - /// - /// - returns: Instance of delegate proxy that wraps `dataSource`. - public func createRxDataSourceProxy() -> RxCollectionViewDataSourceProxy { - return RxCollectionViewDataSourceProxy(parentObject: self) - } - -} - extension Reactive where Base: UICollectionView { /// Reactive wrapper for `dataSource`. diff --git a/RxCocoa/iOS/UINavigationController+Rx.swift b/RxCocoa/iOS/UINavigationController+Rx.swift index 10e0a4d8..1795e75b 100644 --- a/RxCocoa/iOS/UINavigationController+Rx.swift +++ b/RxCocoa/iOS/UINavigationController+Rx.swift @@ -13,15 +13,6 @@ import RxSwift #endif import UIKit -extension UINavigationController { - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxNavigationControllerDelegateProxy { - return RxNavigationControllerDelegateProxy(parentObject: self) - } -} - extension Reactive where Base: UINavigationController { public typealias ShowEvent = (viewController: UIViewController, animated: Bool) diff --git a/RxCocoa/iOS/UIPickerView+Rx.swift b/RxCocoa/iOS/UIPickerView+Rx.swift index 7d4b0741..9290e0d1 100644 --- a/RxCocoa/iOS/UIPickerView+Rx.swift +++ b/RxCocoa/iOS/UIPickerView+Rx.swift @@ -13,17 +13,6 @@ #endif import UIKit - extension UIPickerView { - - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxPickerViewDelegateProxy { - return RxPickerViewDelegateProxy(parentObject: self) - } - - } - extension Reactive where Base: UIPickerView { /// Reactive wrapper for `delegate`. diff --git a/RxCocoa/iOS/UIScrollView+Rx.swift b/RxCocoa/iOS/UIScrollView+Rx.swift index cd2609aa..805bb963 100644 --- a/RxCocoa/iOS/UIScrollView+Rx.swift +++ b/RxCocoa/iOS/UIScrollView+Rx.swift @@ -14,26 +14,6 @@ import UIKit - extension UIScrollView { - - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxScrollViewDelegateProxy { - switch self { - case self as UICollectionView: - return RxCollectionViewDelegateProxy(parentObject: self) - case self as UITableView: - return RxTableViewDelegateProxy(parentObject: self) - case self as UITextView: - return RxTextViewDelegateProxy(parentObject: self) - default: - return RxScrollViewDelegateProxy(parentObject: self) - } - } - - } - extension Reactive where Base: UIScrollView { /// Reactive wrapper for `delegate`. diff --git a/RxCocoa/iOS/UISearchBar+Rx.swift b/RxCocoa/iOS/UISearchBar+Rx.swift index 4d05ea9c..c472782e 100644 --- a/RxCocoa/iOS/UISearchBar+Rx.swift +++ b/RxCocoa/iOS/UISearchBar+Rx.swift @@ -13,19 +13,6 @@ import RxSwift #endif import UIKit - -#if os(iOS) - extension UISearchBar { - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxSearchBarDelegateProxy { - return RxSearchBarDelegateProxy(parentObject: self) - } - - } -#endif - extension Reactive where Base: UISearchBar { /// Reactive wrapper for `delegate`. diff --git a/RxCocoa/iOS/UISearchController+Rx.swift b/RxCocoa/iOS/UISearchController+Rx.swift index 96d4a38d..9c58cba8 100644 --- a/RxCocoa/iOS/UISearchController+Rx.swift +++ b/RxCocoa/iOS/UISearchController+Rx.swift @@ -12,15 +12,6 @@ import RxSwift #endif import UIKit - - extension UISearchController { - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxSearchControllerDelegateProxy { - return RxSearchControllerDelegateProxy(parentObject: self) - } - } @available(iOS 8.0, *) extension Reactive where Base: UISearchController { diff --git a/RxCocoa/iOS/UITabBar+Rx.swift b/RxCocoa/iOS/UITabBar+Rx.swift index 35ea415b..a4ea8e38 100644 --- a/RxCocoa/iOS/UITabBar+Rx.swift +++ b/RxCocoa/iOS/UITabBar+Rx.swift @@ -70,17 +70,7 @@ extension Reactive where Base: UITabBar { /** iOS and tvOS */ -extension UITabBar { - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxTabBarDelegateProxy { - return RxTabBarDelegateProxy(parentObject: self) - } - -} - extension Reactive where Base: UITabBar { /// Reactive wrapper for `delegate`. /// diff --git a/RxCocoa/iOS/UITabBarController+Rx.swift b/RxCocoa/iOS/UITabBarController+Rx.swift index b7c8ecb7..47aacecd 100644 --- a/RxCocoa/iOS/UITabBarController+Rx.swift +++ b/RxCocoa/iOS/UITabBarController+Rx.swift @@ -58,16 +58,6 @@ extension Reactive where Base: UITabBarController { /** iOS and tvOS */ -extension UITabBarController { - - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxTabBarControllerDelegateProxy { - return RxTabBarControllerDelegateProxy(parentObject: self) - } - -} extension Reactive where Base: UITabBarController { /// Reactive wrapper for `delegate`. diff --git a/RxCocoa/iOS/UITableView+Rx.swift b/RxCocoa/iOS/UITableView+Rx.swift index 3c06c1be..0c9e3ff5 100644 --- a/RxCocoa/iOS/UITableView+Rx.swift +++ b/RxCocoa/iOS/UITableView+Rx.swift @@ -165,19 +165,6 @@ extension Reactive where Base: UITableView { } -extension UITableView { - - /** - Factory method that enables subclasses to implement their own `rx.dataSource`. - - - returns: Instance of delegate proxy that wraps `dataSource`. - */ - public func createRxDataSourceProxy() -> RxTableViewDataSourceProxy { - return RxTableViewDataSourceProxy(parentObject: self) - } - -} - extension Reactive where Base: UITableView { /** Reactive wrapper for `dataSource`. diff --git a/RxCocoa/iOS/UIWebView+Rx.swift b/RxCocoa/iOS/UIWebView+Rx.swift index fbc18aac..2014272e 100644 --- a/RxCocoa/iOS/UIWebView+Rx.swift +++ b/RxCocoa/iOS/UIWebView+Rx.swift @@ -14,17 +14,6 @@ import UIKit import RxSwift #endif - extension UIWebView { - - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxWebViewDelegateProxy { - return RxWebViewDelegateProxy(parentObject: self) - } - - } - extension Reactive where Base: UIWebView { /// Reactive wrapper for `delegate`. diff --git a/RxCocoa/macOS/NSTextField+Rx.swift b/RxCocoa/macOS/NSTextField+Rx.swift index df46a50c..7847f17d 100644 --- a/RxCocoa/macOS/NSTextField+Rx.swift +++ b/RxCocoa/macOS/NSTextField+Rx.swift @@ -20,6 +20,10 @@ public class RxTextFieldDelegateProxy : DelegateProxy , NSTextFieldDelegate , DelegateProxyType { + + public static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxTextFieldDelegateProxy(parentObject: $0) } + ] fileprivate let textSubject = PublishSubject() @@ -45,12 +49,6 @@ public class RxTextFieldDelegateProxy // MARK: Delegate proxy methods - /// For more information take a look at `DelegateProxyType`. - public override class func createProxyForObject(_ object: AnyObject) -> AnyObject { - let control: NSTextField = castOrFatalError(object) - return control.createRxDelegateProxy() - } - /// For more information take a look at `DelegateProxyType`. public class func currentDelegateFor(_ object: AnyObject) -> AnyObject? { let textField: NSTextField = castOrFatalError(object) @@ -65,16 +63,6 @@ public class RxTextFieldDelegateProxy } -extension NSTextField { - - /// Factory method that enables subclasses to implement their own `delegate`. - /// - /// - returns: Instance of delegate proxy that wraps `delegate`. - public func createRxDelegateProxy() -> RxTextFieldDelegateProxy { - return RxTextFieldDelegateProxy(parentObject: self) - } -} - extension Reactive where Base: NSTextField { /// Reactive wrapper for `delegate`. diff --git a/RxExample/Extensions/RxCLLocationManagerDelegateProxy.swift b/RxExample/Extensions/RxCLLocationManagerDelegateProxy.swift index 56c5f32a..5005b06a 100644 --- a/RxExample/Extensions/RxCLLocationManagerDelegateProxy.swift +++ b/RxExample/Extensions/RxCLLocationManagerDelegateProxy.swift @@ -15,6 +15,10 @@ import CoreLocation class RxCLLocationManagerDelegateProxy : DelegateProxy , CLLocationManagerDelegate , DelegateProxyType { + + static var factories: [((AnyObject) -> AnyObject?)] = [ + { RxCLLocationManagerDelegateProxy(parentObject: $0) } + ] internal lazy var didUpdateLocationsSubject = PublishSubject<[CLLocation]>() internal lazy var didFailWithErrorSubject = PublishSubject() diff --git a/Tests/RxCocoaTests/DelegateProxyTest+Cocoa.swift b/Tests/RxCocoaTests/DelegateProxyTest+Cocoa.swift index fd930a3e..5b36045a 100644 --- a/Tests/RxCocoaTests/DelegateProxyTest+Cocoa.swift +++ b/Tests/RxCocoaTests/DelegateProxyTest+Cocoa.swift @@ -32,10 +32,6 @@ class ExtendNSTextFieldDelegateProxy final class NSTextFieldSubclass : NSTextField , TestDelegateControl { - override func createRxDelegateProxy() -> RxTextFieldDelegateProxy { - return ExtendNSTextFieldDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } diff --git a/Tests/RxCocoaTests/DelegateProxyTest+UIKit.swift b/Tests/RxCocoaTests/DelegateProxyTest+UIKit.swift index fcfc44b3..b0173fea 100644 --- a/Tests/RxCocoaTests/DelegateProxyTest+UIKit.swift +++ b/Tests/RxCocoaTests/DelegateProxyTest+UIKit.swift @@ -120,10 +120,6 @@ final class ExtendTableViewDelegateProxy final class UITableViewSubclass1 : UITableView , TestDelegateControl { - override func createRxDelegateProxy() -> RxScrollViewDelegateProxy { - return ExtendTableViewDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -151,10 +147,7 @@ final class ExtendTableViewDataSourceProxy final class UITableViewSubclass2 : UITableView , TestDelegateControl { - override func createRxDataSourceProxy() -> RxTableViewDataSourceProxy { - return ExtendTableViewDataSourceProxy(parentObject: self) - } - + func doThatTest(_ value: Int) { if dataSource != nil { (dataSource as! TestDelegateProtocol).testEventHappened?(value) @@ -184,10 +177,6 @@ final class ExtendCollectionViewDelegateProxy final class UICollectionViewSubclass1 : UICollectionView , TestDelegateControl { - override func createRxDelegateProxy() -> RxScrollViewDelegateProxy { - return ExtendCollectionViewDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -215,9 +204,6 @@ final class ExtendCollectionViewDataSourceProxy final class UICollectionViewSubclass2 : UICollectionView , TestDelegateControl { - override func createRxDataSourceProxy() -> RxCollectionViewDataSourceProxy { - return ExtendCollectionViewDataSourceProxy(parentObject: self) - } func doThatTest(_ value: Int) { if dataSource != nil { @@ -248,10 +234,6 @@ final class ExtendScrollViewDelegateProxy final class UIScrollViewSubclass : UIScrollView , TestDelegateControl { - override func createRxDelegateProxy() -> RxScrollViewDelegateProxy { - return ExtendScrollViewDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -280,11 +262,6 @@ final class ExtendSearchBarDelegateProxy final class UISearchBarSubclass : UISearchBar , TestDelegateControl { - - override func createRxDelegateProxy() -> RxSearchBarDelegateProxy { - return ExtendSearchBarDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -313,10 +290,6 @@ final class ExtendTextViewDelegateProxy final class UITextViewSubclass : UITextView , TestDelegateControl { - override func createRxDelegateProxy() -> RxScrollViewDelegateProxy { - return ExtendTextViewDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -341,11 +314,6 @@ final class ExtendSearchControllerDelegateProxy final class UISearchControllerSubclass : UISearchController , TestDelegateControl { - - override func createRxDelegateProxy() -> RxSearchControllerDelegateProxy { - return ExtendSearchControllerDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -371,11 +339,6 @@ final class ExtendPickerViewDelegateProxy final class UIPickerViewSubclass : UIPickerView , TestDelegateControl { - - public override func createRxDelegateProxy() -> RxPickerViewDelegateProxy { - return ExtendPickerViewDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -400,11 +363,6 @@ final class ExtendWebViewDelegateProxy } final class UIWebViewSubclass: UIWebView, TestDelegateControl { - - override func createRxDelegateProxy() -> RxWebViewDelegateProxy { - return ExtendWebViewDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -437,11 +395,6 @@ final class ExtendTextStorageDelegateProxy final class NSTextStorageSubclass : NSTextStorage , TestDelegateControl { - - override func createRxDelegateProxy() -> RxTextStorageDelegateProxy { - return ExtendTextStorageDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -489,10 +442,6 @@ final class ExtendTabBarDelegateProxy } final class UINavigationControllerSubclass: UINavigationController, TestDelegateControl { - override func createRxDelegateProxy() -> RxNavigationControllerDelegateProxy { - return ExtendNavigationControllerDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -511,10 +460,6 @@ final class UINavigationControllerSubclass: UINavigationController, TestDelegate final class UITabBarControllerSubclass : UITabBarController , TestDelegateControl { - override func createRxDelegateProxy() -> RxTabBarControllerDelegateProxy { - return ExtendTabBarControllerDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } @@ -529,10 +474,6 @@ final class UITabBarControllerSubclass } final class UITabBarSubclass: UITabBar, TestDelegateControl { - override func createRxDelegateProxy() -> RxTabBarDelegateProxy { - return ExtendTabBarDelegateProxy(parentObject: self) - } - func doThatTest(_ value: Int) { (delegate as! TestDelegateProtocol).testEventHappened?(value) } diff --git a/Tests/RxCocoaTests/DelegateProxyTest.swift b/Tests/RxCocoaTests/DelegateProxyTest.swift index 5755275f..52157210 100644 --- a/Tests/RxCocoaTests/DelegateProxyTest.swift +++ b/Tests/RxCocoaTests/DelegateProxyTest.swift @@ -56,6 +56,62 @@ extension TestDelegateControl { // MARK: Tests final class DelegateProxyTest : RxTest { + override func setUp() { + super.setUp() + // setup extending of DelegateProxies + #if os(iOS) || os(tvOS) + RxScrollViewDelegateProxy.extend { (parentObject: UIScrollViewSubclass) in + ExtendScrollViewDelegateProxy(parentObject: parentObject) + } + RxScrollViewDelegateProxy.extend { (parentObject: UITableViewSubclass1) in + ExtendTableViewDelegateProxy(parentObject: parentObject) + } + RxTableViewDataSourceProxy.extend { (parentObject: UITableViewSubclass2) in + ExtendTableViewDataSourceProxy(parentObject: parentObject) + } + RxScrollViewDelegateProxy.extend { (parentObject: UICollectionViewSubclass1) in + ExtendCollectionViewDelegateProxy(parentObject: parentObject) + } + RxCollectionViewDataSourceProxy.extend { (parentObject: UICollectionViewSubclass2) in + ExtendCollectionViewDataSourceProxy(parentObject: parentObject) + } + RxScrollViewDelegateProxy.extend { (parentObject: UITextViewSubclass) in + ExtendTextViewDelegateProxy(parentObject: parentObject) + } + RxTextStorageDelegateProxy.extend { (parentObject: NSTextStorageSubclass) in + ExtendTextStorageDelegateProxy(parentObject: parentObject) + } + RxNavigationControllerDelegateProxy.extend { (parentObject: UINavigationControllerSubclass) in + ExtendNavigationControllerDelegateProxy(parentObject: parentObject) + } + RxTabBarControllerDelegateProxy.extend { (parentObject: UITabBarControllerSubclass) in + ExtendTabBarControllerDelegateProxy(parentObject: parentObject) + } + RxTabBarDelegateProxy.extend { (parentObject: UITabBarSubclass) -> AnyObject in + ExtendTabBarDelegateProxy(parentObject: parentObject) + } + #endif + #if os(iOS) + RxSearchBarDelegateProxy.extend { (parentObject: UISearchBarSubclass) in + ExtendSearchBarDelegateProxy(parentObject: parentObject) + } + RxSearchControllerDelegateProxy.extend { (parentObject: UISearchControllerSubclass) in + ExtendSearchControllerDelegateProxy(parentObject: parentObject) + } + RxPickerViewDelegateProxy.extend { (parentObject: UIPickerViewSubclass) in + ExtendPickerViewDelegateProxy(parentObject: parentObject) + } + RxWebViewDelegateProxy.extend { (parentObject: UIWebViewSubclass) in + ExtendWebViewDelegateProxy(parentObject: parentObject) + } + #endif + #if os(macOS) + RxTextFieldDelegateProxy.extend { (parentObject: NSTextFieldSubclass) in + ExtendNSTextFieldDelegateProxy(parentObject: parentObject) + } + #endif + } + func test_OnInstallDelegateIsRetained() { let view = ThreeDSectionedView() let mock = MockThreeDSectionedViewProtocol() @@ -419,6 +475,11 @@ final class ThreeDSectionedView: NSObject { final class ThreeDSectionedViewDelegateProxy : DelegateProxy , ThreeDSectionedViewProtocol , DelegateProxyType { + + static var factories: [((AnyObject) -> AnyObject?)] = [ + { ThreeDSectionedViewDelegateProxy(parentObject: $0) } + ] + required init(parentObject: AnyObject) { super.init(parentObject: parentObject) }