Skip to content

x0000ff/XNotificationCenter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Swift 4.0 License Status

I’m Going To Build My Own Theme Park

X Notification Center

My type-safe Notifications with Blackjack and Hookers

⭐️ Key Features
🐧 Swift 4.0
🏰 Strong typed
🎭 Extendable

Why?

  • Avoid string-based notification syntax 😡
  • Avoid casting magic in notification handlers 🤢
func userNotificationCenter(center: NSUserNotificationCenter!, didActivateNotification notification: NSUserNotification!) {
        let userInfo:Dictionary<String,String!> = notification.userInfo as! Dictionary<String,String!>

    guard let type = userInfo["type"] as? String else {
            print("It can't be :(")        
    }
      
    if type == "LoggedIn" {
        guard let accessToken = userInfo["accessToken"] as? String else {
            print("It can't be :(")        
        }
    }
}  

Installation

Copy XNotificationCenter.swift to your project

Usage

Create new type of notification

You can add any properties you need.

public struct NewUserNotification: Notification {
    public let userName: String

    public init(userName: String) {
        self.userName = userName
    }
}

Create instance of Notification Center

let notificationCenter = XNotificationCenter()

Create listener

let listener = notificationCenter.subscribe(forNotification: NewUserNotification.self) { (notification) in
    print("Hi, \(notification.userName)")
}

Send notification

// Create an instance
let notification = NewUserNotification(userName: "Admin")

// And send it
notificationCenter.send(notification: notification)

Remove specific listener

notificationCenter.remove(listener: listener)

Remove all listeners of some notification type

notificationCenter.removeListeners(ofNotification: NewUserNotification.self)

Remove all listeners for all the notificaitons

notificationCenter.removeAllListeners()

For more examples...

see XNotificationCenter.playground

Releases

No releases published

Packages

No packages published

Languages