Skip to content

A small group of extensions on top of NSNotification center and RxSwift that allows for strong-typed notifications

License

Notifications You must be signed in to change notification settings

croppka1/rxswift-notifications

 
 

Repository files navigation

RxSwiftNotifications

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.

About

A small group of extensions on top of NSNotification center and RxSwift that allows for strong-typed notifications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 92.0%
  • Ruby 8.0%