Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mtj0928/typed-notifications
A library attaching type-information to NotificationCenter
https://github.com/mtj0928/typed-notifications
Last synced: about 2 months ago
JSON representation
A library attaching type-information to NotificationCenter
- Host: GitHub
- URL: https://github.com/mtj0928/typed-notifications
- Owner: mtj0928
- License: mit
- Created: 2023-09-09T04:55:29.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-30T12:56:03.000Z (about 2 months ago)
- Last Synced: 2024-10-30T13:40:46.553Z (about 2 months ago)
- Language: Swift
- Homepage: https://mtj0928.github.io/typed-notifications/documentation/typednotifications/
- Size: 44.9 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typed-notifications
This library attaches type-information to `NotificationCenter`.
You can post and observe notifications in a type-safe manner.```swift
TypedNotificationCenter.default
.publisher(for: .userNameUpdate, object: user)
.sink { notification in
// Notifications can be received in a type safe manner.
let storage: UserNameUpdateNotificationStorage = notification.storage
let user: User? = notification.object
// ...
}extension TypedNotificationDefinition {
@Notification
static var userNameUpdate: TypedNotificationDefinition
}@UserInfoRepresentable
struct UserNameUpdateNotificationStorage {
let oldName: String
let newName: String
}
```## How to Use
Define a notification and how to encode/decode the userInfo in `TypedNotificationDefinition`.
```swift
extension TypedNotificationDefinition {
static var userNameWillUpdate: TypedNotificationDefinition {
.init(name: "userWillUpdate") { newName in
["newName": newName]
} decode: { userInfo in
userInfo?["newName"] as? String ?? ""
}
}
}
```And then, you can post/observe the notifications in type safe manner.
```swift
// [Post]
// Notifications can be posted in a type safe manner.
let newName: String = ...
let user: User = ...
TypedNotificationCenter.default.post(.userNameWillUpdate, storage: newName, object: user)// [Observation]
TypedNotificationCenter.default.publisher(for: .userNameWillUpdate, object: user)
.sink { notification in
// Notifications can be received in a type safe manner.
let newName = notification.storage
let user: User? = notification.object
// ...
}
```### Notification macro
You can use `@Notification` macro, if `@UserInforRepresentable` macro is attached to your type.
```swift
extension TypedNotificationDefinition {
@Notification
static var userNameUpdate: TypedNotificationDefinition
}@UserInfoRepresentable
struct UserNameUpdateNotificationStorage {
let oldName: String
let newName: String
}
```## Pre-defined Notifications
This repository contains frequent system notifications.
- UIKit
- [UIApplication](Sources/TypedNotifications/UIKit/UIApplication.swift)
- [UIScene](Sources/TypedNotifications/UIKit/UIScene.swift)
- [UIResponder](Sources/TypedNotifications/UIKit/UIResponder.swift)
- Core Data
- [NSManagedObjectContext](Sources/TypedNotifications/CoreData/NSManagedObjectContext.swift)Your PR adding new notifications is appreciated. Feel free to make a new PR.