ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - SwiftKitz/Notificationz: 📡 Helping you own NotificationCenter in Swift!
📡 Helping you own NotificationCenter in Swift! Contribute to SwiftKitz/Notificationz development by creating an account on GitHub.
Visit Site

GitHub - SwiftKitz/Notificationz: 📡 Helping you own NotificationCenter in Swift!

GitHub - SwiftKitz/Notificationz: 📡 Helping you own NotificationCenter in Swift!

Highlights

  • Keep Your Naming Conventions: This library gives you convenient access to NotificationCenter, but it's up to you to set it up the way you like!

  • Nothing to Hide: Not trying to hide NotificationCenter functionality. Just an attempt to provide a more convenient API

  • Full and Simple Testing: Testing this library was simple, since it only forwards calls to NotificationCenter for the most part. Mocking that object allowed tests to reach 100% coverage.

Features

You can try them in the playground shipped with the framework!

Use your own naming convention to wrap NotificationCenter

let nsCenter = NotificationCenter.default
let 📡 = NotificationCenterAdapter(notificationCenter: nsCenter)
📡.post(.💃)

// my personal preference, define this in Globals.swift
let NC = NotificationCenterAdapter(notificationCenter: nsCenter)
// Now, you can use `NC` throughout the app

Four simple keywords to remember

let obj = Object()
NC.add(obj, selector: #selector(Object.call))   // normal add observer
NC.observe { notification in }                  // observe using blocks
NC.post(.tenhut)                                // post a notification
NC.remove(obj)                                  // remove from nsCenter

Transparent and convenient API

let keys = ["observe", "many", "keys"].map { Notification.Name($0) }
NC.observe(keys) { _ in }       // observe on the same thread
NC.observeUI(keys) { _ in }     // delivered to the main thread

NC.post(.test)
NC.post(.test, userInfo: ["info":5])
NC.post(Notification(name: .test, object: nil))

RAII-based observers

class Dummy {

    // declare the observer as optional
    var broadcastObserver: Observer?

    init() {
        // assign it anywhere you like
        broadcastObserver = NC.observe { [unowned self] _ in
            self.doSomething()
        }.execute() // this is a neat bonus feature
    }

    func doSomething() {
        // exectued twice, since we call "execute" manually
        print("it works!")
    }
}

var dummy: Dummy? = Dummy()
NC.post(.test)  // calls doSomething
dummy = nil     // clean up is automatic
NC.post(.test)  // doesn't crash!

Getting Started

Carthage

Carthage is fully supported. Simply add the following line to your Cartfile:

github "SwiftKitz/Notificationz"

Cocoapods

Cocoapods is fully supported. Simply add the following line to your Podfile:

pod 'Notificationz'

Submodule

For manual installation, you can grab the source directly or through git submodules, then simply:

  • Drop the Notificationz.xcodeproj file as a subproject (make sure Copy resources is not enabled)
  • Navigate to your root project settings. Under "Embedded Binaries", click the "+" button and select the Notificationz.framework

Motivation

After migrating to Swift, the NotificationCenter APIs really stood out in the code. Writing so much boiler plate all over the place just to register, handle, and cleanup notifications. Coming from C++, RAII seemed a pretty invaluable pattern to be applied here.

With this framework, one can easily declare all their observers as properties:

class Sample {
    private var keyboardObserver: Observer?
    private var reachabilityObserver: Observer?
}

Other programmers should be pleased with this consistency! Moreover, there is no need to worry handling notifications in some other function somewhere nor do cleanup in deinit. It just works:

keyboardObserver = NC.observeUI(UIKeyboardWillShowNotification) { [unowned self] _ in
    // you can write your handler code here, maybe call another function
}

// you can force cleanup by setting the property to nil, but don't have to
keyboardObserver = nil

Author

Maz (@Mazyod)

License

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

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory