Last active
December 15, 2021 10:50
-
-
Save pofat/b0a05cd0348878e23027eae4273f0bed to your computer and use it in GitHub Desktop.
Deal with notification with protocol-oriented programing in Swift
This file contains 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
//: Playground - noun: a place where people can play | |
import UIKit | |
// This is for dmoe, you can use a generice type to limit your observer to an UIViewController for common usage. | |
protocol Notifiable { | |
var name: Notification.Name { get } | |
func observe(by observer: Any, withSelector selector: Selector, object: Any?) | |
func post(object: Any? ,userInfo: [AnyHashable: Any]?) | |
static func remove(observer: Any) | |
} | |
extension Notifiable { | |
func observe(by observer: Any, withSelector selector: Selector, object: Any? = nil) { | |
NotificationCenter.default.addObserver(observer, selector: selector, name: name, object: object) | |
} | |
func post(object: Any? = nil, userInfo: [AnyHashable: Any]? = nil) { | |
NotificationCenter.default.post(name: name, object: object, userInfo: userInfo) | |
} | |
static func remove(observer: Any) { | |
NotificationCenter.default.removeObserver(observer) | |
} | |
} | |
class MyClass { | |
@objc func handleKeyboardWillShow() { | |
print("do after keyboard shows") | |
} | |
} | |
enum Notifier: Notifiable { | |
case keyBoardWillHide, keyBoardWillShow | |
var name: Notification.Name { | |
switch self { | |
case .keyBoardWillHide: | |
return Notification.Name.UIKeyboardWillHide | |
case .keyBoardWillShow: | |
return Notification.Name.UIKeyboardWillShow | |
} | |
} | |
} | |
let myClass = MyClass() | |
Notifier.keyBoardWillShow.observe(by: myClass, withSelector: #selector(myClass.handleKeyboardWillShow)) | |
Notifier.keyBoardWillShow.post() | |
Notifier.remove(observer: myClass) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment