Latest 0.0.1
Homepage https://github.com/leandromperez/rxswift-notifications
License MIT
Platforms ios 10.0, requires ARC
Dependencies RxSwift, RxCocoa
Frameworks UIKit
Authors

Build Status
Version
License
Platform

Introduction

This project contains some extensions on top of NSNotificationCenter that allows the creation of strong-typed notifications. My goal was to have a type-safe, reactive and easier-to-use mechanism.

How to use them?

1. Declare the notification type

Create a struct, enum, or class, that will represent the notification using the type system. :

enum UserNotification : String, Notifiable {
    typealias ParameterType = User

    case userDidLogin
    case userDidLogout
}

1. Subscribe to the notification

Subscribe to the notification and use the parameter directly.

⚠️ Notice that this is type safe, you don’t need to extract the parameter and cast it!
The extensions will safely do that.

Option 1: Add a listener to the notification

    UserNotification.userDidLogin
        .addListener { [unowned self] (user: User) in
            self.loadPreferences(of: user) 
        }
        .disposed(by: disposeBag)

Option 2: You can also treat the notification like a regular Observable:

    UserNotification.userDidLogin.asObservable()
        .subscribe(onNext:{ [unowned self] (user:User) in
            self.loadPreferences(of: user)
        })    
        .disposed(by: disposeBag)

Notifications with no parameters.

NoParamsNotifiable, lets you post notifications with no parameters:


    enum CalendarNotification : NoParamsNotifiable {
        case calendarDidSynchronize
    }

    CalendarNotification.caledarDidSynchronize
        .addListener{     
            print("Calendar synchronized")
        }
        .disposed(by:disposeBag)

Test cases

To run the example project, clone the repo, and run pod install from the Example directory first.
It contains a set of unit tests that explain how to use other features.

Requirements

Installation

RxSwiftNotifications is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'RxSwiftNotifications'

Author

Leandro Perez, [email protected]

License

RxSwiftNotifications is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "RxSwiftNotifications",
    "version": "0.0.1",
    "platforms": {
        "ios": "10.0"
    },
    "requires_arc": true,
    "summary": "A small group of extensions on top of NSNotification center and RxSwift that allows for strong-typed notifications",
    "description": "Have strong-typed notifications and parameters. Easyly register listeners and access the parameters of the notification with no boilerplate code. Transform the notification in a rx observavble to react to it in a seamless way.",
    "homepage": "https://github.com/leandromperez/rxswift-notifications",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Leandro Perez": "[email protected]"
    },
    "social_media_url": "https://medium.com/@leandromperez",
    "source": {
        "git": "https://github.com/leandromperez/rxswift-notifications.git",
        "tag": "0.0.1"
    },
    "source_files": "RxSwiftNotifications/Classes/**/*",
    "frameworks": "UIKit",
    "swift_version": "4.2",
    "dependencies": {
        "RxSwift": [],
        "RxCocoa": []
    }
}

Pin It on Pinterest

Share This