Skip to content

Instantly share code, notes, and snippets.

@matthewcheok
Created December 13, 2015 19:49
Show Gist options
  • Save matthewcheok/b6083e11c63ec1705550 to your computer and use it in GitHub Desktop.
Save matthewcheok/b6083e11c63ec1705550 to your computer and use it in GitHub Desktop.
extension ImagePreheatingController {
public var rx_delegate: DelegateProxy {
return proxyForObject(RxImagePreheatingControllerDelegateProxy.self, self)
}
public var rx_preheatItems: Observable<([Int], [Int])> {
return rx_delegate.observe("preheatingController:didUpdateWithAddedIndexPaths:removedIndexPaths:")
.map { a in
print(a)
return ([], [])
}
.startWith(([], []))
}
}
class ImagePreheatingControllerDelegateNotSet:
NSObject,
ImagePreheatingControllerDelegate {
func preheatingController(controller: ImagePreheatingController, didUpdateWithAddedIndexPaths addedIndexPaths: [NSIndexPath], removedIndexPaths: [NSIndexPath]) {
}
}
private let delegateNotSet = ImagePreheatingControllerDelegateNotSet()
class RxImagePreheatingControllerDelegateProxy :
DelegateProxy,
DelegateProxyType,
ImagePreheatingControllerDelegate {
private weak var _requiredMethodsDelegate: ImagePreheatingControllerDelegate? = delegateNotSet
class func currentDelegateFor(object: AnyObject) -> AnyObject? {
let preheatingController = object as! ImagePreheatingController
return preheatingController.delegate
}
class func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject) {
let preheatingController = object as! ImagePreheatingController
preheatingController.delegate = delegate as? ImagePreheatingControllerDelegate
}
override func setForwardToDelegate(forwardToDelegate: AnyObject?, retainDelegate: Bool) {
let requiredMethodsDelegate = forwardToDelegate as? ImagePreheatingControllerDelegate
_requiredMethodsDelegate = requiredMethodsDelegate ?? delegateNotSet
super.setForwardToDelegate(forwardToDelegate, retainDelegate: retainDelegate)
}
func preheatingController(controller: ImagePreheatingController, didUpdateWithAddedIndexPaths addedIndexPaths: [NSIndexPath], removedIndexPaths: [NSIndexPath]) {
(_requiredMethodsDelegate ?? delegateNotSet).preheatingController(controller, didUpdateWithAddedIndexPaths: addedIndexPaths, removedIndexPaths: removedIndexPaths)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment