DelegateProxy 1.1.0

DelegateProxy 1.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Nov 2016
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by ra1028.



  • By
  • ra1028

DelegateProxy


Codebeat

Proxy for receive delegate events more practically


About DelegateProxy

DelegateProxy enable you to receive delegate events by subscribed handler.

This is generic version of DelegateProxy by RxSwift

It means be able to use in combination with any other reactive-framework like ReactiveCocoa or SwiftBond, etc.


Requirements

  • Swift 3 / Xcode 8
  • OS X 10.9 or later
  • iOS 8.0 or later
  • watchOS 2.0 or later
  • tvOS 9.0 or later


Basic Example

Create DelegateProxy inherited class.

final class ScrollViewDelegateProxy: DelegateProxy, UIScrollViewDelegate, DelegateProxyType {
    func resetDelegateProxy(owner: UIScrollView) {
        owner.delegate = self
    }
}

It can be useful to implements extension.

extension UIScrollView {
    var delegateProxy: ScrollViewDelegateProxy {
        return .proxy(for: self)
    }
}

You can receive delegate events as following.

scrollView.delegateProxy
    .receive(#selector(UIScrollViewDelegate.scrollViewDidScroll(_:))) { args in
        guard let scrollView: UIScrollView = args.value(at: 0) else { return }
        print(scrollView.contentOffset)
}

Custom Example

You can receive delegate events by Receivable protocol implemented class.
Followings are examples of use DelegateProxy with reactive-frameworks.

With ReactiveCocoa

Create receiver class.

final class RACReceiver: Receivable {
    let (signal, observer) = Signal<Arguments, NoError>.pipe()

    func send(arguments: Arguments) {
        observer.send(value: arguments)
    }
}

Extension

extension DelegateProxy {
    func rac_receive(selector: Selector) -> Signal<Arguments, NoError> {
        return RACReceiver().subscribe(to: self, selector: selector).signal
    }
}

Receive events by streams.

scrollView.delegateProxy
    .rac_receive(selector: #selector(UIScrollViewDelegate.scrollViewDidScroll(_:)))
    .map { $0.value(at: 0, as: UIScrollView.self)?.contentOffset }
    .skipNil()
    .observeValues { print("ContentOffset: \($0)") }

With SwiftBond

Create receiver class.

final class BondReceiver: Receivable {
    let subject = PublishSubject<Arguments, NoError>()

    func send(arguments: Arguments) {
        subject.next(arguments)
    }
}

Extension

extension DelegateProxy {
    func bnd_receive(selector: Selector) -> Signal<Arguments, NoError> {
        return BondReceiver().subscribe(to: self, selector: selector).subject.toSignal()
    }
}

Receive events by streams.

scrollView.delegateProxy
    .bnd_receive(selector: #selector(UIScrollViewDelegate.scrollViewDidScroll(_:)))
    .map { $0.value(at: 0, as: UIScrollView.self)?.contentOffset }
    .ignoreNil()
    .observeNext { print("ContentOffset: \($0)") }


Contribution

Welcome to fork and submit pull requests!!

Before submitting pull request, please ensure you have passed the included tests.
If your pull request including new function, please write test cases for it.


License

DelegateProxy is released under the MIT License.