Created
March 8, 2017 02:29
-
-
Save matsuda/858b402a8c1441a5029a01f118ac467d to your computer and use it in GitHub Desktop.
Notification extensions in Swift 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NotificationCenter.default.post(identifier: .DidChangeSession, object: nil) | |
NotificationCenter.default.addObserver( | |
self, selector: #selector(didChangeNoticeState(didChangeSession:)), | |
identifier: .DidChangeSession, object: nil) | |
NotificationCenter.default.rx.notification(.didChangeSession) | |
.subscribe(onNext: { [weak self] (_) in | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Notification { | |
// MARK: - Identifier | |
enum Identifier: String { | |
case DidChangeSession | |
} | |
// MARK: - UserInfoKey | |
enum UserInfoKey: String { | |
case Session | |
} | |
} | |
/// | |
/// MARK: - Notification.Name | |
/// | |
extension Notification.Name { | |
init(identifier: Notification.Identifier) { | |
self.init(identifier.rawValue) | |
} | |
} | |
/// | |
/// MARK: - NotificationCenter | |
/// | |
extension NotificationCenter { | |
func addObserver(_ observer: Any, selector aSelector: Selector, identifier aIdentifier: Notification.Identifier, object anObject: Any?) { | |
addObserver(observer, selector: aSelector, name: Notification.Name(identifier: aIdentifier), object: anObject) | |
} | |
func removeObserver(_ observer: Any, identifier aIdentifier: Notification.Identifier, object anObject: Any?) { | |
removeObserver(observer, name: Notification.Name(identifier: aIdentifier), object: anObject) | |
} | |
func post(identifier aIdentifier: Notification.Identifier, object anObject: Any?, userInfo aUserInfo: [AnyHashable : Any]? = nil) { | |
post(name: Notification.Name(identifier: aIdentifier), object: anObject, userInfo: aUserInfo) | |
} | |
} | |
/// | |
/// MARK: - Notification | |
/// | |
extension Notification { | |
func userInfoValue(forKey key: Notification.UserInfoKey) -> Any? { | |
guard let userInfo = userInfo else { return nil } | |
return userInfo[key.rawValue] | |
} | |
} | |
/// | |
/// MARK: - Rx | |
/// | |
import RxSwift | |
extension Reactive where Base : NotificationCenter { | |
/** | |
Transforms notifications posted to notification center to observable sequence of notifications. | |
- parameter aIdentifier: Optional identifier used to filter notifications. | |
- parameter object: Optional object used to filter notifications. | |
- returns: Observable sequence of posted notifications. | |
*/ | |
func notification(_ aIdentifier: Notification.Identifier, object: AnyObject? = nil) -> Observable<Notification> { | |
return notification(Notification.Name(identifier: aIdentifier), object: object) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment