https://github.com/marty-suzuki/NoticeObserveKit
NoticeObserveKit is type-safe NotificationCenter wrapper.
https://github.com/marty-suzuki/NoticeObserveKit
ios macos notificationcenter swift tvos type-safe watchos
Last synced: 3 months ago
JSON representation
NoticeObserveKit is type-safe NotificationCenter wrapper.
- Host: GitHub
- URL: https://github.com/marty-suzuki/NoticeObserveKit
- Owner: marty-suzuki
- License: mit
- Created: 2016-12-27T02:44:09.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-06-30T18:27:37.000Z (over 1 year ago)
- Last Synced: 2024-12-06T03:10:51.671Z (11 months ago)
- Topics: ios, macos, notificationcenter, swift, tvos, type-safe, watchos
- Language: Swift
- Homepage:
- Size: 93.8 KB
- Stars: 150
- Watchers: 5
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ios - NoticeObserveKit - NoticeObserveKit is type-safe NotificationCenter wrapper that associates notice type with info type. (EventBus / Getting Started)
- awesome-swift - NoticeObserveKit - NoticeObserveKit is type-safe NotificationCenter wrapper that associates notice type with info type. (Libs / Events)
- awesome-swift - NoticeObserveKit - NoticeObserveKit is type-safe NotificationCenter wrapper that associates notice type with info type. (Libs / Events)
- awesome-ios-star - NoticeObserveKit - NoticeObserveKit is type-safe NotificationCenter wrapper that associates notice type with info type. (EventBus / Getting Started)
- fucking-awesome-swift - NoticeObserveKit - NoticeObserveKit is type-safe NotificationCenter wrapper that associates notice type with info type. (Libs / Events)
- awesome-swift - NoticeObserveKit - NoticeObserveKit is type-safe NotificationCenter wrapper. ` π 2 years ago ` (Events [π](#readme))
README
# NoticeObserveKit
[](http://cocoapods.org/pods/NoticeObserveKit)
[](http://cocoapods.org/pods/NoticeObserveKit)
[](https://github.com/Carthage/Carthage)
[](http://cocoapods.org/pods/NoticeObserveKit)
NoticeObserveKit is type-safe NotificationCenter wrapper.
Swift Concurrency (since macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0)
```swift
Task {
// .keyboardWillShow is a static property.
for try await keyboardInfo in NotificationCenter.default.nok.notifications(named: .keyboardWillShow) {
// In this case, keyboardInfo is UIKeyboardInfo type.
// It is inferred from a generic parameter of Notice.Name.
print(keyboardInfo)
}
}
```
Combine (since macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0)
```swift
// .keyboardWillShow is a static property.
NotificationCenter.default.nok.publisher(for: .keyboardWillShow)
.sink(
receiveCompletion: { _ in },
receiveValue: { keyboardInfo in
// In this case, keyboardInfo is UIKeyboardInfo type.
// It is inferred from a generic parameter of Notice.Name.
print(keyboardInfo)
}
)
.store(in: &cancellables)
```
NoticeObserveKit original
```swift
// .keyboardWillShow is a static property.
NotificationCenter.default.nok.observe(name: .keyboardWillShow) { keyboardInfo in
// In this case, keyboardInfo is UIKeyboardInfo type.
// It is inferred from a generic parameter of Notice.Name.
print(keyboardInfo)
}
// pool is Notice.ObserverPool.
// If pool is released, Notice.Observes are automatically removed.
.invalidated(by: pool)
```
## Usage
First of all, you need to implement `Notice.Name` like this.
`T` is type of value in notification.userInfo.
```swift
extension Notice.Names {
static let keyboardWillShow = Notice.Name(
UIResponder.keyboardWillShowNotification
) { userInfo in
// Implementing decode is only required if you want to use an already defined Notification.Name (e.g. UIResponder.keyboardWillShowNotification).
guard
let frame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval,
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt
else {
throw DecodeError()
}
return UIKeyboardInfo(
frame: frame,
animationDuration: duration,
animationCurve: UIViewAnimationOptions(rawValue: curve)
)
}
}
```
## Customization
If you can post custom Notification like this.
```swift
extension Notice.Names {
// If you define your own custom Notification.Name, no implementation of decode is required.
static let navigationControllerDidShow = Notice.Name(name: "navigationControllerDidShow")
}
let content = NavigationControllerContent(viewController: viewController, animated: animated)
NotificationCenter.default.nok.post(name: .navigationControllerDidShow, value: content)
```
You can invalidate manually like this.
```swift
let observer = NotificationCenter.default.nok.observe(name: .keyboardWillShow) { keyboardInfo in
print(keyboardInfo)
}
observer.invalidate()
```
## Sample
```swift
import UIKit
import NoticeObserveKit
class ViewController: UIViewController {
private let searchBar = UISearchBar(frame: .zero)
private lazy var keyboardNotificationTasks: [Task] = [
Task {
for try await value in NotificationCenter.default.nok.notifications(named: .keyboardWillShow) {
print("UIKeyboard will show = \(value)")
}
},
Task {
for try await value in NotificationCenter.default.nok.notifications(named: .keyboardWillHide) {
print("UIKeyboard will hide = \(value)")
}
}
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationItem.titleView = searchBar
configureObservers()
}
private func configureObservers() {
_ = keyboardNotificationTasks
}
}
```
## Requirements
- Swift 5
- Xcode 15.0 or greater
- iOS 10.0 or greater
- tvOS 10.0 or greater
- macOS 10.10 or greater
- watchOS 3.0 or greater
## Installation
#### CocoaPods
NoticeObserveKit is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod "NoticeObserveKit"
```
#### Carthage
If youβre using [Carthage](https://github.com/Carthage/Carthage), simply add
NoticeObserveKit to your `Cartfile`:
```
github "marty-suzuki/NoticeObserveKit"
```
Make sure to add `NoticeObserveKit.framework` to "Linked Frameworks and Libraries" and "copy-frameworks" Build Phases.
## Author
marty-suzuki, s1180183@gmail.com
## License
NoticeObserveKit is available under the MIT license. See the LICENSE file for more info.