Coder Social home page Coder Social logo

0x384c0 / simplifiednotificationcenter Goto Github PK

View Code? Open in Web Editor NEW
2.0 5.0 4.0 105 KB

A tiny swift wrapper around NSNotificationCenter with generics, that aids in the creation of Notifications.

Home Page: https://cocoapods.org/pods/SimplifiedNotificationCenter

License: MIT License

Ruby 8.49% Shell 1.84% Swift 89.67%
nsnotificationcenter swift

simplifiednotificationcenter's Introduction

SimplifiedNotificationCenter

tests workflow Swift Package Manager Version License Platform Readme Score

This is a tiny swift wrapper around NSNotificationCenter with generics, that aids in the creation of Notifications.

Installation

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding SimplifiedNotificationCenter as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/0x384c0/SimplifiedNotificationCenter.git")
]

CocoaPods

SimplifiedNotificationCenter is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "SimplifiedNotificationCenter"

Usage

Add import SimplifiedNotificationCenter to your source code

// create notification
let notification = SimpleNotification<String>(name: "Example.notification")

// subscribe
notification.subscribe { value in
    print("value: \(value)")
}

//scheck is notification subscribed
print(notification.isSubscribed)

//post
notification.post("sample text")
//sample text be printed

Passing notifications between different places of application

Notifications holder:

class Notifications{
    let testNotification    = SimpleNotification<String>         (name: "Example.testNotification")
}

Notifications handler:

class SampleClass {
        //instance of notifications holder
        var notifications = Notifications()
        init(){
            //subscribe
            notifications.testNotification.subscribe{ value in
                print("value: \(value)")
        }
    }
}

Notifications caller:

class AnotherClass {
    func post(){
        //post
        Notifications().testNotification.post("comunicationBetweenDifferentClassesExample Test text")
        //or use
        //SimpleNotification<String>(name: "Example.testNotification").post("comunicationBetweenDifferentClassesExample Test text")
        //but it is not safe
    }
}

Example:

let
sampleClass = SampleClass(),
anotherClass = AnotherClass()
anotherClass.post()
//comunicationBetweenDifferentClassesExample Test text will be printed

Example App

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • ios 11.0 and higher
  • swift v5.0

Unit Tests

SimplifiedNotificationCenter includes a suite of unit tests within the Tests subdirectory. These tests can be run simply be executed the test action on the platform framework you would like to test.

Author

0x384c0, [email protected]

License

SimplifiedNotificationCenter is available under the MIT license. See the LICENSE file for more info.

simplifiednotificationcenter's People

Contributors

0x384c0 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.