Skip to content

Commit

Permalink
add CocoaProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauddorgans committed Dec 27, 2017
1 parent 1fbb837 commit 03e9dbd
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 95 deletions.
2 changes: 2 additions & 0 deletions Example/InfiniteLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
PODS:
- InfiniteLayout (0.1.0)
- CocoaProxy (0.1.1)
- InfiniteLayout (0.1.3):
- CocoaProxy (~> 0.1)

DEPENDENCIES:
- InfiniteLayout (from `../`)
Expand All @@ -9,7 +11,8 @@ EXTERNAL SOURCES:
:path: ../

SPEC CHECKSUMS:
InfiniteLayout: 82a21b8255623e2d72174f3082bef9e576140621
CocoaProxy: 35ab81e24325b33834cffe45a3d1fd48ca67ef3a
InfiniteLayout: 0ab39bf2a4d9ce5c473ebccfc0d3b7278de18ac9

PODFILE CHECKSUM: 3a658536624f41ec07c5a7a2c55407b9b8f48528

Expand Down
4 changes: 2 additions & 2 deletions InfiniteLayout.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
6 changes: 2 additions & 4 deletions InfiniteLayout/Classes/InfiniteCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 0 additions & 19 deletions InfiniteLayout/Classes/InfiniteCollectionViewProxy.h

This file was deleted.

52 changes: 0 additions & 52 deletions InfiniteLayout/Classes/InfiniteCollectionViewProxy.m

This file was deleted.

85 changes: 85 additions & 0 deletions InfiniteLayout/Classes/InfiniteCollectionViewProxy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// Proxy.swift
// InfiniteLayout
//
// Created by Arnaud Dorgans on 20/12/2017.
//

import UIKit
import CocoaProxy

class InfiniteCollectionViewProxy<T: NSObjectProtocol>: 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>, 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>, 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)
}
}
16 changes: 0 additions & 16 deletions InfiniteLayout/Classes/Proxy.swift

This file was deleted.

0 comments on commit 03e9dbd

Please sign in to comment.