Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leandromperez/rxswift-notifications
A small group of extensions on top of NSNotification center and RxSwift that allows for strong-typed notifications
https://github.com/leandromperez/rxswift-notifications
Last synced: about 2 months ago
JSON representation
A small group of extensions on top of NSNotification center and RxSwift that allows for strong-typed notifications
- Host: GitHub
- URL: https://github.com/leandromperez/rxswift-notifications
- Owner: leandromperez
- License: mit
- Created: 2019-01-05T14:43:07.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-28T14:57:41.000Z (over 4 years ago)
- Last Synced: 2024-10-12T01:16:02.420Z (3 months ago)
- Language: Swift
- Size: 340 KB
- Stars: 33
- Watchers: 3
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rxswift - rxswift-notifications - typed notifications (Libraries)
README
# RxSwiftNotifications
[![Build Status](https://travis-ci.org/leandromperez/rxswift-notifications.svg?branch=master)](https://travis-ci.org/leandromperez/rxswift-notifications)
[![Version](https://img.shields.io/cocoapods/v/RxSwiftNotifications.svg?style=flat)](https://cocoapods.org/pods/RxSwiftNotifications)
[![License](https://img.shields.io/cocoapods/l/RxSwiftNotifications.svg?style=flat)](https://cocoapods.org/pods/RxSwiftNotifications)
[![Platform](https://img.shields.io/cocoapods/p/RxSwiftNotifications.svg?style=flat)](https://cocoapods.org/pods/RxSwiftNotifications)## 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. :
```swift
enum UserNotification : String, Notifiable {
typealias ParameterType = Usercase userDidLogin
case userDidLogout
}
```
### 1. Subscribe to the notificationSubscribe 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
```swift
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:
```swift
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:
```swiftenum 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](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'RxSwiftNotifications'
```## Author
Leandro Perez, [email protected]
## License
RxSwiftNotifications is available under the MIT license. See the LICENSE file for more info.