Swiftpack.co -  dbi1463/typed-notification as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
dbi1463/typed-notification
A strong type way to receive notification
.package(url: "https://github.com/dbi1463/typed-notification.git", from: "0.0.1")

TypedNotification

Build Status Swift Package Manager release MIT license

A type-safe way to receive notifications.

This library is inspired by

  • Typed Message pattern described in Pattern Hatching: Design Patterns Applied, Chapter 4
  • Disposable pattern
  • C# event delegate += syntax.

With this library, it is able to register notification handlers that will receive type-safe information without writing boilerplate codes to decode notification userInfo object.

Get Started

Installation

Current, this library only supports Swift Package Manager. In Package.swift, add the following into dependencies array:

.package(url: "https://github.com/dbi1463/typed-notification.git", .branch("master"))

Next, add TypedNotification to your targets dependencies like:

.target(
    name: "[your target]",
    dependencies: [ "TypedNotification" ]
),

Register an observer

To receive a type-safe notification is easy. All supported notifications are listed in Notifications. For example, to receive a keyboard will show notification, use the add operator (+) to subscribe the Notifications.keyboard.willShow notification.

let subscription = Notifications.keyboard.willShow + { [weak self] info in
    // info is a KeyboardInfo struct, can access the beginFrame, endFram, animation duration,
    // and animation curve like:
    // info.beginFrame, info.endFrame, ...
}

The register handler will be invoked before the keyboard trying to show, and the information has been decoded as KeyboardInfo and pass to the handler. Note that the returned subscription should be kept, and in the closure, to access self, remember to add [weak self] to avoid reference cycle.

Remove the observer

The returned subscription is a disposable object that will remove notification observer for you when itself has been deallocated. Thus, the only thing to do is to keep the subscription object as member data in the view controller (or the object that handles the notification). When the view controller disappeared (dellocated), the subscription will remove the observer automatically.

License

TypedNotification is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 0
Last commit: 6 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API