Swiftpack.co -  crane-hiromu/NotificationSubscribable as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
crane-hiromu/NotificationSubscribable
Library makes notifications can be implemented cleanly with combine.
.package(url: "https://github.com/crane-hiromu/NotificationSubscribable.git", from: "1.0.0")

NotificationSubscribable

Description

Do you get a mess of similar code with only different notification names?

func observe() {
    NotificationCenter
            .Publisher(center: .default, name: Notification.Name("Name_A"), object: nil)
            .sink { [weak self] notification in
                // do something when receive `Name_A`
            }
            .store(in: &cancellables)

    NotificationCenter
            .Publisher(center: .default, name: Notification.Name("Name_B"), object: nil)
            .sink { [weak self] notification in
                // do something when receive `Name_B`
            }
            .store(in: &cancellables)

    NotificationCenter
            .Publisher(center: .default, name: Notification.Name("Name_C"), object: nil)
            .sink { [weak self] notification in
                // do something when receive `Name_C`
            }
            .store(in: &cancellables)
}

You can use Combine to standardize the code and have a clean implementation.

func observe() {
    [Notification.Name("Name_A"),
     Notification.Name("Name_B"),
     Notification.Name("Name_C")]
        .map { NotificationCenter.Publisher(center: .default, name: $0) }
        .publisher
        .flatMap { $0 }
        .sink(receiveValue: { [weak self] notification in
            // do something
        })
        .store(in: &cancellable)
}

We make the code has been cloaked and made into a library of NotificationSubscribable.

Example

final class ViewController: UIViewController, NotificationSubscribable {

    override func loadView() {
        super.loadView()

        startObserveNotificationCenter(notifications: [
            Notification.Name("Name_A"),
            Notification.Name("Name_B"),
            Notification.Name("Name_C")
        ]) { [weak self] notification in
            // do something
        }
    }
}

Installing

Swift Package Manager

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/crane-hiromu/NotificationSubscribable.git", from: "1.0.0")

License

MIT, of course ;-) See the LICENSE file.

GitHub

link
Stars: 1
Last commit: 6 days 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.

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