• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Proxy for receive delegate events more practically

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

CocoaPods

Add the following to your Podfile:

use_frameworks!

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

Carthage

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.

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.

More Repositories

1

DifferenceKit

💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
Swift
3,421
star
2

RACollectionViewReorderableTripletLayout

The custom collectionView layout that can perform reordering of cells by dragging it.
Objective-C
1,481
star
3

Former

Former is a fully customizable Swift library for easy creating UITableView based form.
Swift
1,307
star
4

Carbon

🚴 A declarative library for building component-based user interfaces in UITableView and UICollectionView.
Swift
1,291
star
5

RAReorderableLayout

A UICollectionView layout whitch can move item with drag and drop.
Swift
867
star
6

DiffableDataSources

💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.
Swift
836
star
7

swiftui-hooks

🪝 A SwiftUI implementation of React Hooks. Enhances reusability of stateful logic and gives state and lifecycle to function view.
Swift
479
star
8

SwiftUI-Combine

This is an example project of SwiftUI and Combine using GitHub API.
Swift
452
star
9

VueFlux

♻️ Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux
Swift
331
star
10

PathDynamicModal

A modal view using UIDynamicAnimator, like the Path for iOS.
Swift
286
star
11

swiftui-atom-properties

⚛️ Atomic approach state management and dependency injection for SwiftUI
Swift
232
star
12

FloatingActionSheetController

FloatingActionSheetController is a cool design ActionSheetController library written in Swift2.
Swift
140
star
13

RASlideInViewController

RASlideInViewController has an transition effect expressing the depth, and you can dismiss it by draging
Objective-C
127
star
14

Alembic

⚗️ Functional JSON Parser - Linux Ready 🐧
Swift
116
star
15

swift-mod

A tool for Swift code modification intermediating between code generation and formatting.
Swift
101
star
16

SwiftUI-Flux

🚀 This is a tiny experimental application using SwiftUI with Flux architecture.
Swift
54
star
17

monkey-lang-swift

The Monkey Programming Language written in Swift -- Writing An Interpreter In Go
Swift
17
star
18

LiveStreamingApp

A sample app repository that broadcasting and player of HLS for iOS.
Swift
16
star
19

KenBurnsSlideshowView

Slideshow with Ken Burns effect for iOS.
Swift
11
star
20

VueFluxExample-GitHub

VueFlux VueFluxReactive example project
Swift
8
star
21

OwnKit

My own utility toolkit for ios
Swift
7
star
22

ra1028

2
star
23

Dribbble_client_sample

Swift
1
star