πŸ“‘ Helping you own NotificationCenter in Swift!

Overview

Notificationz πŸ“‘

Helping you own NotificationCenter

Version Version Swift Platforms Carthage

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.

You might also like...
Headline News Widget for Pock.You can display the articles fetched by rss.
Headline News Widget for Pock.You can display the articles fetched by rss.

Headline News Widget for Pock This is a headline news widget plugin for Pock You can display the articles fetched by rss. Demo In the demo video, the

Generates a random photo after you click the button

Swift Random Photo Generator Generates a random photo after you click the button! Things you need to do πŸ“– clone this repository git clone https://git

ALO sync allows you to sync resources form an ALO endpoint to your macOS file system.
ALO sync allows you to sync resources form an ALO endpoint to your macOS file system.

ALO sync allows you to sync resources form an ALO endpoint to your macOS file system. Prerequisites macOS 11 No support for search* No suppor

LibAuthentication will simplify your code when if you want to use FaceID/TouchID in your tweaks.

LibAuthentication will simplify your code when if you want to use FaceID/TouchID in your tweaks.

What if you could give your wallpapers, a little touch? On the fly, of course
What if you could give your wallpapers, a little touch? On the fly, of course

AmΔ“lija On the fly preferences. Features Custom Blurs for your LockScreen. Custom Blurs for your HomeScreen. Blur Types Epic (Gaussian). Dark. Light.

Haven't you wished for `try` to sometimes try a little harder? Meet `retry`

Retry Example Haven't you wished for try to sometimes try a little harder? Meet retry To run the example project, clone the repo, and run pod install

A powerful app that lets you play Blackjack on Mac for free 🎰

Blackjack A powerful application for Mac to play the classic casino game, Blackjack. This app was created so I can understand the basics of Swift. Mad

FreshPlan - meet-up app that plans when you guys want to meet

FreshPlan meet-up app that plans when you guys want to meet. Requirements Cocoap

Weather and forecasts for humans. Information you can act on.

Tropos Weather and forecasts for humans. Information you can act on. Most weather apps throw a lot of information at you but that doesn't answer the q

Comments
  • Example Project

    Example Project

    Hi I have been trying to get this work though haven't been able to get the notifications to trigger other devices. That what the notifications are meant to do right?

    Thanks in advance for your help.

    opened by Ged2323 2
Releases(v1.0.0)
  • v1.0.0(Dec 18, 2015)

    The library has been in private beta, and I only made it public after shipping it with my apps to the AppStore. You can rest assured it is battle tested.

    Source code(tar.gz)
    Source code(zip)
Owner
Kitz
Collection of quality Swift kits
Kitz
Pavel SurovΓ½ 0 Jan 1, 2022
SuggestionsBox helps you build better a product trough your user suggestions. Written in Swift. πŸ—³

SuggestionsBox An iOS library to aggregate users feedback about suggestions, features or comments in order to help you build a better product. Swift V

Manuel Escrig 100 Feb 6, 2022
Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Protected is a Swift Package that allows you to specify the read and write rights for any type, depending on context by using Phantom types

Mathias Quintero 9 Sep 25, 2022
FancyGradient is a UIView subclass which let's you animate gradients in your iOS app. It is purely written in Swift.

FancyGradient is a UIView subclass which let's you animate gradients in your iOS app. It is purely written in Swift. Quickstart Static gradient let fa

Derek Jones 11 Aug 25, 2022
Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm

Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm. Creating a chan

Christoffer Winterkvist 127 Jun 3, 2022
Ethereum Wallet Toolkit for iOS - You can implement an Ethereum wallet without a server and blockchain knowledge.

Introduction EtherWalletKit is an Ethereum Wallet Toolkit for iOS. I hope cryptocurrency and decentralized token economy become more widely adapted. H

Sung Woo Chang 136 Dec 25, 2022
LifetimeTracker can surface retain cycle / memory issues right as you develop your application

LifetimeTracker Bar style Circular style LifetimeTracker can surface retain cycle / memory issues right as you develop your application, and it will s

Krzysztof ZabΕ‚ocki 2.8k Jan 4, 2023
WhatsNewKit enables you to easily showcase your awesome new app features.

WhatsNewKit enables you to easily showcase your awesome new app features. It's designed from the ground up to be fully customized to your needs. Featu

Sven Tiigi 2.8k Jan 3, 2023
Extensions that allow you to work with optionals

RxOptionals Sometimes it happens that you need to bind to several optional binders or to an optional method (for example, when using weak). To do this

Dane4ka 3 Aug 9, 2021