Coder Social home page Coder Social logo

delegateproxy's Introduction

DelegateProxy

Build Status Swift3 Platform
CocoaPods Carthage Codebeat Lincense

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

Installation

Add the following to your Podfile:

use_frameworks!

target 'YOUR_TARGET_NAME' do
  pod 'DelegateProxy'
end
$ pod install

Add the following to your Cartfile:

github "ra1028/DelegateProxy"
$ carthage update

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.

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)") }

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.

delegateproxy's People

Contributors

ra1028 avatar

Watchers

Austin Chen avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.