Swiftpack.co - crane-hiromu/NotificationSubscribable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by crane-hiromu.
crane-hiromu/NotificationSubscribable 1.0.0
Library makes notifications can be implemented cleanly with combine.
⭐️ 1
🕓 2 years ago
.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: 2 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

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