diff --git a/Example/InfiniteLayout.xcodeproj/project.pbxproj b/Example/InfiniteLayout.xcodeproj/project.pbxproj index 3dfe31e..14dba1f 100644 --- a/Example/InfiniteLayout.xcodeproj/project.pbxproj +++ b/Example/InfiniteLayout.xcodeproj/project.pbxproj @@ -291,10 +291,12 @@ ); inputPaths = ( "${SRCROOT}/Pods/Target Support Files/Pods-InfiniteLayout_Example/Pods-InfiniteLayout_Example-frameworks.sh", + "${BUILT_PRODUCTS_DIR}/CocoaProxy/CocoaProxy.framework", "${BUILT_PRODUCTS_DIR}/InfiniteLayout/InfiniteLayout.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/CocoaProxy.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/InfiniteLayout.framework", ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Example/Podfile.lock b/Example/Podfile.lock index fed76dc..6e4f726 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,7 @@ PODS: - - InfiniteLayout (0.1.0) + - CocoaProxy (0.1.1) + - InfiniteLayout (0.1.3): + - CocoaProxy (~> 0.1) DEPENDENCIES: - InfiniteLayout (from `../`) @@ -9,7 +11,8 @@ EXTERNAL SOURCES: :path: ../ SPEC CHECKSUMS: - InfiniteLayout: 82a21b8255623e2d72174f3082bef9e576140621 + CocoaProxy: 35ab81e24325b33834cffe45a3d1fd48ca67ef3a + InfiniteLayout: 0ab39bf2a4d9ce5c473ebccfc0d3b7278de18ac9 PODFILE CHECKSUM: 3a658536624f41ec07c5a7a2c55407b9b8f48528 diff --git a/InfiniteLayout.podspec b/InfiniteLayout.podspec index adff8c3..2466318 100644 --- a/InfiniteLayout.podspec +++ b/InfiniteLayout.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'InfiniteLayout' - s.version = '0.1.3' + s.version = '0.1.4' s.summary = 'Horizontal and Vertical infinite scrolling feature for UICollectionView' # This description is used to generate tags and improve search results. @@ -40,5 +40,5 @@ Horizontal and Vertical infinite scrolling feature for UICollectionView with Pag # s.public_header_files = 'Pod/Classes/**/*.h' # s.frameworks = 'UIKit', 'MapKit' - # s.dependency 'AFNetworking', '~> 2.3' + s.dependency 'CocoaProxy', '~> 0.1' end diff --git a/InfiniteLayout/Classes/InfiniteCollectionView.swift b/InfiniteLayout/Classes/InfiniteCollectionView.swift index 27de5cb..f609f46 100644 --- a/InfiniteLayout/Classes/InfiniteCollectionView.swift +++ b/InfiniteLayout/Classes/InfiniteCollectionView.swift @@ -9,10 +9,8 @@ import UIKit open class InfiniteCollectionView: UICollectionView { - lazy var delegateProxy = InfiniteCollectionViewDelegateProxy(self, exceptions: ["scrollViewDidScroll:", - "scrollViewWillEndDragging:withVelocity:targetContentOffset:"]) - lazy var dataSourceProxy = InfiniteCollectionViewDataSourceProxy(self, exceptions: ["numberOfSectionsInCollectionView:", - "collectionView:numberOfItemsInSection:"]) + lazy var delegateProxy = InfiniteCollectionViewDelegateProxy(collectionView: self) + lazy var dataSourceProxy = InfiniteCollectionViewDataSourceProxy(collectionView: self) @IBInspectable var isItemPagingEnabled: Bool = false @IBInspectable var velocityMultiplier: CGFloat = 500 { diff --git a/InfiniteLayout/Classes/InfiniteCollectionViewProxy.h b/InfiniteLayout/Classes/InfiniteCollectionViewProxy.h deleted file mode 100644 index c5f5026..0000000 --- a/InfiniteLayout/Classes/InfiniteCollectionViewProxy.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// InfiniteCollectionViewProxy.h -// InfiniteLayout -// -// Created by Arnaud Dorgans on 21/12/2017. -// - -#import - -@interface _InfiniteCollectionViewProxy> : NSProxy - --(nonnull instancetype)init:(nullable T)collectionView exceptions:(nonnull NSArray*)exceptions; - -@property (nonatomic, weak, nullable) T collectionView; -@property (nonatomic, weak, nullable) T delegate; -@property (nonatomic, nonnull, retain) NSArray* exceptions; - - -@end diff --git a/InfiniteLayout/Classes/InfiniteCollectionViewProxy.m b/InfiniteLayout/Classes/InfiniteCollectionViewProxy.m deleted file mode 100644 index 806bfe8..0000000 --- a/InfiniteLayout/Classes/InfiniteCollectionViewProxy.m +++ /dev/null @@ -1,52 +0,0 @@ -// -// InfiniteCollectionViewProxy.m -// InfiniteLayout -// -// Created by Arnaud Dorgans on 21/12/2017. -// - -#import "InfiniteCollectionViewProxy.h" - -@implementation _InfiniteCollectionViewProxy - --(instancetype)init:(nullable id)collectionView exceptions:(nonnull NSArray*)exceptions { - self.collectionView = collectionView; - self.exceptions = exceptions; - return self; -} - -- (BOOL)respondsToSelector:(SEL)aSelector -{ - return ([self.collectionView respondsToSelector:aSelector] || [self.delegate respondsToSelector:aSelector]); -} - -- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { - NSObject *delegateForResonse = [self.delegate respondsToSelector:aSelector] ? self.delegate : self.collectionView; - NSMethodSignature *signature = [delegateForResonse respondsToSelector:aSelector] ? [delegateForResonse methodSignatureForSelector:aSelector] : nil; - return signature; -} - -- (void)forwardInvocation:(NSInvocation *)invocation -{ - NSString *selectorName = NSStringFromSelector(invocation.selector); - - NSArray> *delegates = @[self.delegate, self.collectionView]; - if ([self.exceptions containsObject:selectorName]) { - delegates = [[delegates reverseObjectEnumerator] allObjects]; - } - for (int i = 0; i < delegates.count; i++) { - if ([delegates[i] respondsToSelector:invocation.selector]) { - [self invokeInvocation:invocation onDelegate:delegates[i]]; - return; - } - } -} - -- (void)invokeInvocation:(NSInvocation *)invocation onDelegate:(id)delegate -{ - if ([delegate respondsToSelector:invocation.selector]) { - [invocation invokeWithTarget:delegate]; - } -} - -@end diff --git a/InfiniteLayout/Classes/InfiniteCollectionViewProxy.swift b/InfiniteLayout/Classes/InfiniteCollectionViewProxy.swift new file mode 100644 index 0000000..60ad3c8 --- /dev/null +++ b/InfiniteLayout/Classes/InfiniteCollectionViewProxy.swift @@ -0,0 +1,85 @@ +// +// Proxy.swift +// InfiniteLayout +// +// Created by Arnaud Dorgans on 20/12/2017. +// + +import UIKit +import CocoaProxy + +class InfiniteCollectionViewProxy: CocoaProxy { + + var collectionView: InfiniteCollectionView! { + get { return self.proxies.first as? InfiniteCollectionView } + set { + if !self.proxies.isEmpty { + self.proxies.removeFirst() + } + self.proxies.insert(newValue, at: 0) + } + } + + var delegate: T? { + get { + guard self.proxies.count > 1 else { + return nil + } + return self.proxies.last as? T + } set { + while self.proxies.count > 1 { + self.proxies.removeLast() + } + guard let delegate = newValue else { + return + } + self.proxies.append(delegate) + } + } + + override func proxies(for aSelector: Selector) -> [NSObjectProtocol] { + return super.proxies(for: aSelector).reversed() + } + + init(collectionView: InfiniteCollectionView) { + super.init(proxies: []) + self.collectionView = collectionView + } +} + +class InfiniteCollectionViewDelegateProxy: InfiniteCollectionViewProxy, UICollectionViewDelegate { + + override func proxies(for aSelector: Selector) -> [NSObjectProtocol] { + return super.proxies(for: aSelector) + .first { proxy in + guard !(aSelector == #selector(UIScrollViewDelegate.scrollViewDidScroll(_:)) || + aSelector == #selector(UIScrollViewDelegate.scrollViewWillEndDragging(_:withVelocity:targetContentOffset:))) else { + return proxy is InfiniteCollectionView + } + return true + }.flatMap { [$0] } ?? [] + } +} + +class InfiniteCollectionViewDataSourceProxy: InfiniteCollectionViewProxy, UICollectionViewDataSource { + + override func proxies(for aSelector: Selector) -> [NSObjectProtocol] { + return super.proxies(for: aSelector) + .first { proxy in + guard !(aSelector == #selector(UICollectionViewDataSource.numberOfSections(in:)) || + aSelector == #selector(UICollectionViewDataSource.collectionView(_:numberOfItemsInSection:))) else { + return proxy is InfiniteCollectionView + } + return true + }.flatMap { [$0] } ?? [] + } + + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return self.collectionView.collectionView(collectionView, numberOfItemsInSection: section) + } + + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + return self.delegate?.collectionView(collectionView, cellForItemAt: indexPath) ?? + self.collectionView.collectionView(collectionView, cellForItemAt: indexPath) + } +} diff --git a/InfiniteLayout/Classes/Proxy.swift b/InfiniteLayout/Classes/Proxy.swift deleted file mode 100644 index 5c9b926..0000000 --- a/InfiniteLayout/Classes/Proxy.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// Proxy.swift -// InfiniteLayout -// -// Created by Arnaud Dorgans on 20/12/2017. -// - -import UIKit - -class InfiniteCollectionViewDelegateProxy: _InfiniteCollectionViewProxy { - -} - -class InfiniteCollectionViewDataSourceProxy: _InfiniteCollectionViewProxy { - -}